-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
executable file
·140 lines (116 loc) · 2.96 KB
/
server.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "server.hpp"
//#include "client.hpp"
#include "remoteclient.hpp"
#include "main.hpp"
#include "packet.hpp"
#include <string>
#include <cassert>
Server::Server(short port, SOCKET sockHandle)
: NetworkNode(port, sockHandle)
{
if (!listenOn())
EXIT_ASSERT;
}
Server::~Server()
{
}
void Server::processIncoming()
{
unsigned int ipAddr;
SOCKET clientSocket = acceptConnection(&ipAddr);
int dummyLength = 1;
setsockopt(
clientSocket,
SOL_SOCKET,
SO_DONTLINGER,
reinterpret_cast<char *>(&dummyLength),
sizeof(dummyLength));
// If a client is trying to connect, add him to the game.
if (clientSocket != INVALID_SOCKET)
addClient(ipAddr, clientSocket);
NetworkNode::processIncoming();
}
void Server::addClient(unsigned int ipAddr, SOCKET clientSocket)
{
struct in_addr addr;
addr.S_un.S_addr = ntohl(ipAddr);
char ipStr[128];
strcpy(ipStr, inet_ntoa(addr));
RemoteClient *remoteClient = NEW RemoteClient(clientSocket);
network->addConnection(remoteClient);
}
int Server::getNumClients() const
{
// Don't count yourself as a client.
return network->getNumNodes() - 1;
}
bool Server::listenOn()
{
struct sockaddr_in sa;
int dummyLength = 1;
// Create socket handle
socketHandle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketHandle == INVALID_SOCKET)
return false;
// Reuse server socket addresses even if busy
if (setsockopt(
socketHandle,
SOL_SOCKET,
SO_REUSEADDR,
reinterpret_cast<char *>(&dummyLength),
sizeof(dummyLength)) == SOCKET_ERROR)
{
closesocket(socketHandle);
socketHandle = INVALID_SOCKET;
return false;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(portNumber);
// Bind the socket
if (bind(socketHandle, reinterpret_cast<struct sockaddr *>(&sa), sizeof(sa)) == SOCKET_ERROR)
{
closesocket(socketHandle);
socketHandle = INVALID_SOCKET;
return false;
}
// Start listening
if (listen(socketHandle, 256) == SOCKET_ERROR)
{
closesocket(socketHandle);
socketHandle = INVALID_SOCKET;
return false;
}
// Set socket to non-blocking
unsigned long val = 1;
if (ioctlsocket(socketHandle, FIONBIO, &val) == SOCKET_ERROR)
{
closesocket(socketHandle);
socketHandle = INVALID_SOCKET;
return false;
}
return true;
}
SOCKET Server::acceptConnection(unsigned int *ipData)
{
SOCKET newSocketHandle;
struct sockaddr_in sa;
int socketSize = sizeof(sa);
newSocketHandle = accept(
socketHandle,
reinterpret_cast<struct sockaddr *>(&sa),
&socketSize);
if (newSocketHandle == INVALID_SOCKET)
return INVALID_SOCKET;
if (getpeername(
newSocketHandle,
reinterpret_cast<struct sockaddr *>(&sa),
&socketSize) == SOCKET_ERROR)
{
closesocket(newSocketHandle);
return INVALID_SOCKET;
}
*ipData = ntohl(sa.sin_addr.s_addr);
return newSocketHandle;
}