1#include <fc/network/tcp_socket.hpp>
3#include <fc/network/tcp_socket_io_hooks.hpp>
7#include <fc/io/stdio.hpp>
10#if defined _WIN32 || defined WIN32 || defined OS_WIN64 || defined _WIN64 || defined WIN64 || defined WINNT
15 using boost::fibers::future;
54 virtual size_t readsome(boost::asio::ip::tcp::socket& socket,
char* buffer,
size_t length)
override;
55 virtual size_t readsome(boost::asio::ip::tcp::socket& socket,
const std::shared_ptr<char>& buffer,
size_t length,
size_t offset)
override;
56 virtual size_t writesome(boost::asio::ip::tcp::socket& socket,
const char* buffer,
size_t length)
override;
57 virtual size_t writesome(boost::asio::ip::tcp::socket& socket,
const std::shared_ptr<const char>& buffer,
size_t length,
size_t offset)
override;
61 boost::asio::ip::tcp::socket
_sock;
69 size_t tcp_socket::impl::readsome(boost::asio::ip::tcp::socket& socket,
const std::shared_ptr<char>& buffer,
size_t length,
size_t offset)
71 return (_read_in_progress = fc::asio::read_some(socket, buffer, length, offset)).get();
75 return (_write_in_progress = fc::asio::write_some(socket, buffer, length)).get();
77 size_t tcp_socket::impl::writesome(boost::asio::ip::tcp::socket& socket,
const std::shared_ptr<const char>& buffer,
size_t length,
size_t offset)
79 return (_write_in_progress = fc::asio::write_some(socket, buffer, length, offset)).get();
83 void tcp_socket::open()
85 my->_sock.open(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0).protocol());
88 bool tcp_socket::is_open()
const {
89 return my->_sock.is_open();
92 tcp_socket::tcp_socket(){};
94 tcp_socket::~tcp_socket(){};
96 void tcp_socket::flush() {}
97 void tcp_socket::close() {
106 bool tcp_socket::eof()
const {
107 return !my->_sock.is_open();
110 size_t tcp_socket::writesome(
const char*
buf,
size_t len)
112 return my->_io_hooks->writesome(my->_sock,
buf,
len);
115 size_t tcp_socket::writesome(
const std::shared_ptr<const char>&
buf,
size_t len,
size_t offset)
117 return my->_io_hooks->writesome(my->_sock,
buf,
len, offset);
124 auto rep = my->_sock.remote_endpoint();
135 auto boost_local_endpoint = my->_sock.local_endpoint();
136 return fc::ip::endpoint(boost_local_endpoint.address().to_v4().to_ulong(), boost_local_endpoint.port() );
141 size_t tcp_socket::readsome(
char*
buf,
size_t len )
143 return my->_io_hooks->readsome(my->_sock,
buf,
len);
146 size_t tcp_socket::readsome(
const std::shared_ptr<char>&
buf,
size_t len,
size_t offset ) {
147 return my->_io_hooks->readsome(my->_sock,
buf,
len, offset);
151 fc::asio::tcp::connect(my->_sock, fc::asio::tcp::endpoint( boost::asio::ip::address_v4(remote_endpoint.
get_address()), remote_endpoint.
port() ) );
158 my->_sock.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(local_endpoint.
get_address()),
159 local_endpoint.
port()));
161 catch (
const std::exception& except)
163 elog(
"Exception binding outgoing connection to desired local endpoint ${endpoint}: ${what}", (
"endpoint", local_endpoint)(
"what", except.what()));
164 FC_THROW(
"error binding to ${endpoint}: ${what}", (
"endpoint", local_endpoint)(
"what", except.what()));
170 if (interval.
count())
172 boost::asio::socket_base::keep_alive
option(
true);
173 my->_sock.set_option(
option);
174#if defined _WIN32 || defined WIN32 || defined OS_WIN64 || defined _WIN64 || defined WIN64 || defined WINNT
175 struct tcp_keepalive keepalive_settings;
176 keepalive_settings.onoff = 1;
180 DWORD dwBytesRet = 0;
181 if (WSAIoctl(my->_sock.native(), SIO_KEEPALIVE_VALS, &keepalive_settings,
sizeof(keepalive_settings),
182 NULL, 0, &dwBytesRet, NULL, NULL) == SOCKET_ERROR)
183 wlog(
"Error setting TCP keepalive values");
184#elif !defined(__clang__) || (__clang_major__ >= 6)
187 if (setsockopt(my->_sock.native(), IPPROTO_TCP,
188 #
if defined( __APPLE__ )
193 (
char*)&timeout_sec,
sizeof(timeout_sec)) < 0)
194 wlog(
"Error setting TCP keepalive idle time");
195# if !defined(__APPLE__) || defined(TCP_KEEPINTVL)
196 if (setsockopt(my->_sock.native(), IPPROTO_TCP, TCP_KEEPINTVL,
197 (
char*)&timeout_sec,
sizeof(timeout_sec)) < 0)
198 wlog(
"Error setting TCP keepalive interval");
204 boost::asio::socket_base::keep_alive
option(
false);
205 my->_sock.set_option(
option);
209 void tcp_socket::set_io_hooks(tcp_socket_io_hooks* new_hooks)
211 my->_io_hooks = new_hooks ? new_hooks : &*my;
214 void tcp_socket::set_reuse_address(
bool enable )
217 boost::asio::socket_base::reuse_address
option(enable);
218 my->_sock.set_option(
option);
219#if defined(__APPLE__) || defined(__linux__)
221# define SO_REUSEPORT 15
227 int reuseport_value = 1;
228 if (setsockopt(my->_sock.native(), SOL_SOCKET, SO_REUSEPORT,
229 (
char*)&reuseport_value,
sizeof(reuseport_value)) < 0)
231 if (errno == ENOPROTOOPT)
234 wlog(
"Error setting SO_REUSEPORT");
244 :_accept(
fc::
asio::default_io_service() )
246 _accept.open(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0).protocol());
253 catch ( boost::system::system_error& )
261 void tcp_server::close() {
262 if( my && my->_accept.is_open() )
267 tcp_server::tcp_server()
270 tcp_server::~tcp_server() {
275 void tcp_server::accept( tcp_socket&
s )
280 fc::asio::tcp::accept( my->_accept,
s.my->_sock );
283 void tcp_server::set_reuse_address(
bool enable )
287 boost::asio::ip::tcp::acceptor::reuse_address
option(enable);
288 my->_accept.set_option(
option);
289#if defined(__APPLE__) || (defined(__linux__) && defined(SO_REUSEPORT))
294 int reuseport_value = 1;
295 if (setsockopt(my->_accept.native(), SOL_SOCKET, SO_REUSEPORT,
296 (
char*)&reuseport_value,
sizeof(reuseport_value)) < 0)
298 if (errno == ENOPROTOOPT)
301 wlog(
"Error setting SO_REUSEPORT");
306 void tcp_server::listen(
uint16_t port )
312 my->_accept.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(), port));
313 my->_accept.listen(256);
323 my->_accept.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string((
string)ep.
get_address()), ep.
port()));
324 my->_accept.listen();
332 return fc::ip::endpoint(my->_accept.local_endpoint().address().to_v4().to_ulong(),
333 my->_accept.local_endpoint().port() );
336 uint16_t tcp_server::get_port()
const
339 return my->_accept.local_endpoint().port();
const address & get_address() const
constexpr int64_t count() const
boost::asio::ip::tcp::acceptor _accept
virtual size_t readsome(boost::asio::ip::tcp::socket &socket, char *buffer, size_t length) override
future< size_t > _write_in_progress
future< size_t > _read_in_progress
boost::asio::ip::tcp::socket _sock
virtual size_t writesome(boost::asio::ip::tcp::socket &socket, const char *buffer, size_t length) override
tcp_socket_io_hooks * _io_hooks
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
constexpr microseconds milliseconds(int64_t s)
constexpr microseconds seconds(int64_t s)