10#include "absl/memory/memory.h"
11#include "absl/strings/str_cat.h"
12#include "asio/buffer.hpp"
13#include "asio/connect.hpp"
14#include "asio/ssl/stream_base.hpp"
15#include "asio/write.hpp"
20 const std::string& host, uint16_t port, asio::ssl::context& tls,
21 const std::string& token,
bool udp) {
22 std::unique_ptr<Connection> connection =
24 const absl::Status started = connection->Start(host, port, token, udp);
32 : stream_(io_, tls), udp_socket_(io_), hello_timer_(io_), token_(token) {}
34absl::Status Connection::Start(
const std::string& host, uint16_t port,
35 const std::string& token,
bool udp) {
36 asio::ip::tcp::resolver resolver(io_);
37 std::error_code error;
39 const asio::ip::tcp::resolver::results_type endpoints =
40 resolver.resolve(asio::ip::tcp::v4(), host, std::to_string(port), error);
42 asio::connect(stream_.lowest_layer(), endpoints, error);
45 return absl::UnavailableError(absl::StrCat(
46 "couldn't connect to ", host,
":", port,
": ", error.message()));
48 std::error_code ignored;
49 stream_.lowest_layer().set_option(asio::ip::tcp::no_delay(
true), ignored);
51 stream_.handshake(asio::ssl::stream_base::client, error);
53 return absl::UnavailableError(
54 absl::StrCat(
"couldn't shake hands with the world: ",
57 protocol::Hello hello;
58 hello.set_token(token);
59 asio::write(stream_, asio::buffer(
Frame(hello)), error);
61 return absl::UnavailableError(
62 absl::StrCat(
"couldn't say hello to the server: ", error.message()));
66 const asio::ip::tcp::endpoint server =
67 stream_.lowest_layer().remote_endpoint(error);
69 udp_socket_.connect({server.address(), server.port()}, error);
72 return absl::UnavailableError(absl::StrCat(
73 "couldn't reach the server over UDP: ", error.message()));
78 return absl::OkStatus();
87 std::error_code error;
88 asio::write(stream_, asio::buffer(
Frame(input)), error);
94void Connection::Read() {
95 stream_.async_read_some(asio::buffer(buffer_),
96 [
this](std::error_code error, std::size_t size) {
101 reader_.
Append({buffer_.data(), size});
102 while (
const auto message = reader_.Next()) {
103 if (!inbox_.Take(*message).ok()) {
108 if (!reader_.status().ok()) {
116void Connection::HelloOverUdp() {
120 protocol::Hello hello;
121 hello.set_token(token_);
122 std::error_code ignored;
123 udp_socket_.send(asio::buffer(hello.SerializeAsString()), 0, ignored);
124 hello_timer_.expires_after(std::chrono::milliseconds(250));
125 hello_timer_.async_wait([
this](std::error_code error) {
132void Connection::ReceiveOverUdp() {
133 udp_socket_.async_receive(
134 asio::buffer(datagram_), [
this](std::error_code error, std::size_t size) {
135 protocol::World part;
137 part.ParseFromArray(datagram_.data(),
static_cast<int>(size))) {
Connection(const Connection &)=delete
void Poll() override
Takes in what's arrived, without waiting.
static absl::StatusOr< std::unique_ptr< Connection > > Create(const std::string &host, uint16_t port, asio::ssl::context &tls, const std::string &token, bool udp)
Connects to the server over TLS, or returns why it couldn't.
void Send(const protocol::Input &input) override
void Append(std::string_view bytes)
std::string HandshakeError(TlsStream &stream, const std::string &host, const std::error_code &error)
Why a client's handshake with host failed, and what to do about it.
std::string Frame(const google::protobuf::MessageLite &message)
The size goes in front, as four bytes in network byte order.
void ExpectName(TlsStream &stream, const std::string &host)