OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tcp_message_write.h
Go to the documentation of this file.
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 <tbb/mutex.h>
9 
10 #include <list>
11 #include <boost/asio.hpp>
12 #include <boost/bind.hpp>
13 #include <boost/asio/buffer.hpp>
14 #include <boost/intrusive_ptr.hpp>
15 #include <boost/system/error_code.hpp>
16 #include "base/util.h"
17 
18 class TcpSession;
19 
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);
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  bool IsWritePending() const {
39  return (buffer_queue_.size() != 0);
40  }
41 
42  size_t GetBufferQueueSize() const {
43  size_t total = 0;
44  BufferQueue::const_iterator it = buffer_queue_.begin();
45  for (; it != buffer_queue_.end(); ++it) {
46  boost::asio::mutable_buffer buffer = *it;
47  total += buffer_size(buffer);
48  }
49  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 
63  size_t offset_;
64  size_t last_write_;
67 };
68 
69 #endif // SRC_IO_TCP_MESSAGE_WRITE_H_
bool UpdateBufferQueue(size_t wrote, bool *send_ready)
TcpSession * session_
static const int kDefaultBufferSize
int Send(const uint8_t *msg, size_t len, boost::system::error_code *ec)
void BufferAppend(const uint8_t *data, int len)
boost::intrusive_ptr< TcpSession > TcpSessionPtr
std::list< boost::asio::mutable_buffer > BufferQueue
void DeleteBuffer(boost::asio::mutable_buffer buffer)
BufferQueue buffer_queue_
bool IsWritePending() const
static const int kMaxPendingBufferSize
TcpMessageWriter(TcpSession *session, size_t buffer_send_size)
size_t GetBufferQueueSize() const
int AsyncSend(const uint8_t *msg, size_t len, boost::system::error_code *ec)
static const int kDefaultWriteBufferSize
static const int kMinPendingBufferSize