-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncServerClient.c
More file actions
253 lines (227 loc) · 7.16 KB
/
ncServerClient.c
File metadata and controls
253 lines (227 loc) · 7.16 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "toyserver.h"
#include <time.h>
static List Clients;
static int MaxNbClients;
static void (*ncServerClientDisconnectCallback)(Client *);
static void (*ncServerClientNewCallback)(Client *);
static void (*ncServerClientInfoCallback)(NetMsgT7 *);
static void clientSocketCallback(void *arg);
void ncServerRemoveClient(Client *client) {
ncListDelete(&Clients, (ListItem *)client);
}
void ncServerDisconnectClient(Client *client)
{
if (client->status >= CliConnected)
{
if (ncServerClientDisconnectCallback != NULL)
(*ncServerClientDisconnectCallback)(client);
client->status = CliDisconnecting;
char *ip = ncGetAddrString(client->ipAddress);
ncLogPrintf(1,"DISCONNECT : Client '%s' (ID=%d) IP %s , %d, udp %d on socket %d.",
client->listItem.name, client->listItem.id, ip, ntohs(client->tcpPort), client->udpPort, client->sock.fd);
ncSocketClose(&client->sock.fd);
}
}
int ncServerSendClientTcpMsg(Client *client, NetMsg *msg)
{
if (client == NULL || msg == NULL)
return 0;
if (client->status <= CliDisconnecting)
return 0;
if (ncSocketTcpSendMsg(&client->sock, msg) == 0)
{
ncLogPrintf(1, "ERROR sending TCP message to client '%s' (ID=%d).", client->listItem.name,
client->listItem.id);
ncServerDisconnectClient(client);
return 0;
}
return 1;
}
int ncServerSendClientUdpMsg(Client *client, NetMsg *msg)
{
if (client == NULL || msg == NULL)
return 0;
if (client->status <= CliDisconnecting)
return 0;
int rc = ncSocketUdpSendMsg(ncServerUdpSocket, client->ipAddress, client->udpPort, msg);
if (rc == 0) {
ncLogPrintf(1, "ERROR sending UDP message to client '%s' (ID=%d).", client->listItem.name,
client->listItem.id);
return 0;
}
return 1;
}
void ncServerSendAllClientsStandardTcpMsg(short msgType, int msgId)
{
NetMsg msg;
msg.msgType = msgType;
msg.msgId = msgId;
msg.size = 12;
for (Client *client = (Client *)Clients.head; client != NULL;
client = (Client *)client->listItem.next) {
ncServerSendClientTcpMsg(client, &msg);
}
}
int ncServerSendClientTcpStandardMsg(Client *client, short msgType, int msgId)
{
if (client == NULL)
return 0;
NetMsg msg;
msg.msgType = msgType;
msg.msgId = msgId;
msg.size = 12;
return ncServerSendClientTcpMsg(client, &msg);
}
int ncServerLoginCallback(int sockfd, uint32_t srcIp, uint16_t tcpPort, NetMsgT1 *msg)
{
if (msg->clientVersion != SERVER_VERSION) {
ncSocketTcpSendStandardMsg(sockfd, 41, SERVER_VERSION);
return 0;
}
if (msg->head.msgId != 0) {
ncSocketTcpSendStandardMsg(sockfd, 4, 0);
return 0;
}
if (Clients.length >= Clients.capacity) {
ncLogPrintf(1, "!!! SERVER IS FULL connection from %s , %d on socket %d rejected !!!",
ncGetAddrString(srcIp), ntohs(tcpPort), sockfd);
ncSocketTcpSendStandardMsg(sockfd, 6, 0);
}
int bufsize = 0x4000;
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &bufsize, 4) == -1)
{
ncLogPrintf(1, "ERROR ncServerLoginCallback()-> setsockopt( SO_SNDBUF ) failed : ");
ncSocketError(ncSocketGetLastError());
ncServerSocketRelease();
return 0;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &bufsize, 4) == -1)
{
ncLogPrintf(1, "ERROR ncServerLoginCallback()-> setsockopt( SO_RCVBUF ) failed : ");
ncSocketError(ncSocketGetLastError());
ncServerSocketRelease();
return 0;
}
char userName[16];
strcpy(userName, msg->userName);
int i = 0;
while (ncListCheckNameUsed(&Clients, userName) != 0)
{
i = i + 1;
if (999 < i) {
ncSocketTcpSendStandardMsg(sockfd, 4, 0);
return 0;
}
if (i != 0)
// the game limits user names to 10 chars max
sprintf(userName, "%.10s_%03d", msg->userName, i);
}
Client *client = (Client *)ncListAdd(&Clients, userName);
if (client == NULL)
return 0;
client->sock.fd = sockfd;
client->status = CliConnected;
client->ipAddress = srcIp;
client->tcpPort = tcpPort;
client->udpPort = msg->udpPort;
client->language = msg->language;
client->unk5 = msg->cli_unk5;
time(&client->connectTime);
client->timer = TimerRef + 5000;
const char *timestr = ncGetTimeString(client->connectTime);
char *addrstr = ncGetAddrString(client->ipAddress);
ncLogPrintf(1,"New client '%s' (ID=%d) from %s , %d, udp %d on socket %d at %s.",
client->listItem.name, client->listItem.id, addrstr, ntohs(tcpPort), client->udpPort, sockfd, timestr);
cancelAsyncRead(sockfd);
cancelAsyncWrite(sockfd);
asyncRead(sockfd, clientSocketCallback, client);
if (ncServerClientNewCallback != NULL)
(*ncServerClientNewCallback)(client);
strcpy(msg->userName, client->listItem.name);
msg->udpPort = ncServerUdpPort;
msg->head.msgId = client->listItem.id;
msg->udpTimeout = ncServerGetClientUdpTimeout();
ncServerGetClientTimers(&msg->dynPackPerSec, &msg->cmdPackPerSec);
if (ncServerSendClientTcpMsg(client, (NetMsg *)msg) != 0) {
ncChatSendList(client);
ncGameSendList(client);
}
return 1;
}
int ncServerClientInit(void) {
ncServerSetLoginCallback(ncServerLoginCallback);
return 1;
}
int ncServerClientAllocate(int maxPlayers) {
MaxNbClients = maxPlayers;
return ncListAllocate(&Clients, maxPlayers, sizeof(Client));
}
void ncServerClientRelease(void) {
ncListRelease(&Clients);
return;
}
void ncServerSetNewClientCallback(void *callback) {
ncServerClientNewCallback = callback;
}
void ncServerSetDisconnectClientCallback(void *callback) {
ncServerClientDisconnectCallback = callback;
}
void ncServerSendAllClientsTcpMsgExceptFrom(NetMsg *msg, int clientId)
{
for (Client *client = (Client *)Clients.head; client != NULL;
client = (Client *)client->listItem.next) {
if (client->listItem.id != clientId)
ncServerSendClientTcpMsg(client, msg);
}
}
void ncServerSendAllClientsTcpMsgExceptFromAndRoom(NetMsg *msg, int clientId, ChatRoom *chatRoom)
{
for (Client *client = (Client *)Clients.head; client != NULL;
client = (Client *)client->listItem.next) {
if (client->listItem.id != clientId && client->chatRoom != chatRoom)
ncServerSendClientTcpMsg(client, msg);
}
}
static void serverManageClient(Client *client, void *arg)
{
if (client->status <= CliDisconnecting) {
if (client->chatRoom == NULL)
ncServerRemoveClient(client);
}
else if (client->sock.sendBufSize == 0)
{
if (client->sock.fd != -1 && client->timer != 0 && client->timer < TimerRef)
{
client->timer = 0;
NetMsgT2 pingMsg;
pingMsg.timer = (uint32_t)TimerRef;
pingMsg.head.size = 16;
pingMsg.head.msgType = 2;
pingMsg.head.msgId = 0;
ncServerSendClientTcpMsg(client, (NetMsg *)&pingMsg);
}
}
}
void ncServerManageClients(void) {
ncServerEnumClients(serverManageClient, NULL);
}
static void clientSocketCallback(void *arg)
{
Client *client = (Client *)arg;
ssize_t len = ncSocketBufReadMsg(&client->sock, (void (*)(uint8_t *, void *))ncServerClientReadMsgCallback, client);
if (len < 0)
ncServerDisconnectClient(client);
}
Client *ncClientFindById(int clientId) {
return (Client *)ncListFindById(&Clients, clientId);
}
void ncServerEnumClients(void (*callback)(Client *, void *), void *arg) {
ncListEnum(&Clients, (ListEnumCallback)callback, arg);
}
void ncServerClientReadClientInfosMessage(Client *client, NetMsg *msg) {
if (msg->size == 472 && ncServerClientInfoCallback != NULL)
(*ncServerClientInfoCallback)((NetMsgT7 *)msg);
}
void ncServerClientSetInfosCallback(void *callback) {
ncServerClientInfoCallback = callback;
}