aboutsummaryrefslogtreecommitdiff
path: root/src/stable/serversockettcp.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/stable/serversockettcp.cpp (renamed from src/stable/tcpserversocket.cpp)101
1 files changed, 49 insertions, 52 deletions
diff --git a/src/stable/tcpserversocket.cpp b/src/stable/serversockettcp.cpp
index b1e3461..c3750b2 100644
--- a/src/stable/tcpserversocket.cpp
+++ b/src/stable/serversockettcp.cpp
@@ -23,12 +23,13 @@
23#include <sys/types.h> 23#include <sys/types.h>
24//#include <termios.h> 24//#include <termios.h>
25#include <fcntl.h> 25#include <fcntl.h>
26#include "bu/tcpserversocket.h" 26#include "bu/serversockettcp.h"
27#include "bu/sockettcp.h"
27 28
28namespace Bu { subExceptionDef( TcpServerSocketException ) } 29namespace Bu { subExceptionDef( ServerSocketTcpException ) }
29 30
30Bu::TcpServerSocket::TcpServerSocket( int nPort, int nPoolSize ) : 31Bu::ServerSocketTcp::ServerSocketTcp( int iPort, int nPoolSize ) :
31 nPort( nPort ) 32 iPort( iPort )
32{ 33{
33#ifdef WIN32 34#ifdef WIN32
34 Bu::Winsock2::getInstance(); 35 Bu::Winsock2::getInstance();
@@ -39,7 +40,7 @@ Bu::TcpServerSocket::TcpServerSocket( int nPort, int nPoolSize ) :
39 40
40 /* Give the socket a name. */ 41 /* Give the socket a name. */
41 name.sin_family = AF_INET; 42 name.sin_family = AF_INET;
42 name.sin_port = bu_htons( nPort ); 43 name.sin_port = bu_htons( iPort );
43 44
44 // I think this specifies who we will accept connections from, 45 // I think this specifies who we will accept connections from,
45 // a good thing to make configurable later on 46 // a good thing to make configurable later on
@@ -48,8 +49,8 @@ Bu::TcpServerSocket::TcpServerSocket( int nPort, int nPoolSize ) :
48 startServer( name, nPoolSize ); 49 startServer( name, nPoolSize );
49} 50}
50 51
51Bu::TcpServerSocket::TcpServerSocket(const String &sAddr,int nPort, int nPoolSize) : 52Bu::ServerSocketTcp::ServerSocketTcp(const String &sAddr,int iPort, int nPoolSize) :
52 nPort( nPort ) 53 iPort( iPort )
53{ 54{
54#ifdef WIN32 55#ifdef WIN32
55 Bu::Winsock2::getInstance(); 56 Bu::Winsock2::getInstance();
@@ -61,7 +62,7 @@ Bu::TcpServerSocket::TcpServerSocket(const String &sAddr,int nPort, int nPoolSiz
61 /* Give the socket a name. */ 62 /* Give the socket a name. */
62 name.sin_family = AF_INET; 63 name.sin_family = AF_INET;
63 64
64 name.sin_port = bu_htons( nPort ); 65 name.sin_port = bu_htons( iPort );
65 66
66#ifdef WIN32 67#ifdef WIN32
67 name.sin_addr.s_addr = bu_inet_addr( sAddr.getStr() ); 68 name.sin_addr.s_addr = bu_inet_addr( sAddr.getStr() );
@@ -72,9 +73,9 @@ Bu::TcpServerSocket::TcpServerSocket(const String &sAddr,int nPort, int nPoolSiz
72 startServer( name, nPoolSize ); 73 startServer( name, nPoolSize );
73} 74}
74 75
75Bu::TcpServerSocket::TcpServerSocket( socket_t nServer, bool bInit, int nPoolSize ) : 76Bu::ServerSocketTcp::ServerSocketTcp( socket_t iSocket, bool bInit, int nPoolSize ) :
76 nServer( nServer ), 77 iSocket( iSocket ),
77 nPort( 0 ) 78 iPort( 0 )
78{ 79{
79#ifdef WIN32 80#ifdef WIN32
80 Bu::Winsock2::getInstance(); 81 Bu::Winsock2::getInstance();
@@ -84,56 +85,56 @@ Bu::TcpServerSocket::TcpServerSocket( socket_t nServer, bool bInit, int nPoolSiz
84 { 85 {
85 struct sockaddr name; 86 struct sockaddr name;
86 socklen_t namelen = sizeof(name); 87 socklen_t namelen = sizeof(name);
87 getpeername( nServer, &name, &namelen ); 88 getpeername( iSocket, &name, &namelen );
88 89
89 initServer( *((sockaddr_in *)&name), nPoolSize ); 90 initServer( *((sockaddr_in *)&name), nPoolSize );
90 } 91 }
91 else 92 else
92 { 93 {
93 FD_ZERO( &fdActive ); 94 FD_ZERO( &fdActive );
94 FD_SET( nServer, &fdActive ); 95 FD_SET( iSocket, &fdActive );
95 } 96 }
96} 97}
97 98
98Bu::TcpServerSocket::TcpServerSocket( const TcpServerSocket &rSrc ) 99Bu::ServerSocketTcp::ServerSocketTcp( const ServerSocketTcp &rSrc )
99{ 100{
100#ifdef WIN32 101#ifdef WIN32
101 Bu::Winsock2::getInstance(); 102 Bu::Winsock2::getInstance();
102#endif 103#endif
103 104
104 nServer = dup( rSrc.nServer ); 105 iSocket = dup( rSrc.iSocket );
105 nPort = rSrc.nPort; 106 iPort = rSrc.iPort;
106 FD_ZERO( &fdActive ); 107 FD_ZERO( &fdActive );
107 FD_SET( nServer, &fdActive ); 108 FD_SET( iSocket, &fdActive );
108} 109}
109 110
110Bu::TcpServerSocket::~TcpServerSocket() 111Bu::ServerSocketTcp::~ServerSocketTcp()
111{ 112{
112#ifdef WIN32 113#ifdef WIN32
113 if( nServer != INVALID_SOCKET ) 114 if( iSocket != INVALID_SOCKET )
114#else 115#else
115 if( nServer > -1 ) 116 if( iSocket > -1 )
116#endif 117#endif
117 ::close( nServer ); 118 ::close( iSocket );
118} 119}
119 120
120void Bu::TcpServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) 121void Bu::ServerSocketTcp::startServer( struct sockaddr_in &name, int nPoolSize )
121{ 122{
122 /* Create the socket. */ 123 /* Create the socket. */
123 nServer = bu_socket( PF_INET, SOCK_STREAM, 0 ); 124 iSocket = bu_socket( PF_INET, SOCK_STREAM, 0 );
124 125
125#ifdef WIN32 126#ifdef WIN32
126 if( nServer == INVALID_SOCKET ) 127 if( iSocket == INVALID_SOCKET )
127#else 128#else
128 if( nServer < 0 ) 129 if( iSocket < 0 )
129#endif 130#endif
130 { 131 {
131 throw Bu::TcpServerSocketException("Couldn't create a listen socket."); 132 throw Bu::ServerSocketTcpException("Couldn't create a listen socket.");
132 } 133 }
133 134
134 int opt = 1; 135 int opt = 1;
135 bu_setsockopt( 136 bu_setsockopt(
136 nServer, 137 iSocket,
137 SOL_SOCKET, 138 SOL_SOCKET,
138 SO_REUSEADDR, 139 SO_REUSEADDR,
139 (char *)&opt, 140 (char *)&opt,
@@ -143,31 +144,26 @@ void Bu::TcpServerSocket::startServer( struct sockaddr_in &name, int nPoolSize )
143 initServer( name, nPoolSize ); 144 initServer( name, nPoolSize );
144} 145}
145 146
146void Bu::TcpServerSocket::initServer( struct sockaddr_in &name, int nPoolSize ) 147void Bu::ServerSocketTcp::initServer( struct sockaddr_in &name, int nPoolSize )
147{ 148{
148 if( bu_bind( nServer, (struct sockaddr *) &name, sizeof(name) ) < 0 ) 149 if( bu_bind( iSocket, (struct sockaddr *) &name, sizeof(name) ) < 0 )
149 { 150 {
150 throw Bu::TcpServerSocketException("Couldn't bind to the listen socket."); 151 throw Bu::ServerSocketTcpException("Couldn't bind to the listen socket.");
151 } 152 }
152 153
153 if( bu_listen( nServer, nPoolSize ) < 0 ) 154 if( bu_listen( iSocket, nPoolSize ) < 0 )
154 { 155 {
155 throw Bu::TcpServerSocketException( 156 throw Bu::ServerSocketTcpException(
156 "Couldn't begin listening to the server socket." 157 "Couldn't begin listening to the server socket."
157 ); 158 );
158 } 159 }
159 160
160 FD_ZERO( &fdActive ); 161 FD_ZERO( &fdActive );
161 /* Initialize the set of active sockets. */ 162 /* Initialize the set of active sockets. */
162 FD_SET( nServer, &fdActive ); 163 FD_SET( iSocket, &fdActive );
163} 164}
164 165
165int Bu::TcpServerSocket::getSocket() 166Bu::Socket *Bu::ServerSocketTcp::accept( int nTimeoutSec, int nTimeoutUSec )
166{
167 return nServer;
168}
169
170int Bu::TcpServerSocket::accept( int nTimeoutSec, int nTimeoutUSec )
171{ 167{
172 fd_set fdRead = fdActive; 168 fd_set fdRead = fdActive;
173 169
@@ -177,14 +173,14 @@ int Bu::TcpServerSocket::accept( int nTimeoutSec, int nTimeoutUSec )
177 xT.tv_usec = nTimeoutUSec; 173 xT.tv_usec = nTimeoutUSec;
178 174
179 if( TEMP_FAILURE_RETRY( 175 if( TEMP_FAILURE_RETRY(
180 bu_select( nServer+1, &fdRead, NULL, NULL, &xT )) < 0 ) 176 bu_select( iSocket+1, &fdRead, NULL, NULL, &xT )) < 0 )
181 { 177 {
182 throw Bu::TcpServerSocketException( 178 throw Bu::ServerSocketTcpException(
183 "Error scanning for new connections: %s", strerror( errno ) 179 "Error scanning for new connections: %s", strerror( errno )
184 ); 180 );
185 } 181 }
186 182
187 if( FD_ISSET( nServer, &fdRead ) ) 183 if( FD_ISSET( iSocket, &fdRead ) )
188 { 184 {
189 struct sockaddr_in clientname; 185 struct sockaddr_in clientname;
190 socklen_t size; 186 socklen_t size;
@@ -192,23 +188,23 @@ int Bu::TcpServerSocket::accept( int nTimeoutSec, int nTimeoutUSec )
192 188
193 size = sizeof( clientname ); 189 size = sizeof( clientname );
194#ifdef WIN32 190#ifdef WIN32
195 nClient = bu_accept( nServer, (struct sockaddr *)&clientname, &size); 191 nClient = bu_accept( iSocket, (struct sockaddr *)&clientname, &size);
196#else /* not-WIN32 */ 192#else /* not-WIN32 */
197#ifdef __CYGWIN__ 193#ifdef __CYGWIN__
198 nClient = ::accept( nServer, (struct sockaddr *)&clientname, 194 nClient = ::accept( iSocket, (struct sockaddr *)&clientname,
199 (int *)&size 195 (int *)&size
200 ); 196 );
201#else /* not-cygwin */ 197#else /* not-cygwin */
202#ifdef __APPLE__ 198#ifdef __APPLE__
203 nClient = ::accept( nServer, (struct sockaddr *)&clientname, (socklen_t*)&size ); 199 nClient = ::accept( iSocket, (struct sockaddr *)&clientname, (socklen_t*)&size );
204#else /* linux */ 200#else /* linux */
205 nClient = ::accept( nServer, (struct sockaddr *)&clientname, &size ); 201 nClient = ::accept( iSocket, (struct sockaddr *)&clientname, &size );
206#endif /* __APPLE__ */ 202#endif /* __APPLE__ */
207#endif /* __CYGWIN__ */ 203#endif /* __CYGWIN__ */
208#endif /* WIN32 */ 204#endif /* WIN32 */
209 if( nClient < 0 ) 205 if( nClient < 0 )
210 { 206 {
211 throw Bu::TcpServerSocketException( 207 throw Bu::ServerSocketTcpException(
212 "Error accepting a new connection: %s", strerror( errno ) 208 "Error accepting a new connection: %s", strerror( errno )
213 ); 209 );
214 } 210 }
@@ -227,7 +223,7 @@ int Bu::TcpServerSocket::accept( int nTimeoutSec, int nTimeoutUSec )
227 flags |= O_NONBLOCK; 223 flags |= O_NONBLOCK;
228 if( fcntl( nClient, F_SETFL, flags ) < 0) 224 if( fcntl( nClient, F_SETFL, flags ) < 0)
229 { 225 {
230 throw Bu::TcpServerSocketException( 226 throw Bu::ServerSocketTcpException(
231 "Error setting option on client socket: %s", 227 "Error setting option on client socket: %s",
232 strerror( errno ) 228 strerror( errno )
233 ); 229 );
@@ -244,14 +240,15 @@ int Bu::TcpServerSocket::accept( int nTimeoutSec, int nTimeoutUSec )
244#endif 240#endif
245 } 241 }
246 242
247 return nClient; 243 return new SocketTcp( nClient );
248 } 244 }
249 245
250 return -1; 246 return NULL;
251} 247}
252 248
253int Bu::TcpServerSocket::getPort() 249bool Bu::ServerSocketTcp::getFd( int &rFdOut ) const
254{ 250{
255 return nPort; 251 rFdOut = iSocket;
252 return true;
256} 253}
257 254