Added byte counters to sockets, needed for issue #1

This commit is contained in:
Thulinma 2011-11-26 02:31:56 +01:00
parent f9dcedb0e1
commit 0fb103a3b4
2 changed files with 35 additions and 4 deletions

View file

@ -15,6 +15,8 @@
/// \param sockNo Integer representing the socket to convert.
Socket::Connection::Connection(int sockNo){
sock = sockNo;
up = 0;
down = 0;
Error = false;
Blocking = false;
}//Socket::Connection basic constructor
@ -23,6 +25,8 @@ Socket::Connection::Connection(int sockNo){
/// A socket created like this is always disconnected and should/could be overwritten at some point.
Socket::Connection::Connection(){
sock = -1;
up = 0;
down = 0;
Error = false;
Blocking = false;
}//Socket::Connection basic constructor
@ -58,6 +62,8 @@ Socket::Connection::Connection(std::string address, bool nonblock){
}
Error = false;
Blocking = false;
up = 0;
down = 0;
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, address.c_str(), address.size()+1);
@ -84,6 +90,8 @@ Socket::Connection::Connection(std::string host, int port, bool nonblock){
struct addrinfo *result, *rp, hints;
Error = false;
Blocking = false;
up = 0;
down = 0;
std::stringstream ss;
ss << port;
@ -187,6 +195,16 @@ bool Socket::Connection::connected(){
return (sock >= 0);
}
/// Returns total amount of bytes sent.
unsigned int Socket::Connection::dataUp(){
return up;
}
/// Returns total amount of bytes received.
unsigned int Socket::Connection::dataDown(){
return down;
}
/// Writes data to socket. This function blocks if the socket is blocking and all data cannot be written right away.
/// If the socket is nonblocking and not all data can be written, this function sets internal variable Blocking to true
/// and returns false.
@ -204,15 +222,16 @@ bool Socket::Connection::write(const void * buffer, int len){
fprintf(stderr, "Could not write data! Error: %s\n", strerror(errno));
#endif
close();
up += sofar;
return false;
}else{
sofar += r;
}
}
up += sofar;
return true;
}//DDv::Socket::write
/// Reads data from socket. This function blocks if the socket is blocking and all data cannot be read right away.
/// If the socket is nonblocking and not all data can be read, this function sets internal variable Blocking to true
/// and returns false.
@ -226,13 +245,17 @@ bool Socket::Connection::read(void * buffer, int len){
int r = recv(sock, (char*)buffer + sofar, len-sofar, 0);
if (r < 0){
switch (errno){
case EWOULDBLOCK: return 0; break;
case EWOULDBLOCK:
down += sofar;
return 0;
break;
default:
Error = true;
#if DEBUG >= 2
fprintf(stderr, "Could not read data! Error %i: %s\n", r, strerror(errno));
#endif
close();
down += sofar;
break;
}
return false;
@ -243,11 +266,13 @@ bool Socket::Connection::read(void * buffer, int len){
fprintf(stderr, "Could not read data! Socket is closed.\n");
#endif
close();
down += sofar;
return false;
}
sofar += r;
}
}
down += sofar;
return true;
}//Socket::Connection::read
@ -285,6 +310,7 @@ int Socket::Connection::iwrite(void * buffer, int len){
#endif
close();
}
up += r;
return r;
}//Socket::Connection::iwrite
@ -315,6 +341,7 @@ int Socket::Connection::iread(void * buffer, int len){
#endif
close();
}
down += r;
return r;
}//Socket::Connection::iread

View file

@ -23,6 +23,8 @@ namespace Socket{
private:
int sock; ///< Internally saved socket number.
std::string remotehost; ///< Stores remote host address.
unsigned int up;
unsigned int down;
public:
Connection(); ///< Create a new disconnected base socket.
Connection(int sockNo); ///< Create a new base socket.
@ -30,8 +32,6 @@ namespace Socket{
Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
bool canRead(); ///< Calls poll() on the socket, checking if data is available.
bool canWrite(); ///< Calls poll() on the socket, checking if data can be written.
bool Error; ///< Set to true if a socket error happened.
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
signed int ready(); ///< Returns the ready-state for this socket.
bool connected(); ///< Returns the connected-state for this socket.
bool read(void * buffer, int len); ///< Reads data from socket.
@ -49,7 +49,11 @@ namespace Socket{
std::string getHost(); ///< Gets hostname for connection, if available.
int getSocket(); ///< Returns internal socket number.
std::string getError(); ///< Returns a string describing the last error that occured.
unsigned int dataUp(); ///< Returns total amount of bytes sent.
unsigned int dataDown(); ///< Returns total amount of bytes received.
friend class Server;
bool Error; ///< Set to true if a socket error happened.
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
};
/// This class is for easily setting up listening socket, either TCP or Unix.