source: sample/AribaTunnel/Udp.cpp@ 12766

Last change on this file since 12766 was 12457, checked in by hock@…, 11 years ago

added AribaTunnel to the repo

File size: 2.3 KB
RevLine 
[12457]1#include "Udp.h"
2
3using namespace std;
4
5udp_server::~udp_server()
6{
7}
8
9void udp_server::listen_on(uint16_t port)
10{
11 std::cout << "[UDP] listening on port: " << port << std::endl;
12
13 socket_.open(udp::v4());
14 socket_.bind(udp::endpoint(udp::v4(), port));
15 start_receive();
16}
17
18void udp_server::connect_to(uint16_t port)
19{
20 std::cout << "[UDP] connecting to port: " << port << " (on localhost)" << std::endl;
21
22 remote_endpoint_ = udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port);
23
24 socket_.open(udp::v4());
25 socket_.connect(remote_endpoint_);
26 start_receive();
27}
28
29
30/* MessageSenderInterface */
31void udp_server::SendMessage(reboost::shared_buffer_t msg)
32{
33 cout << "[UDP] sending " << msg.size() << " bytes to " << remote_endpoint_ << endl;
34
35 // send over udp
36 socket_.async_send_to(
37 boost::asio::buffer(msg.data(), msg.size()),
38 remote_endpoint_,
39 boost::bind(&udp_server::handle_send, this, msg,
40 boost::asio::placeholders::error,
41 boost::asio::placeholders::bytes_transferred));
42}
43void udp_server::SendMessage(reboost::message_t msg)
44{
45 reboost::shared_buffer_t buf = msg.linearize();
46 this->SendMessage(buf);
47}
48
49
50
51/* asio udp */
52void udp_server::start_receive()
53{
54 recv_buffer_ = reboost::shared_buffer_t(3000);
55
56 socket_.async_receive_from(
57 boost::asio::buffer(recv_buffer_.mutable_data(), recv_buffer_.size()),
58 remote_endpoint_,
59 boost::bind(&udp_server::handle_receive, this,
60 boost::asio::placeholders::error,
61 boost::asio::placeholders::bytes_transferred));
62}
63
64void udp_server::handle_receive(const boost::system::error_code& error, std::size_t bytes_transferred)
65{
66 if (!error || error == boost::asio::error::message_size)
67 {
68 cout << "[UDP] received " << bytes_transferred << " bytes from " << remote_endpoint_ << endl;
69
70 // send sub-buffer trough tunnel
71 send_via_ariba( recv_buffer_(0, bytes_transferred) );
72
73 // wait for further messages...
74 start_receive();
75 }
76}
77
78void udp_server::handle_send(reboost::shared_buffer_t msg, const boost::system::error_code& , std::size_t )
79{
80 // empty
81}
82
83
84/// forward data over ariba
85void udp_server::send_via_ariba(reboost::shared_buffer_t buffer)
86{
87 tunnel.SendMessage(buffer);
88}
Note: See TracBrowser for help on using the repository browser.