#ifndef SendBuffer_hh #define SendBuffer_hh #include #if CHAR_BIT != 8 #error Only 8 bit char supported #endif #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) #define WIN32_LEAN_AND_MEAN #include #else #include #endif #include #include #include #include #include class SendBuffer { uint32_t bufsize_; uint32_t index_; unsigned char* buf_; public: #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) #ifdef EE_FILEIO HANDLE sock_; #else SOCKET sock_; #endif #else int sock_; #endif explicit SendBuffer(uint32_t bufsize) : bufsize_(bufsize), index_(0) { if (bufsize_ < 8) { throw failure("buffer size too small"); } buf_ = new unsigned char[bufsize_]; } ~SendBuffer() { delete [] buf_; } void Flush() { if (index_ > 0) { PersistentWrite(sock_, buf_, index_); index_ = 0; } } void Receive(void const* data, uint32_t dlen) { if (dlen > bufsize_ - index_) { #ifdef UDP memcpy(buf_ + index_, data, bufsize_ - index_); data += bufsize_ - index_; dlen -= bufsize_ - index_; index_ = bufsize_ ; #endif Flush(); if (dlen > bufsize_) { PersistentWrite(sock_, data, dlen); return; } } memcpy(buf_ + index_, data, dlen); index_ += dlen; } void Receive32(uint32_t val) { Receive(&val, sizeof(val)); } void Resize(uint32_t newsize) { if (newsize < index_) { Flush(); } unsigned char* tmp = new unsigned char[newsize]; memcpy(tmp, buf_, index_); delete [] buf_; buf_ = tmp; bufsize_ = newsize; } void Reset() { index_ = 0; } private: SendBuffer(SendBuffer const&); SendBuffer& operator=(SendBuffer const&); }; #endif