Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception on invalid URI query #27

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xtransmit/generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void xtransmit::generate::run(const string& dst_url, const config& cfg, const at
}
catch (const socket::exception& e)
{
spdlog::warn(LOG_SC_GENERATE "{}", e.what());
spdlog::error(LOG_SC_GENERATE "{}", e.what());
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion xtransmit/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void xtransmit::receive::run(const string &src_url, const config &cfg, const ato
}
catch (const socket::exception & e)
{
cerr << e.what() << endl;
spdlog::error(LOG_SC_RECEIVE "{}", e.what());
}
}

Expand Down
2 changes: 1 addition & 1 deletion xtransmit/route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void xtransmit::route::run(const string& src_url, const string& dst_url,
}
catch (const socket::exception & e)
{
cerr << e.what() << endl;
spdlog::error(LOG_SC_ROUTE "{}", e.what());
}
}

Expand Down
19 changes: 13 additions & 6 deletions xtransmit/srt_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ socket::srt::srt(const UriParser &src_uri)
throw socket::exception(srt_getlasterror_str());
}

check_options_exist();
assert_options_valid();

if (SRT_SUCCESS != configure_pre(m_bind_socket))
throw socket::exception(srt_getlasterror_str());
Expand Down Expand Up @@ -242,13 +242,13 @@ std::future<shared_srt> socket::srt::async_read(std::vector<char> &buffer)
return std::future<shared_srt>();
}

void socket::srt::check_options_exist() const
void socket::srt::assert_options_valid(const std::map<string, string>& options)
{
#ifdef ENABLE_CXX17
for (const auto& [key, val] : myMap)
for (const auto& [key, val] : options)
{
#else
for (const auto el : m_options)
for (const auto el : options)
{
const string& key = el.first;
const string& val = el.second;
Expand All @@ -266,11 +266,18 @@ void socket::srt::check_options_exist() const
if (opt_found || key == "bind" || key == "mode")
continue;

spdlog::warn(LOG_SOCK_SRT "srt://{}:{:d}: Ignoring socket option '{}={}' (not recognized)!",
m_host, m_port, key, val);
stringstream ss;
ss << "Invalid URI query option '";
ss << key << "=" << val << " (not recognized)!";
throw socket::exception(ss.str());
}
}

void socket::srt::assert_options_valid() const
{
assert_options_valid(m_options);
}


int socket::srt::configure_pre(SRTSOCKET sock)
{
Expand Down
70 changes: 37 additions & 33 deletions xtransmit/srt_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
#include "udt.h"
#include "uriparser.hpp"


namespace xtransmit {
namespace socket {

namespace xtransmit
{
namespace socket
{

class srt
: public std::enable_shared_from_this<srt>
, public isocket
{
using string = std::string;
using shared_srt = std::shared_ptr<srt>;
using string = std::string;
using shared_srt = std::shared_ptr<srt>;

public:
explicit srt(const UriParser& src_uri);
Expand All @@ -34,45 +34,50 @@ class srt

virtual ~srt();


public:
std::future<shared_srt> async_connect() noexcept(false);
std::future<shared_srt> async_accept() noexcept(false);

std::future<shared_srt> async_accept() noexcept(false);

shared_srt connect();
shared_srt accept();
shared_srt connect();
shared_srt accept();

/**
* Start listening on the incomming connection requests.
*
* May throw a socket_exception.
* May throw a socket::exception.
*/
void listen() noexcept(false);
void listen() noexcept(false);

/// Verifies URI options provided are valid.
///
/// @param [in] options a map of options key-value pairs to validate
/// @throw socket::exception on failure
///
static void assert_options_valid(const std::map<string, string>& options);

private:
void configure(const std::map<string, string> &options);
void configure(const std::map<string, string>& options);

void check_options_exist() const;
void assert_options_valid() const;
int configure_pre(SRTSOCKET sock);
int configure_post(SRTSOCKET sock);
void handle_hosts();

public:
std::future<shared_srt> async_read(std::vector<char> &buffer);
void async_write();
std::future<shared_srt> async_read(std::vector<char>& buffer);
void async_write();

/**
* @returns The number of bytes received.
*
* @throws socket_exception Thrown on failure.
*/
size_t read(const mutable_buffer& buffer, int timeout_ms = -1) final;
int write(const const_buffer& buffer, int timeout_ms = -1) final;
int write(const const_buffer& buffer, int timeout_ms = -1) final;

enum connection_mode
{
FAILURE =-1,
FAILURE = -1,
LISTENER = 0,
CALLER = 1,
RENDEZVOUS = 2
Expand All @@ -83,28 +88,27 @@ class srt
bool is_caller() const final { return m_mode == CALLER; }

public:
int id() const final { return m_bind_socket; }
int statistics(SRT_TRACEBSTATS &stats, bool instant = true);
bool supports_statistics() const final { return true; }
const std::string statistics_csv(bool print_header) final;
int id() const final { return m_bind_socket; }
int statistics(SRT_TRACEBSTATS& stats, bool instant = true);
bool supports_statistics() const final { return true; }
const std::string statistics_csv(bool print_header) final;
static const std::string stats_to_csv(int socketid, const SRT_TRACEBSTATS& stats, bool print_header);

private:
void raise_exception(const string&& place) const;
void raise_exception(const string &&place, const string&& reason) const;
void raise_exception(const string&& place, const string&& reason) const;

private:
int m_bind_socket = SRT_INVALID_SOCK;
int m_epoll_connect = -1;
int m_epoll_io = -1;

connection_mode m_mode = FAILURE;
bool m_blocking_mode = false;
string m_host;
int m_port = -1;
int m_bind_socket = SRT_INVALID_SOCK;
int m_epoll_connect = -1;
int m_epoll_io = -1;

connection_mode m_mode = FAILURE;
bool m_blocking_mode = false;
string m_host;
int m_port = -1;
std::map<string, string> m_options; // All other options, as provided in the URI
};

} // namespace socket
} // namespace xtransmit