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( const char *str, ... ){
50 va_start( argptr,str );
51 vsprintf( text, str, argptr );
57 void WinPrint( const char *str, ... ){
61 //===========================================================================
66 //===========================================================================
67 void Net_SetAddressPort( address_t *address, int port ){
70 WINS_StringToAddr( address->ip, &addr );
71 WINS_SetSocketPort( &addr, port );
72 strcpy( address->ip, WINS_AddrToString( &addr ) );
73 } //end of the function Net_SetAddressPort
74 //===========================================================================
79 //===========================================================================
80 int Net_AddressCompare( address_t *addr1, address_t *addr2 ){
82 return _stricmp( addr1->ip, addr2->ip );
84 return strcasecmp( addr1->ip, addr2->ip );
86 } //end of the function Net_AddressCompare
87 //===========================================================================
92 //===========================================================================
93 void Net_SocketToAddress( socket_t *sock, address_t *address ){
94 strcpy( address->ip, WINS_AddrToString( &sock->addr ) );
95 } //end of the function Net_SocketToAddress
96 //===========================================================================
100 // Changes Globals: -
101 //===========================================================================
102 int Net_Send( socket_t *sock, netmessage_t *msg ){
107 NMSG_WriteLong( msg, size - 4 );
109 //WinPrint("Net_Send: message of size %d\n", sendmsg.size);
110 return WINS_Write( sock->socket, msg->data, msg->size, NULL );
111 } //end of the function Net_SendSocketReliable
112 //===========================================================================
113 // returns the number of bytes recieved
118 // Changes Globals: -
119 //===========================================================================
120 int Net_Receive( socket_t *sock, netmessage_t *msg ){
123 if ( sock->remaining > 0 ) {
124 curread = WINS_Read( sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL );
125 if ( curread == -1 ) {
126 WinPrint( "Net_Receive: read error\n" );
129 sock->remaining -= curread;
130 sock->msg.size += curread;
131 if ( sock->remaining <= 0 ) {
133 memcpy( msg, &sock->msg, sizeof( netmessage_t ) );
135 return msg->size - 4;
139 sock->msg.size = WINS_Read( sock->socket, sock->msg.data, 4, NULL );
140 if ( sock->msg.size == 0 ) {
143 if ( sock->msg.size == -1 ) {
144 WinPrint( "Net_Receive: size header read error\n" );
147 //WinPrint("Net_Receive: message size header %d\n", msg->size);
149 sock->remaining = NMSG_ReadLong( &sock->msg );
150 if ( sock->remaining == 0 ) {
153 if ( sock->remaining < 0 || sock->remaining > MAX_NETMESSAGE ) {
154 WinPrint( "Net_Receive: invalid message size %d\n", sock->remaining );
157 //try to read the message
158 curread = WINS_Read( sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL );
159 if ( curread == -1 ) {
160 WinPrint( "Net_Receive: read error\n" );
163 sock->remaining -= curread;
164 sock->msg.size += curread;
165 if ( sock->remaining <= 0 ) {
167 memcpy( msg, &sock->msg, sizeof( netmessage_t ) );
169 return msg->size - 4;
171 //the message has not been completely read yet
173 printf( "++timo TODO: debug the Net_Receive on big size messages\n" );
176 } //end of the function Net_Receive
177 //===========================================================================
181 // Changes Globals: -
182 //===========================================================================
183 socket_t *Net_AllocSocket( void ){
186 sock = (socket_t *) GetMemory( sizeof( socket_t ) );
187 memset( sock, 0, sizeof( socket_t ) );
189 } //end of the function Net_AllocSocket
190 //===========================================================================
194 // Changes Globals: -
195 //===========================================================================
196 void Net_FreeSocket( socket_t *sock ){
198 } //end of the function Net_FreeSocket
199 //===========================================================================
203 // Changes Globals: -
204 //===========================================================================
205 socket_t *Net_Connect( address_t *address, int port ){
210 // see if we can resolve the host name
211 WINS_StringToAddr( address->ip, &sendaddr );
213 newsock = WINS_OpenReliableSocket( port );
214 if ( newsock == -1 ) {
218 sock = Net_AllocSocket();
219 if ( sock == NULL ) {
220 WINS_CloseSocket( newsock );
223 sock->socket = newsock;
225 //connect to the host
226 if ( WINS_Connect( newsock, &sendaddr ) == -1 ) {
227 Net_FreeSocket( sock );
228 WINS_CloseSocket( newsock );
229 WinPrint( "Net_Connect: error connecting\n" );
233 memcpy( &sock->addr, &sendaddr, sizeof( sockaddr_t ) );
234 //now we can send messages
237 } //end of the function Net_Connect
239 //===========================================================================
243 // Changes Globals: -
244 //===========================================================================
245 socket_t *Net_ListenSocket( int port ){
249 newsock = WINS_OpenReliableSocket( port );
250 if ( newsock == -1 ) {
254 if ( WINS_Listen( newsock ) == -1 ) {
255 WINS_CloseSocket( newsock );
258 sock = Net_AllocSocket();
259 if ( sock == NULL ) {
260 WINS_CloseSocket( newsock );
263 sock->socket = newsock;
264 WINS_GetSocketAddr( newsock, &sock->addr );
265 WinPrint( "listen socket opened at %s\n", WINS_AddrToString( &sock->addr ) );
268 } //end of the function Net_ListenSocket
269 //===========================================================================
273 // Changes Globals: -
274 //===========================================================================
275 socket_t *Net_Accept( socket_t *sock ){
280 newsocket = WINS_Accept( sock->socket, &sendaddr );
281 if ( newsocket == -1 ) {
285 newsock = Net_AllocSocket();
286 if ( newsock == NULL ) {
287 WINS_CloseSocket( newsocket );
290 newsock->socket = newsocket;
291 memcpy( &newsock->addr, &sendaddr, sizeof( sockaddr_t ) );
294 } //end of the function Net_Accept
295 //===========================================================================
299 // Changes Globals: -
300 //===========================================================================
301 void Net_Disconnect( socket_t *sock ){
302 WINS_CloseSocket( sock->socket );
303 Net_FreeSocket( sock );
304 } //end of the function Net_Disconnect
305 //===========================================================================
309 // Changes Globals: -
310 //===========================================================================
311 void Net_StringToAddress( const char *string, address_t *address ){
312 strcpy( address->ip, string );
313 } //end of the function Net_StringToAddress
314 //===========================================================================
318 // Changes Globals: -
319 //===========================================================================
320 void Net_MyAddress( address_t *address ){
321 strcpy( address->ip, WINS_MyAddress() );
322 } //end of the function Net_MyAddress
323 //===========================================================================
327 // Changes Globals: -
328 //===========================================================================
329 int Net_Setup( void ){
332 WinPrint( "my address is %s\n", WINS_MyAddress() );
335 } //end of the function Net_Setup
336 //===========================================================================
340 // Changes Globals: -
341 //===========================================================================
342 void Net_Shutdown( void ){
344 } //end of the function Net_Shutdown
345 //===========================================================================
349 // Changes Globals: -
350 //===========================================================================
351 void NMSG_Clear( netmessage_t *msg ){
353 } //end of the function NMSG_Clear
354 //===========================================================================
358 // Changes Globals: -
359 //===========================================================================
360 void NMSG_WriteChar( netmessage_t *msg, int c ){
361 if ( c < -128 || c > 127 ) {
362 WinPrint( "NMSG_WriteChar: range error\n" );
365 if ( msg->size >= MAX_NETMESSAGE ) {
366 WinPrint( "NMSG_WriteChar: overflow\n" );
369 msg->data[msg->size] = c;
371 } //end of the function NMSG_WriteChar
372 //===========================================================================
376 // Changes Globals: -
377 //===========================================================================
378 void NMSG_WriteByte( netmessage_t *msg, int c ){
379 if ( c < -128 || c > 127 ) {
380 WinPrint( "NMSG_WriteByte: range error\n" );
383 if ( msg->size + 1 >= MAX_NETMESSAGE ) {
384 WinPrint( "NMSG_WriteByte: overflow\n" );
387 msg->data[msg->size] = c;
389 } //end of the function NMSG_WriteByte
390 //===========================================================================
394 // Changes Globals: -
395 //===========================================================================
396 void NMSG_WriteShort( netmessage_t *msg, int c ){
397 if ( c < ( (short)0x8000 ) || c > (short)0x7fff ) {
398 WinPrint( "NMSG_WriteShort: range error" );
401 if ( msg->size + 2 >= MAX_NETMESSAGE ) {
402 WinPrint( "NMSG_WriteShort: overflow\n" );
405 msg->data[msg->size] = c & 0xff;
406 msg->data[msg->size + 1] = c >> 8;
408 } //end of the function NMSG_WriteShort
409 //===========================================================================
413 // Changes Globals: -
414 //===========================================================================
415 void NMSG_WriteLong( netmessage_t *msg, int c ){
416 if ( msg->size + 4 >= MAX_NETMESSAGE ) {
417 WinPrint( "NMSG_WriteLong: overflow\n" );
420 msg->data[msg->size] = c & 0xff;
421 msg->data[msg->size + 1] = ( c >> 8 ) & 0xff;
422 msg->data[msg->size + 2] = ( c >> 16 ) & 0xff;
423 msg->data[msg->size + 3] = c >> 24;
425 } //end of the function NMSG_WriteLong
426 //===========================================================================
430 // Changes Globals: -
431 //===========================================================================
432 void NMSG_WriteFloat( netmessage_t *msg, float c ){
433 if ( msg->size + 4 >= MAX_NETMESSAGE ) {
434 WinPrint( "NMSG_WriteLong: overflow\n" );
437 msg->data[msg->size] = *( (int *)&c ) & 0xff;
438 msg->data[msg->size + 1] = ( *( (int *)&c ) >> 8 ) & 0xff;
439 msg->data[msg->size + 2] = ( *( (int *)&c ) >> 16 ) & 0xff;
440 msg->data[msg->size + 3] = *( (int *)&c ) >> 24;
442 } //end of the function NMSG_WriteFloat
443 //===========================================================================
447 // Changes Globals: -
448 //===========================================================================
449 void NMSG_WriteString( netmessage_t *msg, char *string ){
450 if ( msg->size + strlen( string ) + 1 >= MAX_NETMESSAGE ) {
451 WinPrint( "NMSG_WriteString: overflow\n" );
454 memcpy( &msg->data[msg->size], string, strlen( string ) + 1 );
455 msg->size += strlen( string ) + 1;
456 } //end of the function NMSG_WriteString
457 //===========================================================================
461 // Changes Globals: -
462 //===========================================================================
463 void NMSG_ReadStart( netmessage_t *msg ){
464 msg->readoverflow = qfalse;
466 } //end of the function NMSG_ReadStart
467 //===========================================================================
471 // Changes Globals: -
472 //===========================================================================
473 int NMSG_ReadChar( netmessage_t *msg ){
474 if ( msg->read + 1 > msg->size ) {
475 msg->readoverflow = qtrue;
476 WinPrint( "NMSG_ReadChar: read overflow\n" );
480 return msg->data[msg->read - 1];
481 } //end of the function NMSG_ReadChar
482 //===========================================================================
486 // Changes Globals: -
487 //===========================================================================
488 int NMSG_ReadByte( netmessage_t *msg ){
489 if ( msg->read + 1 > msg->size ) {
490 msg->readoverflow = qtrue;
491 WinPrint( "NMSG_ReadByte: read overflow\n" );
495 return msg->data[msg->read - 1];
496 } //end of the function NMSG_ReadByte
497 //===========================================================================
501 // Changes Globals: -
502 //===========================================================================
503 int NMSG_ReadShort( netmessage_t *msg ){
506 if ( msg->read + 2 > msg->size ) {
507 msg->readoverflow = qtrue;
508 WinPrint( "NMSG_ReadShort: read overflow\n" );
511 c = (short)( msg->data[msg->read] + ( msg->data[msg->read + 1] << 8 ) );
514 } //end of the function NMSG_ReadShort
515 //===========================================================================
519 // Changes Globals: -
520 //===========================================================================
521 int NMSG_ReadLong( netmessage_t *msg ){
524 if ( msg->read + 4 > msg->size ) {
525 msg->readoverflow = qtrue;
526 WinPrint( "NMSG_ReadLong: read overflow\n" );
529 c = msg->data[msg->read]
530 + ( msg->data[msg->read + 1] << 8 )
531 + ( msg->data[msg->read + 2] << 16 )
532 + ( msg->data[msg->read + 3] << 24 );
535 } //end of the function NMSG_ReadLong
536 //===========================================================================
540 // Changes Globals: -
541 //===========================================================================
542 float NMSG_ReadFloat( netmessage_t *msg ){
545 if ( msg->read + 4 > msg->size ) {
546 msg->readoverflow = qtrue;
547 WinPrint( "NMSG_ReadLong: read overflow\n" );
550 c = msg->data[msg->read]
551 + ( msg->data[msg->read + 1] << 8 )
552 + ( msg->data[msg->read + 2] << 16 )
553 + ( msg->data[msg->read + 3] << 24 );
556 } //end of the function NMSG_ReadFloat
557 //===========================================================================
561 // Changes Globals: -
562 //===========================================================================
563 char *NMSG_ReadString( netmessage_t *msg ){
564 static char string[2048];
570 if ( msg->read + 1 > msg->size ) {
571 msg->readoverflow = qtrue;
572 WinPrint( "NMSG_ReadString: read overflow\n" );
576 c = msg->data[msg->read];
583 } while ( (size_t) l < sizeof( string ) - 1 );
586 } //end of the function NMSG_ReadString