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
|
/*
* Modification History
*
* 2001-January-9 Jason Rohrer
* Created.
*
* 2001-January-15 Jason Rohrer
* Commented out redundant status messages that should be handled at a
* higher level.
*
* 2001-January-28 Jason Rohrer
* Changed to comply with new initSocketFramework Socket interface.
*
* 2001-January-29 Jason Rohrer
* Fixed an endian bug with the port number.
*
* 2001-May-12 Jason Rohrer
* Added a port number to the "bad socket bind" error message.
*
* 2001-November-13 Jason Rohrer
* Added a missing include.
*
* 2001-December-12 Jason Rohrer
* Changed some type usage and the include order to make BSD compatible.
* Added a BSD definition test to make socklen_t type compatible
* on both BSD and Linux.
*
* 2002-March-27 Jason Rohrer
* Added a timeout to accept.
*
* 2002-March-29 Jason Rohrer
* Fixed a bug in return value when accept fails.
*
* 2002-April-11 Jason Rohrer
* Changed to use select instead of poll to make it more portable.
* Fixed a bug in the timeout implementation.
*
* 2002-April-12 Jason Rohrer
* Changed to use poll on Linux and select on BSD,
* since select does not seem to work on LinuxX86.
* Fixed X86 accept/select bug by getting rid of union
* and some unused parameters (it seems that stack space was a problem).
*
* 2004-February-23 Jason Rohrer
* Added socket option to clear up lingering bound ports after shutdown.
* Changed to close server socket instead of using shutdown.
*
* 2010-January-26 Jason Rohrer
* Fixed socklen_t on later versions of MacOSX.
*/
#include "minorGems/network/SocketServer.h"
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
// BSD does not define socklen_t
#ifdef BSD
#ifndef IPHONE
#ifndef __OpenBSD__
#ifndef _SOCKLEN_T // later versions of MacOS define it and mark it
typedef int socklen_t;
#endif
#endif
#endif
#endif
#ifndef BSD
// poll not available on BSD
#include <sys/poll.h>
#endif
/**
* BSD Compilation note:
*
* Use g++ option -DBSD to define the BSD preprocessor variable.
*/
SocketServer::SocketServer( int inPort, int inMaxQueuedConnections ) {
int error = 0;
if( !Socket::isFrameworkInitialized() ) {
// try to init the framework
int error = Socket::initSocketFramework();
if( error == -1 ) {
printf( "initializing network socket framework failed\n" );
exit( 1 );
}
}
// create the socket
int sockID = socket( AF_INET, SOCK_STREAM, 0 );
if( sockID == -1 ) {
printf( "Failed to construct a socket\n" );
exit( 1 );
}
// store socket id in native object pointer
int *idStorage = new int[1];
idStorage[0] = sockID;
mNativeObjectPointer = (void *)idStorage;
// this setsockopt code partially copied from gnut
// set socket option to enable reusing addresses so that the socket is
// unbound immediately when the server is shut down
// (otherwise, rebinding to the same port will fail for a while
// after the server is shut down)
int reuseAddressValue = 1;
error = setsockopt( sockID,
SOL_SOCKET, // socket-level option
SO_REUSEADDR, // reuse address option
&reuseAddressValue, // value to set for this option
sizeof( reuseAddressValue) ); // size of the value
if( error == -1 ) {
printf( "Failed to set socket options\n" );
exit( 1 );
}
// bind socket to the port
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons( inPort );
address.sin_addr.s_addr = INADDR_ANY;
error = bind( sockID, (struct sockaddr *) &address, sizeof( address ) );
if( error == -1 ) {
printf( "Bad socket bind, port %d\n", inPort );
exit( 1 );
}
// start listening for connections
error = listen( sockID, inMaxQueuedConnections );
if( error == -1 ) {
printf( "Bad socket listen\n" );
exit(1);
}
}
SocketServer::~SocketServer() {
int *socketIDptr = (int *)( mNativeObjectPointer );
int socketID = socketIDptr[0];
close( socketID );
delete [] socketIDptr;
}
Socket *SocketServer::acceptConnection( long inTimeoutInMilliseconds,
char *outTimedOut ) {
// printf( "Waiting for a connection.\n" );
// extract socket id from native object pointer
int *socketIDptr = (int *)( mNativeObjectPointer );
int socketID = socketIDptr[0];
if( outTimedOut != NULL ) {
*outTimedOut = false;
}
if( inTimeoutInMilliseconds != -1 ) {
// if we have a timeout specified, select before accepting
// this found in the Linux man page for select,
// but idea (which originally used poll) was found
// in the Unix Socket FAQ
fd_set rfds;
struct timeval tv;
int retval;
// insert our socket descriptor into this set
FD_ZERO( &rfds );
FD_SET( socketID, &rfds );
// convert our timeout into the structure's format
tv.tv_sec = inTimeoutInMilliseconds / 1000;
tv.tv_usec = ( inTimeoutInMilliseconds % 1000 ) * 1000 ;
retval = select( socketID + 1, &rfds, NULL, NULL, &tv );
if( retval == 0 ) {
// timeout
if( outTimedOut != NULL ) {
*outTimedOut = true;
}
return NULL;
}
}
int acceptedID = accept( socketID, NULL, NULL );
if( acceptedID == -1 ) {
printf( "Failed to accept a network connection.\n" );
return NULL;
}
else {
Socket *acceptedSocket = new Socket();
int *idStorage = new int[1];
idStorage[0] = acceptedID;
acceptedSocket->mNativeObjectPointer = (void *)idStorage;
//printf( "Connection received.\n" );
return acceptedSocket;
}
}
|