OpenSDN source code
tcp_message_write.h
Go to the documentation of this file.
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 
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);
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  bool IsWritePending() const {
40  return (buffer_queue_.size() != 0);
41  }
42 
43  size_t GetBufferQueueSize() const {
44  size_t total = 0;
45  BufferQueue::const_iterator it = buffer_queue_.begin();
46  for (; it != buffer_queue_.end(); ++it) {
47  boost::asio::mutable_buffer buffer = *it;
48  total += buffer_size(buffer);
49  }
50  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 
64  size_t offset_;
65  size_t last_write_;
68 };
69 
70 #endif // SRC_IO_TCP_MESSAGE_WRITE_H_
TcpSession * session_
boost::intrusive_ptr< TcpSession > TcpSessionPtr
int Send(const uint8_t *msg, size_t len, boost::system::error_code *ec)
std::list< boost::asio::mutable_buffer > BufferQueue
bool IsWritePending() const
size_t GetBufferQueueSize() const
BufferQueue buffer_queue_
void DeleteBuffer(boost::asio::mutable_buffer buffer)
Definition: tcp_session.cc:128