1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
#include "../gt2.h"
#include "../../darray.h"
#include <time.h>
#define STATS 1
GT2Socket Socket;
char RemoteAddress[256];
char LocalAddress[256];
GT2Bool Quit;
DArray ServerSockets;
#if STATS
time_t startTime;
int numConnectAttempts;
int connectResults[8];
int clientReliableMessagesSent;
int serverReliableMessagesSent;
int clientUnreliableMessagesSent;
int serverUnreliableMessagesSent;
int clientReliableBytesSent;
int serverReliableBytesSent;
int clientUnreliableBytesSent;
int serverUnreliableBytesSent;
int clientCloses;
int serverCloses;
#endif
void FreeServerSocket(void * elem)
{
GT2Socket socket = *(GT2Socket *)elem;
gt2CloseSocket(socket);
}
int ServerSocketCompare(const void * elem1, const void * elem2)
{
GT2Socket socket1 = *(GT2Socket *)elem1;
GT2Socket socket2 = *(GT2Socket *)elem2;
if(socket1 == socket2)
return 0;
return 1;
}
void RemoveServerSocket(GT2Socket socket)
{
int index;
// find it first
index = ArraySearch(ServerSockets, &socket, ServerSocketCompare, 0, 0);
if(index != NOT_FOUND)
ArrayDeleteAt(ServerSockets, index);
}
/* CLIENT CONNECTIONS */
void ClientReceivedCallback
(
GT2Connection connection,
GT2Byte * message,
int len,
GT2Bool reliable
)
{
GT2Connection serverConnection;
// The server connection for this client connection.
////////////////////////////////////////////////////
serverConnection = (GT2Connection)gt2GetConnectionData(connection);
// Pass the data along.
///////////////////////
gt2Send(serverConnection, message, len, reliable);
#if STATS
// Update stats.
////////////////
if(reliable)
{
clientReliableMessagesSent++;
clientReliableBytesSent += len;
}
else
{
clientUnreliableMessagesSent++;
clientUnreliableBytesSent += len;
}
#endif
}
void ClientClosedCallback
(
GT2Connection connection,
GT2CloseReason reason
)
{
GT2Connection serverConnection;
// The server connection for this client connection.
////////////////////////////////////////////////////
serverConnection = (GT2Connection)gt2GetConnectionData(connection);
if(serverConnection)
{
// We don't want them to try closing us....
///////////////////////////////////////////
gt2SetConnectionData(serverConnection, NULL);
// Close the connection to the server.
//////////////////////////////////////
gt2CloseConnection(serverConnection);
}
#if STATS
// Update stats.
////////////////
if(reason == GT2LocalClose)
clientCloses++;
#endif
}
GT2ConnectionCallbacks ClientConnectionCallbacks =
{
NULL,
ClientReceivedCallback,
ClientClosedCallback
};
/* SERVER CONNECTIONS */
void ServerSocketErrorCallback
(
GT2Socket socket
)
{
printf("Server socket error\n");
Quit = GT2True;
GSI_UNUSED(socket);
}
void ServerConnectedCallback
(
GT2Connection connection,
GT2Result result,
GT2Byte * message,
int len
)
{
GT2Connection clientConnection;
// The client connection for this server connection.
////////////////////////////////////////////////////
clientConnection = (GT2Connection)gt2GetConnectionData(connection);
// Check the result.
////////////////////
if(result == GT2Success)
{
// Accept it.
/////////////
if(!gt2Accept(clientConnection, &ClientConnectionCallbacks))
gt2CloseConnection(connection);
}
else
{
if(result == GT2Rejected)
gt2Reject(clientConnection, message, len);
else
gt2Reject(clientConnection, (const GT2Byte *)("Proxy failed to connect to server."), -1);
// Close the socket.
////////////////////
RemoveServerSocket(gt2GetConnectionSocket(connection));
}
#if STATS
// Update stats.
////////////////
connectResults[result]++;
#endif
}
void ServerReceivedCallback
(
GT2Connection connection,
GT2Byte * message,
int len,
GT2Bool reliable
)
{
GT2Connection clientConnection;
// The client connection for this server connection.
////////////////////////////////////////////////////
clientConnection = (GT2Connection)gt2GetConnectionData(connection);
// Send it the data.
////////////////////
gt2Send(clientConnection, message, len, reliable);
#if STATS
// Update stats.
////////////////
if(reliable)
{
serverReliableMessagesSent++;
serverReliableBytesSent += len;
}
else
{
serverUnreliableMessagesSent++;
serverUnreliableBytesSent += len;
}
#endif
}
void ServerClosedCallback
(
GT2Connection connection,
GT2CloseReason reason
)
{
GT2Connection clientConnection;
// The client connection for this server connection.
////////////////////////////////////////////////////
clientConnection = (GT2Connection)gt2GetConnectionData(connection);
if(clientConnection)
{
// We don't want them to try closing us....
///////////////////////////////////////////
gt2SetConnectionData(clientConnection, NULL);
// Close the connection.
////////////////////////
gt2CloseConnection(clientConnection);
}
// Close the socket.
////////////////////
RemoveServerSocket(gt2GetConnectionSocket(connection));
#if STATS
// Update stats.
////////////////
if(reason == GT2LocalClose)
serverCloses++;
#endif
}
GT2ConnectionCallbacks ServerConnectionCallbacks =
{
ServerConnectedCallback,
ServerReceivedCallback,
ServerClosedCallback
};
/* LISTENER */
void SocketErrorCallback
(
GT2Socket socket
)
{
printf("Socket error (incoming connections socket)\n");
Quit = GT2True;
GSI_UNUSED(socket);
}
void ConnectAttemptCallback
(
GT2Socket socket,
GT2Connection connection,
unsigned int ip,
unsigned short port,
int latency,
GT2Byte * message,
int len
)
{
GT2Socket serverSocket;
GT2Connection serverConnection;
GT2Result result;
// Create a socket for this connection.
///////////////////////////////////////
result = gt2CreateSocket(&serverSocket, NULL, 0, 0, NULL);
if(result != GT2Success)
{
gt2Reject(connection, (const GT2Byte *)("Proxy failed to create a new socket."), -1);
return;
}
// Connect to the real server.
//////////////////////////////
result = gt2Connect(serverSocket, &serverConnection, RemoteAddress, message, len, 0, &ServerConnectionCallbacks, GT2False);
if(result != GT2Success)
{
gt2Reject(connection, (const GT2Byte *)("Proxy failed to connect to server."), -1);
gt2CloseSocket(serverSocket);
return;
}
// Add the socket to the array.
///////////////////////////////
ArrayAppend(ServerSockets, &serverSocket);
// The user data for each connection is the other.
//////////////////////////////////////////////////
gt2SetConnectionData(serverConnection, connection);
gt2SetConnectionData(connection, serverConnection);
#if STATS
// Update stats.
////////////////
numConnectAttempts++;
#endif
GSI_UNUSED(latency);
GSI_UNUSED(port);
GSI_UNUSED(ip);
GSI_UNUSED(socket);
}
#ifdef WIN32
BOOL WINAPI CtrlHandler
(
DWORD type
)
{
// Quit.
////////
Quit = GT2True;
// We handled it.
/////////////////
return TRUE;
GSI_UNUSED(type);
}
#endif
#if STATS
void DisplayStats(void)
{
time_t runTime;
time_t days;
time_t hours;
time_t minutes;
time_t seconds;
time_t ctimeTime;
int numAccepted;
int numRejected;
int total;
// Do the time stuff.
/////////////////////
ctimeTime = startTime;
runTime = (time(NULL) - startTime);
seconds = (runTime % 60);
runTime /= 60;
minutes = (runTime % 60);
runTime /= 60;
hours = (runTime % 24);
runTime /= 24;
days = runTime;
// Do some other stuff.
///////////////////////
numAccepted = connectResults[GT2Success];
numRejected = connectResults[GT2Rejected];
// Print stats.
///////////////
printf("Proxying since %s", gsiSecondsToString(&ctimeTime));
printf("(%d days, %d hours, %d minutes, %d seconds)\n",
days,
hours,
minutes,
seconds);
printf("%d connect attempts, %d (%d%%) accepted, %d (%d%%) rejected\n",
numConnectAttempts,
numAccepted,
(numConnectAttempts)?(numAccepted * 100) / numConnectAttempts:0,
numRejected,
(numConnectAttempts)?(numRejected * 100) / numConnectAttempts:0);
total = (clientReliableMessagesSent + clientUnreliableMessagesSent);
printf("client: %d messages, %d (%d%%) reliable, %d (%d%%) unreliable\n",
total,
clientReliableMessagesSent,
(total)?(clientReliableMessagesSent * 100) / total:0,
clientUnreliableMessagesSent,
(total)?(clientUnreliableMessagesSent * 100) / total:0);
total = (serverReliableMessagesSent + serverUnreliableMessagesSent);
printf("server: %d messages, %d (%d%%) reliable, %d (%d%%) unreliable\n",
total,
serverReliableMessagesSent,
(total)?(serverReliableMessagesSent * 100) / total:0,
serverUnreliableMessagesSent,
(total)?(serverUnreliableMessagesSent * 100) / total:0);
total = (clientReliableBytesSent + clientUnreliableBytesSent);
printf("client: %d bytes, %d (%d%%) reliable, %d (%d%%) unreliable\n",
total,
clientReliableBytesSent,
(total)?(clientReliableBytesSent * 100) / total:0,
clientUnreliableBytesSent,
(total)?(clientUnreliableBytesSent * 100) / total:0);
total = (serverReliableBytesSent + serverUnreliableBytesSent);
printf("server: %d bytes, %d (%d%%) reliable, %d (%d%%) unreliable\n",
total,
serverReliableBytesSent,
(total)?(serverReliableBytesSent * 100) / total:0,
serverUnreliableBytesSent,
(total)?(serverUnreliableBytesSent * 100) / total:0);
total = (clientCloses + serverCloses);
printf("%d closes, %d (%d%%) client, %d (%d%%) server\n",
total,
clientCloses,
(total)?(clientCloses * 100) / total:0,
serverCloses,
(total)?(serverCloses * 100) / total:0);
}
#endif
int main
(
int argc,
char ** argv
)
{
GT2Result result;
int num;
int i;
#if STATS
unsigned int lastStatsTime = 0;
unsigned int now;
#endif
// Check args.
//////////////
if((argc < 2) || (argc > 3))
{
printf("%s <remote host:port> [localhost][:port]\n", argv[0]);
return 1;
}
// First is the remote address.
///////////////////////////////
strcpy(RemoteAddress, argv[1]);
// Second is the local address.
///////////////////////////////
if(argc >= 3)
strcpy(LocalAddress, argv[2]);
// Create the array of server sockets.
//////////////////////////////////////
ServerSockets = ArrayNew(sizeof(GT2Socket), 10, FreeServerSocket);
if(!ServerSockets)
{
printf("Failed to create the array of server sockets\n");
return 1;
}
// Create the socket.
/////////////////////
result = gt2CreateSocket(&Socket, LocalAddress, 0, 0, SocketErrorCallback);
if(result != GT2Success)
{
printf("Error creating the socket (%d)\n", result);
return 1;
}
// Start listening.
///////////////////
gt2Listen(Socket, ConnectAttemptCallback);
// Show the port.
/////////////////
printf("Listening on port %d\n", gt2GetLocalPort(Socket));
// For win32, setup a ctrl-c handler.
/////////////////////////////////////
SetConsoleCtrlHandler(CtrlHandler, TRUE);
// We're starting.
//////////////////
startTime = time(NULL);
// Loop until we quit.
//////////////////////
while(!Quit)
{
#if STATS
// Get the current time.
////////////////////////
now = current_time();
// Display stats?
/////////////////
if((now - lastStatsTime) >= 30000)
{
DisplayStats();
lastStatsTime = now;
}
#endif
// Yield.
/////////
msleep(1);
// Think.
/////////
gt2Think(Socket);
// Let the server sockets think.
////////////////////////////////
num = ArrayLength(ServerSockets);
for(i = (num - 1) ; i >= 0 ; i--)
gt2Think(*(GT2Socket *)ArrayNth(ServerSockets, i));
}
#if STATS
// Display some final stats.
////////////////////////////
DisplayStats();
#endif
return 0;
}
|