nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
login_server.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_LOGIN_SERVER_H_
2#define NANOMMO_SERVER_LOGIN_SERVER_H_
3
4#include <chrono>
5#include <cstdint>
6#include <memory>
7
8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
10#include "asio/io_context.hpp"
11#include "asio/ip/tcp.hpp"
12#include "asio/ssl/context.hpp"
13#include "protocol/nanommo.pb.h"
14#include "server/accounts.h"
15#include "server/characters.h"
16#include "server/consul.h"
17#include "server/metrics.h"
18#include "server/tokens.h"
19
20namespace nanommo {
21
22/// Answers a request: a login, or a new account, with a session and the
23/// account's characters, and then, with that session, a new character, a
24/// deleted one, or a token to enter a world as one, with the characters as
25/// they are after it. A player's mistake, such as a wrong password or a name
26/// that's taken, comes back as an error they can read. Returns an error status
27/// when the servers it uses can't answer, such as MySQL or Redis being down.
28absl::StatusOr<protocol::LoginReply> HandleLogin(
29 const protocol::LoginRequest& request, Accounts& accounts, Tokens& tokens,
30 Characters& characters);
31
32/// What came of a request, for the metrics: "ok", "refused" when the reply
33/// says why not, as for a wrong password, which is the player's mistake, or
34/// "failed" when the servers couldn't answer it.
35const char* Outcome(const absl::StatusOr<protocol::LoginReply>& handled);
36
37class LoginServer {
38 public:
39 /// Starts listening on the port, or returns why it couldn't. Each client
40 /// talks to it over TLS, with the context. Each request it answers goes
41 /// into the metrics, with how long it took.
42 static absl::StatusOr<std::unique_ptr<LoginServer>> Create(
43 asio::io_context& io, uint16_t port, asio::ssl::context& tls,
44 Accounts& accounts, Tokens& tokens, Characters& characters,
45 Consul& consul, LoginMetrics& metrics);
46
47 private:
48 LoginServer(asio::io_context& io, asio::ssl::context& tls, Accounts& accounts,
49 Tokens& tokens, Characters& characters, Consul& consul,
50 LoginMetrics& metrics);
51
52 absl::Status Start(uint16_t port);
53 void Accept();
54 protocol::LoginReply ListWorlds();
55
56 asio::ip::tcp::acceptor acceptor_;
57 asio::ssl::context& tls_;
58 Accounts& accounts_;
59 Tokens& tokens_;
60 Characters& characters_;
61 Consul& consul_;
62 LoginMetrics& metrics_;
63 protocol::LoginReply worlds_;
64 std::chrono::steady_clock::time_point worlds_at_;
65};
66
67} // namespace nanommo
68
69#endif // NANOMMO_SERVER_LOGIN_SERVER_H_
static absl::StatusOr< std::unique_ptr< LoginServer > > Create(asio::io_context &io, uint16_t port, asio::ssl::context &tls, Accounts &accounts, Tokens &tokens, Characters &characters, Consul &consul, LoginMetrics &metrics)
Definition chat.cc:9
const char * Outcome(const absl::StatusOr< protocol::LoginReply > &handled)
absl::StatusOr< protocol::LoginReply > HandleLogin(const protocol::LoginRequest &request, Accounts &accounts, Tokens &tokens, Characters &characters)