2 Copyright (C) 1999-2006 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 //===========================================================================
351 WinPrint("my address is %s\n", WINS_MyAddress());
354 } //end of the function Net_Setup
355 //===========================================================================
359 // Changes Globals: -
360 //===========================================================================
361 void Net_Shutdown(void)
364 } //end of the function Net_Shutdown
365 //===========================================================================
369 // Changes Globals: -
370 //===========================================================================
371 void NMSG_Clear(netmessage_t *msg)
374 } //end of the function NMSG_Clear
375 //===========================================================================
379 // Changes Globals: -
380 //===========================================================================
381 void NMSG_WriteChar (netmessage_t *msg, int c)
383 if (c < -128 || c > 127)
384 WinPrint("NMSG_WriteChar: range error\n");
386 if (msg->size >= MAX_NETMESSAGE)
388 WinPrint("NMSG_WriteChar: overflow\n");
391 msg->data[msg->size] = c;
393 } //end of the function NMSG_WriteChar
394 //===========================================================================
398 // Changes Globals: -
399 //===========================================================================
400 void NMSG_WriteByte(netmessage_t *msg, int c)
402 if (c < -128 || c > 127)
403 WinPrint("NMSG_WriteByte: range error\n");
405 if (msg->size + 1 >= MAX_NETMESSAGE)
407 WinPrint("NMSG_WriteByte: overflow\n");
410 msg->data[msg->size] = c;
412 } //end of the function NMSG_WriteByte
413 //===========================================================================
417 // Changes Globals: -
418 //===========================================================================
419 void NMSG_WriteShort(netmessage_t *msg, int c)
421 if (c < ((short)0x8000) || c > (short)0x7fff)
422 WinPrint("NMSG_WriteShort: range error");
424 if (msg->size + 2 >= MAX_NETMESSAGE)
426 WinPrint("NMSG_WriteShort: overflow\n");
429 msg->data[msg->size] = c&0xff;
430 msg->data[msg->size+1] = c>>8;
432 } //end of the function NMSG_WriteShort
433 //===========================================================================
437 // Changes Globals: -
438 //===========================================================================
439 void NMSG_WriteLong(netmessage_t *msg, int c)
441 if (msg->size + 4 >= MAX_NETMESSAGE)
443 WinPrint("NMSG_WriteLong: overflow\n");
446 msg->data[msg->size] = c&0xff;
447 msg->data[msg->size+1] = (c>>8)&0xff;
448 msg->data[msg->size+2] = (c>>16)&0xff;
449 msg->data[msg->size+3] = c>>24;
451 } //end of the function NMSG_WriteLong
452 //===========================================================================
456 // Changes Globals: -
457 //===========================================================================
458 void NMSG_WriteFloat(netmessage_t *msg, float c)
460 if (msg->size + 4 >= MAX_NETMESSAGE)
462 WinPrint("NMSG_WriteLong: overflow\n");
465 msg->data[msg->size] = *((int *)&c)&0xff;
466 msg->data[msg->size+1] = (*((int *)&c)>>8)&0xff;
467 msg->data[msg->size+2] = (*((int *)&c)>>16)&0xff;
468 msg->data[msg->size+3] = *((int *)&c)>>24;
470 } //end of the function NMSG_WriteFloat
471 //===========================================================================
475 // Changes Globals: -
476 //===========================================================================
477 void NMSG_WriteString(netmessage_t *msg, char *string)
479 if (msg->size + strlen(string) + 1 >= MAX_NETMESSAGE)
481 WinPrint("NMSG_WriteString: overflow\n");
484 strcpy(&msg->data[msg->size], string);
485 msg->size += strlen(string) + 1;
486 } //end of the function NMSG_WriteString
487 //===========================================================================
491 // Changes Globals: -
492 //===========================================================================
493 void NMSG_ReadStart(netmessage_t *msg)
495 msg->readoverflow = qfalse;
497 } //end of the function NMSG_ReadStart
498 //===========================================================================
502 // Changes Globals: -
503 //===========================================================================
504 int NMSG_ReadChar(netmessage_t *msg)
506 if (msg->size + 1 > msg->size)
508 msg->readoverflow = qtrue;
509 WinPrint("NMSG_ReadChar: read overflow\n");
513 return msg->data[msg->read-1];
514 } //end of the function NMSG_ReadChar
515 //===========================================================================
519 // Changes Globals: -
520 //===========================================================================
521 int NMSG_ReadByte(netmessage_t *msg)
523 if (msg->read + 1 > msg->size)
525 msg->readoverflow = qtrue;
526 WinPrint("NMSG_ReadByte: read overflow\n");
530 return msg->data[msg->read-1];
531 } //end of the function NMSG_ReadByte
532 //===========================================================================
536 // Changes Globals: -
537 //===========================================================================
538 int NMSG_ReadShort(netmessage_t *msg)
542 if (msg->read + 2 > msg->size)
544 msg->readoverflow = qtrue;
545 WinPrint("NMSG_ReadShort: read overflow\n");
548 c = (short)(msg->data[msg->read] + (msg->data[msg->read+1]<<8));
551 } //end of the function NMSG_ReadShort
552 //===========================================================================
556 // Changes Globals: -
557 //===========================================================================
558 int NMSG_ReadLong(netmessage_t *msg)
562 if (msg->read + 4 > msg->size)
564 msg->readoverflow = qtrue;
565 WinPrint("NMSG_ReadLong: read overflow\n");
568 c = msg->data[msg->read]
569 + (msg->data[msg->read+1]<<8)
570 + (msg->data[msg->read+2]<<16)
571 + (msg->data[msg->read+3]<<24);
574 } //end of the function NMSG_ReadLong
575 //===========================================================================
579 // Changes Globals: -
580 //===========================================================================
581 float NMSG_ReadFloat(netmessage_t *msg)
585 if (msg->read + 4 > msg->size)
587 msg->readoverflow = qtrue;
588 WinPrint("NMSG_ReadLong: read overflow\n");
591 c = msg->data[msg->read]
592 + (msg->data[msg->read+1]<<8)
593 + (msg->data[msg->read+2]<<16)
594 + (msg->data[msg->read+3]<<24);
597 } //end of the function NMSG_ReadFloat
598 //===========================================================================
602 // Changes Globals: -
603 //===========================================================================
604 char *NMSG_ReadString(netmessage_t *msg)
606 static char string[2048];
612 if (msg->read + 1 > msg->size)
614 msg->readoverflow = qtrue;
615 WinPrint("NMSG_ReadString: read overflow\n");
619 c = msg->data[msg->read];
624 } while (l < sizeof(string)-1);
627 } //end of the function NMSG_ReadString