diff options
| author | David <david@xagasoft.com> | 2008-10-27 16:07:50 +0000 |
|---|---|---|
| committer | David <david@xagasoft.com> | 2008-10-27 16:07:50 +0000 |
| commit | 61edfe804eb1a1dbf96fcdf2f1b700ed1cfa7ff9 (patch) | |
| tree | 530a0d86ca1043dbc04cd7513bc5dd65768a266d /src/serversocket.cpp | |
| parent | f7e8658f2274044bb452492b1af7e2cd5f82082c (diff) | |
| download | libbu++-61edfe804eb1a1dbf96fcdf2f1b700ed1cfa7ff9.tar.gz libbu++-61edfe804eb1a1dbf96fcdf2f1b700ed1cfa7ff9.tar.bz2 libbu++-61edfe804eb1a1dbf96fcdf2f1b700ed1cfa7ff9.tar.xz libbu++-61edfe804eb1a1dbf96fcdf2f1b700ed1cfa7ff9.zip | |
david - apparently windows prefers to dynamically load winsock and a couple other libraries, so I got it all compiling and working in windows, yay!... I tried to minimize the impact on the code: I made a DYNLOAD macro that evaluates to nothing on everything else, but runs the dynamic code if compiled for windows... also, apparently I had been randomly switching between ifdef and ifndef WIN32: so i made most of them ifdefs so it was less confusing...
Diffstat (limited to 'src/serversocket.cpp')
| -rw-r--r-- | src/serversocket.cpp | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/src/serversocket.cpp b/src/serversocket.cpp index 5ed661e..1908a8a 100644 --- a/src/serversocket.cpp +++ b/src/serversocket.cpp | |||
| @@ -35,11 +35,11 @@ Bu::ServerSocket::ServerSocket( int nPort, int nPoolSize ) : | |||
| 35 | 35 | ||
| 36 | /* Give the socket a name. */ | 36 | /* Give the socket a name. */ |
| 37 | name.sin_family = AF_INET; | 37 | name.sin_family = AF_INET; |
| 38 | name.sin_port = htons( nPort ); | 38 | name.sin_port = DYNLOAD htons( nPort ); |
| 39 | 39 | ||
| 40 | // I think this specifies who we will accept connections from, | 40 | // I think this specifies who we will accept connections from, |
| 41 | // a good thing to make configurable later on | 41 | // a good thing to make configurable later on |
| 42 | name.sin_addr.s_addr = htonl( INADDR_ANY ); | 42 | name.sin_addr.s_addr = DYNLOAD htonl( INADDR_ANY ); |
| 43 | 43 | ||
| 44 | startServer( name, nPoolSize ); | 44 | startServer( name, nPoolSize ); |
| 45 | } | 45 | } |
| @@ -52,10 +52,11 @@ Bu::ServerSocket::ServerSocket(const FString &sAddr,int nPort, int nPoolSize) : | |||
| 52 | 52 | ||
| 53 | /* Give the socket a name. */ | 53 | /* Give the socket a name. */ |
| 54 | name.sin_family = AF_INET; | 54 | name.sin_family = AF_INET; |
| 55 | name.sin_port = htons( nPort ); | 55 | |
| 56 | name.sin_port = DYNLOAD htons( nPort ); | ||
| 56 | 57 | ||
| 57 | #ifdef WIN32 | 58 | #ifdef WIN32 |
| 58 | name.sin_addr.s_addr = inet_addr( sAddr.getStr() ); | 59 | name.sin_addr.s_addr = DYNLOAD inet_addr( sAddr.getStr() ); |
| 59 | #else | 60 | #else |
| 60 | inet_aton( sAddr.getStr(), &name.sin_addr ); | 61 | inet_aton( sAddr.getStr(), &name.sin_addr ); |
| 61 | #endif | 62 | #endif |
| @@ -70,27 +71,29 @@ Bu::ServerSocket::~ServerSocket() | |||
| 70 | void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) | 71 | void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) |
| 71 | { | 72 | { |
| 72 | /* Create the socket. */ | 73 | /* Create the socket. */ |
| 73 | nServer = socket( PF_INET, SOCK_STREAM, 0 ); | 74 | nServer = DYNLOAD socket( PF_INET, SOCK_STREAM, 0 ); |
| 75 | |||
| 74 | if( nServer < 0 ) | 76 | if( nServer < 0 ) |
| 75 | { | 77 | { |
| 76 | throw Bu::ServerSocketException("Couldn't create a listen socket."); | 78 | throw Bu::ServerSocketException("Couldn't create a listen socket."); |
| 77 | } | 79 | } |
| 78 | 80 | ||
| 79 | int opt = 1; | 81 | int opt = 1; |
| 80 | setsockopt( | 82 | DYNLOAD setsockopt( |
| 81 | nServer, | 83 | nServer, |
| 82 | SOL_SOCKET, | 84 | SOL_SOCKET, |
| 83 | SO_REUSEADDR, | 85 | SO_REUSEADDR, |
| 84 | (char *)&opt, | 86 | (char *)&opt, |
| 85 | sizeof( opt ) | 87 | sizeof( opt ) |
| 86 | ); | 88 | ); |
| 87 | 89 | ||
| 88 | if( bind( nServer, (struct sockaddr *) &name, sizeof(name) ) < 0 ) | 90 | if( DYNLOAD bind( |
| 91 | nServer, (struct sockaddr *) &name, sizeof(name) ) < 0 ) | ||
| 89 | { | 92 | { |
| 90 | throw Bu::ServerSocketException("Couldn't bind to the listen socket."); | 93 | throw Bu::ServerSocketException("Couldn't bind to the listen socket."); |
| 91 | } | 94 | } |
| 92 | 95 | ||
| 93 | if( listen( nServer, nPoolSize ) < 0 ) | 96 | if( DYNLOAD listen( nServer, nPoolSize ) < 0 ) |
| 94 | { | 97 | { |
| 95 | throw Bu::ServerSocketException( | 98 | throw Bu::ServerSocketException( |
| 96 | "Couldn't begin listening to the server socket." | 99 | "Couldn't begin listening to the server socket." |
| @@ -116,20 +119,29 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) | |||
| 116 | xT.tv_sec = nTimeoutSec; | 119 | xT.tv_sec = nTimeoutSec; |
| 117 | xT.tv_usec = nTimeoutUSec; | 120 | xT.tv_usec = nTimeoutUSec; |
| 118 | 121 | ||
| 119 | if( TEMP_FAILURE_RETRY(select( nServer+1, &fdRead, NULL, NULL, &xT )) < 0 ) | 122 | if( TEMP_FAILURE_RETRY( |
| 123 | DYNLOAD select( nServer+1, &fdRead, NULL, NULL, &xT )) < 0 ) | ||
| 120 | { | 124 | { |
| 121 | throw Bu::ServerSocketException( | 125 | throw Bu::ServerSocketException( |
| 122 | "Error scanning for new connections: %s", strerror( errno ) | 126 | "Error scanning for new connections: %s", strerror( errno ) |
| 123 | ); | 127 | ); |
| 124 | } | 128 | } |
| 125 | 129 | ||
| 130 | #ifdef WIN32 | ||
| 131 | if( DynamicWinsock2::DYN_FD_ISSET( nServer, &fdRead ) ) | ||
| 132 | #else | ||
| 126 | if( FD_ISSET( nServer, &fdRead ) ) | 133 | if( FD_ISSET( nServer, &fdRead ) ) |
| 134 | #endif | ||
| 127 | { | 135 | { |
| 128 | struct sockaddr_in clientname; | 136 | struct sockaddr_in clientname; |
| 129 | socklen_t size; | 137 | socklen_t size; |
| 130 | int nClient; | 138 | int nClient; |
| 131 | 139 | ||
| 132 | size = sizeof( clientname ); | 140 | size = sizeof( clientname ); |
| 141 | #ifdef WIN32 | ||
| 142 | nClient = DYNLOAD accept( | ||
| 143 | nServer, (struct sockaddr *)&clientname, (int *)&size); | ||
| 144 | #else /* not-WIN32 */ | ||
| 133 | #ifdef __CYGWIN__ | 145 | #ifdef __CYGWIN__ |
| 134 | nClient = ::accept( nServer, (struct sockaddr *)&clientname, | 146 | nClient = ::accept( nServer, (struct sockaddr *)&clientname, |
| 135 | (int *)&size | 147 | (int *)&size |
| @@ -141,6 +153,7 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) | |||
| 141 | nClient = ::accept( nServer, (struct sockaddr *)&clientname, &size ); | 153 | nClient = ::accept( nServer, (struct sockaddr *)&clientname, &size ); |
| 142 | #endif /* __APPLE__ */ | 154 | #endif /* __APPLE__ */ |
| 143 | #endif /* __CYGWIN__ */ | 155 | #endif /* __CYGWIN__ */ |
| 156 | #endif /* WIN32 */ | ||
| 144 | if( nClient < 0 ) | 157 | if( nClient < 0 ) |
| 145 | { | 158 | { |
| 146 | throw Bu::ServerSocketException( | 159 | throw Bu::ServerSocketException( |
| @@ -175,7 +188,7 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) | |||
| 175 | // If iMode = 0, blocking is enabled; | 188 | // If iMode = 0, blocking is enabled; |
| 176 | // If iMode != 0, non-blocking mode is enabled. | 189 | // If iMode != 0, non-blocking mode is enabled. |
| 177 | u_long iMode = 1; | 190 | u_long iMode = 1; |
| 178 | ioctlsocket(nClient, FIONBIO, &iMode); | 191 | DYNLOAD ioctlsocket(nClient, FIONBIO, &iMode); |
| 179 | #endif | 192 | #endif |
| 180 | } | 193 | } |
| 181 | 194 | ||
