Updated sockets lib with buffers for easy sending/receiving and such.

This commit is contained in:
Thulinma 2011-11-27 02:22:04 +01:00
parent 2f5d5fcecb
commit 67c92852b5
2 changed files with 21 additions and 0 deletions

View file

@ -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.

View file

@ -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.