Added byte counters to sockets, needed for issue #1
This commit is contained in:
		
							parent
							
								
									f9dcedb0e1
								
							
						
					
					
						commit
						0fb103a3b4
					
				
					 2 changed files with 35 additions and 4 deletions
				
			
		|  | @ -15,6 +15,8 @@ | ||||||
| /// \param sockNo Integer representing the socket to convert.
 | /// \param sockNo Integer representing the socket to convert.
 | ||||||
| Socket::Connection::Connection(int sockNo){ | Socket::Connection::Connection(int sockNo){ | ||||||
|   sock = sockNo; |   sock = sockNo; | ||||||
|  |   up = 0; | ||||||
|  |   down = 0; | ||||||
|   Error = false; |   Error = false; | ||||||
|   Blocking = false; |   Blocking = false; | ||||||
| }//Socket::Connection basic constructor
 | }//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.
 | /// A socket created like this is always disconnected and should/could be overwritten at some point.
 | ||||||
| Socket::Connection::Connection(){ | Socket::Connection::Connection(){ | ||||||
|   sock = -1; |   sock = -1; | ||||||
|  |   up = 0; | ||||||
|  |   down = 0; | ||||||
|   Error = false; |   Error = false; | ||||||
|   Blocking = false; |   Blocking = false; | ||||||
| }//Socket::Connection basic constructor
 | }//Socket::Connection basic constructor
 | ||||||
|  | @ -58,6 +62,8 @@ Socket::Connection::Connection(std::string address, bool nonblock){ | ||||||
|   } |   } | ||||||
|   Error = false; |   Error = false; | ||||||
|   Blocking = false; |   Blocking = false; | ||||||
|  |   up = 0; | ||||||
|  |   down = 0; | ||||||
|   sockaddr_un addr; |   sockaddr_un addr; | ||||||
|   addr.sun_family = AF_UNIX; |   addr.sun_family = AF_UNIX; | ||||||
|   strncpy(addr.sun_path, address.c_str(), address.size()+1); |   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; |   struct addrinfo *result, *rp, hints; | ||||||
|   Error = false; |   Error = false; | ||||||
|   Blocking = false; |   Blocking = false; | ||||||
|  |   up = 0; | ||||||
|  |   down = 0; | ||||||
|   std::stringstream ss; |   std::stringstream ss; | ||||||
|   ss << port; |   ss << port; | ||||||
| 
 | 
 | ||||||
|  | @ -187,6 +195,16 @@ bool Socket::Connection::connected(){ | ||||||
|   return (sock >= 0); |   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.
 | /// 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
 | /// If the socket is nonblocking and not all data can be written, this function sets internal variable Blocking to true
 | ||||||
| /// and returns false.
 | /// 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)); |       fprintf(stderr, "Could not write data! Error: %s\n", strerror(errno)); | ||||||
|       #endif |       #endif | ||||||
|       close(); |       close(); | ||||||
|  |       up += sofar; | ||||||
|       return false; |       return false; | ||||||
|     }else{ |     }else{ | ||||||
|       sofar += r; |       sofar += r; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |   up += sofar; | ||||||
|   return true; |   return true; | ||||||
| }//DDv::Socket::write
 | }//DDv::Socket::write
 | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| /// Reads data from socket. This function blocks if the socket is blocking and all data cannot be read right away.
 | /// 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
 | /// If the socket is nonblocking and not all data can be read, this function sets internal variable Blocking to true
 | ||||||
| /// and returns false.
 | /// 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); |     int r = recv(sock, (char*)buffer + sofar, len-sofar, 0); | ||||||
|     if (r < 0){ |     if (r < 0){ | ||||||
|       switch (errno){ |       switch (errno){ | ||||||
|         case EWOULDBLOCK: return 0; break; |         case EWOULDBLOCK: | ||||||
|  |           down += sofar; | ||||||
|  |           return 0; | ||||||
|  |           break; | ||||||
|         default: |         default: | ||||||
|           Error = true; |           Error = true; | ||||||
|           #if DEBUG >= 2 |           #if DEBUG >= 2 | ||||||
|           fprintf(stderr, "Could not read data! Error %i: %s\n", r, strerror(errno)); |           fprintf(stderr, "Could not read data! Error %i: %s\n", r, strerror(errno)); | ||||||
|           #endif |           #endif | ||||||
|           close(); |           close(); | ||||||
|  |           down += sofar; | ||||||
|           break; |           break; | ||||||
|       } |       } | ||||||
|       return false; |       return false; | ||||||
|  | @ -243,11 +266,13 @@ bool Socket::Connection::read(void * buffer, int len){ | ||||||
|         fprintf(stderr, "Could not read data! Socket is closed.\n"); |         fprintf(stderr, "Could not read data! Socket is closed.\n"); | ||||||
|         #endif |         #endif | ||||||
|         close(); |         close(); | ||||||
|  |         down += sofar; | ||||||
|         return false; |         return false; | ||||||
|       } |       } | ||||||
|       sofar += r; |       sofar += r; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |   down += sofar; | ||||||
|   return true; |   return true; | ||||||
| }//Socket::Connection::read
 | }//Socket::Connection::read
 | ||||||
| 
 | 
 | ||||||
|  | @ -285,6 +310,7 @@ int Socket::Connection::iwrite(void * buffer, int len){ | ||||||
|     #endif |     #endif | ||||||
|     close(); |     close(); | ||||||
|   } |   } | ||||||
|  |   up += r; | ||||||
|   return r; |   return r; | ||||||
| }//Socket::Connection::iwrite
 | }//Socket::Connection::iwrite
 | ||||||
| 
 | 
 | ||||||
|  | @ -315,6 +341,7 @@ int Socket::Connection::iread(void * buffer, int len){ | ||||||
|     #endif |     #endif | ||||||
|     close(); |     close(); | ||||||
|   } |   } | ||||||
|  |   down += r; | ||||||
|   return r; |   return r; | ||||||
| }//Socket::Connection::iread
 | }//Socket::Connection::iread
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -23,6 +23,8 @@ namespace Socket{ | ||||||
|     private: |     private: | ||||||
|       int sock; ///< Internally saved socket number.
 |       int sock; ///< Internally saved socket number.
 | ||||||
|       std::string remotehost; ///< Stores remote host address.
 |       std::string remotehost; ///< Stores remote host address.
 | ||||||
|  |       unsigned int up; | ||||||
|  |       unsigned int down; | ||||||
|     public: |     public: | ||||||
|       Connection(); ///< Create a new disconnected base socket.
 |       Connection(); ///< Create a new disconnected base socket.
 | ||||||
|       Connection(int sockNo); ///< Create a new 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.
 |       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 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 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.
 |       signed int ready(); ///< Returns the ready-state for this socket.
 | ||||||
|       bool connected(); ///< Returns the connected-state for this socket.
 |       bool connected(); ///< Returns the connected-state for this socket.
 | ||||||
|       bool read(void * buffer, int len); ///< Reads data from 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.
 |       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.
 |       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; |       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.
 |   /// This class is for easily setting up listening socket, either TCP or Unix.
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Thulinma
						Thulinma