3 #include <boost/asio.hpp> 4 #include <boost/bind.hpp> 5 #include <boost/lexical_cast.hpp> 6 #include <boost/asio/deadline_timer.hpp> 7 #include <boost/enable_shared_from_this.hpp> 9 using boost::lexical_cast;
18 namespace ba = boost::asio;
19 namespace bs = boost::system;
20 namespace dummy = ba::placeholders;
22 using boost::lexical_cast;
30 class tcp_connection :
public ba::ip::tcp::socket,
public boost::enable_shared_from_this<tcp_connection>
44 ba::async_write(*
this, buffers,
46 dummy::error, dummy::bytes_transferred));
48 void AsyncWait(ba::deadline_timer &timer,
int seconds,
51 timer.expires_from_now(boost::posix_time::seconds(seconds));
52 timer.async_wait(boost::bind(handler, shared_from_this(), dummy::error));
55 ba::deadline_timer fTriggerSendData;
59 fTriggerSendData(ioservice)
64 void HandleSentData(
const boost::system::error_code& error,
size_t bytes_transferred)
66 cout <<
"Data sent: (transmitted=" << bytes_transferred <<
") rc=" << error.message() <<
" (" << error <<
")" << endl;
79 "enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00001111 \n" 80 " done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00001111 \n" 81 "values:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 \n" 84 "enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n" 85 " done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n" 87 "1000.16 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 88 "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 89 "1197.07 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 90 "558.59 677.92 817.26 989.39 1200.35 1503.06 1799.90 2204.18 \n" 91 "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 92 "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 93 "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 94 "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n" 97 AsyncWrite(
ba::buffer(ba::const_buffer(fBuffer.str().c_str(), fBuffer.str().length())));
109 if (ec==ba::error::basic_errors::operation_aborted)
116 if (fTriggerSendData.expires_at() > ba::deadline_timer::traits_type::now())
129 static shared_ptr
create(ba::io_service& io_service)
145 tcp::acceptor(ioservice, tcp::endpoint(tcp::v4(), port))
159 cout <<
"Start accept..." << flush;
163 async_accept(*new_connection,
167 ba::placeholders::error));
169 cout <<
"start-done." << endl;
177 cout <<
"Handle accept..." << flush;
180 new_connection->start();
187 cout <<
"handle-done." << endl;
191 int main(
int argc,
const char **argv)
195 ba::io_service io_service;
197 int Port = argc==2 ? lexical_cast<
int>(argv[1]) : 5000;
void TriggerSendData(const boost::system::error_code &ec)
void AsyncWait(ba::deadline_timer &timer, int seconds, void(tcp_connection::*handler)(const bs::error_code &))
tcp_connection(ba::io_service &ioservice)
void handle_accept(tcp_connection::shared_ptr new_connection, const boost::system::error_code &error)
void HandleSentData(const boost::system::error_code &error, size_t bytes_transferred)
boost::shared_ptr< tcp_connection > shared_ptr
void AsyncWrite(const ba::const_buffers_1 &buffers)
tcp_server(ba::io_service &ioservice, int port)
int main(int argc, const char **argv)
static shared_ptr create(ba::io_service &io_service)
static void handler(int conn_id, char *packet, int size, int status)
void HandleSentData(const boost::system::error_code &, size_t)
static shared_ptr create(ba::io_service &io_service, int boardid)