Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
udp_socket.cpp
Go to the documentation of this file.
2#include <fc/network/ip.hpp>
3
4
5namespace fc
6{
8 {
9 public:
10 impl(){}
12
13 std::shared_ptr<boost::asio::ip::udp::socket> _sock;
14 };
15
17 : my(new impl())
18 {
19 }
20
21 void udp_socket::initialize(boost::asio::io_service& service)
22 {
23 my->_sock.reset(new boost::asio::ip::udp::socket(service));
24 }
25
27 {
28 try
29 {
30 if(my->_sock)
31 my->_sock->close(); //close boost socket to make any pending reads run their completion handler
32 }
33 catch (...) //avoid destructor throw and likely this is just happening because socket wasn't open.
34 {
35 }
36 }
37
38 void udp_socket::send_to(const char* buffer, size_t length, boost::asio::ip::udp::endpoint& to)
39 {
40 try
41 {
42 my->_sock->send_to(boost::asio::buffer(buffer, length), to);
43 return;
44 }
45 catch(const boost::system::system_error& e)
46 {
47 if(e.code() == boost::asio::error::would_block)
48 {
49 auto send_buffer_ptr = std::make_shared<std::vector<char>>(buffer, buffer+length);
50 my->_sock->async_send_to(boost::asio::buffer(send_buffer_ptr.get(), length), to,
51 [send_buffer_ptr](const boost::system::error_code& /*ec*/, std::size_t /*bytes_transferred*/)
52 {
53 // Swallow errors. Currently only used for GELF logging, so depend on local
54 // log to catch anything that doesn't make it across the network.
55 });
56 }
57 // All other exceptions ignored.
58 }
59 }
60
61 void udp_socket::send_to(const std::shared_ptr<const char>& buffer, size_t length,
62 boost::asio::ip::udp::endpoint& to)
63 {
64 try
65 {
66 my->_sock->send_to(boost::asio::buffer(buffer.get(), length), to);
67 return;
68 }
69 catch(const boost::system::system_error& e)
70 {
71 if(e.code() == boost::asio::error::would_block)
72 {
73 auto preserved_buffer_ptr = buffer;
74 my->_sock->async_send_to(boost::asio::buffer(preserved_buffer_ptr.get(), length), to,
75 [preserved_buffer_ptr](const boost::system::error_code& /*ec*/, std::size_t /*bytes_transferred*/)
76 {
77 // Swallow errors. Currently only used for GELF logging, so depend on local
78 // log to catch anything that doesn't make it across the network.
79 });
80 }
81 // All other exceptions ignored.
82 }
83 }
84
86 {
87 my->_sock->open(boost::asio::ip::udp::v4());
88 my->_sock->non_blocking(true);
89 }
90
92 {
93 my->_sock->close();
94 }
95
96 const boost::asio::ip::udp::endpoint udp_socket::local_endpoint() const
97 {
98 return my->_sock->local_endpoint();
99 }
100
101 void udp_socket::connect(const boost::asio::ip::udp::endpoint& e)
102 {
103 my->_sock->connect(e);
104 }
105
107 {
108 my->_sock->set_option( boost::asio::ip::udp::socket::reuse_address(s) );
109 }
110
111}
std::shared_ptr< boost::asio::ip::udp::socket > _sock
const boost::asio::ip::udp::endpoint local_endpoint() const
void set_reuse_address(bool)
void send_to(const char *b, size_t l, boost::asio::ip::udp::endpoint &to)
void connect(const boost::asio::ip::udp::endpoint &e)
void initialize(boost::asio::io_service &)
namespace sysio::chain
Definition authority.cpp:3
char * s