Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/lib/network/network_log.cc @ 10509

Last change on this file since 10509 was 9877, checked in by bensch, 18 years ago

orxonox compiles in Windows again… but it does not run anymore

File size: 3.8 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: ...
14*/
15
16#include "network_log.h"
17#include <cassert>
18#include <stdarg.h>
19/**
20 *  the singleton reference to this class
21 */
22NetworkLog* NetworkLog::singletonRef = NULL;
23
24
25/**
26 * standard constructor
27 */
28NetworkLog::NetworkLog ()
29{
30  listensock = NULL;
31}
32
33
34/**
35   @brief standard deconstructor
36 */
37NetworkLog::~NetworkLog ()
38{
39  NetworkLog::singletonRef = NULL;
40}
41
42/**
43 * listens on port for connections
44 * @param port port number
45 * @return true on success
46 */
47bool NetworkLog::listen( int port )
48{
49  IPaddress ip;
50
51  if ( SDLNet_ResolveHost( &ip, NULL, port ) == -1 ) {
52    PRINT(1)( "SDLNet_ResolveHost: %s\n", SDLNet_GetError() );
53    listensock = NULL;
54    return false;
55  }
56
57  listensock = SDLNet_TCP_Open( &ip );
58
59  if( !listensock ) {
60    PRINT(1)( "SDLNet_TCP_Open: %s\n", SDLNet_GetError() );
61    return false;
62  }
63
64  return true;
65}
66
67/**
68 * prints string to all connected sockets
69 */
70void NetworkLog::printfn( char * format, ... )
71{
72  va_list ap;
73  va_start( ap, format );
74
75  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
76
77  va_end( ap );
78
79  printfnet();
80}
81
82/**
83 * accepts new connections
84 */
85void NetworkLog::acceptNewConnections( )
86{
87  TCPsocket newSock = SDLNet_TCP_Accept( listensock );
88
89  if ( newSock )
90    sockets.push_back( newSock );
91}
92
93/**
94 * prints to all connected sockets and to PRINTF(0)
95 */
96void NetworkLog::printf0( char * format, ... )
97{
98  va_list ap;
99  va_start( ap, format );
100
101  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
102
103  va_end( ap );
104
105  PRINT(0)( buf );
106  printfnet();
107
108}
109
110/**
111 * prints to all connected sockets and to PRINTF(1)
112 */
113void NetworkLog::printf1( char * format, ... )
114{
115  va_list ap;
116  va_start( ap, format );
117
118  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
119
120  va_end( ap );
121
122  PRINT(1)( buf );
123  printfnet();
124
125}
126
127/**
128 * prints to all connected sockets and to PRINTF(2)
129 */
130void NetworkLog::printf2( char * format, ... )
131{
132  va_list ap;
133  va_start( ap, format );
134
135  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
136
137  va_end( ap );
138
139  PRINT(2)( buf );
140  printfnet();
141
142}
143
144/**
145 * prints to all connected sockets and to PRINTF(3)
146 */
147void NetworkLog::printf3( char * format, ... )
148{
149  va_list ap;
150  va_start( ap, format );
151
152  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
153
154  va_end( ap );
155
156  PRINT(3)( buf );
157  printfnet();
158
159}
160
161/**
162 * prints to all connected sockets and to PRINTF(4)
163 */
164void NetworkLog::printf4( char * format, ... )
165{
166  va_list ap;
167  va_start( ap, format );
168
169  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
170
171  va_end( ap );
172
173  PRINT(4)( buf );
174  printfnet();
175
176}
177
178/**
179 * prints to all connected sockets and to PRINTF(5)
180 */
181void NetworkLog::printf5( char * format, ... )
182{
183  va_list ap;
184  va_start( ap, format );
185
186  assert( vsnprintf( buf, NETWORK_LOG_BUFLEN, format, ap ) < NETWORK_LOG_BUFLEN );
187
188  va_end( ap );
189
190  PRINT(5)( buf );
191  printfnet();
192
193}
194
195
196/**
197 * prints buf to network sockets
198 */
199void NetworkLog::printfnet()
200{
201  if ( !listensock )
202    return;
203
204  acceptNewConnections();
205
206  for ( std::list<TCPsocket>::iterator it = sockets.begin(); it != sockets.end(); )
207  {
208    if ( SDLNet_TCP_Send( *it, buf, strlen( buf) ) < strlen( buf ) )
209    {
210      SDLNet_TCP_Close( *it );
211      std::list<TCPsocket>::iterator delIt = it;
212      it++;
213      sockets.erase( delIt );
214      continue;
215    }
216
217    it++;
218  }
219}
Note: See TracBrowser for help on using the repository browser.