From 67c92852b590e9e204aa796e97b0fd97782d29cd Mon Sep 17 00:00:00 2001 From: Thulinma Date: Sun, 27 Nov 2011 02:22:04 +0100 Subject: [PATCH] Updated sockets lib with buffers for easy sending/receiving and such. --- util/socket.cpp | 16 ++++++++++++++++ util/socket.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/util/socket.cpp b/util/socket.cpp index 29b02cd1..4fdaceec 100644 --- a/util/socket.cpp +++ b/util/socket.cpp @@ -221,6 +221,22 @@ std::string Socket::Connection::getStats(std::string C){ return getHost() + " " + C + " " + uint2string(time(0) - conntime) + " " + uint2string(up) + " " + uint2string(down) + "\n"; } +/// Updates the downbuffer and upbuffer internal variables. +void Socket::Connection::spool(){ + iread(downbuffer); + iwrite(upbuffer); +} + +/// Returns a reference to the download buffer. +std::string & Socket::Connection::Received(){ + return downbuffer; +} + +/// Appends data to the upbuffer. +void Socket::Connection::Send(std::string data){ + upbuffer.append(data); +} + /// 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 a7a16dc9..bfe824e5 100644 --- a/util/socket.h +++ b/util/socket.h @@ -26,6 +26,8 @@ namespace Socket{ unsigned int up; unsigned int down; unsigned int conntime; + std::string downbuffer; ///< Stores temporary data coming in. + std::string upbuffer; ///< Stores temporary data going out. public: Connection(); ///< Create a new disconnected base socket. Connection(int sockNo); ///< Create a new base socket. @@ -46,6 +48,9 @@ namespace Socket{ bool swrite(std::string & buffer); ///< Read call that is compatible with std::string. bool iread(std::string & buffer); ///< Incremental write call that is compatible with std::string. bool iwrite(std::string & buffer); ///< Write call that is compatible with std::string. + void spool(); ///< Updates the downbuffer and upbuffer internal variables. + std::string & Received(); ///< Returns a reference to the download buffer. + void Send(std::string data); ///< Appends data to the upbuffer. void close(); ///< Close connection. std::string getHost(); ///< Gets hostname for connection, if available. int getSocket(); ///< Returns internal socket number.