Timeout option added
This commit is contained in:
parent
de9a3f94f7
commit
aa94eeda65
10 changed files with 79 additions and 54 deletions
|
|
@ -7,15 +7,14 @@
|
|||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <unistd.h>
|
||||
#include <asio.hpp>
|
||||
#include "Socket.h"
|
||||
#include "Packet.h"
|
||||
#include "Options.h"
|
||||
#include "Host.h"
|
||||
#include "Types.h"
|
||||
|
||||
Socket::Socket(asio::io_service& io_service) :
|
||||
send_socket_(io_service), receive_socket_(io_service) {
|
||||
Socket::Socket(boost::asio::io_service& io_service) :
|
||||
send_socket_(io_service), receive_socket_(io_service), timer(io_service) {
|
||||
}
|
||||
//, resolver( io_service)
|
||||
void Socket::init(short dst_port, short src_port) {
|
||||
|
|
@ -29,23 +28,35 @@ void Socket::init(short dst_port, short src_port) {
|
|||
if (options.flags & FLAG_DEBUG)
|
||||
std::cout << "Local IP:\t" << local_ip << "\n";
|
||||
|
||||
wildcard_endpoint_ = asio::ip::udp::endpoint(
|
||||
asio::ip::address_v4::from_string("0.0.0.0"), src_port);
|
||||
local_endpoint_ = asio::ip::udp::endpoint(asio::ip::address_v4(local_ip),
|
||||
src_port);
|
||||
broadcast_endpoint_ = asio::ip::udp::endpoint(
|
||||
asio::ip::address_v4::from_string("255.255.255.255"), dst_port);
|
||||
wildcard_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||
boost::asio::ip::address_v4::from_string("0.0.0.0"), src_port);
|
||||
local_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||
boost::asio::ip::address_v4(local_ip), src_port);
|
||||
broadcast_endpoint_ = boost::asio::ip::udp::endpoint(
|
||||
boost::asio::ip::address_v4::from_string("255.255.255.255"),
|
||||
dst_port);
|
||||
|
||||
send_socket_.open(asio::ip::udp::v4());
|
||||
send_socket_.set_option(asio::socket_base::broadcast(true));
|
||||
send_socket_.set_option(asio::socket_base::reuse_address(true));
|
||||
send_socket_.open(boost::asio::ip::udp::v4());
|
||||
send_socket_.set_option(boost::asio::socket_base::broadcast(true));
|
||||
send_socket_.set_option(boost::asio::socket_base::reuse_address(true));
|
||||
send_socket_.bind(local_endpoint_);
|
||||
|
||||
receive_socket_.open(asio::ip::udp::v4());
|
||||
receive_socket_.set_option(asio::socket_base::broadcast(true));
|
||||
receive_socket_.set_option(asio::socket_base::reuse_address(true));
|
||||
receive_socket_.open(boost::asio::ip::udp::v4());
|
||||
receive_socket_.set_option(boost::asio::socket_base::broadcast(true));
|
||||
receive_socket_.set_option(boost::asio::socket_base::reuse_address(true));
|
||||
receive_socket_.bind(wildcard_endpoint_);
|
||||
|
||||
if (options.timeout != 0) {
|
||||
timer.expires_from_now(
|
||||
boost::posix_time::milliseconds(options.timeout));
|
||||
timer.async_wait([this](const boost::system::error_code& error)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
receive_socket_.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::setHostIp(ipAddr ip) {
|
||||
|
|
@ -54,9 +65,9 @@ void Socket::setHostIp(ipAddr ip) {
|
|||
|
||||
void Socket::send(bytes data) {
|
||||
unsigned char * a = &data[0];
|
||||
send_socket_.async_send_to(asio::buffer(a, data.size()),
|
||||
send_socket_.async_send_to(boost::asio::buffer(a, data.size()),
|
||||
broadcast_endpoint_,
|
||||
[this](asio::error_code ec, std::size_t bytes_sent)
|
||||
[this](boost::system::error_code ec, std::size_t bytes_sent)
|
||||
{
|
||||
listen();
|
||||
});
|
||||
|
|
@ -64,12 +75,13 @@ void Socket::send(bytes data) {
|
|||
|
||||
void Socket::listen() {
|
||||
data.resize(MAX_LENGTH);
|
||||
receive_socket_.async_receive_from(asio::buffer(data, MAX_LENGTH),
|
||||
receive_socket_.async_receive_from(boost::asio::buffer(data, MAX_LENGTH),
|
||||
remote_endpoint_,
|
||||
[this](asio::error_code ec, std::size_t bytes_recvd)
|
||||
[this](boost::system::error_code ec, std::size_t bytes_recvd)
|
||||
{
|
||||
if (ec || bytes_recvd == 0) {
|
||||
listen();
|
||||
//listen();
|
||||
// TODO distinguish error codes
|
||||
} else {
|
||||
data.resize(bytes_recvd);
|
||||
Packet p = Packet(Packet::RETURN);
|
||||
|
|
@ -77,8 +89,9 @@ void Socket::listen() {
|
|||
p.parse(data);
|
||||
datasets l = p.getPayload();
|
||||
if(!callback(p)) {
|
||||
listen();
|
||||
//TODO do something
|
||||
}
|
||||
listen();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue