nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
gateway.h
Go to the documentation of this file.
1#ifndef NANOMMO_GATEWAY_GATEWAY_H_
2#define NANOMMO_GATEWAY_GATEWAY_H_
3
4#include <cstdint>
5#include <functional>
6#include <memory>
7#include <string>
8#include <string_view>
9
10#include "absl/status/statusor.h"
11#include "asio/io_context.hpp"
12#include "asio/ip/tcp.hpp"
13#include "asio/ssl/context.hpp"
14
15namespace nanommo {
16
17/// A server the gateway connects a page to: where, and the name its
18/// certificate has to have, as a client on a computer would check it.
19struct Upstream {
20 std::string host;
21 uint16_t port = 0;
22 std::string name;
23};
24
25/// Where a page's path leads, or why nowhere.
26using Route = std::function<absl::StatusOr<Upstream>(std::string_view path)>;
27
28/// Finds a world's own address by the host and port players reach it at.
29using WorldAddress = std::function<absl::StatusOr<std::string>(
30 std::string_view public_host, uint16_t port)>;
31
32/// The gateway's two routes: /login to the login server, and
33/// /world/<host>/<port> to a world, only where the lookup finds one, so the
34/// gateway connects pages to our servers and nothing else.
35Route Routes(Upstream login, WorldAddress world_address);
36
37/// Lets a browser's page reach the servers, which a page can't open a socket
38/// to: it takes WebSocket connections, and relays each one's messages to a
39/// server over TLS, and the server's bytes back, as they are.
40class Gateway {
41 public:
42 /// A page's message larger than this is refused, as the servers would.
43 static constexpr std::size_t kMaxMessage = 64 * 1024;
44
45 /// Listens on the port (0 for any), or returns why it can't.
46 static absl::StatusOr<std::unique_ptr<Gateway>> Create(
47 asio::io_context& io, uint16_t port, asio::ssl::context& tls,
48 Route route);
49
50 Gateway(const Gateway&) = delete;
51 Gateway& operator=(const Gateway&) = delete;
52
53 uint16_t port() const { return acceptor_.local_endpoint().port(); }
54
55 private:
56 Gateway(asio::io_context& io, asio::ssl::context& tls, Route route);
57
58 void Accept();
59
60 asio::ip::tcp::acceptor acceptor_;
61 asio::ssl::context& tls_;
62 Route route_;
63};
64
65} // namespace nanommo
66
67#endif // NANOMMO_GATEWAY_GATEWAY_H_
uint16_t port() const
Definition gateway.h:53
Gateway(const Gateway &)=delete
static constexpr std::size_t kMaxMessage
A page's message larger than this is refused, as the servers would.
Definition gateway.h:43
static absl::StatusOr< std::unique_ptr< Gateway > > Create(asio::io_context &io, uint16_t port, asio::ssl::context &tls, Route route)
Listens on the port (0 for any), or returns why it can't.
Definition gateway.cc:387
Gateway & operator=(const Gateway &)=delete
Definition chat.cc:9
std::function< absl::StatusOr< std::string >( std::string_view public_host, uint16_t port)> WorldAddress
Finds a world's own address by the host and port players reach it at.
Definition gateway.h:29
std::function< absl::StatusOr< Upstream >(std::string_view path)> Route
Where a page's path leads, or why nowhere.
Definition gateway.h:26
std::string Accept(std::string_view key)
The server's answer that switches the connection over to WebSocket.
Definition websocket.cc:132
Route Routes(Upstream login, WorldAddress world_address)
Definition gateway.cc:359
std::string host
Definition gateway.h:20
std::string name
Definition gateway.h:22
uint16_t port
Definition gateway.h:21