/* */ #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: class BufEntry { public: virtual ~BufEntry() {} virtual ssize_t send (const SharedHandle& socket, size_t offset) = 0; virtual bool final(size_t offset) const = 0; }; class ByteArrayBufEntry:public BufEntry { public: ByteArrayBufEntry(unsigned char* bytes, size_t length); virtual ~ByteArrayBufEntry(); virtual ssize_t send (const SharedHandle& socket, size_t offset); virtual bool final(size_t offset) const; private: unsigned char* bytes_; size_t length_; }; class StringBufEntry:public BufEntry { public: StringBufEntry(const std::string& s); StringBufEntry(); virtual ssize_t send (const SharedHandle& socket, size_t offset); virtual bool final(size_t offset) const; void swap(std::string& s); private: 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