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"
40 void WinPrint( const char *str, ... ){
44 va_start( argptr,str );
45 vsprintf( text, str, argptr );
51 void WinPrint( const char *str, ... ){
55 //===========================================================================
60 //===========================================================================
61 void Net_SetAddressPort( address_t *address, int port ){
64 WINS_StringToAddr( address->ip, &addr );
65 WINS_SetSocketPort( &addr, port );
66 strcpy( address->ip, WINS_AddrToString( &addr ) );
67 } //end of the function Net_SetAddressPort
68 //===========================================================================
73 //===========================================================================
74 int Net_AddressCompare( address_t *addr1, address_t *addr2 ){
76 return _stricmp( addr1->ip, addr2->ip );
78 return strcasecmp( addr1->ip, addr2->ip );
80 } //end of the function Net_AddressCompare
81 //===========================================================================
86 //===========================================================================
87 void Net_SocketToAddress( socket_t *sock, address_t *address ){
88 strcpy( address->ip, WINS_AddrToString( &sock->addr ) );
89 } //end of the function Net_SocketToAddress
90 //===========================================================================
95 //===========================================================================
96 int Net_Send( socket_t *sock, netmessage_t *msg ){
101 NMSG_WriteLong( msg, size - 4 );
103 //WinPrint("Net_Send: message of size %d\n", sendmsg.size);
104 return WINS_Write( sock->socket, msg->data, msg->size, NULL );
105 } //end of the function Net_SendSocketReliable
106 //===========================================================================
107 // returns the number of bytes recieved
112 // Changes Globals: -
113 //===========================================================================
114 int Net_Receive( socket_t *sock, netmessage_t *msg ){
117 if ( sock->remaining > 0 ) {
118 curread = WINS_Read( sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL );
119 if ( curread == -1 ) {
120 WinPrint( "Net_Receive: read error\n" );
123 sock->remaining -= curread;
124 sock->msg.size += curread;
125 if ( sock->remaining <= 0 ) {
127 memcpy( msg, &sock->msg, sizeof( netmessage_t ) );
129 return msg->size - 4;
133 sock->msg.size = WINS_Read( sock->socket, sock->msg.data, 4, NULL );
134 if ( sock->msg.size == 0 ) {
137 if ( sock->msg.size == -1 ) {
138 WinPrint( "Net_Receive: size header read error\n" );
141 //WinPrint("Net_Receive: message size header %d\n", msg->size);
143 sock->remaining = NMSG_ReadLong( &sock->msg );
144 if ( sock->remaining == 0 ) {
147 if ( sock->remaining < 0 || sock->remaining > MAX_NETMESSAGE ) {
148 WinPrint( "Net_Receive: invalid message size %d\n", sock->remaining );
151 //try to read the message
152 curread = WINS_Read( sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL );
153 if ( curread == -1 ) {
154 WinPrint( "Net_Receive: read error\n" );
157 sock->remaining -= curread;
158 sock->msg.size += curread;
159 if ( sock->remaining <= 0 ) {
161 memcpy( msg, &sock->msg, sizeof( netmessage_t ) );
163 return msg->size - 4;
165 //the message has not been completely read yet
167 printf( "++timo TODO: debug the Net_Receive on big size messages\n" );
170 } //end of the function Net_Receive
171 //===========================================================================
175 // Changes Globals: -
176 //===========================================================================
177 socket_t *Net_AllocSocket( void ){
180 sock = (socket_t *) malloc( sizeof( socket_t ) );
181 memset( sock, 0, sizeof( socket_t ) );
183 } //end of the function Net_AllocSocket
184 //===========================================================================
188 // Changes Globals: -
189 //===========================================================================
190 void Net_FreeSocket( socket_t *sock ){
192 } //end of the function Net_FreeSocket
193 //===========================================================================
197 // Changes Globals: -
198 //===========================================================================
199 socket_t *Net_Connect( address_t *address, int port ){
204 // see if we can resolve the host name
205 WINS_StringToAddr( address->ip, &sendaddr );
207 newsock = WINS_OpenReliableSocket( port );
208 if ( newsock == -1 ) {
212 sock = Net_AllocSocket();
213 if ( sock == NULL ) {
214 WINS_CloseSocket( newsock );
217 sock->socket = newsock;
219 //connect to the host
220 if ( WINS_Connect( newsock, &sendaddr ) == -1 ) {
221 Net_FreeSocket( sock );
222 WINS_CloseSocket( newsock );
223 WinPrint( "Net_Connect: error connecting\n" );
227 memcpy( &sock->addr, &sendaddr, sizeof( sockaddr_t ) );
228 //now we can send messages
231 } //end of the function Net_Connect
233 //===========================================================================
237 // Changes Globals: -
238 //===========================================================================
239 socket_t *Net_ListenSocket( int port ){
243 newsock = WINS_OpenReliableSocket( port );
244 if ( newsock == -1 ) {
248 if ( WINS_Listen( newsock ) == -1 ) {
249 WINS_CloseSocket( newsock );
252 sock = Net_AllocSocket();
253 if ( sock == NULL ) {
254 WINS_CloseSocket( newsock );
257 sock->socket = newsock;
258 WINS_GetSocketAddr( newsock, &sock->addr );
259 WinPrint( "listen socket opened at %s\n", WINS_AddrToString( &sock->addr ) );
262 } //end of the function Net_ListenSocket
263 //===========================================================================
267 // Changes Globals: -
268 //===========================================================================
269 socket_t *Net_Accept( socket_t *sock ){
274 newsocket = WINS_Accept( sock->socket, &sendaddr );
275 if ( newsocket == -1 ) {
279 newsock = Net_AllocSocket();
280 if ( newsock == NULL ) {
281 WINS_CloseSocket( newsocket );
284 newsock->socket = newsocket;
285 memcpy( &newsock->addr, &sendaddr, sizeof( sockaddr_t ) );
288 } //end of the function Net_Accept
289 //===========================================================================
293 // Changes Globals: -
294 //===========================================================================
295 void Net_Disconnect( socket_t *sock ){
296 WINS_CloseSocket( sock->socket );
297 Net_FreeSocket( sock );
298 } //end of the function Net_Disconnect
299 //===========================================================================
303 // Changes Globals: -
304 //===========================================================================
305 void Net_StringToAddress( const char *string, address_t *address ){
306 strcpy( address->ip, string );
307 } //end of the function Net_StringToAddress
308 //===========================================================================
312 // Changes Globals: -
313 //===========================================================================
314 void Net_MyAddress( address_t *address ){
315 strcpy( address->ip, WINS_MyAddress() );
316 } //end of the function Net_MyAddress
317 //===========================================================================
321 // Changes Globals: -
322 //===========================================================================
323 int Net_Setup( void ){
326 WinPrint( "my address is %s\n", WINS_MyAddress() );
329 } //end of the function Net_Setup
330 //===========================================================================
334 // Changes Globals: -
335 //===========================================================================
336 void Net_Shutdown( void ){
338 } //end of the function Net_Shutdown
339 //===========================================================================
343 // Changes Globals: -
344 //===========================================================================
345 void NMSG_Clear( netmessage_t *msg ){
347 } //end of the function NMSG_Clear
348 //===========================================================================
352 // Changes Globals: -
353 //===========================================================================
354 void NMSG_WriteChar( netmessage_t *msg, int c ){
355 if ( c < -128 || c > 127 ) {
356 WinPrint( "NMSG_WriteChar: range error\n" );
359 if ( msg->size >= MAX_NETMESSAGE ) {
360 WinPrint( "NMSG_WriteChar: overflow\n" );
363 msg->data[msg->size] = c;
365 } //end of the function NMSG_WriteChar
366 //===========================================================================
370 // Changes Globals: -
371 //===========================================================================
372 void NMSG_WriteByte( netmessage_t *msg, int c ){
373 if ( c < -128 || c > 127 ) {
374 WinPrint( "NMSG_WriteByte: range error\n" );
377 if ( msg->size + 1 >= MAX_NETMESSAGE ) {
378 WinPrint( "NMSG_WriteByte: overflow\n" );
381 msg->data[msg->size] = c;
383 } //end of the function NMSG_WriteByte
384 //===========================================================================
388 // Changes Globals: -
389 //===========================================================================
390 void NMSG_WriteShort( netmessage_t *msg, int c ){
391 if ( c < ( (short)0x8000 ) || c > (short)0x7fff ) {
392 WinPrint( "NMSG_WriteShort: range error" );
395 if ( msg->size + 2 >= MAX_NETMESSAGE ) {
396 WinPrint( "NMSG_WriteShort: overflow\n" );
399 msg->data[msg->size] = c & 0xff;
400 msg->data[msg->size + 1] = c >> 8;
402 } //end of the function NMSG_WriteShort
403 //===========================================================================
407 // Changes Globals: -
408 //===========================================================================
409 void NMSG_WriteLong( netmessage_t *msg, int c ){
410 if ( msg->size + 4 >= MAX_NETMESSAGE ) {
411 WinPrint( "NMSG_WriteLong: overflow\n" );
414 msg->data[msg->size] = c & 0xff;
415 msg->data[msg->size + 1] = ( c >> 8 ) & 0xff;
416 msg->data[msg->size + 2] = ( c >> 16 ) & 0xff;
417 msg->data[msg->size + 3] = c >> 24;
419 } //end of the function NMSG_WriteLong
420 //===========================================================================
424 // Changes Globals: -
425 //===========================================================================
426 void NMSG_WriteFloat( netmessage_t *msg, float c ){
427 if ( msg->size + 4 >= MAX_NETMESSAGE ) {
428 WinPrint( "NMSG_WriteLong: overflow\n" );
431 msg->data[msg->size] = *( (int *)&c ) & 0xff;
432 msg->data[msg->size + 1] = ( *( (int *)&c ) >> 8 ) & 0xff;
433 msg->data[msg->size + 2] = ( *( (int *)&c ) >> 16 ) & 0xff;
434 msg->data[msg->size + 3] = *( (int *)&c ) >> 24;
436 } //end of the function NMSG_WriteFloat
437 //===========================================================================
441 // Changes Globals: -
442 //===========================================================================
443 void NMSG_WriteString( netmessage_t *msg, char *string ){
444 if ( msg->size + strlen( string ) + 1 >= MAX_NETMESSAGE ) {
445 WinPrint( "NMSG_WriteString: overflow\n" );
448 memcpy( &msg->data[msg->size], string, strlen( string ) + 1 );
449 msg->size += strlen( string ) + 1;
450 } //end of the function NMSG_WriteString
451 //===========================================================================
455 // Changes Globals: -
456 //===========================================================================
457 void NMSG_ReadStart( netmessage_t *msg ){
458 msg->readoverflow = qfalse;
460 } //end of the function NMSG_ReadStart
461 //===========================================================================
465 // Changes Globals: -
466 //===========================================================================
467 int NMSG_ReadChar( netmessage_t *msg ){
468 if ( msg->read + 1 > msg->size ) {
469 msg->readoverflow = qtrue;
470 WinPrint( "NMSG_ReadChar: read overflow\n" );
474 return msg->data[msg->read - 1];
475 } //end of the function NMSG_ReadChar
476 //===========================================================================
480 // Changes Globals: -
481 //===========================================================================
482 int NMSG_ReadByte( netmessage_t *msg ){
483 if ( msg->read + 1 > msg->size ) {
484 msg->readoverflow = qtrue;
485 WinPrint( "NMSG_ReadByte: read overflow\n" );
489 return msg->data[msg->read - 1];
490 } //end of the function NMSG_ReadByte
491 //===========================================================================
495 // Changes Globals: -
496 //===========================================================================
497 int NMSG_ReadShort( netmessage_t *msg ){
500 if ( msg->read + 2 > msg->size ) {
501 msg->readoverflow = qtrue;
502 WinPrint( "NMSG_ReadShort: read overflow\n" );
505 c = (short)( msg->data[msg->read] + ( msg->data[msg->read + 1] << 8 ) );
508 } //end of the function NMSG_ReadShort
509 //===========================================================================
513 // Changes Globals: -
514 //===========================================================================
515 int NMSG_ReadLong( netmessage_t *msg ){
518 if ( msg->read + 4 > msg->size ) {
519 msg->readoverflow = qtrue;
520 WinPrint( "NMSG_ReadLong: read overflow\n" );
523 c = msg->data[msg->read]
524 + ( msg->data[msg->read + 1] << 8 )
525 + ( msg->data[msg->read + 2] << 16 )
526 + ( msg->data[msg->read + 3] << 24 );
529 } //end of the function NMSG_ReadLong
530 //===========================================================================
534 // Changes Globals: -
535 //===========================================================================
536 float NMSG_ReadFloat( netmessage_t *msg ){
539 if ( msg->read + 4 > msg->size ) {
540 msg->readoverflow = qtrue;
541 WinPrint( "NMSG_ReadLong: read overflow\n" );
544 c = msg->data[msg->read]
545 + ( msg->data[msg->read + 1] << 8 )
546 + ( msg->data[msg->read + 2] << 16 )
547 + ( msg->data[msg->read + 3] << 24 );
550 } //end of the function NMSG_ReadFloat
551 //===========================================================================
555 // Changes Globals: -
556 //===========================================================================
557 char *NMSG_ReadString( netmessage_t *msg ){
558 static char string[2048];
564 if ( msg->read + 1 > msg->size ) {
565 msg->readoverflow = qtrue;
566 WinPrint( "NMSG_ReadString: read overflow\n" );
570 c = msg->data[msg->read];
577 } while ( (size_t) l < sizeof( string ) - 1 );
580 } //end of the function NMSG_ReadString