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);
88 return strcasecmp(addr1->ip, addr2->ip);
90 } //end of the function Net_AddressCompare
91 //===========================================================================
96 //===========================================================================
97 void Net_SocketToAddress(socket_t *sock, address_t *address)
99 strcpy(address->ip, WINS_AddrToString(&sock->addr));
100 } //end of the function Net_SocketToAddress
101 //===========================================================================
105 // Changes Globals: -
106 //===========================================================================
107 int Net_Send(socket_t *sock, netmessage_t *msg)
113 NMSG_WriteLong(msg, size-4);
115 //WinPrint("Net_Send: message of size %d\n", sendmsg.size);
116 return WINS_Write(sock->socket, msg->data, msg->size, NULL);
117 } //end of the function Net_SendSocketReliable
118 //===========================================================================
119 // returns the number of bytes recieved
124 // Changes Globals: -
125 //===========================================================================
126 int Net_Receive(socket_t *sock, netmessage_t *msg)
130 if (sock->remaining > 0)
132 curread = WINS_Read(sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL);
135 WinPrint("Net_Receive: read error\n");
138 sock->remaining -= curread;
139 sock->msg.size += curread;
140 if (sock->remaining <= 0)
143 memcpy(msg, &sock->msg, sizeof(netmessage_t));
145 return msg->size - 4;
149 sock->msg.size = WINS_Read(sock->socket, sock->msg.data, 4, NULL);
150 if (sock->msg.size == 0) return 0;
151 if (sock->msg.size == -1)
153 WinPrint("Net_Receive: size header read error\n");
156 //WinPrint("Net_Receive: message size header %d\n", msg->size);
158 sock->remaining = NMSG_ReadLong(&sock->msg);
159 if (sock->remaining == 0) return 0;
160 if (sock->remaining < 0 || sock->remaining > MAX_NETMESSAGE)
162 WinPrint("Net_Receive: invalid message size %d\n", sock->remaining);
165 //try to read the message
166 curread = WINS_Read(sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL);
169 WinPrint("Net_Receive: read error\n");
172 sock->remaining -= curread;
173 sock->msg.size += curread;
174 if (sock->remaining <= 0)
177 memcpy(msg, &sock->msg, sizeof(netmessage_t));
179 return msg->size - 4;
181 //the message has not been completely read yet
183 printf("++timo TODO: debug the Net_Receive on big size messages\n");
186 } //end of the function Net_Receive
187 //===========================================================================
191 // Changes Globals: -
192 //===========================================================================
193 socket_t *Net_AllocSocket(void)
197 sock = (socket_t *) GetMemory(sizeof(socket_t));
198 memset(sock, 0, sizeof(socket_t));
200 } //end of the function Net_AllocSocket
201 //===========================================================================
205 // Changes Globals: -
206 //===========================================================================
207 void Net_FreeSocket(socket_t *sock)
210 } //end of the function Net_FreeSocket
211 //===========================================================================
215 // Changes Globals: -
216 //===========================================================================
217 socket_t *Net_Connect(address_t *address, int port)
223 // see if we can resolve the host name
224 WINS_StringToAddr(address->ip, &sendaddr);
226 newsock = WINS_OpenReliableSocket(port);
227 if (newsock == -1) return NULL;
229 sock = Net_AllocSocket();
232 WINS_CloseSocket(newsock);
235 sock->socket = newsock;
237 //connect to the host
238 if (WINS_Connect(newsock, &sendaddr) == -1)
240 Net_FreeSocket(sock);
241 WINS_CloseSocket(newsock);
242 WinPrint("Net_Connect: error connecting\n");
246 memcpy(&sock->addr, &sendaddr, sizeof(sockaddr_t));
247 //now we can send messages
250 } //end of the function Net_Connect
252 //===========================================================================
256 // Changes Globals: -
257 //===========================================================================
258 socket_t *Net_ListenSocket(int port)
263 newsock = WINS_OpenReliableSocket(port);
264 if (newsock == -1) return NULL;
266 if (WINS_Listen(newsock) == -1)
268 WINS_CloseSocket(newsock);
271 sock = Net_AllocSocket();
274 WINS_CloseSocket(newsock);
277 sock->socket = newsock;
278 WINS_GetSocketAddr(newsock, &sock->addr);
279 WinPrint("listen socket opened at %s\n", WINS_AddrToString(&sock->addr));
282 } //end of the function Net_ListenSocket
283 //===========================================================================
287 // Changes Globals: -
288 //===========================================================================
289 socket_t *Net_Accept(socket_t *sock)
295 newsocket = WINS_Accept(sock->socket, &sendaddr);
296 if (newsocket == -1) return NULL;
298 newsock = Net_AllocSocket();
301 WINS_CloseSocket(newsocket);
304 newsock->socket = newsocket;
305 memcpy(&newsock->addr, &sendaddr, sizeof(sockaddr_t));
308 } //end of the function Net_Accept
309 //===========================================================================
313 // Changes Globals: -
314 //===========================================================================
315 void Net_Disconnect(socket_t *sock)
317 WINS_CloseSocket(sock->socket);
318 Net_FreeSocket(sock);
319 } //end of the function Net_Disconnect
320 //===========================================================================
324 // Changes Globals: -
325 //===========================================================================
326 void Net_StringToAddress(char *string, address_t *address)
328 strcpy(address->ip, string);
329 } //end of the function Net_StringToAddress
330 //===========================================================================
334 // Changes Globals: -
335 //===========================================================================
336 void Net_MyAddress(address_t *address)
338 strcpy(address->ip, WINS_MyAddress());
339 } //end of the function Net_MyAddress
340 //===========================================================================
344 // Changes Globals: -
345 //===========================================================================
350 WinPrint("my address is %s\n", WINS_MyAddress());
353 } //end of the function Net_Setup
354 //===========================================================================
358 // Changes Globals: -
359 //===========================================================================
360 void Net_Shutdown(void)
363 } //end of the function Net_Shutdown
364 //===========================================================================
368 // Changes Globals: -
369 //===========================================================================
370 void NMSG_Clear(netmessage_t *msg)
373 } //end of the function NMSG_Clear
374 //===========================================================================
378 // Changes Globals: -
379 //===========================================================================
380 void NMSG_WriteChar (netmessage_t *msg, int c)
382 if (c < -128 || c > 127)
383 WinPrint("NMSG_WriteChar: range error\n");
385 if (msg->size >= MAX_NETMESSAGE)
387 WinPrint("NMSG_WriteChar: overflow\n");
390 msg->data[msg->size] = c;
392 } //end of the function NMSG_WriteChar
393 //===========================================================================
397 // Changes Globals: -
398 //===========================================================================
399 void NMSG_WriteByte(netmessage_t *msg, int c)
401 if (c < -128 || c > 127)
402 WinPrint("NMSG_WriteByte: range error\n");
404 if (msg->size + 1 >= MAX_NETMESSAGE)
406 WinPrint("NMSG_WriteByte: overflow\n");
409 msg->data[msg->size] = c;
411 } //end of the function NMSG_WriteByte
412 //===========================================================================
416 // Changes Globals: -
417 //===========================================================================
418 void NMSG_WriteShort(netmessage_t *msg, int c)
420 if (c < ((short)0x8000) || c > (short)0x7fff)
421 WinPrint("NMSG_WriteShort: range error");
423 if (msg->size + 2 >= MAX_NETMESSAGE)
425 WinPrint("NMSG_WriteShort: overflow\n");
428 msg->data[msg->size] = c&0xff;
429 msg->data[msg->size+1] = c>>8;
431 } //end of the function NMSG_WriteShort
432 //===========================================================================
436 // Changes Globals: -
437 //===========================================================================
438 void NMSG_WriteLong(netmessage_t *msg, int c)
440 if (msg->size + 4 >= MAX_NETMESSAGE)
442 WinPrint("NMSG_WriteLong: overflow\n");
445 msg->data[msg->size] = c&0xff;
446 msg->data[msg->size+1] = (c>>8)&0xff;
447 msg->data[msg->size+2] = (c>>16)&0xff;
448 msg->data[msg->size+3] = c>>24;
450 } //end of the function NMSG_WriteLong
451 //===========================================================================
455 // Changes Globals: -
456 //===========================================================================
457 void NMSG_WriteFloat(netmessage_t *msg, float c)
459 if (msg->size + 4 >= MAX_NETMESSAGE)
461 WinPrint("NMSG_WriteLong: overflow\n");
464 msg->data[msg->size] = *((int *)&c)&0xff;
465 msg->data[msg->size+1] = (*((int *)&c)>>8)&0xff;
466 msg->data[msg->size+2] = (*((int *)&c)>>16)&0xff;
467 msg->data[msg->size+3] = *((int *)&c)>>24;
469 } //end of the function NMSG_WriteFloat
470 //===========================================================================
474 // Changes Globals: -
475 //===========================================================================
476 void NMSG_WriteString(netmessage_t *msg, char *string)
478 if (msg->size + strlen(string) + 1 >= MAX_NETMESSAGE)
480 WinPrint("NMSG_WriteString: overflow\n");
483 strcpy(&msg->data[msg->size], string);
484 msg->size += strlen(string) + 1;
485 } //end of the function NMSG_WriteString
486 //===========================================================================
490 // Changes Globals: -
491 //===========================================================================
492 void NMSG_ReadStart(netmessage_t *msg)
494 msg->readoverflow = qfalse;
496 } //end of the function NMSG_ReadStart
497 //===========================================================================
501 // Changes Globals: -
502 //===========================================================================
503 int NMSG_ReadChar(netmessage_t *msg)
505 if (msg->size + 1 > msg->size)
507 msg->readoverflow = qtrue;
508 WinPrint("NMSG_ReadChar: read overflow\n");
512 return msg->data[msg->read-1];
513 } //end of the function NMSG_ReadChar
514 //===========================================================================
518 // Changes Globals: -
519 //===========================================================================
520 int NMSG_ReadByte(netmessage_t *msg)
522 if (msg->read + 1 > msg->size)
524 msg->readoverflow = qtrue;
525 WinPrint("NMSG_ReadByte: read overflow\n");
529 return msg->data[msg->read-1];
530 } //end of the function NMSG_ReadByte
531 //===========================================================================
535 // Changes Globals: -
536 //===========================================================================
537 int NMSG_ReadShort(netmessage_t *msg)
541 if (msg->read + 2 > msg->size)
543 msg->readoverflow = qtrue;
544 WinPrint("NMSG_ReadShort: read overflow\n");
547 c = (short)(msg->data[msg->read] + (msg->data[msg->read+1]<<8));
550 } //end of the function NMSG_ReadShort
551 //===========================================================================
555 // Changes Globals: -
556 //===========================================================================
557 int NMSG_ReadLong(netmessage_t *msg)
561 if (msg->read + 4 > msg->size)
563 msg->readoverflow = qtrue;
564 WinPrint("NMSG_ReadLong: read overflow\n");
567 c = msg->data[msg->read]
568 + (msg->data[msg->read+1]<<8)
569 + (msg->data[msg->read+2]<<16)
570 + (msg->data[msg->read+3]<<24);
573 } //end of the function NMSG_ReadLong
574 //===========================================================================
578 // Changes Globals: -
579 //===========================================================================
580 float NMSG_ReadFloat(netmessage_t *msg)
584 if (msg->read + 4 > msg->size)
586 msg->readoverflow = qtrue;
587 WinPrint("NMSG_ReadLong: read overflow\n");
590 c = msg->data[msg->read]
591 + (msg->data[msg->read+1]<<8)
592 + (msg->data[msg->read+2]<<16)
593 + (msg->data[msg->read+3]<<24);
596 } //end of the function NMSG_ReadFloat
597 //===========================================================================
601 // Changes Globals: -
602 //===========================================================================
603 char *NMSG_ReadString(netmessage_t *msg)
605 static char string[2048];
611 if (msg->read + 1 > msg->size)
613 msg->readoverflow = qtrue;
614 WinPrint("NMSG_ReadString: read overflow\n");
618 c = msg->data[msg->read];
623 } while (l < sizeof(string)-1);
626 } //end of the function NMSG_ReadString