-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.h
118 lines (94 loc) · 2.33 KB
/
server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include "thread.h"
#include "network.h"
#include "log.h"
#include <psp2/kernel/threadmgr.h>
template <typename TConnection>
class Server : private Thread
{
public:
Server();
bool listen(int port);
private:
int m_socket = -1;
void onNewConnection(int socket);
void run() override;
};
template <typename TConnection>
Server<TConnection>::Server()
{
m_socket = sceNetSocket("vdbtcp-server", SCE_NET_AF_INET, SCE_NET_SOCK_STREAM, 0);
if (m_socket < 0)
{
LOG("failed to create server socket: 0x%08X\n", m_socket);
return;
}
auto nonblocking = 1;
auto res = sceNetSetsockopt(m_socket, SCE_NET_SOL_SOCKET, SCE_NET_SO_NBIO, &nonblocking, sizeof(nonblocking));
if (res < 0)
{
LOG("failed to mark port as non-blocking I/O: 0x%08X\n", res);
return;
}
}
template <typename TConnection>
bool Server<TConnection>::listen(int port)
{
SceNetSockaddrIn addr = { 0 };
addr.sin_family = SCE_NET_AF_INET;
addr.sin_port = sceNetHtons(port);
addr.sin_addr.s_addr = 0;
auto res = sceNetBind(m_socket, (const SceNetSockaddr *)&addr, sizeof(addr));
if (res < 0)
{
LOG("failed to bind to port %d: 0x%08X\n", port, res);
return false;
}
res = sceNetListen(m_socket, 1);
if (res < 0)
{
LOG("failed to listen on port %d: 0x%08X\n", port, res);
return false;
}
if (start() < 0)
{
LOG("could not start thread for listening\n");
return false;
}
return true;
}
template <typename TConnection>
void Server<TConnection>::run()
{
while (1)
{
SceNetSockaddr addr;
unsigned int len = sizeof(addr);
auto res = sceNetAccept(m_socket, &addr, &len);
if (res < 0)
{
if (static_cast<unsigned>(res) != SCE_NET_ERROR_EAGAIN
&& static_cast<unsigned>(res) != 0x804101A3)
{
// an actual error occured
break;
}
}
else
{
onNewConnection(res);
}
// yield
sceKernelDelayThread(100);
}
}
template <typename TConnection>
void Server<TConnection>::onNewConnection(int socket)
{
TConnection connection(socket);
while (connection.valid())
{
// yield
sceKernelDelayThread(100);
}
}