Wrath of Zeus
Made by Torchlight Games for CSE 125 SP24
Loading...
Searching...
No Matches
session.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/multi_index_container.hpp>
4#include <boost/multi_index/hashed_index.hpp>
5#include <boost/multi_index/member.hpp>
6
7#include <iostream>
8#include <boost/asio.hpp>
9#include <memory>
10#include <array>
11#include <queue>
12#include <vector>
13#include <mutex>
14#include <thread>
15#include <string>
16#include <optional>
17
20
21using namespace boost::asio::ip;
22
36};
37
38
47 SessionInfo(std::optional<std::string> client_name,
48 std::optional<EntityID> client_eid, std::optional<bool> is_dungeon_master)
50
51 std::optional<std::string> client_name;
52 std::optional<EntityID> client_eid;
53 std::optional<bool> is_dungeon_master;
54};
55
56
63class Session : public std::enable_shared_from_this<Session> {
64public:
72 Session(tcp::socket socket, SessionInfo info);
73 ~Session();
74
78 bool isOkay() const;
79
86 std::vector<Event> handleAllReceivedPackets();
87
93 [[nodiscard("You should check if the connection was successful")]]
94 bool connectTo(basic_resolver_results<class boost::asio::ip::tcp> endpoints);
95
101 void sendPacket(std::shared_ptr<PackagedPacket> packet);
102
109 void sendEvent(Event evt);
110
114 const SessionInfo& getInfo() const;
115
116 void setDM(bool is_dm);
117
118private:
120 bool okay;
121
122 std::optional<PacketHeader> prev_hdr;
123
124 tcp::socket socket;
125
126 std::vector<std::shared_ptr<PackagedPacket>> packets_to_send;
127
128 std::array<char, NETWORK_BUFFER_SIZE> buffer;
129
130 SessionInfo info;
131
141 std::optional<Event> _handleReceivedPacket(PacketType type, const std::string& data);
142
150 SocketError _classifySocketError(boost::system::error_code ec, const char* where);
151
156 bool socketHasEnoughBytes(std::size_t bytes);
157};
158
165 boost::asio::ip::address ip,
166 std::shared_ptr<Session> session)
168
171 boost::asio::ip::address ip;
172 std::shared_ptr<Session> session;
173};
174
181 size_t operator()(const boost::asio::ip::address& v) const {
182 if (v.is_v4())
183 return v.to_v4().to_ulong();
184 if (v.is_v6()) {
185 auto const& range = v.to_v6().to_bytes();
186 return boost::hash_range(range.begin(), range.end());
187 }
188 if (v.is_unspecified()) {
189 // guaranteed to be random: chosen by fair dice roll
190 return static_cast<size_t>(0x4751301174351161ul);
191 }
192 return boost::hash_value(v.to_string());
193 }
194};
195
196// Tag structs to let you index into a Sessions Multi_index map by a name
197struct IndexByID {};
198// Tag structs to let you index into a Sessions Multi_index map by a name
199struct IndexByIP {};
200
208using Sessions = boost::multi_index_container<
210 boost::multi_index::indexed_by<
211 boost::multi_index::hashed_unique<
212 boost::multi_index::tag<IndexByID>,
213 boost::multi_index::member<SessionEntry, EntityID, &SessionEntry::id>
214 >,
215 boost::multi_index::hashed_unique<
216 boost::multi_index::tag<IndexByIP>,
217 boost::multi_index::member<SessionEntry, boost::asio::ip::address, &SessionEntry::ip>,
219 >
220 >
221>;
Definition: session.hpp:63
bool connectTo(basic_resolver_results< class boost::asio::ip::tcp > endpoints)
Definition: session.cpp:89
bool isOkay() const
Definition: session.cpp:24
~Session()
Definition: session.cpp:21
void setDM(bool is_dm)
Definition: session.cpp:170
void sendEvent(Event evt)
Definition: session.cpp:136
void sendPacket(std::shared_ptr< PackagedPacket > packet)
Definition: session.cpp:119
const SessionInfo & getInfo() const
Definition: session.cpp:28
std::vector< Event > handleAllReceivedPackets()
Definition: session.cpp:32
const GLdouble * v
Definition: glad.h:1999
GLuint id
Definition: glad.h:1776
GLboolean * data
Definition: glad.h:1600
GLuint buffer
Definition: glad.h:1791
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glad.h:1531
GLenum GLint * range
Definition: glad.h:2816
PacketType
Definition: packet.hpp:30
SocketError
Definition: session.hpp:29
@ FATAL
We cannot recover from this error, so the socket is probably dead.
Definition: session.hpp:33
@ RETRY
There was an error, but it doesn't compromise the socket connection.
Definition: session.hpp:35
@ NONE
No error, everything is good.
Definition: session.hpp:31
boost::multi_index_container< SessionEntry, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::tag< IndexByID >, boost::multi_index::member< SessionEntry, EntityID, &SessionEntry::id > >, boost::multi_index::hashed_unique< boost::multi_index::tag< IndexByIP >, boost::multi_index::member< SessionEntry, boost::asio::ip::address, &SessionEntry::ip >, ip_address_hash > > > Sessions
Definition: session.hpp:221
Definition: event.hpp:348
Definition: session.hpp:197
Definition: session.hpp:199
Definition: session.hpp:162
EntityID id
Definition: session.hpp:169
SessionEntry(EntityID id, bool is_dungeon_master, boost::asio::ip::address ip, std::shared_ptr< Session > session)
Definition: session.hpp:163
boost::asio::ip::address ip
Definition: session.hpp:171
bool is_dungeon_master
Definition: session.hpp:170
std::shared_ptr< Session > session
Definition: session.hpp:172
Definition: session.hpp:46
SessionInfo(std::optional< std::string > client_name, std::optional< EntityID > client_eid, std::optional< bool > is_dungeon_master)
Definition: session.hpp:47
std::optional< EntityID > client_eid
Definition: session.hpp:52
std::optional< std::string > client_name
Definition: session.hpp:51
std::optional< bool > is_dungeon_master
Definition: session.hpp:53
Definition: session.hpp:180
size_t operator()(const boost::asio::ip::address &v) const
Definition: session.hpp:181
uint32_t EntityID
Global Object ID (when the server or client references an object with a particular EntityID,...
Definition: typedefs.hpp:9