1#ifndef _BOOST_ASYNCTCP_HPP_
2#define _BOOST_ASYNCTCP_HPP_
4#include <boost/array.hpp>
5#include <boost/asio.hpp>
12using boost::asio::ip::tcp;
14#define ASYNC_WRITE_FLAG_COPY 0x01
19typedef std::function<void(
void*,
AsyncClient*,
size_t len, uint32_t time)>
22typedef std::function<void(
void*,
AsyncClient*,
void* data,
size_t len)>
26typedef std::function<void(
void*,
AsyncClient*, uint32_t time)>
37 namespace ip = boost::asio::ip;
38 auto endpoint = ip::tcp::endpoint(ipaddress, port);
47 [&](
auto& ec,
auto len) { this->
handleData(ec, len); });
50 size_t write(
const void* data,
size_t len,
58 [&](
auto& ec,
auto len) { this->
handleWrite(ec, len); });
61 boost::asio::buffer(data, len),
62 [&](
auto& ec,
auto len) { this->
handleWrite(ec, len); });
182 void handleData(
const boost::system::error_code& ec,
size_t len) {
195 void handleWrite(
const boost::system::error_code& ec,
size_t len) {
211 if (ec != boost::asio::error::eof &&
_error_cb) {
237 setsockopt(
mAcceptor.native_handle(), SOL_SOCKET,
238 SO_REUSEADDR | SO_REUSEPORT,
reinterpret_cast<const char*
>(&one),
sizeof(one));
240 setsockopt(
mAcceptor.native_handle(), SOL_SOCKET,
241 SO_REUSEADDR | SO_REUSEPORT, &one,
sizeof(one));
243 boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),
_port);
244 mAcceptor.set_option(boost::asio::socket_base::reuse_address(
true));
245 mAcceptor.set_option(boost::asio::ip::tcp::no_delay(
true));
246 mAcceptor.set_option(boost::asio::socket_base::send_buffer_size(65536));
247 mAcceptor.set_option(boost::asio::socket_base::receive_buffer_size(65536));
267#if BOOST_VERSION >= 107000
273 client->
socket(), [
this, client](
const boost::system::error_code& e) {
274 if (!e && this->_connect_cb) {
275 this->_connect_cb(this->_connect_cb_arg, client);
279 std::cout <<
"Error: " << e.message() << std::endl;
std::function< void(void *, AsyncClient *)> AcConnectHandler
Definition asynctcp.hpp:18
boost::asio::ip::address IPAddress
Definition asynctcp.hpp:29
std::function< void(void *, AsyncClient *, size_t len, uint32_t time)> AcAckHandler
Definition asynctcp.hpp:20
std::function< void(void *, AsyncClient *, void *data, size_t len)> AcDataHandler
Definition asynctcp.hpp:23
std::function< void(void *, AsyncClient *, uint32_t time)> AcTimeoutHandler
Definition asynctcp.hpp:27
std::function< void(void *, AsyncClient *, int8_t error)> AcErrorHandler
Definition asynctcp.hpp:21
#define ASYNC_WRITE_FLAG_COPY
Definition asynctcp.hpp:14
#define TCP_MSS
Definition asynctcp.hpp:9
Definition asynctcp.hpp:31
bool freeable()
Definition asynctcp.hpp:113
size_t space()
Definition asynctcp.hpp:129
AcDataHandler _recv_cb
Definition asynctcp.hpp:164
bool connected()
Definition asynctcp.hpp:111
tcp::socket & socket()
Definition asynctcp.hpp:144
bool disconnectCalled
Definition asynctcp.hpp:154
AcConnectHandler _connect_cb
Definition asynctcp.hpp:156
char mWriteBuffer[TCP_MSS]
Definition asynctcp.hpp:151
void handleConnect(const boost::system::error_code &ec)
Definition asynctcp.hpp:171
void onAck(AcAckHandler cb, void *arg=0)
Definition asynctcp.hpp:86
AcErrorHandler _error_cb
Definition asynctcp.hpp:162
AcAckHandler _sent_cb
Definition asynctcp.hpp:160
void abort()
Definition asynctcp.hpp:74
~AsyncClient()
Definition asynctcp.hpp:127
boost::asio::io_context & _io_service
Definition asynctcp.hpp:147
void * _recv_cb_arg
Definition asynctcp.hpp:165
void onPoll(AcConnectHandler cb, void *arg=0)
Definition asynctcp.hpp:106
void initRead()
Definition asynctcp.hpp:44
void * _poll_cb_arg
Definition asynctcp.hpp:169
size_t write(const void *data, size_t len, size_t copy=ASYNC_WRITE_FLAG_COPY)
Definition asynctcp.hpp:50
void setNoDelay(bool value=true)
Definition asynctcp.hpp:69
void onConnect(AcConnectHandler cb, void *arg=0)
Definition asynctcp.hpp:76
void handleWrite(const boost::system::error_code &ec, size_t len)
Definition asynctcp.hpp:195
bool connect(IPAddress ipaddress, uint16_t port)
Definition asynctcp.hpp:36
void onData(AcDataHandler cb, void *arg=0)
Definition asynctcp.hpp:96
void * _error_cb_arg
Definition asynctcp.hpp:163
void * _discard_cb_arg
Definition asynctcp.hpp:159
AsyncClient(boost::asio::io_context &io_service)
Definition asynctcp.hpp:33
void onTimeout(AcTimeoutHandler cb, void *arg=0)
Definition asynctcp.hpp:101
AcConnectHandler _poll_cb
Definition asynctcp.hpp:168
void handleError(const boost::system::error_code &ec)
Definition asynctcp.hpp:210
void onDisconnect(AcConnectHandler cb, void *arg=0)
Definition asynctcp.hpp:81
void setRxTimeout(uint32_t timeout)
Definition asynctcp.hpp:70
tcp::socket mSocket
Definition asynctcp.hpp:148
void handleData(const boost::system::error_code &ec, size_t len)
Definition asynctcp.hpp:182
void send()
Definition asynctcp.hpp:68
AcConnectHandler _discard_cb
Definition asynctcp.hpp:158
void close(bool now=true)
Definition asynctcp.hpp:115
void onError(AcErrorHandler cb, void *arg=0)
Definition asynctcp.hpp:91
size_t ack(size_t len)
Definition asynctcp.hpp:137
char mInputBuffer[TCP_MSS]
Definition asynctcp.hpp:150
void * _timeout_cb_arg
Definition asynctcp.hpp:167
bool writing
Definition asynctcp.hpp:152
const char * errorToString(int8_t error)
Definition asynctcp.hpp:71
bool canSend()
Definition asynctcp.hpp:135
void * _connect_cb_arg
Definition asynctcp.hpp:157
void * _sent_cb_arg
Definition asynctcp.hpp:161
AcTimeoutHandler _timeout_cb
Definition asynctcp.hpp:166
Definition asynctcp.hpp:217
boost::asio::io_context & _io_service
Definition asynctcp.hpp:260
void * _connect_cb_arg
Definition asynctcp.hpp:264
~AsyncServer()
Definition asynctcp.hpp:225
void initAccept()
Definition asynctcp.hpp:266
void setNoDelay(bool value=true)
Definition asynctcp.hpp:257
void begin()
Definition asynctcp.hpp:233
uint16_t _port
Definition asynctcp.hpp:261
void onClient(AcConnectHandler cb, void *arg=0)
Definition asynctcp.hpp:227
void end()
Definition asynctcp.hpp:254
AcConnectHandler _connect_cb
Definition asynctcp.hpp:263
AsyncServer(boost::asio::io_context &io_service, uint16_t port)
Definition asynctcp.hpp:219
tcp::acceptor mAcceptor
Definition asynctcp.hpp:262