1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| void Session::Start() { memset(_data, 0, max_length); _socket.async_read_some( boost::asio::buffer(_data, max_length), std::bind(&Session::handle_read, this, placeholders::_1, placeholders::_2) );
}
void Session::handle_read(const boost::system::error_code& error, size_t bytes_transfered) { if (!error) { cout << "server receive data is " << _data << endl; boost::asio::async_write(_socket, boost::asio::buffer(_data, bytes_transfered), std::bind(&Session::handle_write, this, placeholders::_1)); } else { cout << "read error" << endl; delete this; } }
void Session::handle_write(const boost::system::error_code& error) { if (!error) { memset(_data, 0, max_length); _socket.async_read_some(boost::asio::buffer(_data, max_length), std::bind(&Session::handle_read, this, placeholders::_1, placeholders::_2)); } else { cout << "write error"<<error.value() << endl; delete this; } }
Server::Server(boost::asio::io_context& ioc, short port):_ioc(ioc),_acceptor(ioc,tcp::endpoint(tcp::v4(),port)) { cout << "Server start success, on port: " << port << endl; start_accept(); }
void Server::start_accept() { Session* new_session = new Session(_ioc); _acceptor.async_accept(new_session->Socket(),std::bind(&Server::handle_accept, this, new_session, placeholders::_1)); }
void Server::handle_accept(Session* new_session, const boost::system::error_code& error) { if (!error) { new_session->Start(); } else { delete new_session; } start_accept(); }
|