23 #ifndef INCLUDED_GR_SOCKET_PDU_H
24 #define INCLUDED_GR_SOCKET_PDU_H
31 #include <boost/array.hpp>
32 #include <boost/asio.hpp>
40 :
public boost::enable_shared_from_this<tcp_connection>
45 boost::array<char, 10000>
buf;
52 boost::asio::ip::tcp::socket&
socket()
66 socket_.async_read_some(
67 boost::asio::buffer(
buf),
68 boost::bind(&tcp_connection::handle_read,
this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
73 boost::array<char, 10000> txbuf;
75 boost::asio::async_write(socket_, boost::asio::buffer(txbuf, len),
76 boost::bind(&tcp_connection::handle_write, shared_from_this(),
77 boost::asio::placeholders::error,
78 boost::asio::placeholders::bytes_transferred));
91 void handle_read(
const boost::system::error_code& error,
size_t bytes_transferred);
93 void handle_write(
const boost::system::error_code& ,
98 boost::asio::ip::tcp::socket socket_;
113 boost::asio::io_service _io_service;
115 boost::array<char, 10000> rxbuf;
118 boost::asio::ip::tcp::endpoint _tcp_endpoint;
122 std::vector<tcp_connection::pointer> d_tcp_connections;
131 boost::asio::ip::udp::endpoint _udp_endpoint;
132 boost::asio::ip::udp::endpoint _udp_endpoint_other;
135 void handle_receive(
const boost::system::error_code& error, std::size_t ){
138 void start_tcp_accept(){
142 _acceptor_tcp->async_accept(new_connection->socket(),
143 boost::bind(&gr_socket_pdu::handle_tcp_accept,
this, new_connection,
144 boost::asio::placeholders::error));
150 new_connection->start(
this);
151 d_tcp_connections.push_back(new_connection);
154 std::cout << error << std::endl;
158 void run_io_service(){
162 void handle_udp_read(
const boost::system::error_code& error,
size_t bytes_transferred){
169 _udp_socket->async_receive_from( boost::asio::buffer(rxbuf), _udp_endpoint_other,
170 boost::bind(&gr_socket_pdu::handle_udp_read,
this,
171 boost::asio::placeholders::error,
172 boost::asio::placeholders::bytes_transferred));
174 throw boost::system::system_error(error);
178 void handle_tcp_read(
const boost::system::error_code& error,
size_t bytes_transferred){
186 _tcp_socket->async_read_some(
187 boost::asio::buffer(rxbuf),
188 boost::bind(&gr_socket_pdu::handle_tcp_read,
this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
192 throw boost::system::system_error(error);
197 gr_socket_pdu (std::string type, std::string addr, std::string port,
int MTU=10000);