/* */ #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; } } size_t size() const { if(type == TYPE_BYTES) { return bytesLen; } else { return str->size(); } } BufEntry(unsigned char* bytes, size_t len): type(TYPE_BYTES), bytes(bytes), bytesLen(len) {} BufEntry(const std::string& str): type(TYPE_STR), str(new std::string(str)) {} }; 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(); // Don't allow copying SocketBuffer(const SocketBuffer&); SocketBuffer& operator=(const 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. This function doesn't send data. void pushBytes(unsigned char* bytes, size_t len); // Feeds data into queue. This function doesn't send data. void pushStr(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