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