From fc707da6b00e3ffbe048559db3b385703f724ebb Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 17 Jan 2018 00:19:14 +0100 Subject: [PATCH] Socket::Connection::skipBytes function implemented --- lib/socket.cpp | 20 ++++++++++++++++++++ lib/socket.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/lib/socket.cpp b/lib/socket.cpp index 2607c47d..c2e2e395 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -341,6 +341,7 @@ Socket::Connection::Connection(int sockNo){ conntime = Util::epoch(); Error = false; Blocking = false; + skipCount = 0; }// Socket::Connection basic constructor /// Simulate a socket using two file descriptors. @@ -355,6 +356,7 @@ Socket::Connection::Connection(int write, int read){ conntime = Util::epoch(); Error = false; Blocking = false; + skipCount = 0; }// Socket::Connection basic constructor /// Create a new disconnected base socket. This is a basic constructor for placeholder purposes. @@ -368,6 +370,7 @@ Socket::Connection::Connection(){ conntime = Util::epoch(); Error = false; Blocking = false; + skipCount = 0; }// Socket::Connection basic constructor void Socket::Connection::resetCounter(){ @@ -629,6 +632,11 @@ void Socket::Connection::SendNow(const std::string &data){ SendNow(data.data(), data.size()); } +void Socket::Connection::skipBytes(uint32_t byteCount){ + INFO_MSG("Skipping first %lu bytes going to socket", byteCount); + skipCount = byteCount; +} + /// Incremental write call. This function tries to write len bytes to the socket from the buffer, /// returning the amount of bytes it actually wrote. /// \param buffer Location of the buffer to write from. @@ -636,6 +644,18 @@ void Socket::Connection::SendNow(const std::string &data){ /// \returns The amount of bytes actually written. unsigned int Socket::Connection::iwrite(const void *buffer, int len){ if (!connected() || len < 1){return 0;} + if (skipCount){ + //We have bytes to skip writing. + //Pretend we write them, but don't really. + if (len <= skipCount){ + skipCount -= len; + return len; + }else{ + unsigned int retCode = iwrite((((char*)buffer)+skipCount), len-skipCount); + skipCount = 0; + return retCode; + } + } int r; if (sock >= 0){ r = send(sock, buffer, len, 0); diff --git a/lib/socket.h b/lib/socket.h index 73c391c3..cf8a4749 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -106,6 +106,8 @@ namespace Socket{ void SendNow(const std::string &data); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const char *data); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const char *data, size_t len); ///< Will not buffer anything but always send right away. Blocks. + void skipBytes(uint32_t byteCount); + uint32_t skipCount; // stats related methods unsigned int connTime(); ///< Returns the time this socket has been connected. uint64_t dataUp(); ///< Returns total amount of bytes sent.