-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
279 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#include "tcp_link.hpp" | ||
#include <vanetza/access/data_request.hpp> | ||
#include <vanetza/net/ethernet_header.hpp> | ||
#include <boost/asio/ip/multicast.hpp> | ||
#include <boost/bind.hpp> | ||
#include <iostream> | ||
|
||
namespace ip = boost::asio::ip; | ||
using namespace vanetza; | ||
|
||
TcpLink::TcpLink(boost::asio::io_service& io_service) : | ||
io_service_(&io_service) | ||
{ | ||
|
||
} | ||
|
||
TcpLink::~TcpLink() | ||
{ | ||
for (auto &ts : sockets_) { | ||
delete ts; | ||
} | ||
for (auto &acceptor : acceptors_) { | ||
delete acceptor.second; | ||
} | ||
} | ||
|
||
void TcpLink::indicate(IndicationCallback cb) | ||
{ | ||
callback_ = cb; | ||
} | ||
|
||
void TcpLink::request(const access::DataRequest& request, std::unique_ptr<ChunkPacket> packet) | ||
{ | ||
packet->layer(OsiLayer::Link) = create_ethernet_header(request.destination_addr, request.source_addr, request.ether_type); | ||
|
||
std::array<boost::asio::const_buffer, layers_> const_buffers; | ||
for (auto& layer : osi_layer_range<OsiLayer::Link, OsiLayer::Application>()) { | ||
const auto index = distance(OsiLayer::Link, layer); | ||
packet->layer(layer).convert(tx_buffers_[index]); | ||
const_buffers[index] = boost::asio::buffer(tx_buffers_[index]); | ||
} | ||
|
||
std::list<TcpSocket*>::iterator i = sockets_.begin(); | ||
|
||
while (i != sockets_.end()) { | ||
if ((*i)->connected()) { | ||
(*i)->request(const_buffers); | ||
i++; | ||
} else { | ||
sockets_.erase(i++); | ||
std::cerr << "Socket removed" << std::endl; | ||
} | ||
} | ||
} | ||
|
||
void TcpLink::connect(ip::tcp::endpoint ep) | ||
{ | ||
TcpSocket* ts = new TcpSocket(*io_service_, callback_); | ||
ts->connect(ep); | ||
sockets_.push_back(ts); | ||
} | ||
|
||
void TcpLink::accept(ip::tcp::endpoint ep) | ||
{ | ||
if (!acceptors_[ep]) { | ||
acceptors_[ep] = new ip::tcp::acceptor(*io_service_, ep); | ||
} | ||
|
||
TcpSocket* ts = new TcpSocket(*io_service_, callback_); | ||
boost::system::error_code ec; | ||
|
||
acceptors_[ep]->async_accept( | ||
ts->socket(), | ||
boost::bind( | ||
&TcpLink::accept_handler, | ||
this, | ||
ec, | ||
ts, | ||
ep | ||
) | ||
); | ||
|
||
} | ||
|
||
void TcpLink::accept_handler(boost::system::error_code& ec, TcpSocket* ts, ip::tcp::endpoint ep) | ||
{ | ||
sockets_.push_back(ts); | ||
ts->do_receive(); | ||
ts->connected(true); | ||
accept(ep); | ||
} | ||
|
||
|
||
/** | ||
* TcpSocket | ||
*/ | ||
|
||
TcpSocket::TcpSocket(boost::asio::io_service& io_service, IndicationCallback& cb) : | ||
socket_(io_service), | ||
callback_(&cb), | ||
rx_buffer_(2560, 0x00) | ||
{ | ||
|
||
} | ||
|
||
void TcpSocket::connect(ip::tcp::endpoint ep) | ||
{ | ||
boost::system::error_code ec; | ||
socket_.connect(ep, ec); | ||
if (!ec) { | ||
is_connected_ = true; | ||
do_receive(); | ||
} | ||
|
||
} | ||
|
||
void TcpSocket::request(std::array<boost::asio::const_buffer, layers_> const_buffers) | ||
{ | ||
boost::system::error_code ec; | ||
socket_.write_some(const_buffers, ec); | ||
|
||
if (ec) { | ||
is_connected_ = false; | ||
} | ||
} | ||
|
||
void TcpSocket::do_receive() | ||
{ | ||
socket_.async_read_some(boost::asio::buffer(rx_buffer_), | ||
[this](boost::system::error_code ec, std::size_t length) { | ||
if (!ec) { | ||
is_connected_ = true; | ||
ByteBuffer buffer(rx_buffer_.begin(), rx_buffer_.begin() + length); | ||
CohesivePacket packet(std::move(buffer), OsiLayer::Link); | ||
if (packet.size(OsiLayer::Link) < EthernetHeader::length_bytes) { | ||
std::cerr << "Dropped TCP packet too short to contain Ethernet header\n"; | ||
} else { | ||
packet.set_boundary(OsiLayer::Link, EthernetHeader::length_bytes); | ||
auto link_range = packet[OsiLayer::Link]; | ||
EthernetHeader eth = decode_ethernet_header(link_range.begin(), link_range.end()); | ||
if (callback_) { | ||
(*callback_)(std::move(packet), eth); | ||
} | ||
} | ||
do_receive(); | ||
} else { | ||
is_connected_ = false; | ||
} | ||
}); | ||
|
||
} | ||
|
||
ip::tcp::socket& TcpSocket::socket() | ||
{ | ||
return socket_; | ||
} | ||
|
||
bool TcpSocket::connected() | ||
{ | ||
return is_connected_; | ||
} | ||
|
||
void TcpSocket::connected(bool b) | ||
{ | ||
is_connected_ = b; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef TCP_LINK_HPP_A16QFBX3 | ||
#define TCP_LINK_HPP_A16QFBX3 | ||
|
||
#include "link_layer.hpp" | ||
#include <vanetza/common/byte_buffer.hpp> | ||
#include <boost/asio/ip/tcp.hpp> | ||
#include <boost/asio/io_service.hpp> | ||
#include <array> | ||
#include <list> | ||
|
||
static constexpr std::size_t layers_ = num_osi_layers(vanetza::OsiLayer::Link, vanetza::OsiLayer::Application); | ||
|
||
|
||
class TcpSocket | ||
{ | ||
public: | ||
using IndicationCallback = std::function<void(vanetza::CohesivePacket&&, const vanetza::EthernetHeader&)>; | ||
|
||
TcpSocket(boost::asio::io_service&, IndicationCallback&); | ||
|
||
void connect(boost::asio::ip::tcp::endpoint); | ||
void request(std::array<boost::asio::const_buffer, layers_>); | ||
void do_receive(); | ||
|
||
boost::asio::ip::tcp::socket& socket(); | ||
bool connected(); | ||
void connected(bool); | ||
|
||
private: | ||
|
||
bool is_connected_; | ||
boost::asio::io_service* io_service_; | ||
boost::asio::ip::tcp::endpoint endpoint_; | ||
boost::asio::ip::tcp::socket socket_; | ||
vanetza::ByteBuffer rx_buffer_; | ||
IndicationCallback* callback_; | ||
|
||
}; | ||
|
||
|
||
class TcpLink : public LinkLayer | ||
{ | ||
public: | ||
TcpLink(boost::asio::io_service&); | ||
~TcpLink(); | ||
|
||
void indicate(IndicationCallback) override; | ||
void request(const vanetza::access::DataRequest&, std::unique_ptr<vanetza::ChunkPacket>) override; | ||
void connect(boost::asio::ip::tcp::endpoint); | ||
void accept(boost::asio::ip::tcp::endpoint); | ||
void accept_handler(boost::system::error_code& ec, TcpSocket* ts, boost::asio::ip::tcp::endpoint ep); | ||
|
||
private: | ||
std::list<TcpSocket*> sockets_; | ||
std::map<boost::asio::ip::tcp::endpoint, boost::asio::ip::tcp::acceptor*> acceptors_; | ||
IndicationCallback callback_; | ||
boost::asio::io_service* io_service_; | ||
std::array<vanetza::ByteBuffer, layers_> tx_buffers_; | ||
}; | ||
|
||
|
||
|
||
#endif /* TCP_LINK_HPP_A16QFBX3 */ | ||
|