Socket::Connection::skipBytes function implemented

This commit is contained in:
Thulinma 2018-01-17 00:19:14 +01:00
parent 05c144fb9e
commit fc707da6b0
2 changed files with 22 additions and 0 deletions

View file

@ -341,6 +341,7 @@ Socket::Connection::Connection(int sockNo){
conntime = Util::epoch(); conntime = Util::epoch();
Error = false; Error = false;
Blocking = false; Blocking = false;
skipCount = 0;
}// Socket::Connection basic constructor }// Socket::Connection basic constructor
/// Simulate a socket using two file descriptors. /// Simulate a socket using two file descriptors.
@ -355,6 +356,7 @@ Socket::Connection::Connection(int write, int read){
conntime = Util::epoch(); conntime = Util::epoch();
Error = false; Error = false;
Blocking = false; Blocking = false;
skipCount = 0;
}// Socket::Connection basic constructor }// Socket::Connection basic constructor
/// Create a new disconnected base socket. This is a basic constructor for placeholder purposes. /// Create a new disconnected base socket. This is a basic constructor for placeholder purposes.
@ -368,6 +370,7 @@ Socket::Connection::Connection(){
conntime = Util::epoch(); conntime = Util::epoch();
Error = false; Error = false;
Blocking = false; Blocking = false;
skipCount = 0;
}// Socket::Connection basic constructor }// Socket::Connection basic constructor
void Socket::Connection::resetCounter(){ void Socket::Connection::resetCounter(){
@ -629,6 +632,11 @@ void Socket::Connection::SendNow(const std::string &data){
SendNow(data.data(), data.size()); 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, /// Incremental write call. This function tries to write len bytes to the socket from the buffer,
/// returning the amount of bytes it actually wrote. /// returning the amount of bytes it actually wrote.
/// \param buffer Location of the buffer to write from. /// \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. /// \returns The amount of bytes actually written.
unsigned int Socket::Connection::iwrite(const void *buffer, int len){ unsigned int Socket::Connection::iwrite(const void *buffer, int len){
if (!connected() || len < 1){return 0;} 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; int r;
if (sock >= 0){ if (sock >= 0){
r = send(sock, buffer, len, 0); r = send(sock, buffer, len, 0);

View file

@ -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 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); ///< 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 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 // stats related methods
unsigned int connTime(); ///< Returns the time this socket has been connected. unsigned int connTime(); ///< Returns the time this socket has been connected.
uint64_t dataUp(); ///< Returns total amount of bytes sent. uint64_t dataUp(); ///< Returns total amount of bytes sent.