nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
main.cc
Go to the documentation of this file.
1#include <csignal>
2#include <cstdint>
3#include <iostream>
4#include <memory>
5#include <string>
6#include <string_view>
7#include <utility>
8#include <vector>
9
10#include "absl/status/statusor.h"
11#include "absl/strings/numbers.h"
12#include "asio/io_context.hpp"
13#include "asio/signal_set.hpp"
14#include "asio/ssl/context.hpp"
15#include "gateway/gateway.h"
16#include "gateway/world_list.h"
17#include "protocol/settings.h"
18#include "protocol/tls.h"
19#include "protocol/version.h"
20#include "server/consul.h"
21
22// The gateway between browsers' pages and the servers: WebSocket on its port,
23// TLS to the login server and to the worlds Consul lists. In a deployment, a
24// reverse proxy with a certificate browsers trust puts wss:// in front of it.
25int main() {
26 std::cout << "nanommo gateway: version " << nanommo::kVersion << std::endl;
27 uint32_t port = 0;
28 if (!absl::SimpleAtoi(nanommo::Setting("NANOMMO_GATEWAY_PORT", "8090"),
29 &port) ||
30 port > 65535) {
31 std::cerr << "nanommo gateway: NANOMMO_GATEWAY_PORT isn't a port\n";
32 return 1;
33 }
34 // The servers' certificates, checked as a client on a computer checks them.
35 absl::StatusOr<asio::ssl::context> tls =
36 nanommo::ClientTls(nanommo::FileSetting("NANOMMO_TLS_CA", "tls/ca.crt"));
37 if (!tls.ok()) {
38 std::cerr << "nanommo gateway: " << tls.status() << "\n";
39 return 1;
40 }
41 const std::string login = nanommo::Setting("NANOMMO_LOGIN_HOST", "localhost");
42 const nanommo::Consul consul(
43 nanommo::Setting("NANOMMO_CONSUL_HOST", "127.0.0.1"));
44
45 asio::io_context io;
46 // The worlds a page may reach, from Consul, asked on a thread of their own.
47 nanommo::WorldList worlds(
48 io, [&consul]() -> absl::StatusOr<std::vector<nanommo::ListedWorld>> {
49 absl::StatusOr<std::vector<nanommo::WorldInstance>> instances =
50 consul.Instances();
51 if (!instances.ok()) {
52 return instances.status();
53 }
54 std::vector<nanommo::ListedWorld> listed;
55 for (nanommo::WorldInstance& instance : *instances) {
56 listed.push_back({.public_host = std::move(instance.public_host),
57 .port = instance.port,
58 .address = std::move(instance.address)});
59 }
60 return listed;
61 });
62 worlds.Start();
63 absl::StatusOr<std::unique_ptr<nanommo::Gateway>> gateway =
65 io, static_cast<uint16_t>(port), *tls,
67 {.host = login, .port = 7778, .name = login},
68 [&worlds](std::string_view host, uint16_t world_port) {
69 return worlds.Find(host, world_port,
70 nanommo::WorldList::Clock::now());
71 }));
72 if (!gateway.ok()) {
73 std::cerr << "nanommo gateway: " << gateway.status() << "\n";
74 return 1;
75 }
76 std::cout << "nanommo gateway: listening on " << (*gateway)->port()
77 << ", for the login server at " << login << std::endl;
78
79 asio::signal_set signals(io, SIGINT, SIGTERM);
80 signals.async_wait([&io](std::error_code, int) { io.stop(); });
81 io.run();
82 std::cout << "nanommo gateway: stopped" << std::endl;
83 return 0;
84}
absl::StatusOr< std::vector< WorldInstance > > Instances() const
Definition consul.cc:168
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
void Start()
Asks for the list now, and every second after.
Definition world_list.cc:23
int main()
Definition login_main.cc:28
std::string Setting(const char *name, const std::string &fallback)
Definition settings.h:9
std::string FileSetting(const char *name, const std::string &path)
Definition settings.cc:22
absl::StatusOr< asio::ssl::context > ClientTls(const std::string &authority)
Definition tls.cc:41
Route Routes(Upstream login, WorldAddress world_address)
Definition gateway.cc:359
A world instance as players reach it, and as the servers do.
Definition consul.h:29