smrtlink/src/transfer/Socket.cpp

90 lines
2.2 KiB
C++
Raw Normal View History

2015-09-23 20:29:29 +00:00
/*
* Socket.cpp
*
* Created on: 02.09.2015
* Author: jdi
*/
#include <sys/types.h>
#include <sys/socket.h>
2015-09-24 18:01:26 +00:00
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <cstdlib>
2015-09-23 20:29:29 +00:00
#include <iostream>
2015-09-24 18:01:26 +00:00
#include <unistd.h>
2015-09-23 20:29:29 +00:00
#include <asio.hpp>
#include "Socket.h"
#include "../Types.h"
2015-09-25 14:40:16 +00:00
#include "../Utils.h"
2015-09-23 21:28:44 +00:00
#include "../device/Host.h"
#include "Packet.h"
2015-09-23 20:29:29 +00:00
2015-09-24 18:01:26 +00:00
Socket::Socket(asio::io_service& io_service) :
send_socket_(io_service), receive_socket_(io_service) {
}
void Socket::init(short dst_port, short src_port) {
2015-09-23 20:29:29 +00:00
2015-09-24 18:01:26 +00:00
wildcard_endpoint_ = asio::ip::udp::endpoint(
asio::ip::address::from_string("0.0.0.0"), src_port);
2015-09-23 20:29:29 +00:00
local_endpoint_ = asio::ip::udp::endpoint(
2015-09-24 18:01:26 +00:00
asio::ip::address::from_string("192.168.0.3"), src_port);
2015-09-23 20:29:29 +00:00
broadcast_endpoint_ = asio::ip::udp::endpoint(
asio::ip::address_v4::from_string("255.255.255.255"), dst_port);
2015-09-24 18:01:26 +00:00
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_.bind(local_endpoint_); //TODO reuse Address
2015-09-23 20:29:29 +00:00
2015-09-24 18:01:26 +00:00
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_.bind(wildcard_endpoint_); //TODO reuse Address
2015-09-23 20:29:29 +00:00
}
void Socket::send(bytes data) {
unsigned char * a = &data[0];
2015-09-24 18:01:26 +00:00
send_socket_.async_send_to(asio::buffer(a, data.size()),
broadcast_endpoint_,
2015-09-23 20:52:32 +00:00
[this](asio::error_code ec, std::size_t bytes_sent)
{
listen();
});
2015-09-23 20:29:29 +00:00
}
void Socket::listen() {
2015-09-24 18:01:26 +00:00
receive_socket_.async_receive_from(asio::buffer(data_, MAX_LENGTH),
2015-09-23 20:29:29 +00:00
remote_endpoint_,
[this](asio::error_code ec, std::size_t bytes_recvd)
{
2015-09-23 21:28:44 +00:00
if (ec || bytes_recvd == 0) {
listen();
} else {
2015-09-24 18:01:26 +00:00
data_.resize(bytes_recvd);
2015-09-23 21:28:44 +00:00
Packet p = Packet(Packet::DISCOVERY);
2015-09-24 18:01:26 +00:00
p.encode(data_);
p.parse(data_);
2015-09-25 14:40:16 +00:00
datasets l = p.getPayload();
utils::printSets(l);
2015-09-24 18:01:26 +00:00
/*
2015-09-25 14:40:16 +00:00
sleep(1);
bytes b = {255, 255, 0, 0};
Host h = Host();
p = Packet(Packet::DISCOVERY);
p.setBody(b);
p.setHostMac(h.getMac());
bytes a = p.getBytes();
p.encode(a);
send(a);
*/
listen();
2015-09-23 20:29:29 +00:00
}
});
2015-09-25 14:40:16 +00:00
//printf("Listen\n");
2015-09-23 20:29:29 +00:00
}