[12457] | 1 | /*
|
---|
| 2 | * The MIT License (MIT)
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2013 Mario Hock mails2013@omnifile.org
|
---|
| 5 | *
|
---|
| 6 | * Permission is hereby granted, free of charge, to any person
|
---|
| 7 | * obtaining a copy of this software and associated documentation
|
---|
| 8 | * files (the "Software"), to deal in the Software without
|
---|
| 9 | * restriction, including without limitation the rights to use,
|
---|
| 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 11 | * copies of the Software, and to permit persons to whom the
|
---|
| 12 | * Software is furnished to do so, subject to the following
|
---|
| 13 | * conditions:
|
---|
| 14 | *
|
---|
| 15 | * The above copyright notice and this permission notice shall be
|
---|
| 16 | * included in all copies or substantial portions of the Software.
|
---|
| 17 | *
|
---|
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 25 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 26 | *
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef UDP_H
|
---|
| 30 | #define UDP_H
|
---|
| 31 |
|
---|
| 32 | #include "MessageSenderInterface.h"
|
---|
| 33 |
|
---|
| 34 | #include <ctime>
|
---|
| 35 | #include <iostream>
|
---|
| 36 | #include <string>
|
---|
| 37 | #include <boost/array.hpp>
|
---|
| 38 | #include <boost/bind.hpp>
|
---|
| 39 | #include <boost/shared_ptr.hpp>
|
---|
| 40 | #include <boost/asio.hpp>
|
---|
| 41 |
|
---|
| 42 | using boost::asio::ip::udp;
|
---|
| 43 |
|
---|
| 44 | class udp_server :
|
---|
| 45 | public MessageSenderInterface
|
---|
| 46 | {
|
---|
| 47 | public:
|
---|
| 48 | /// constructor
|
---|
| 49 | udp_server(boost::asio::io_service& io_service,
|
---|
| 50 | MessageSenderInterface& tunnel) :
|
---|
| 51 | socket_(io_service),
|
---|
| 52 | tunnel(tunnel)
|
---|
| 53 | {
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | virtual ~udp_server();
|
---|
| 57 |
|
---|
| 58 | /// opens an udp "server-socket" on the given port
|
---|
| 59 | void listen_on(uint16_t port);
|
---|
| 60 |
|
---|
| 61 | /// connects an udp "client-socket" to the given port (on localhost)
|
---|
| 62 | void connect_to(uint16_t port);
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | /// MessageSenderInterface
|
---|
| 66 | virtual void SendMessage(reboost::message_t msg);
|
---|
| 67 | virtual void SendMessage(reboost::shared_buffer_t msg);
|
---|
| 68 |
|
---|
| 69 | private:
|
---|
| 70 | void start_receive();
|
---|
| 71 |
|
---|
| 72 | void handle_receive(const boost::system::error_code& error,
|
---|
| 73 | std::size_t bytes_transferred);
|
---|
| 74 |
|
---|
| 75 | void handle_send(reboost::shared_buffer_t msg,
|
---|
| 76 | const boost::system::error_code& /*error*/,
|
---|
| 77 | std::size_t /*bytes_transferred*/);
|
---|
| 78 |
|
---|
| 79 | /// forward data over ariba
|
---|
| 80 | void send_via_ariba(reboost::shared_buffer_t buffer);
|
---|
| 81 |
|
---|
| 82 | private:
|
---|
| 83 | udp::socket socket_;
|
---|
| 84 | MessageSenderInterface& tunnel;
|
---|
| 85 | udp::endpoint remote_endpoint_;
|
---|
| 86 | reboost::shared_buffer_t recv_buffer_;
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | #endif // UDP_H
|
---|