aboutsummaryrefslogtreecommitdiff
path: root/src/socket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/socket.cpp')
-rw-r--r--src/socket.cpp27
1 files changed, 14 insertions, 13 deletions
diff --git a/src/socket.cpp b/src/socket.cpp
index 651a2e1..531d8ac 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -19,11 +19,12 @@
19#include <errno.h> 19#include <errno.h>
20#include <fcntl.h> 20#include <fcntl.h>
21#include "socket.h" 21#include "socket.h"
22#include "exceptions.h"
23#include "osx_compatibility.h" 22#include "osx_compatibility.h"
24 23
25#define RBS (1024*2) 24#define RBS (1024*2)
26 25
26namespace Bu { subExceptionDef( SocketException ) }
27
27Bu::Socket::Socket( int nSocket ) : 28Bu::Socket::Socket( int nSocket ) :
28 nSocket( nSocket ), 29 nSocket( nSocket ),
29 bActive( true ) 30 bActive( true )
@@ -50,7 +51,7 @@ Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout )
50 flags |= O_NONBLOCK; 51 flags |= O_NONBLOCK;
51 if (fcntl(nSocket, F_SETFL, flags) < 0) 52 if (fcntl(nSocket, F_SETFL, flags) < 0)
52 { 53 {
53 throw ExceptionBase("Couldn't set socket options.\n"); 54 throw Bu::SocketException("Couldn't set socket options.\n");
54 } 55 }
55 56
56 /* Connect to the server. */ 57 /* Connect to the server. */
@@ -63,7 +64,7 @@ Bu::Socket::Socket( const Bu::FString &sAddr, int nPort, int nTimeout )
63 hostinfo = gethostbyname( sAddr.getStr() ); 64 hostinfo = gethostbyname( sAddr.getStr() );
64 if (hostinfo == NULL) 65 if (hostinfo == NULL)
65 { 66 {
66 throw ExceptionBase("Couldn't resolve hostname.\n"); 67 throw Bu::SocketException("Couldn't resolve hostname.\n");
67 } 68 }
68 xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr; 69 xServerName.sin_addr = *(struct in_addr *) hostinfo->h_addr;
69 } 70 }
@@ -136,8 +137,8 @@ void Bu::Socket::read()
136 { 137 {
137 //printf("errno: %d, %s\n", errno, strerror( errno ) ); 138 //printf("errno: %d, %s\n", errno, strerror( errno ) );
138 //perror("readInput"); 139 //perror("readInput");
139 throw ConnectionException( 140 throw SocketException(
140 excodeReadError, 141 SocketException::cRead,
141 "Read error: %s", 142 "Read error: %s",
142 strerror( errno ) 143 strerror( errno )
143 ); 144 );
@@ -162,8 +163,8 @@ void Bu::Socket::read()
162 struct timeval tv = { 0, 0 }; 163 struct timeval tv = { 0, 0 };
163 int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); 164 int retval = select( nSocket+1, &rfds, NULL, NULL, &tv );
164 if( retval == -1 ) 165 if( retval == -1 )
165 throw ConnectionException( 166 throw SocketException(
166 excodeBadReadError, 167 SocketException::cBadRead,
167 "Bad Read error" 168 "Bad Read error"
168 ); 169 );
169 if( !FD_ISSET( nSocket, &rfds ) ) 170 if( !FD_ISSET( nSocket, &rfds ) )
@@ -178,7 +179,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes )
178 int nRead = TEMP_FAILURE_RETRY( ::read( nSocket, pBuf, nBytes ) ); 179 int nRead = TEMP_FAILURE_RETRY( ::read( nSocket, pBuf, nBytes ) );
179 if( nRead < 0 ) 180 if( nRead < 0 )
180 { 181 {
181 throw ConnectionException( excodeReadError, strerror(errno) ); 182 throw SocketException( SocketException::cRead, strerror(errno) );
182 } 183 }
183 return nRead; 184 return nRead;
184} 185}
@@ -220,7 +221,7 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes )
220 if( nWrote < 0 ) 221 if( nWrote < 0 )
221 { 222 {
222 if( errno == EAGAIN ) return 0; 223 if( errno == EAGAIN ) return 0;
223 throw ConnectionException( excodeWriteError, strerror(errno) ); 224 throw SocketException( SocketException::cWrite, strerror(errno) );
224 } 225 }
225 return nWrote; 226 return nWrote;
226} 227}
@@ -288,8 +289,8 @@ bool Bu::Socket::canRead()
288 struct timeval tv = { 0, 0 }; 289 struct timeval tv = { 0, 0 };
289 int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); 290 int retval = select( nSocket+1, &rfds, NULL, NULL, &tv );
290 if( retval == -1 ) 291 if( retval == -1 )
291 throw ConnectionException( 292 throw SocketException(
292 excodeBadReadError, 293 SocketException::cBadRead,
293 "Bad Read error" 294 "Bad Read error"
294 ); 295 );
295 if( !FD_ISSET( nSocket, &rfds ) ) 296 if( !FD_ISSET( nSocket, &rfds ) )
@@ -305,8 +306,8 @@ bool Bu::Socket::canWrite()
305 struct timeval tv = { 0, 0 }; 306 struct timeval tv = { 0, 0 };
306 int retval = select( nSocket+1, NULL, &wfds, NULL, &tv ); 307 int retval = select( nSocket+1, NULL, &wfds, NULL, &tv );
307 if( retval == -1 ) 308 if( retval == -1 )
308 throw ConnectionException( 309 throw SocketException(
309 excodeBadReadError, 310 SocketException::cBadRead,
310 "Bad Read error" 311 "Bad Read error"
311 ); 312 );
312 if( !FD_ISSET( nSocket, &wfds ) ) 313 if( !FD_ISSET( nSocket, &wfds ) )