nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
servers_web.cc
Go to the documentation of this file.
1#include <emscripten.h>
2
3#include <memory>
4#include <string>
5#include <utility>
6
7#include "absl/status/status.h"
8#include "absl/strings/str_cat.h"
9#include "client/servers.h"
11
12namespace nanommo {
13namespace {
14
15// The gateway the page was opened with, as in
16// nanommo_client.html?gateway=ws://localhost:8090, or nothing.
17std::string PageGateway() {
18 return emscripten_run_script_string(
19 "new URLSearchParams(window.location.search).get('gateway') || ''");
20}
21
22// A page can't open a socket, but it can open a WebSocket, to the gateway,
23// which carries each one to the login server or a world, over TLS. The worlds
24// come over that one stream, as a page has no UDP either.
25class GatewayServers final : public Servers {
26 public:
27 explicit GatewayServers(std::string gateway) : gateway_(std::move(gateway)) {}
28
29 absl::StatusOr<protocol::LoginReply> Ask(
30 const protocol::LoginRequest& request) override {
31 return AskOverWebSocket(absl::StrCat(gateway_, "/login"), request);
32 }
33
34 absl::StatusOr<std::unique_ptr<Link>> Enter(
35 const protocol::WorldListing& world, const std::string& token) override {
36 return Open(world.host(), world.port(), token);
37 }
38
39 absl::StatusOr<std::unique_ptr<Link>> Move(
40 const protocol::Move& move) override {
41 return Open(move.host(), move.port(), move.token());
42 }
43
44 private:
45 // The world at host and port, as the login server lists it, through the
46 // gateway.
47 absl::StatusOr<std::unique_ptr<Link>> Open(const std::string& host,
48 uint32_t port,
49 const std::string& token) {
50 absl::StatusOr<std::unique_ptr<WebSocketLink>> link = WebSocketLink::Open(
51 absl::StrCat(gateway_, "/world/", host, "/", port), token);
52 if (!link.ok()) {
53 return link.status();
54 }
55 return std::unique_ptr<Link>(*std::move(link));
56 }
57
58 std::string gateway_;
59};
60
61// Without a gateway, there's no server to reach from a page, so it plays
62// offline, and nothing asks these.
63class NoServers final : public Servers {
64 public:
65 absl::StatusOr<protocol::LoginReply> Ask(
66 const protocol::LoginRequest&) override {
67 return Unreachable();
68 }
69
70 absl::StatusOr<std::unique_ptr<Link>> Enter(const protocol::WorldListing&,
71 const std::string&) override {
72 return Unreachable();
73 }
74
75 absl::StatusOr<std::unique_ptr<Link>> Move(const protocol::Move&) override {
76 return Unreachable();
77 }
78
79 private:
80 static absl::Status Unreachable() {
81 return absl::UnavailableError(
82 "the page was opened without a gateway to the servers");
83 }
84};
85
86} // namespace
87
88bool CanReachServers() { return !PageGateway().empty(); }
89
90absl::StatusOr<std::unique_ptr<Servers>> Servers::Create(std::string, bool) {
91 std::string gateway = PageGateway();
92 if (gateway.empty()) {
93 return std::make_unique<NoServers>();
94 }
95 return std::make_unique<GatewayServers>(std::move(gateway));
96}
97
98} // namespace nanommo
static absl::StatusOr< std::unique_ptr< Servers > > Create(std::string host, bool udp)
Definition servers.cc:75
Definition chat.cc:9
bool CanReachServers()
Whether this build can reach the servers at all: a browser's can't.
Definition servers.cc:73
absl::StatusOr< protocol::LoginReply > AskOverWebSocket(const std::string &url, const protocol::LoginRequest &request)
The login server's answer to the request, through the gateway at url.
absl::StatusOr< std::vector< std::string > > Unreachable(const smollergraph::Graph &graph)
Definition graph.cc:257