X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Finspsocket.h;h=53eca2e91d4c292c31a396161cae342ae2bc9401;hb=b4f82cff9b2efdc05e29bfe9db3f0bbb40d9ce2b;hp=221b92cc6feb94923f47423e46fd8f02a1637a38;hpb=8f5efbc7aa33b792e02d01e3288f553e6e98ccaa;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/inspsocket.h b/include/inspsocket.h index 221b92cc6..53eca2e91 100644 --- a/include/inspsocket.h +++ b/include/inspsocket.h @@ -103,14 +103,119 @@ class CoreExport SocketTimeout : public Timer */ class CoreExport StreamSocket : public EventHandler { + public: + /** Socket send queue + */ + class SendQueue + { + public: + /** One element of the queue, a continuous buffer + */ + typedef std::string Element; + + /** Sequence container of buffers in the queue + */ + typedef std::deque Container; + + /** Container iterator + */ + typedef Container::const_iterator const_iterator; + + SendQueue() : nbytes(0) { } + + /** Return whether the queue is empty + * @return True if the queue is empty, false otherwise + */ + bool empty() const { return (nbytes == 0); } + + /** Get the number of individual buffers in the queue + * @return Number of individual buffers in the queue + */ + Container::size_type size() const { return data.size(); } + + /** Get the number of queued bytes + * @return Size in bytes of the data in the queue + */ + size_t bytes() const { return nbytes; } + + /** Get the first buffer of the queue + * @return A reference to the first buffer in the queue + */ + const Element& front() const { return data.front(); } + + /** Get an iterator to the first buffer in the queue. + * The returned iterator cannot be used to make modifications to the queue, + * for that purpose the member functions push_*(), pop_front(), erase_front() and clear() can be used. + * @return Iterator referring to the first buffer in the queue, or end() if there are no elements. + */ + const_iterator begin() const { return data.begin(); } + + /** Get an iterator to the (theoretical) buffer one past the end of the queue. + * @return Iterator referring to one element past the end of the container + */ + const_iterator end() const { return data.end(); } + + /** Remove the first buffer in the queue + */ + void pop_front() + { + nbytes -= data.front().length(); + data.pop_front(); + } + + /** Remove bytes from the beginning of the first buffer + * @param n Number of bytes to remove + */ + void erase_front(Element::size_type n) + { + nbytes -= n; + data.front().erase(0, n); + } + + /** Insert a new buffer at the beginning of the queue + * @param newdata Data to add + */ + void push_front(const Element& newdata) + { + data.push_front(newdata); + nbytes += newdata.length(); + } + + /** Insert a new buffer at the end of the queue + * @param newdata Data to add + */ + void push_back(const Element& newdata) + { + data.push_back(newdata); + nbytes += newdata.length(); + } + + /** Clear the queue + */ + void clear() + { + data.clear(); + nbytes = 0; + } + + private: + /** Private send queue. Note that individual strings may be shared. + */ + Container data; + + /** Length, in bytes, of the sendq + */ + size_t nbytes; + }; + + private: /** The IOHook that handles raw I/O for this socket, or NULL */ IOHook* iohook; - /** Private send queue. Note that individual strings may be shared + /** Send queue of the socket */ - std::deque sendq; - /** Length, in bytes, of the sendq */ - size_t sendq_len; + SendQueue sendq; + /** Error - if nonempty, the socket is dead, and this is the reason. */ std::string error; @@ -126,7 +231,7 @@ class CoreExport StreamSocket : public EventHandler protected: std::string recvq; public: - StreamSocket() : iohook(NULL), sendq_len(0) {} + StreamSocket() : iohook(NULL) { } IOHook* GetIOHook() const; void AddIOHook(IOHook* hook); void DelIOHook(); @@ -169,7 +274,9 @@ class CoreExport StreamSocket : public EventHandler */ bool GetNextLine(std::string& line, char delim = '\n'); /** Useful for implementing sendq exceeded */ - inline size_t getSendQSize() const { return sendq_len; } + size_t getSendQSize() const { return sendq.size(); } + + SendQueue& GetSendQ() { return sendq; } /** * Close the socket, remove from socket engine, etc