Cleanup in TS dir (Erik! No binaries in the git! ), added getError() to socket lib, fixed Buffer problems when a Connector is being slow.

This commit is contained in:
Thulinma 2011-08-12 02:03:07 +02:00
parent cad02ccc8e
commit 7d3acba737
2 changed files with 34 additions and 6 deletions

View file

@ -33,6 +33,11 @@ void Socket::Connection::close(){
/// Returns internal socket number. /// Returns internal socket number.
int Socket::Connection::getSocket(){return sock;} int Socket::Connection::getSocket(){return sock;}
/// Returns a string describing the last error that occured.
/// Simply calls strerror(errno) - not very reliable!
/// \todo Improve getError at some point to be more reliable and only report socket errors.
std::string Socket::Connection::getError(){return strerror(errno);}
/// Create a new Unix Socket. This socket will (try to) connect to the given address right away. /// Create a new Unix Socket. This socket will (try to) connect to the given address right away.
/// \param address String containing the location of the Unix socket to connect to. /// \param address String containing the location of the Unix socket to connect to.
/// \param nonblock Whether the socket should be nonblocking. False by default. /// \param nonblock Whether the socket should be nonblocking. False by default.
@ -86,7 +91,11 @@ signed int Socket::Connection::ready(){
} }
} }
if (r == 0){ if (r == 0){
close(); return -1; #if DEBUG >= 4
fprintf(stderr, "Socket ready error - socket is closed.\n");
#endif
close();
return -1;
} }
return r; return r;
} }
@ -192,7 +201,12 @@ int Socket::Connection::iwrite(void * buffer, int len){
break; break;
} }
} }
if (r == 0){close();} if (r == 0){
#if DEBUG >= 4
fprintf(stderr, "Could not iwrite data! Socket is closed.\n");
#endif
close();
}
return r; return r;
}//Socket::Connection::iwrite }//Socket::Connection::iwrite
@ -217,7 +231,12 @@ int Socket::Connection::iread(void * buffer, int len){
break; break;
} }
} }
if (r == 0){close();} if (r == 0){
#if DEBUG >= 4
fprintf(stderr, "Could not iread data! Socket is closed.\n");
#endif
close();
}
return r; return r;
}//Socket::Connection::iread }//Socket::Connection::iread
@ -285,7 +304,7 @@ std::string Socket::Connection::getHost(){
Socket::Server::Server(){ Socket::Server::Server(){
sock = -1; sock = -1;
}//Socket::Server base Constructor }//Socket::Server base Constructor
/// Create a new TCP Server. The socket is immediately bound and set to listen. /// Create a new TCP Server. The socket is immediately bound and set to listen.
/// A maximum of 100 connections will be accepted between accept() calls. /// A maximum of 100 connections will be accepted between accept() calls.
/// Any further connections coming in will be dropped. /// Any further connections coming in will be dropped.
@ -399,7 +418,12 @@ Socket::Connection Socket::Server::accept(bool nonblock){
} }
Socket::Connection tmp(r); Socket::Connection tmp(r);
if (r < 0){ if (r < 0){
if (errno != EWOULDBLOCK && errno != EAGAIN){close();} if (errno != EWOULDBLOCK && errno != EAGAIN){
#if DEBUG >= 1
fprintf(stderr, "Error during accept - closing server socket.\n");
#endif
close();
}
}else{ }else{
if (addrinfo.sin6_family == AF_INET6){ if (addrinfo.sin6_family == AF_INET6){
tmp.remotehost = inet_ntop(AF_INET6, &(addrinfo.sin6_addr), addrconv, INET6_ADDRSTRLEN); tmp.remotehost = inet_ntop(AF_INET6, &(addrinfo.sin6_addr), addrconv, INET6_ADDRSTRLEN);
@ -426,6 +450,9 @@ Socket::Connection Socket::Server::accept(bool nonblock){
/// Close connection. The internal socket is closed and then set to -1. /// Close connection. The internal socket is closed and then set to -1.
void Socket::Server::close(){ void Socket::Server::close(){
#if DEBUG >= 4
fprintf(stderr, "ServerSocket closed.\n");
#endif
shutdown(sock, SHUT_RDWR); shutdown(sock, SHUT_RDWR);
::close(sock); ::close(sock);
sock = -1; sock = -1;

View file

@ -45,6 +45,7 @@ namespace Socket{
void close(); ///< Close connection. void close(); ///< Close connection.
std::string getHost(); ///< Gets hostname for connection, if available. std::string getHost(); ///< Gets hostname for connection, if available.
int getSocket(); ///< Returns internal socket number. int getSocket(); ///< Returns internal socket number.
std::string getError(); ///< Returns a string describing the last error that occured.
friend class Server; friend class Server;
}; };
@ -61,5 +62,5 @@ namespace Socket{
void close(); ///< Close connection. void close(); ///< Close connection.
int getSocket(); ///< Returns internal socket number. int getSocket(); ///< Returns internal socket number.
}; };
}; };