Skip to content

Commit

Permalink
Re-organized connection_pair into separate rtp_service and connection…
Browse files Browse the repository at this point in the history
… classes. Moved thread managment to source class.
  • Loading branch information
maaron committed Feb 20, 2015
1 parent c04b9f1 commit ac55375
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 303 deletions.
71 changes: 71 additions & 0 deletions connection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

#include "connection.h"
#include "log.h"

namespace media
{
using namespace boost;
using namespace boost::asio;

connection::connection(io_service::strand& strand)
: local(strand.get_io_service()), io_strand(strand)
{
}

void connection::open(const boost::asio::ip::address& iface, int& port)
{
ip::udp::endpoint ep(iface, port);
local.open(ep.protocol());
local.bind(ep);

LOG("Opened socket bound to [%s]:%d\n", iface.to_string().c_str(), port);
}

bool connection::try_open(const boost::asio::ip::address& iface, int& port)
{
system::error_code ec;
ip::udp::endpoint ep(iface, port);
local.open(ep.protocol());
local.bind(ep, ec);

if (!ec) LOG("Opened socket bound to [%s]:%d\n", iface.to_string().c_str(), port);

return !ec;
}

void connection::set_peer(const boost::asio::ip::udp::endpoint& ep) { remote = ep; }

void connection::send(void* data, size_t size)
{
local.send_to(buffer(data, size), remote);
}

void connection::async_receive(std::function<void(void*, size_t)> cb)
{
void* data = buf;
local.async_receive_from(buffer(buf), remote, io_strand.wrap(
[cb, data](const boost::system::error_code& ec, size_t bytes_transferred)
{
if (!ec)
{
cb(data, bytes_transferred);
}
else if (ec != boost::asio::error::operation_aborted)
{
LOG("Socket receive error: %d, %s\n", ec.value(), ec.message().c_str());
}
}));
}

void connection::close()
{
if (local.is_open())
{
LOG("Closing socket bound to [%s]:%d\n",
local.local_endpoint().address().to_string().c_str(),
local.local_endpoint().port());

local.close();
}
}
}
27 changes: 27 additions & 0 deletions connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <boost\asio.hpp>

namespace media
{
// This class encapsulates the details associated with a local/remote UDP pair.
class connection
{
public:
boost::asio::io_service::strand& io_strand;
boost::asio::ip::udp::socket local;
boost::asio::ip::udp::endpoint remote;
char buf[2048];

connection(boost::asio::io_service::strand&);

void open(const boost::asio::ip::address&, int& port);
bool try_open(const boost::asio::ip::address&, int& port);
void set_peer(const boost::asio::ip::udp::endpoint& ep);
void send(void* data, size_t size);

void async_receive(std::function<void(void*, size_t)> cb);

void close();
};
}
156 changes: 0 additions & 156 deletions connection_pair.cpp

This file was deleted.

122 changes: 0 additions & 122 deletions connection_pair.h

This file was deleted.

Loading

0 comments on commit ac55375

Please sign in to comment.