nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
login_client.cc
Go to the documentation of this file.
2
3#include <array>
4#include <chrono>
5#include <cstddef>
6#include <functional>
7#include <optional>
8#include <string>
9#include <system_error>
10#include <utility>
11
12#include "absl/status/status.h"
13#include "absl/strings/str_cat.h"
14#include "absl/time/time.h"
15#include "asio/buffer.hpp"
16#include "asio/connect.hpp"
17#include "asio/io_context.hpp"
18#include "asio/ip/tcp.hpp"
19#include "asio/ssl/stream_base.hpp"
20#include "asio/write.hpp"
21#include "protocol/framing.h"
22#include "protocol/tls.h"
23
24namespace nanommo {
25
26absl::StatusOr<protocol::LoginReply> LogIn(
27 const std::string& host, uint16_t port, asio::ssl::context& tls,
28 const protocol::LoginRequest& request, std::chrono::milliseconds deadline) {
29 // Each step waits for the one before, on this io_context, which runs until
30 // there's an answer, or until the deadline. Whatever's still waiting then
31 // goes with it, never called.
32 asio::io_context io;
33 TlsStream stream(io, tls);
34 asio::ip::tcp::resolver resolver(io);
35 const std::string frame = Frame(request);
36 FrameReader reader;
37 std::array<char, 1024> buffer;
38 std::optional<absl::StatusOr<protocol::LoginReply>> answer;
39 const auto unreachable = [&answer](const std::error_code& error) {
40 answer = absl::UnavailableError(
41 absl::StrCat("couldn't reach the login server: ", error.message()));
42 };
43
44 std::function<void()> read = [&] {
45 stream.async_read_some(
46 asio::buffer(buffer), [&](std::error_code error, std::size_t size) {
47 if (error) {
48 unreachable(error);
49 return;
50 }
51 reader.Append({buffer.data(), size});
52 if (const std::optional<std::string> message = reader.Next()) {
53 protocol::LoginReply reply;
54 if (!reply.ParseFromString(*message)) {
55 answer =
56 absl::DataLossError("couldn't read the login server's reply");
57 return;
58 }
59 answer = std::move(reply);
60 } else if (!reader.status().ok()) {
61 answer = reader.status();
62 } else {
63 read();
64 }
65 });
66 };
67 // The servers listen on IPv4 alone (server/listen.cc), so we look up IPv4
68 // addresses alone. Otherwise Windows tries localhost's ::1 first, and gives
69 // up on it only after two seconds.
70 resolver.async_resolve(
71 asio::ip::tcp::v4(), host, std::to_string(port),
72 [&](std::error_code error,
73 const asio::ip::tcp::resolver::results_type& endpoints) {
74 if (error) {
75 unreachable(error);
76 return;
77 }
78 asio::async_connect(
79 stream.lowest_layer(), endpoints,
80 [&](std::error_code error, const asio::ip::tcp::endpoint&) {
81 if (error) {
82 unreachable(error);
83 return;
84 }
85 ExpectName(stream, host);
86 stream.async_handshake(
87 asio::ssl::stream_base::client, [&](std::error_code error) {
88 if (error) {
89 answer = absl::UnavailableError(absl::StrCat(
90 "couldn't shake hands with the login server: ",
91 HandshakeError(stream, host, error)));
92 return;
93 }
94 asio::async_write(stream, asio::buffer(frame),
95 [&](std::error_code error, std::size_t) {
96 if (error) {
97 unreachable(error);
98 return;
99 }
100 read();
101 });
102 });
103 });
104 });
105 io.run_for(deadline);
106 if (!answer) {
107 return absl::DeadlineExceededError(
108 absl::StrCat("the login server didn't answer within ",
109 absl::FormatDuration(absl::FromChrono(deadline))));
110 }
111 return *std::move(answer);
112}
113
114} // namespace nanommo
void Append(std::string_view bytes)
Definition framing.cc:19
std::optional< std::string > Next()
Definition framing.cc:21
const absl::Status & status() const
Definition framing.h:32
Definition chat.cc:9
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.
Definition tls.cc:64
asio::ssl::stream< asio::ip::tcp::socket > TlsStream
Definition tls.h:14
absl::StatusOr< protocol::LoginReply > LogIn(const std::string &host, uint16_t port, asio::ssl::context &tls, const protocol::LoginRequest &request, std::chrono::milliseconds deadline)
std::string Frame(const google::protobuf::MessageLite &message)
The size goes in front, as four bytes in network byte order.
Definition framing.cc:14
void ExpectName(TlsStream &stream, const std::string &host)
Definition tls.cc:52