7 #if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER >= 1800
9 #define ASIO_HAS_BOOST_DATE_TIME
10 #define LRDB_USE_BOOST_ASIO
13 #ifdef LRDB_USE_BOOST_ASIO
14 #include <boost/asio.hpp>
16 #define ASIO_STANDALONE
21 #ifdef LRDB_USE_BOOST_ASIO
23 using boost::system::error_code;
24 using namespace boost::asio;
33 : endpoint_(asio::ip::tcp::v4(), port),
34 acceptor_(io_service_, endpoint_),
35 socket_(io_service_) {
55 std::function<void(
const std::string& data)> on_data;
56 std::function<void()> on_connection;
57 std::function<void()> on_close;
58 std::function<void(
const std::string&)> on_error;
60 bool is_open()
const {
return socket_.is_open(); }
61 void poll() { io_service_.poll(); }
62 void run_one() { io_service_.run_one(); }
63 void wait_for_connection() {
65 io_service_.run_one();
70 bool send_message(
const std::string& message) {
72 std::string data = message +
"\r\n";
73 asio::write(socket_, asio::buffer(data), ec);
76 on_error(ec.message());
86 acceptor_.async_accept(socket_, [&](
const asio::error_code& ec) {
91 on_error(ec.message());
97 void connected_done() {
101 start_receive_commands();
103 void start_receive_commands() {
104 asio::async_read_until(socket_, read_buffer_,
"\n",
105 [&](
const asio::error_code& ec, std::size_t) {
107 std::istream is(&read_buffer_);
109 std::getline(is, command);
113 start_receive_commands();
116 on_error(ec.message());
123 asio::io_service io_service_;
124 asio::ip::tcp::endpoint endpoint_;
125 asio::ip::tcp::acceptor acceptor_;
126 asio::ip::tcp::socket socket_;
127 asio::streambuf read_buffer_;
Definition: socket.hpp:30