Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/network_log.cc @ 9404

Last change on this file since 9404 was 7954, checked in by patrick, 19 years ago

trunk: merged the network branche back to trunk.

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