Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef SRC_IO_TCP_MESSAGE_WRITE_H_ 7 : #define SRC_IO_TCP_MESSAGE_WRITE_H_ 8 : 9 : #include <list> 10 : #include <boost/asio.hpp> 11 : #include <boost/bind/bind.hpp> 12 : #include <boost/asio/buffer.hpp> 13 : #include <boost/intrusive_ptr.hpp> 14 : #include <boost/system/error_code.hpp> 15 : #include "base/util.h" 16 : 17 : using namespace boost::placeholders; 18 : 19 : class TcpSession; 20 : 21 : class TcpMessageWriter { 22 : public: 23 : static const int kDefaultBufferSize = 4 * 1024; 24 : static const int kDefaultWriteBufferSize = 32 * 1024; 25 : static const int kMaxPendingBufferSize = 256 * 1024; 26 : static const int kMinPendingBufferSize = 64 * 1024; 27 : 28 : TcpMessageWriter(TcpSession *session, size_t buffer_send_size); 29 : ~TcpMessageWriter(); 30 : 31 : // return false for send 32 : int Send(const uint8_t *msg, size_t len, 33 : boost::system::error_code *ec); 34 : 35 : int AsyncSend(const uint8_t *msg, size_t len, 36 : boost::system::error_code *ec); 37 : 38 : // caller needs to take a lock. 39 21804 : bool IsWritePending() const { 40 21804 : return (buffer_queue_.size() != 0); 41 : } 42 : 43 1827667 : size_t GetBufferQueueSize() const { 44 1827667 : size_t total = 0; 45 1827667 : BufferQueue::const_iterator it = buffer_queue_.begin(); 46 4702918 : for (; it != buffer_queue_.end(); ++it) { 47 2875622 : boost::asio::mutable_buffer buffer = *it; 48 2875538 : total += buffer_size(buffer); 49 : } 50 1827199 : return total; 51 : } 52 : 53 : private: 54 : friend class TcpSession; 55 : typedef boost::intrusive_ptr<TcpSession> TcpSessionPtr; 56 : typedef std::list<boost::asio::mutable_buffer> BufferQueue; 57 : void BufferAppend(const uint8_t *data, int len); 58 : void DeleteBuffer(boost::asio::mutable_buffer buffer); 59 : /* DeleteBuffer and Update Buffer Queue */ 60 : bool UpdateBufferQueue(size_t wrote, bool *send_ready); 61 : void TriggerAsyncWrite(); 62 : 63 : BufferQueue buffer_queue_; 64 : size_t offset_; 65 : size_t last_write_; 66 : size_t buffer_send_size_; 67 : TcpSession *session_; 68 : }; 69 : 70 : #endif // SRC_IO_TCP_MESSAGE_WRITE_H_