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