nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
session.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_SESSION_H_
2#define NANOMMO_SERVER_SESSION_H_
3
4#include <array>
5#include <deque>
6#include <functional>
7#include <memory>
8#include <string>
9
10#include "asio/ip/tcp.hpp"
11#include "asio/ssl/context.hpp"
12#include "protocol/framing.h"
13#include "protocol/tls.h"
14
15namespace nanommo {
16
17class Session : public std::enable_shared_from_this<Session> {
18 public:
19 using OnMessage = std::function<void(Session&, const std::string&)>;
20 using OnClose = std::function<void()>;
21
22 Session(asio::ip::tcp::socket socket, asio::ssl::context& tls,
23 OnMessage on_message, OnClose on_close);
24
25 /// Shakes hands over TLS, then reads the client's messages.
26 void Start();
27
28 void Send(std::string bytes);
29
30 void Send(std::shared_ptr<const std::string> shared, std::string own);
31
32 void Close();
33
34 private:
35 struct Outgoing {
36 std::string head;
37 std::shared_ptr<const std::string> shared;
38 std::string tail;
39 };
40
41 void Read();
42 void Send(Outgoing outgoing);
43 void Write();
44
45 TlsStream stream_;
46 OnMessage on_message_;
47 OnClose on_close_;
48 std::array<char, 4096> buffer_;
49 FrameReader reader_;
50 std::deque<Outgoing> outbox_;
51 bool closed_ = false;
52};
53
54} // namespace nanommo
55
56#endif // NANOMMO_SERVER_SESSION_H_
Session(asio::ip::tcp::socket socket, asio::ssl::context &tls, OnMessage on_message, OnClose on_close)
Definition session.cc:14
std::function< void()> OnClose
Definition session.h:20
void Start()
Shakes hands over TLS, then reads the client's messages.
Definition session.cc:24
std::function< void(Session &, const std::string &)> OnMessage
Definition session.h:19
void Send(std::string bytes)
Definition session.cc:59
Definition chat.cc:9
asio::ssl::stream< asio::ip::tcp::socket > TlsStream
Definition tls.h:14