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