/* */ #ifndef _D_SOCKET_BUFFER_H_ #define _D_SOCKET_BUFFER_H_ #include "common.h" #include #include #include "SharedHandle.h" namespace aria2 { class SocketCore; class SocketBuffer { private: enum BUF_TYPE { TYPE_BYTES, TYPE_STR }; struct BufEntry { BUF_TYPE type; unsigned char* bytes; size_t bytesLen; std::string* str; void deleteBuf() { if(type == TYPE_BYTES) { delete [] bytes; } else if(type == TYPE_STR) { delete str; } } static BufEntry createBytes(unsigned char* bytes, size_t len) { BufEntry b; b.type = TYPE_BYTES; b.bytes = bytes; b.bytesLen = len; return b; } static BufEntry createStr(const std::string& str) { BufEntry b; b.type = TYPE_STR; b.str = new std::string(str); return b; } }; SharedHandle _socket; std::deque _bufq; // Offset of data in _bufq[0]. SocketBuffer tries to send _bufq[0], // but it cannot always send whole data. In this case, offset points // to the data to be sent in the next send() call. size_t _offset; public: SocketBuffer(const SharedHandle& socket); ~SocketBuffer(); // Feeds data pointered by bytes with length len. into queue. This // object gets ownership of bytes, so caller must not delete or // later bytes after this call. void pushBytes(unsigned char* bytes, size_t len); // Feeds data into queue. This function doesn't send data. void feedSendBuffer(const std::string& data); // Feeds data into queue and sends data in queue. This function is // equivalent to call feedSendBuffer() and send() sequentially. // Returns the number of bytes sent. ssize_t feedAndSend(const std::string& data); // Sends data in queue. Returns the number of bytes sent. ssize_t send(); // Returns true if queue is empty. bool sendBufferIsEmpty() const; }; } // namespace aria2 #endif // _D_SOCKET_BUFFER_H_