diff --git a/util/socket.cpp b/util/socket.cpp index 6482b0a3..c7379470 100644 --- a/util/socket.cpp +++ b/util/socket.cpp @@ -11,12 +11,19 @@ #include #endif +std::string uint2string(unsigned int i){ + std::stringstream st; + st << i; + return st.str(); +} + /// Create a new base socket. This is a basic constructor for converting any valid socket to a Socket::Connection. /// \param sockNo Integer representing the socket to convert. Socket::Connection::Connection(int sockNo){ sock = sockNo; up = 0; down = 0; + conntime = time(0); Error = false; Blocking = false; }//Socket::Connection basic constructor @@ -27,6 +34,7 @@ Socket::Connection::Connection(){ sock = -1; up = 0; down = 0; + conntime = time(0); Error = false; Blocking = false; }//Socket::Connection basic constructor @@ -64,6 +72,7 @@ Socket::Connection::Connection(std::string address, bool nonblock){ Blocking = false; up = 0; down = 0; + conntime = time(0); sockaddr_un addr; addr.sun_family = AF_UNIX; strncpy(addr.sun_path, address.c_str(), address.size()+1); @@ -92,6 +101,7 @@ Socket::Connection::Connection(std::string host, int port, bool nonblock){ Blocking = false; up = 0; down = 0; + conntime = time(0); std::stringstream ss; ss << port; @@ -205,6 +215,11 @@ unsigned int Socket::Connection::dataDown(){ return down; } +/// Returns a std::string of stats, ended by a newline. +std::string Socket::Connection::getStats(){ + return getHost() + uint2string(time(0) - conntime) + " " + uint2string(up) + uint2string(down) + "\n"; +} + /// 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. diff --git a/util/socket.h b/util/socket.h index 95c28d82..b1238abc 100644 --- a/util/socket.h +++ b/util/socket.h @@ -25,6 +25,7 @@ namespace Socket{ std::string remotehost; ///< Stores remote host address. unsigned int up; unsigned int down; + unsigned int conntime; public: Connection(); ///< Create a new disconnected base socket. Connection(int sockNo); ///< Create a new base socket. @@ -51,6 +52,7 @@ namespace Socket{ 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. + std::string getStats(); ///< Returns a std::string of stats, ended by a newline. 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.