nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
world_list.h
Go to the documentation of this file.
1#ifndef NANOMMO_GATEWAY_WORLD_LIST_H_
2#define NANOMMO_GATEWAY_WORLD_LIST_H_
3
4#include <chrono>
5#include <cstdint>
6#include <functional>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <vector>
11
12#include "absl/status/statusor.h"
13#include "asio/io_context.hpp"
14#include "asio/steady_timer.hpp"
15#include "asio/thread_pool.hpp"
16
17namespace nanommo {
18
19/// A world instance, by the host and port players reach it at, and its own
20/// address, where the gateway connects to it.
22 std::string public_host;
23 uint16_t port = 0;
24 std::string address;
25};
26
27/// The worlds Consul lists, which the gateway may connect pages to. Asking
28/// Consul waits on the network, so it's done on a thread of its own, once a
29/// second, and the list it brings back is read and changed on the
30/// io_context's thread alone, where the gateway runs: a page's route never
31/// waits for Consul.
32class WorldList {
33 public:
34 using Clock = std::chrono::steady_clock;
35 using Fetch = std::function<absl::StatusOr<std::vector<ListedWorld>>()>;
36
37 /// A list this old says nothing about now, so every world is refused.
38 static constexpr std::chrono::seconds kStale{10};
39
40 WorldList(asio::io_context& io, Fetch fetch);
41
42 WorldList(const WorldList&) = delete;
43 WorldList& operator=(const WorldList&) = delete;
44
45 /// Destroy it once the io_context has stopped.
46 ~WorldList();
47
48 /// Asks for the list now, and every second after.
49 void Start();
50
51 /// A list that came back at the time, on the io_context's thread. An
52 /// error keeps the list from before, which grows stale.
53 void Take(absl::StatusOr<std::vector<ListedWorld>> worlds,
54 Clock::time_point at);
55
56 /// The world's own address: NotFound when it isn't on the list, and
57 /// Unavailable when there's no list newer than kStale.
58 absl::StatusOr<std::string> Find(std::string_view public_host, uint16_t port,
59 Clock::time_point now) const;
60
61 private:
62 void Ask();
63
64 asio::io_context& io_;
65 Fetch fetch_;
66 asio::steady_timer timer_;
67 asio::thread_pool thread_{1};
68 std::vector<ListedWorld> worlds_;
69 std::optional<Clock::time_point> fetched_;
70 bool asking_ = false;
71};
72
73} // namespace nanommo
74
75#endif // NANOMMO_GATEWAY_WORLD_LIST_H_
WorldList(asio::io_context &io, Fetch fetch)
Definition world_list.cc:15
absl::StatusOr< std::string > Find(std::string_view public_host, uint16_t port, Clock::time_point now) const
Definition world_list.cc:59
void Take(absl::StatusOr< std::vector< ListedWorld > > worlds, Clock::time_point at)
Definition world_list.cc:48
std::chrono::steady_clock Clock
Definition world_list.h:34
WorldList & operator=(const WorldList &)=delete
WorldList(const WorldList &)=delete
~WorldList()
Destroy it once the io_context has stopped.
Definition world_list.cc:18
std::function< absl::StatusOr< std::vector< ListedWorld > >()> Fetch
Definition world_list.h:35
void Start()
Asks for the list now, and every second after.
Definition world_list.cc:23
static constexpr std::chrono::seconds kStale
A list this old says nothing about now, so every world is refused.
Definition world_list.h:38
Definition chat.cc:9
std::string address
Definition world_list.h:24
std::string public_host
Definition world_list.h:22