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