OpenSDN source code
usock_server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 /*
7  * The primary method implemented here is Send(), to transmit a
8  * message over the Unix socket. It uses boost::asio::async_write to
9  * send one message at a time over the socket, that is transmitted
10  * asynchronously. The user can repeatedly call Send(). All those
11  * buffers are tail-queued. Upon write_complete callback, the next
12  * message from the front of the queue is sent.
13  */
14 #include "io/usock_server.h"
15 
16 using boost::asio::buffer_cast;
17 using boost::asio::buffer;
18 using boost::asio::mutable_buffer;
19 
20 UnixDomainSocketSession::~UnixDomainSocketSession() {
21  if (observer_) {
22  observer_(this, CLOSE);
23  }
24 
25  /* Free up any remaining buffers in the queue. */
26  for (BufferQueue::iterator iter = buffer_queue_.begin();
27  iter != buffer_queue_.end(); ++iter) {
28  DeleteBuffer(*iter);
29  }
30  buffer_queue_.clear();
31 }
32 
33 void UnixDomainSocketSession::Start() {
34  if (observer_) {
35  observer_(this, READY);
36  }
37 
38  socket_.async_read_some(boost::asio::buffer(data_),
39  boost::bind(&UnixDomainSocketSession::
40  HandleRead, shared_from_this(),
41  boost::asio::placeholders::error,
42  boost::asio::placeholders::
43  bytes_transferred));
44 }
45 
46 void UnixDomainSocketSession::Send(const uint8_t * data, int data_len) {
47  if (!data || !data_len) {
48  return;
49  }
50  bool write_now = buffer_queue_.empty();
51  AppendBuffer(data, data_len);
52  if (write_now) {
53  WriteToSocket();
54  }
55 }
56 
57 void UnixDomainSocketSession::WriteToSocket() {
58  if (buffer_queue_.empty()) {
59  return;
60  }
61 
62  boost::asio::mutable_buffer head = buffer_queue_.front();
63  boost::asio::async_write(socket_,
64  buffer(buffer_cast <const uint8_t *>(head),
65  boost::asio::buffer_size(head)),
66  boost::bind(&UnixDomainSocketSession::
67  HandleWrite, shared_from_this(),
68  boost::asio::placeholders::error));
69 }
70 
71 void UnixDomainSocketSession::AppendBuffer(const uint8_t *src, int bytes) {
72  u_int8_t *data = new u_int8_t[bytes];
73  memcpy(data, src, bytes);
74  boost::asio::mutable_buffer buffer =
75  boost::asio::mutable_buffer(data, bytes);
76  buffer_queue_.push_back(buffer);
77 }
78 
79 void UnixDomainSocketSession::DeleteBuffer(boost::asio::mutable_buffer buffer) {
80  const uint8_t *data = buffer_cast <const uint8_t *>(buffer);
81  delete []data;
82  return;
83 }
84 
85 void UnixDomainSocketSession::HandleRead(const boost::system::error_code &error,
86  size_t bytes_transferred) {
87  if (error) {
88  return;
89  }
90  if (observer_) {
91  observer_(this, READY);
92  }
93 }
94 
95 void UnixDomainSocketSession::HandleWrite(
96  const boost::system::error_code &error) {
97  /*
98  * async_write() is atomic in that it returns success once the entire message
99  * is sent. If there is an error, it's okay to return from here so that the
100  * session gets closed.
101  */
102  if (error) {
103  return;
104  }
105 
106  /*
107  * We are done with the buffer at the head of the queue. Delete it.
108  */
109  DeleteBuffer(buffer_queue_.front());
110  buffer_queue_.pop_front();
111 
112  /*
113  * Write the next message, if there.
114  */
115  WriteToSocket();
116 
117  /*
118  * Engage on the socket to keep it alive.
119  */
120  socket_.async_read_some(boost::asio::buffer(data_),
121  boost::bind(&UnixDomainSocketSession::
122  HandleRead, shared_from_this(),
123  boost::asio::placeholders::error,
124  boost::asio::placeholders::
125  bytes_transferred));
126 }
127 
128 UnixDomainSocketServer::UnixDomainSocketServer(
129  boost::asio::io_context *io, const std::string &file)
130  : io_service_(io),
131  acceptor_(*io, boost::asio::local::stream_protocol::endpoint(file)),
132  session_idspace_(0) {
133  SessionPtr new_session(new UnixDomainSocketSession(io_service_));
134  acceptor_.async_accept(new_session->socket(),
135  boost::bind(&UnixDomainSocketServer::
136  HandleAccept, this, new_session,
137  boost::asio::placeholders::error));
138 }
139 
140 void
141 UnixDomainSocketServer::HandleAccept(SessionPtr session,
142  const boost::system::error_code &error) {
143  UnixDomainSocketSession *socket_session = session.get();
144 
145  if (error) {
146  if (observer_) {
147  observer_(this, socket_session, DELETE_SESSION);
148  }
149  return;
150  }
151 
152  socket_session->set_session_id(++session_idspace_);
153  if (observer_) {
154  observer_(this, socket_session, NEW_SESSION);
155  session->Start();
156  }
157 
158  SessionPtr new_session(new UnixDomainSocketSession(io_service_));
159  acceptor_.async_accept(new_session->socket(),
160  boost::bind(&UnixDomainSocketServer::
161  HandleAccept, this, new_session,
162  boost::asio::placeholders::error));
163 }
@ CLOSE
Definition: http_client.h:35
Definition: io_utils.cc:12
@ READY
Definition: xmpp_channel.h:18