8 #include <boost/bind/bind.hpp>
15 using boost::asio::buffer_cast;
16 using boost::asio::mutable_buffer;
17 using boost::asio::mutable_buffers_1;
18 using boost::asio::const_buffer;
19 using boost::asio::ip::udp;
20 using namespace boost::placeholders;
27 const const_buffer &buffer)
28 :
Task(server->reader_task_id(),
29 server->reader_task_instance(remote_endpoint)),
31 remote_endpoint_(remote_endpoint),
36 std::scoped_lock lock(server_->state_guard_);
37 if (server_->state_ == OK) {
38 server_->OnRead(buffer_, remote_endpoint_);
39 server_->DeallocateBuffer(buffer_);
43 std::string
Description()
const {
return "UdpServer::Reader"; }
53 buffer_size_(buffer_size),
54 state_(Uninitialized),
65 socket_(*(
evm->io_service())),
66 buffer_size_(buffer_size),
67 state_(Uninitialized),
83 boost::system::error_code ec;
84 s <<
"Udpsocket@" << ep;
96 assert(
pbuf_.empty());
104 while (!
pbuf_.empty()) {
105 delete[]
pbuf_.back();
110 boost::system::error_code ec;
111 socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
114 "ERROR shutdown UDP socket: " << ec);
119 "ERROR closing UDP socket: " << ec);
126 boost::system::error_code error;
129 udp::endpoint local_endpoint = udp::endpoint(ip, port);
133 << ipaddress <<
": " << error);
139 udp::endpoint local_endpoint = udp::endpoint(udp::v4(), port);
146 "Initialize UDP server in WRONG state: " <<
state_);
149 boost::system::error_code error;
150 socket_.open(udp::v4(), error);
157 socket_.bind(local_endpoint, error);
159 boost::system::error_code ec;
161 << error.message() <<
":" <<
socket_.local_endpoint(ec));
172 uint8_t *p =
new uint8_t[s];
177 return mutable_buffer(p, s);
185 const uint8_t *p = buffer_cast<const uint8_t *>(buffer);
188 std::vector<uint8_t *>::iterator f = std::find(
pbuf_.begin(),
190 if (f !=
pbuf_.end())
197 const_buffer buffer) {
199 socket_.async_send_to(boost::asio::buffer(buffer), ep,
202 boost::asio::placeholders::bytes_transferred,
203 boost::asio::placeholders::error));
207 "StartSend UDP server in WRONG state: " <<
state_);
213 udp::endpoint remote_endpoint, std::size_t bytes_transferred,
214 const boost::system::error_code& error) {
219 "Send UDP server in WRONG state: " <<
state_);
225 "Send to " << remote_endpoint <<
" FAILED due to error: " <<
226 error.value() <<
" : " << error.message());
234 HandleSend(send_buffer, remote_endpoint, bytes_transferred, error);
240 const_buffer buffer(buffer_cast<const uint8_t*>(b), buffer_size(b));
241 socket_.async_receive_from(mutable_buffers_1(b),
244 boost::asio::placeholders::bytes_transferred,
245 boost::asio::placeholders::error));
249 "StartReceive UDP server in WRONG state: " <<
state_);
254 std::size_t bytes_transferred,
const boost::system::error_code& error) {
259 "Receive UDP server in WRONG state: " <<
state_);
265 "Read FAILED due to error: " << error.value() <<
" : " <<
279 udp::endpoint remote_endpoint, std::size_t bytes_transferred,
280 const boost::system::error_code& error) {
281 const_buffer rdbuf(buffer_cast<const uint8_t *>(recv_buffer),
291 const udp::endpoint &remote_endpoint) {
293 "Default implementation of OnRead does NOT process received message");
297 udp::endpoint remote_endpoint, std::size_t bytes_transferred,
298 const boost::system::error_code& error) {
304 return socket_.local_endpoint(*error);
308 boost::system::error_code error;
312 return ep.address().to_string();
316 boost::system::error_code error;
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
static void AddServer(ServerType *server)
static void DeleteServer(ServerType *server)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
int GetTaskId(const std::string &name)
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
static TaskScheduler * GetInstance()
Task is a class to describe a computational task within OpenSDN control plane applications....
static const int kTaskInstanceAny
Specifies value for wildcard (any or *) task data ID.
static ServerManager< UdpServer, UdpServerPtr > impl_
static void DeleteServer(UdpServer *server)
static void AddServer(UdpServer *server)
Reader(UdpServerPtr server, const udp::endpoint &remote_endpoint, const const_buffer &buffer)
udp::endpoint remote_endpoint_
std::string Description() const
Gives a description of the task.
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
void HandleReceiveInternal(boost::asio::const_buffer recv_buffer, std::size_t bytes_transferred, const boost::system::error_code &error)
std::vector< uint8_t * > pbuf_
UdpServer(EventManager *evm, int buffer_size=kDefaultBufferSize)
virtual bool Initialize(unsigned short port)
virtual int reader_task_instance(const boost::asio::ip::udp::endpoint &remote_endpoint) const
boost::asio::ip::udp::endpoint remote_endpoint_
boost::asio::ip::udp::socket socket_
boost::asio::ip::udp::endpoint GetLocalEndpoint(boost::system::error_code *error) const
virtual void OnRead(const boost::asio::const_buffer &recv_buffer, const boost::asio::ip::udp::endpoint &remote_endpoint)
int GetLocalEndpointPort() const
boost::asio::mutable_buffer AllocateBuffer()
void GetTxSocketStats(SocketIOStats *socket_stats) const
virtual void HandleReceive(const boost::asio::const_buffer &recv_buffer, boost::asio::ip::udp::endpoint remote_endpoint, std::size_t bytes_transferred, const boost::system::error_code &error)
static int reader_task_id_
void StartSend(boost::asio::ip::udp::endpoint ep, std::size_t bytes_to_send, boost::asio::const_buffer buffer)
std::atomic< int > refcount_
ServerState GetServerState() const
void HandleSendInternal(boost::asio::const_buffer send_buffer, boost::asio::ip::udp::endpoint remote_endpoint, std::size_t bytes_transferred, const boost::system::error_code &error)
void GetRxSocketStats(SocketIOStats *socket_stats) const
void SetName(boost::asio::ip::udp::endpoint ep)
std::string GetLocalEndpointAddress() const
virtual void HandleSend(boost::asio::const_buffer send_buffer, boost::asio::ip::udp::endpoint remote_endpoint, std::size_t bytes_transferred, const boost::system::error_code &error)
void DeallocateBuffer(const boost::asio::const_buffer &buffer)
#define UDP_SERVER_LOG_ERROR(server, dir, arg)
std::atomic< uint64_t > write_errors
std::atomic< uint64_t > write_calls
std::atomic< uint64_t > read_bytes
void GetRxStats(SocketIOStats *socket_stats) const
std::atomic< uint64_t > read_calls
std::atomic< uint64_t > write_bytes
void GetTxStats(SocketIOStats *socket_stats) const
std::atomic< uint64_t > read_errors
boost::intrusive_ptr< UdpServer > UdpServerPtr