nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
session.cc
Go to the documentation of this file.
1#include "server/session.h"
2
3#include <array>
4#include <string>
5#include <utility>
6
7#include "asio/buffer.hpp"
8#include "asio/ssl/stream_base.hpp"
9#include "asio/write.hpp"
10#include "tracy/Tracy.hpp"
11
12namespace nanommo {
13
14Session::Session(asio::ip::tcp::socket socket, asio::ssl::context& tls,
15 OnMessage on_message, OnClose on_close)
16 : stream_(std::move(socket), tls),
17 on_message_(std::move(on_message)),
18 on_close_(std::move(on_close)) {
19 // Turn off Nagle's algorithm, so small messages go out straight away.
20 std::error_code ignored;
21 stream_.lowest_layer().set_option(asio::ip::tcp::no_delay(true), ignored);
22}
23
25 stream_.async_handshake(asio::ssl::stream_base::server,
26 [self = shared_from_this()](std::error_code error) {
27 if (error) {
28 self->Close();
29 return;
30 }
31 self->Read();
32 });
33}
34
35void Session::Read() {
36 // Capturing self, not this, keeps the session alive until the read finishes.
37 stream_.async_read_some(
38 asio::buffer(buffer_),
39 [self = shared_from_this()](std::error_code error, std::size_t size) {
40 if (error) {
41 self->Close();
42 return;
43 }
44 self->reader_.Append({self->buffer_.data(), size});
45 while (const auto message = self->reader_.Next()) {
46 self->on_message_(*self, *message);
47 if (self->closed_) {
48 return;
49 }
50 }
51 if (!self->reader_.status().ok()) {
52 self->Close();
53 return;
54 }
55 self->Read();
56 });
57}
58
59void Session::Send(std::string bytes) {
60 Send(Outgoing{std::move(bytes), nullptr, {}});
61}
62
63void Session::Send(std::shared_ptr<const std::string> shared, std::string own) {
64 const std::size_t size = shared->size() + own.size();
65 Send(Outgoing{Header(size), std::move(shared), std::move(own)});
66}
67
68void Session::Send(Outgoing outgoing) {
69 ZoneScoped;
70 if (closed_) {
71 return;
72 }
73 outbox_.push_back(std::move(outgoing));
74 // Writes mustn't overlap, so if one is under way, it picks these up next.
75 if (outbox_.size() == 1) {
76 Write();
77 }
78}
79
80void Session::Write() {
81 const Outgoing& next = outbox_.front();
82 const std::array buffers{
83 asio::buffer(next.head),
84 next.shared ? asio::buffer(*next.shared) : asio::const_buffer(),
85 asio::buffer(next.tail)};
86 asio::async_write(
87 stream_, buffers,
88 [self = shared_from_this()](std::error_code error, std::size_t) {
89 if (error) {
90 self->Close();
91 return;
92 }
93 self->outbox_.pop_front();
94 if (!self->outbox_.empty()) {
95 self->Write();
96 }
97 });
98}
99
101 if (closed_) {
102 return;
103 }
104 closed_ = true;
105 std::error_code ignored;
106 stream_.lowest_layer().close(ignored);
107 on_close_();
108}
109
110} // namespace nanommo
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
std::string Header(std::size_t size)
Definition framing.cc:8