Skip to content

Commit

Permalink
Teach TCP sockets how to resolve DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellLVP committed Jun 7, 2020
1 parent 5dcab92 commit 63a3b63
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/playground/bindings/modules/socket/socket.cc
Expand Up @@ -54,6 +54,7 @@ void Socket::Open(SocketOpenOptions options, std::shared_ptr<Promise> promise) {
break;
}

state_ = State::kConnecting;
engine_->Open(std::move(options),
boost::bind(&Socket::OnConnect, this, boost::asio::placeholders::error, promise));
}
Expand Down
4 changes: 2 additions & 2 deletions src/playground/bindings/modules/socket/socket_open_options.h
Expand Up @@ -20,10 +20,10 @@ struct SocketOpenOptions {
: port(-1), timeout(30), ssl(SocketSSLMode::kNone) {}

SocketOpenOptions(SocketOpenOptions&&) = default;
SocketOpenOptions(const SocketOpenOptions&) = delete;
SocketOpenOptions(const SocketOpenOptions&) = default;

SocketOpenOptions& operator=(SocketOpenOptions&&) = default;
SocketOpenOptions& operator=(SocketOpenOptions const&) = delete;
SocketOpenOptions& operator=(SocketOpenOptions const&) = default;

std::string host;
int32_t port;
Expand Down
21 changes: 18 additions & 3 deletions src/playground/bindings/modules/socket/tcp_socket.cc
Expand Up @@ -24,6 +24,7 @@ std::shared_ptr<bindings::Runtime> Runtime() {
TcpSocket::TcpSocket()
: main_thread_io_context_(Runtime()->main_thread_io_context()),
background_io_context_(Runtime()->background_io_context()),
resolver_(background_io_context_),
boost_deadline_timer_(background_io_context_),
boost_tcp_socket_(background_io_context_) {}

Expand All @@ -34,9 +35,23 @@ void TcpSocket::CallOnMainThread(boost::function<void()> function) {
}

void TcpSocket::Open(SocketOpenOptions options, OpenCallback open_callback) {
boost::asio::ip::tcp::endpoint endpoint{
boost::asio::ip::address_v4::from_string(options.host),
static_cast<uint16_t>(options.port) };
boost::asio::ip::tcp::resolver::query query(options.host, std::to_string(options.port));

resolver_.async_resolve(
query, boost::bind(&TcpSocket::OnResolved, this, boost::asio::placeholders::error,
boost::asio::placeholders::iterator, options, open_callback));
}

void TcpSocket::OnResolved(const boost::system::error_code& ec,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
SocketOpenOptions options,
OpenCallback open_callback) {
if (ec) {
CallOnMainThread(boost::bind(open_callback, ec));
return;
}

boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;

ssl_mode_ = options.ssl;

Expand Down
10 changes: 10 additions & 0 deletions src/playground/bindings/modules/socket/tcp_socket.h
Expand Up @@ -39,6 +39,13 @@ class TcpSocket : public BaseSocket {
// server frames. This is necessary because Socket I/O is done on a background thread.
void CallOnMainThread(boost::function<void()> function);

// Called when DNS resolution has finished prior to opening the connection. We might now know
// where the connection has to be opened to.
void OnResolved(const boost::system::error_code& ec,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
SocketOpenOptions options,
OpenCallback open_callback);

// Called when the socket has connected. Sockets with a security context will start performing
// the handshake, whereas regular, non-secure sockets are hereafter considered active.
void OnConnected(const boost::system::error_code& ec,
Expand Down Expand Up @@ -75,6 +82,9 @@ class TcpSocket : public BaseSocket {
boost::asio::io_context& main_thread_io_context_;
boost::asio::io_context& background_io_context_;

// The resolver used for resolving DNS, when used rather than an IP address.
boost::asio::ip::tcp::resolver resolver_;

// The SSL context to use with this socket.
std::unique_ptr<boost::asio::ssl::context> boost_ssl_context_;
std::unique_ptr<SecureSocketType> boost_ssl_socket_;
Expand Down

0 comments on commit 63a3b63

Please sign in to comment.