Horizon Official Technical Documentation
Session.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 * Copyright (c) 2020- Horizon Dev Team.
11 *
12 * Base Author - Sagun Khosla. (sagunxp@gmail.com)
13 *
14 * This library is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 **************************************************/
27
28#ifndef HORIZON_NETWORKING_SESSION_HPP
29#define HORIZON_NETWORKING_SESSION_HPP
30
33
34#include <memory>
35
36namespace Horizon
37{
38namespace Networking
39{
40
53template <class SocketType, class SessionType>
54class Session : public std::enable_shared_from_this<SessionType>
55{
56public:
57 Session(uint64_t uid)
58 : _uid(uid)
59 {
60 //
61 }
62
63 virtual ~Session()
64 {
65 //
66 }
67
70 std::shared_ptr<SocketType> get_socket() { return _socket.lock(); }
74 void set_socket(std::weak_ptr<SocketType> socket) { _socket.swap(socket); }
75
79 virtual void update(uint32_t diff) = 0;
80
84 virtual void initialize() = 0;
85
89
93 void set_initialized(bool initialized) { _is_initialized = initialized; }
94
97 uint64_t get_session_id() { return _uid; }
98
102private:
104 std::weak_ptr<SocketType> _socket;
105 bool _is_initialized{ false };
106 uint64_t _uid{ 0 };
107};
108}
109}
110
111#endif /* HORIZON_NETWORKING_SESSION_HPP */
A Session object that handles a single socket. Sockets are moved into the thread by SocketMgr,...
Definition: Session.hpp:55
virtual void update(uint32_t diff)=0
virtual ~Session()
Definition: Session.hpp:63
std::weak_ptr< SocketType > _socket
Definition: Session.hpp:104
ThreadSafeQueue< ByteBuffer > & get_recv_queue()
Receive queue of the buffer received by the socket.
Definition: Session.hpp:101
bool _is_initialized
Definition: Session.hpp:105
std::shared_ptr< SocketType > get_socket()
Get the socket.
Definition: Session.hpp:70
Session(uint64_t uid)
Definition: Session.hpp:57
virtual void initialize()=0
bool is_initialized()
Called to verify if the session is initialized.
Definition: Session.hpp:88
void set_socket(std::weak_ptr< SocketType > socket)
Set the socket.
Definition: Session.hpp:74
uint64_t get_session_id()
Get the unique id of the session.
Definition: Session.hpp:97
ThreadSafeQueue< ByteBuffer > _buffer_recv_queue
Definition: Session.hpp:103
void set_initialized(bool initialized)
Set whether the session is initialized or not.
Definition: Session.hpp:93
uint64_t _uid
Definition: Session.hpp:106
Definition: Element.hpp:7