[7540] | 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 | |
---|
[7541] | 16 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 17 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 18 | */ |
---|
| 19 | #define DEBUG_MODULE_NETWORK |
---|
| 20 | |
---|
[7540] | 21 | #include "tcp_server_socket.h" |
---|
[7541] | 22 | #include "tcp_socket.h" |
---|
[7540] | 23 | |
---|
| 24 | |
---|
[7541] | 25 | /* header for debug output */ |
---|
| 26 | #include "debug.h" |
---|
[7540] | 27 | |
---|
[9869] | 28 | ObjectListDefinition(TcpServerSocket); |
---|
[7540] | 29 | TcpServerSocket::TcpServerSocket( int port ) : ServerSocket( port ) |
---|
| 30 | { |
---|
[7541] | 31 | init(); |
---|
| 32 | listen(port); |
---|
[7540] | 33 | } |
---|
| 34 | |
---|
[7541] | 35 | /** |
---|
| 36 | * Default destructor |
---|
| 37 | */ |
---|
[7540] | 38 | TcpServerSocket::~TcpServerSocket( ) |
---|
| 39 | { |
---|
[7541] | 40 | /* Quit SDL_net */ |
---|
| 41 | // NOTE: what if other instances of NetworkSocket running? |
---|
| 42 | SDLNet_Quit(); |
---|
| 43 | PRINTF(5)("SDL_net shutdown\n"); |
---|
| 44 | |
---|
| 45 | _isListening = false; |
---|
[7540] | 46 | } |
---|
| 47 | |
---|
[7541] | 48 | void TcpServerSocket::init( ) |
---|
[7540] | 49 | { |
---|
[7541] | 50 | /* set the class id for the base object */ |
---|
[9869] | 51 | this->registerObject(this, TcpServerSocket::_objectList); |
---|
[7541] | 52 | |
---|
| 53 | terminateThread = false; |
---|
| 54 | listenSocket = NULL; |
---|
| 55 | _isListening = false; |
---|
| 56 | |
---|
| 57 | if(SDLNet_Init()==-1) |
---|
| 58 | { |
---|
| 59 | PRINTF(1)("SDLNet_Init: %s\n", SDLNet_GetError()); |
---|
| 60 | return; |
---|
| 61 | } |
---|
| 62 | else |
---|
| 63 | PRINTF(5)("SDL_net initialized\n"); |
---|
| 64 | |
---|
| 65 | PRINTF(0)("TcpServerSocket created\n"); |
---|
[7540] | 66 | } |
---|
| 67 | |
---|
[7541] | 68 | |
---|
| 69 | /** |
---|
| 70 | * Tells the NetworkSocket to listen on a specific port for incoming connections. |
---|
| 71 | * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection. |
---|
| 72 | * @param port |
---|
| 73 | */ |
---|
| 74 | bool TcpServerSocket::listen(unsigned int port) |
---|
[7540] | 75 | { |
---|
[7541] | 76 | PRINTF(0)("TcpServerSocket::listen()\n"); |
---|
| 77 | _isListening = true; |
---|
| 78 | //check if not already connected or listening |
---|
| 79 | if (listenSocket) |
---|
| 80 | { |
---|
| 81 | PRINTF(1)("TcpServerSocket::listen: tcpSocket!=NULL! maybe you already called listen or did not call close()!\n"); |
---|
| 82 | _isListening = false; |
---|
| 83 | return false; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | IPaddress ip; |
---|
| 87 | |
---|
| 88 | if (SDLNet_ResolveHost(&ip, NULL, port)==-1) |
---|
| 89 | { |
---|
| 90 | PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
---|
| 91 | _isListening = false; |
---|
| 92 | return false; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | listenSocket = SDLNet_TCP_Open(&ip); |
---|
| 96 | |
---|
| 97 | if (!listenSocket) |
---|
| 98 | { |
---|
| 99 | PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); |
---|
| 100 | _isListening = false; |
---|
| 101 | return false; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return true; |
---|
[7540] | 105 | } |
---|
| 106 | |
---|
[7541] | 107 | |
---|
| 108 | NetworkSocket* TcpServerSocket::getNewSocket( ) |
---|
| 109 | { |
---|
| 110 | if ( !listenSocket ) |
---|
| 111 | { |
---|
| 112 | PRINTF(1)("listenSocket == NULL! Maybe you forgot to call listen()\n"); |
---|
| 113 | close(); |
---|
| 114 | return NULL; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | TCPsocket sock = SDLNet_TCP_Accept(listenSocket); |
---|
| 118 | |
---|
| 119 | if ( !sock ) |
---|
| 120 | { |
---|
| 121 | return NULL; |
---|
| 122 | } |
---|
| 123 | else |
---|
| 124 | { |
---|
| 125 | return new TcpSocket(sock); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[9494] | 129 | |
---|
| 130 | |
---|
[7540] | 131 | void TcpServerSocket::close( ) |
---|
| 132 | { |
---|
[7541] | 133 | if ( listenSocket ) |
---|
| 134 | { |
---|
| 135 | SDLNet_TCP_Close( listenSocket ); |
---|
| 136 | listenSocket = NULL; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | _isListening = false; |
---|
[7540] | 140 | } |
---|
[9406] | 141 | |
---|