-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-organized connection_pair into separate rtp_service and connection…
… classes. Moved thread managment to source class.
- Loading branch information
Showing
13 changed files
with
344 additions
and
303 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
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(); | ||
} | ||
} | ||
} |
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,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(); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.