2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 //====================================================================
26 // Programmer: MrElusive
30 //====================================================================
37 #include "l_net_wins.h"
39 #define GetMemory malloc
40 #define FreeMemory free
46 void WinPrint(char *str, ...)
51 va_start (argptr,str);
52 vsprintf (text, str, argptr);
58 void WinPrint(char *str, ...)
63 //===========================================================================
68 //===========================================================================
69 void Net_SetAddressPort(address_t *address, int port)
73 WINS_StringToAddr(address->ip, &addr);
74 WINS_SetSocketPort(&addr, port);
75 strcpy(address->ip, WINS_AddrToString(&addr));
76 } //end of the function Net_SetAddressPort
77 //===========================================================================
82 //===========================================================================
83 int Net_AddressCompare(address_t *addr1, address_t *addr2)
86 return stricmp(addr1->ip, addr2->ip);
89 return strcasecmp(addr1->ip, addr2->ip);
91 } //end of the function Net_AddressCompare
92 //===========================================================================
97 //===========================================================================
98 void Net_SocketToAddress(socket_t *sock, address_t *address)
100 strcpy(address->ip, WINS_AddrToString(&sock->addr));
101 } //end of the function Net_SocketToAddress
102 //===========================================================================
106 // Changes Globals: -
107 //===========================================================================
108 int Net_Send(socket_t *sock, netmessage_t *msg)
114 NMSG_WriteLong(msg, size-4);
116 //WinPrint("Net_Send: message of size %d\n", sendmsg.size);
117 return WINS_Write(sock->socket, msg->data, msg->size, NULL);
118 } //end of the function Net_SendSocketReliable
119 //===========================================================================
120 // returns the number of bytes recieved
125 // Changes Globals: -
126 //===========================================================================
127 int Net_Receive(socket_t *sock, netmessage_t *msg)
131 if (sock->remaining > 0)
133 curread = WINS_Read(sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL);
136 WinPrint("Net_Receive: read error\n");
139 sock->remaining -= curread;
140 sock->msg.size += curread;
141 if (sock->remaining <= 0)
144 memcpy(msg, &sock->msg, sizeof(netmessage_t));
146 return msg->size - 4;
150 sock->msg.size = WINS_Read(sock->socket, sock->msg.data, 4, NULL);
151 if (sock->msg.size == 0) return 0;
152 if (sock->msg.size == -1)
154 WinPrint("Net_Receive: size header read error\n");
157 //WinPrint("Net_Receive: message size header %d\n", msg->size);
159 sock->remaining = NMSG_ReadLong(&sock->msg);
160 if (sock->remaining == 0) return 0;
161 if (sock->remaining < 0 || sock->remaining > MAX_NETMESSAGE)
163 WinPrint("Net_Receive: invalid message size %d\n", sock->remaining);
166 //try to read the message
167 curread = WINS_Read(sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL);
170 WinPrint("Net_Receive: read error\n");
173 sock->remaining -= curread;
174 sock->msg.size += curread;
175 if (sock->remaining <= 0)
178 memcpy(msg, &sock->msg, sizeof(netmessage_t));
180 return msg->size - 4;
182 //the message has not been completely read yet
184 printf("++timo TODO: debug the Net_Receive on big size messages\n");
187 } //end of the function Net_Receive
188 //===========================================================================
192 // Changes Globals: -
193 //===========================================================================
194 socket_t *Net_AllocSocket(void)
198 sock = (socket_t *) GetMemory(sizeof(socket_t));
199 memset(sock, 0, sizeof(socket_t));
201 } //end of the function Net_AllocSocket
202 //===========================================================================
206 // Changes Globals: -
207 //===========================================================================
208 void Net_FreeSocket(socket_t *sock)
211 } //end of the function Net_FreeSocket
212 //===========================================================================
216 // Changes Globals: -
217 //===========================================================================
218 socket_t *Net_Connect(address_t *address, int port)
224 // see if we can resolve the host name
225 WINS_StringToAddr(address->ip, &sendaddr);
227 newsock = WINS_OpenReliableSocket(port);
228 if (newsock == -1) return NULL;
230 sock = Net_AllocSocket();
233 WINS_CloseSocket(newsock);
236 sock->socket = newsock;
238 //connect to the host
239 if (WINS_Connect(newsock, &sendaddr) == -1)
241 Net_FreeSocket(sock);
242 WINS_CloseSocket(newsock);
243 WinPrint("Net_Connect: error connecting\n");
247 memcpy(&sock->addr, &sendaddr, sizeof(sockaddr_t));
248 //now we can send messages
251 } //end of the function Net_Connect
253 //===========================================================================
257 // Changes Globals: -
258 //===========================================================================
259 socket_t *Net_ListenSocket(int port)
264 newsock = WINS_OpenReliableSocket(port);
265 if (newsock == -1) return NULL;
267 if (WINS_Listen(newsock) == -1)
269 WINS_CloseSocket(newsock);
272 sock = Net_AllocSocket();
275 WINS_CloseSocket(newsock);
278 sock->socket = newsock;
279 WINS_GetSocketAddr(newsock, &sock->addr);
280 WinPrint("listen socket opened at %s\n", WINS_AddrToString(&sock->addr));
283 } //end of the function Net_ListenSocket
284 //===========================================================================
288 // Changes Globals: -
289 //===========================================================================
290 socket_t *Net_Accept(socket_t *sock)
296 newsocket = WINS_Accept(sock->socket, &sendaddr);
297 if (newsocket == -1) return NULL;
299 newsock = Net_AllocSocket();
302 WINS_CloseSocket(newsocket);
305 newsock->socket = newsocket;
306 memcpy(&newsock->addr, &sendaddr, sizeof(sockaddr_t));
309 } //end of the function Net_Accept
310 //===========================================================================
314 // Changes Globals: -
315 //===========================================================================
316 void Net_Disconnect(socket_t *sock)
318 WINS_CloseSocket(sock->socket);
319 Net_FreeSocket(sock);
320 } //end of the function Net_Disconnect
321 //===========================================================================
325 // Changes Globals: -
326 //===========================================================================
327 void Net_StringToAddress(char *string, address_t *address)
329 strcpy(address->ip, string);
330 } //end of the function Net_StringToAddress
331 //===========================================================================
335 // Changes Globals: -
336 //===========================================================================
337 void Net_MyAddress(address_t *address)
339 strcpy(address->ip, WINS_MyAddress());
340 } //end of the function Net_MyAddress
341 //===========================================================================
345 // Changes Globals: -
346 //===========================================================================
353 WinPrint("my address is %s\n", WINS_MyAddress());
356 } //end of the function Net_Setup
357 //===========================================================================
361 // Changes Globals: -
362 //===========================================================================
363 void Net_Shutdown(void)
366 } //end of the function Net_Shutdown
367 //===========================================================================
371 // Changes Globals: -
372 //===========================================================================
373 void NMSG_Clear(netmessage_t *msg)
376 } //end of the function NMSG_Clear
377 //===========================================================================
381 // Changes Globals: -
382 //===========================================================================
383 void NMSG_WriteChar (netmessage_t *msg, int c)
385 if (c < -128 || c > 127)
386 WinPrint("NMSG_WriteChar: range error\n");
388 if (msg->size >= MAX_NETMESSAGE)
390 WinPrint("NMSG_WriteChar: overflow\n");
393 msg->data[msg->size] = c;
395 } //end of the function NMSG_WriteChar
396 //===========================================================================
400 // Changes Globals: -
401 //===========================================================================
402 void NMSG_WriteByte(netmessage_t *msg, int c)
404 if (c < -128 || c > 127)
405 WinPrint("NMSG_WriteByte: range error\n");
407 if (msg->size + 1 >= MAX_NETMESSAGE)
409 WinPrint("NMSG_WriteByte: overflow\n");
412 msg->data[msg->size] = c;
414 } //end of the function NMSG_WriteByte
415 //===========================================================================
419 // Changes Globals: -
420 //===========================================================================
421 void NMSG_WriteShort(netmessage_t *msg, int c)
423 if (c < ((short)0x8000) || c > (short)0x7fff)
424 WinPrint("NMSG_WriteShort: range error");
426 if (msg->size + 2 >= MAX_NETMESSAGE)
428 WinPrint("NMSG_WriteShort: overflow\n");
431 msg->data[msg->size] = c&0xff;
432 msg->data[msg->size+1] = c>>8;
434 } //end of the function NMSG_WriteShort
435 //===========================================================================
439 // Changes Globals: -
440 //===========================================================================
441 void NMSG_WriteLong(netmessage_t *msg, int c)
443 if (msg->size + 4 >= MAX_NETMESSAGE)
445 WinPrint("NMSG_WriteLong: overflow\n");
448 msg->data[msg->size] = c&0xff;
449 msg->data[msg->size+1] = (c>>8)&0xff;
450 msg->data[msg->size+2] = (c>>16)&0xff;
451 msg->data[msg->size+3] = c>>24;
453 } //end of the function NMSG_WriteLong
454 //===========================================================================
458 // Changes Globals: -
459 //===========================================================================
460 void NMSG_WriteFloat(netmessage_t *msg, float c)
462 if (msg->size + 4 >= MAX_NETMESSAGE)
464 WinPrint("NMSG_WriteLong: overflow\n");
467 msg->data[msg->size] = *((int *)&c)&0xff;
468 msg->data[msg->size+1] = (*((int *)&c)>>8)&0xff;
469 msg->data[msg->size+2] = (*((int *)&c)>>16)&0xff;
470 msg->data[msg->size+3] = *((int *)&c)>>24;
472 } //end of the function NMSG_WriteFloat
473 //===========================================================================
477 // Changes Globals: -
478 //===========================================================================
479 void NMSG_WriteString(netmessage_t *msg, char *string)
481 if (msg->size + strlen(string) + 1 >= MAX_NETMESSAGE)
483 WinPrint("NMSG_WriteString: overflow\n");
486 memcpy(&msg->data[msg->size], string, strlen(string) + 1);
487 msg->size += strlen(string) + 1;
488 } //end of the function NMSG_WriteString
489 //===========================================================================
493 // Changes Globals: -
494 //===========================================================================
495 void NMSG_ReadStart(netmessage_t *msg)
497 msg->readoverflow = qfalse;
499 } //end of the function NMSG_ReadStart
500 //===========================================================================
504 // Changes Globals: -
505 //===========================================================================
506 int NMSG_ReadChar(netmessage_t *msg)
508 if (msg->read + 1 > msg->size)
510 msg->readoverflow = qtrue;
511 WinPrint("NMSG_ReadChar: read overflow\n");
515 return msg->data[msg->read-1];
516 } //end of the function NMSG_ReadChar
517 //===========================================================================
521 // Changes Globals: -
522 //===========================================================================
523 int NMSG_ReadByte(netmessage_t *msg)
525 if (msg->read + 1 > msg->size)
527 msg->readoverflow = qtrue;
528 WinPrint("NMSG_ReadByte: read overflow\n");
532 return msg->data[msg->read-1];
533 } //end of the function NMSG_ReadByte
534 //===========================================================================
538 // Changes Globals: -
539 //===========================================================================
540 int NMSG_ReadShort(netmessage_t *msg)
544 if (msg->read + 2 > msg->size)
546 msg->readoverflow = qtrue;
547 WinPrint("NMSG_ReadShort: read overflow\n");
550 c = (short)(msg->data[msg->read] + (msg->data[msg->read+1]<<8));
553 } //end of the function NMSG_ReadShort
554 //===========================================================================
558 // Changes Globals: -
559 //===========================================================================
560 int NMSG_ReadLong(netmessage_t *msg)
564 if (msg->read + 4 > msg->size)
566 msg->readoverflow = qtrue;
567 WinPrint("NMSG_ReadLong: read overflow\n");
570 c = msg->data[msg->read]
571 + (msg->data[msg->read+1]<<8)
572 + (msg->data[msg->read+2]<<16)
573 + (msg->data[msg->read+3]<<24);
576 } //end of the function NMSG_ReadLong
577 //===========================================================================
581 // Changes Globals: -
582 //===========================================================================
583 float NMSG_ReadFloat(netmessage_t *msg)
587 if (msg->read + 4 > msg->size)
589 msg->readoverflow = qtrue;
590 WinPrint("NMSG_ReadLong: read overflow\n");
593 c = msg->data[msg->read]
594 + (msg->data[msg->read+1]<<8)
595 + (msg->data[msg->read+2]<<16)
596 + (msg->data[msg->read+3]<<24);
599 } //end of the function NMSG_ReadFloat
600 //===========================================================================
604 // Changes Globals: -
605 //===========================================================================
606 char *NMSG_ReadString(netmessage_t *msg)
608 static char string[2048];
614 if (msg->read + 1 > msg->size)
616 msg->readoverflow = qtrue;
617 WinPrint("NMSG_ReadString: read overflow\n");
621 c = msg->data[msg->read];
626 } while (l < sizeof(string)-1);
629 } //end of the function NMSG_ReadString