[r2066]: trunk / Dependencies / nlfg / nlfg.c Maximize Restore History

Download this file

nlfg.c    451 lines (392 with data), 11.0 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
Copyright (c) 2010 David Athay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
#include "nlfg.h"
#include <enet/enet.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
const int MAX_QUEUE_SIZE = 32;
struct NLFG_Queue
{
NLFG_Message *head;
} nlfgQueue;
ENetHost *host;
ENetPeer *peer;
int connected = 0;
void nlfg_expand(NLFG_Message *msg, int size)
{
if (size > msg->size)
{
char *tempData;
int oldSize = msg->size;
while (msg->size < size)
{
msg->size = msg->size << 1;
}
tempData = (char*)malloc(oldSize);
memcpy(tempData, msg->data, oldSize);
free(msg->data);
msg->data = (char*)malloc(msg->size);
memcpy(msg->data, tempData, oldSize);
free(tempData);
}
}
int nlfg_addMessage(NLFG_Message *msg)
{
if (nlfgQueue.head == 0)
{
nlfgQueue.head = msg;
msg->child = 0;
}
else
{
NLFG_Message *next = nlfgQueue.head;
while (next->child != 0)
{
next = next->child;
}
next->child = msg;
msg->child = 0;
}
return 0;
}
int nlfg_init_client()
{
enet_initialize();
atexit(enet_deinitialize);
peer = 0;
#ifdef ENET_VERSION
// enet 1.3
host = enet_host_create (NULL,
1 /* only allow 1 outgoing connection */,
0,
57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
nlfgQueue.head = 0;
#else
// enet 1.2
host = enet_host_create (NULL,
1 /* only allow 1 outgoing connection */,
57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
nlfgQueue.head = 0;
#endif
return 1;
}
int nlfg_init_server(unsigned int port)
{
enet_initialize();
atexit(enet_deinitialize);
ENetAddress addr;
addr.host = ENET_HOST_ANY;
addr.port = port;
#ifdef ENET_VERSION
// enet 1.3
host = enet_host_create (&addr,
32 /* only allow 32 incoming connections */,
0, 0, 0);
#else
// enet 1.2
host = enet_host_create (&addr,
32 /* only allow 32 incoming connections */,
0, 0);
#endif
nlfgQueue.head = 0;
return 1;
}
void nlfg_init_packet(NLFG_Message *msg)
{
msg->data = (char*)malloc(5);
msg->size = 5;
msg->position = 0;
msg->id = 0;
msg->reliability = RELIABLE;
msg->child = 0;
}
unsigned int nlfg_connect(const char *hostname, unsigned int port)
{
ENetAddress address;
enet_address_set_host(&address, hostname);
address.port = port;
#ifdef ENET_VERSION
// enet 1.3
peer = enet_host_connect(host, &address, 0, 1);
#else
// enet 1.2
peer = enet_host_connect(host, &address, 1);
#endif
if (peer)
return 1;
return 0;
}
void nlfg_disconnect()
{
if (peer)
enet_peer_disconnect(peer, 0);
}
int nlfg_isConnected()
{
return connected;
}
int nlfg_process()
{
ENetEvent event;
int packets = 0;
// check for data
while (enet_host_service(host, &event, 0) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT:
{
connected = 1;
NLFG_Message *msg = (NLFG_Message*)(malloc(sizeof(NLFG_Message)));
msg->addr.peer = event.peer;
msg->data = 0;
msg->size = 0;
msg->position = 0;
msg->reliability = ENET_PACKET_FLAG_RELIABLE;
msg->id = NLFG_CONNECTED;
nlfg_addMessage(msg);
++packets;
} break;
case ENET_EVENT_TYPE_RECEIVE:
{
NLFG_Message *msg = (NLFG_Message*)(malloc(sizeof(NLFG_Message)));
msg->addr.peer = event.peer;
msg->data = (char*)malloc(event.packet->dataLength);
memcpy(msg->data, event.packet->data, event.packet->dataLength);
msg->size = event.packet->dataLength;
msg->position = 0;
msg->reliability = ENET_PACKET_FLAG_RELIABLE;
msg->id = nlfg_readInteger(msg);
nlfg_addMessage(msg);
enet_packet_destroy (event.packet);
++packets;
} break;
case ENET_EVENT_TYPE_DISCONNECT:
{
connected = 0;
NLFG_Message *msg = (NLFG_Message*)(malloc(sizeof(NLFG_Message)));
msg->addr.peer = event.peer;
msg->data = 0;
msg->size = 0;
msg->position = 0;
msg->reliability = ENET_PACKET_FLAG_RELIABLE;
msg->id = NLFG_DISCONNECTED;
nlfg_addMessage(msg);
++packets;
} break;
}
}
return packets;
}
NLFG_Message* nlfg_getMessage()
{
if (nlfgQueue.head == 0)
return NULL;
NLFG_Message *msg = nlfgQueue.head;
nlfgQueue.head = msg->child;
return msg;
}
unsigned int nlfg_getPosition(NLFG_Message *msg)
{
return msg->position;
}
unsigned int nlfg_getSize(NLFG_Message *msg)
{
return msg->size;
}
unsigned int nlfg_getId(NLFG_Message *msg)
{
return msg->id;
}
void nlfg_setId(NLFG_Message *msg, unsigned int id)
{
msg->id = id;
}
void nlfg_setReliability(NLFG_Message *msg, unsigned int rel)
{
msg->reliability = rel;
}
char nlfg_readByte(NLFG_Message *msg)
{
int c = -1;
if (msg->position < msg->size)
{
c = (unsigned char) msg->data[msg->position];
}
msg->position += 1;
return c;
}
int nlfg_readShort(NLFG_Message *msg)
{
int num = -1;
if (msg->position + 2 <= msg->size)
{
uint16_t t;
memcpy(&t, msg->data + msg->position, 2);
num = ENET_NET_TO_HOST_16(t);
}
msg->position += 2;
return num;
}
int nlfg_readInteger(NLFG_Message *msg)
{
int num = -1;
if (msg->position + 4 <= msg->size)
{
uint32_t t;
memcpy(&t, &msg->data[msg->position], 4);
num = ENET_NET_TO_HOST_32(t);
}
msg->position += 4;
return num;
}
float nlfg_readFloat(NLFG_Message *msg)
{
float f = -1.0f;
int i = nlfg_readInteger(msg);
f = *((float*)(&i));
return f;
}
double nlfg_readDouble(NLFG_Message *msg)
{
double d = -1.0f;
int i1 = nlfg_readInteger(msg);
int i2 = nlfg_readInteger(msg);
uint64_t i = (uint64_t)i1 << 32 | i2;
d = *((double*)(&i));
return d;
}
char* nlfg_readString(NLFG_Message *msg)
{
char *s = (char*)(malloc(256));
int pos = 0;
while (msg->position < msg->size && msg->data[msg->position] != '\0' && pos < 255)
{
s[pos] = msg->data[msg->position];
++(msg->position);
++pos;
}
if (msg->data[msg->position] == '\0')
++(msg->position);
return s;
}
void nlfg_readStringWithSize(NLFG_Message *msg, char *data, int size)
{
if (msg->position + size > msg->size)
{
data = NULL;
return;
}
memcpy(data, &msg->data[msg->position], size);
msg->position += size;
}
void nlfg_writeByte(NLFG_Message *msg, char c)
{
nlfg_expand(msg, msg->position + 1);
msg->data[msg->position] = c;
msg->position += 1;
}
void nlfg_writeShort(NLFG_Message *msg, int i)
{
nlfg_expand(msg, msg->position + 2);
uint32_t t = ENET_HOST_TO_NET_16(i);
memcpy(msg->data + msg->position, &t, 2);
msg->position += 2;
}
void nlfg_writeInteger(NLFG_Message *msg, int i)
{
nlfg_expand(msg, msg->position + 4);
uint32_t t = ENET_HOST_TO_NET_32(i);
memcpy(&msg->data[msg->position], &t, 4);
msg->position += 4;
}
void nlfg_writeFloat(NLFG_Message *msg, float f)
{
unsigned int i = *((unsigned int *)(&f));
nlfg_writeInteger(msg, i);
}
void nlfg_writeDouble(NLFG_Message *msg, double d)
{
uint64_t i = *((uint64_t*)(&d));
uint32_t i2 = (uint32_t)i;
uint32_t i1 = (uint32_t)(i >> 32);
nlfg_writeInteger(msg, i1);
nlfg_writeInteger(msg, i2);
}
void nlfg_writeString(NLFG_Message *msg, const char *s)
{
/* string requires null terminator */
int size = strlen(s)+1;
nlfg_expand(msg, msg->position + size);
memcpy(msg->data + msg->position, s, size);
msg->position += size;
}
void nlfg_writeStringWithSize(NLFG_Message *msg, const char *s, int size)
{
nlfg_expand(msg, msg->position + size);
memcpy(&msg->data[msg->position], s, size);
msg->position += size;
}
void nlfg_destroyPacket(NLFG_Message *msg)
{
free(msg->data);
free(msg);
}
void nlfg_sendPacket(NLFG_Message *msg)
{
if (msg && nlfg_isConnected())
{
char *data = (char*)malloc(msg->position + 4);
memcpy(data, &msg->id, 4);
memcpy(&data[4], msg->data, msg->position);
ENetPacket *p = enet_packet_create(data, msg->position + 4, msg->reliability);
enet_peer_send(peer, 0, p);
enet_host_flush(host);
free(data);
}
}
void nlfg_sendPacketToClient(NLFG_Message *msg, struct NLFG_Address *addr)
{
if (msg && nlfg_isConnected())
{
char *data = (char*)malloc(msg->position + 4);
memcpy(data, &msg->id, 4);
memcpy(&data[4], msg->data, msg->position);
ENetPacket *p = enet_packet_create(data, msg->position + 4, msg->reliability);
enet_peer_send(addr->peer, 0, p);
enet_host_flush(host);
free(data);
}
}
void nlfg_copyAddress(struct NLFG_Address *dest, const struct NLFG_Address *src)
{
dest->peer = src->peer;
}
int nlfg_matchAddress(const struct NLFG_Address *addr1, const struct NLFG_Address *addr2)
{
if (addr1->peer->address.host != addr2->peer->address.host || addr1->peer->address.port != addr2->peer->address.port)
{
/* no match */
return 0;
}
return 1;
}