/* */ #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; } } 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(); // 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_