Horizon Official Technical Documentation
NetworkPacket.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) 2019 Sagun K. (sagunxp@gmail.com).
11 * Copyright (c) 2019 Horizon Dev Team.
12 *
13 * Base Author - Sagun K. (sagunxp@gmail.com)
14 *
15 * This library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 **************************************************/
28
29#ifndef HORIZON_BASE_NETWORKPACKET_HPP
30#define HORIZON_BASE_NETWORKPACKET_HPP
31
34
35#include <memory>
36
37namespace Horizon
38{
39namespace Base
40{
41template <class SessionType>
43{
44public:
45 explicit NetworkPacket(uint16_t packet_id, std::shared_ptr<SessionType> s)
46 : _packet_id(packet_id), _session(s)
47 {
48 //
49 }
50
52 {
53 //
54 }
55
56 void set_packet_id(uint16_t id) { _packet_id = id; }
57 uint16_t get_packet_id() { return _packet_id; }
58
59 ByteBuffer &buf() { return _buffer; }
60
65 std::shared_ptr<SessionType> get_session() { return _session.lock(); }
66
67 virtual void handle(ByteBuffer &&buf) {}
68 virtual void deserialize(ByteBuffer &buf) {}
69
76 void transmit();
77
84 void transmit(std::size_t size);
85protected:
87 uint16_t _packet_id;
88
89private:
90 std::weak_ptr<SessionType> _session;
91};
92
93template <class SessionType>
95{
96 transmit(this->_buffer.active_length());
97}
98
105template <class SessionType>
107{
108 std::shared_ptr<SessionType> s = this->get_session();
109 if (s == nullptr) {
110 HLog(debug) << "NetworkPacket::transmit: Session was null.";
111 return;
112 }
113
114 if (this->_buffer.is_empty()) {
115 HLog(debug) << "Attempted to transmit empty buffer.";
116 return;
117 }
118
119 s->transmit_buffer(std::move(this->_buffer), size);
120}
121
122}
123}
124
125#endif /* HORIZON_BASE_NETWORKPACKET_HPP */
#define HLog(type)
Definition: Logger.hpp:122
Definition: ByteBuffer.hpp:78
Definition: NetworkPacket.hpp:43
ByteBuffer _buffer
Buffer storage facility for the packet stream.
Definition: NetworkPacket.hpp:86
void transmit(std::size_t size)
Send an Asynchronous packet by queueing a buffer of a particular size to the connected session.
Definition: NetworkPacket.hpp:106
void transmit()
Send an Asynchronous packet by queueing a buffer of a particular size to the connected session.
Definition: NetworkPacket.hpp:94
std::weak_ptr< SessionType > _session
Pointer to the instantiated session object.
Definition: NetworkPacket.hpp:90
uint16_t _packet_id
ID of the network packet.
Definition: NetworkPacket.hpp:87
virtual void deserialize(ByteBuffer &buf)
Definition: NetworkPacket.hpp:68
std::shared_ptr< SessionType > get_session()
Retrieves the session from this handler instance.
Definition: NetworkPacket.hpp:65
NetworkPacket(uint16_t packet_id, std::shared_ptr< SessionType > s)
Definition: NetworkPacket.hpp:45
virtual void handle(ByteBuffer &&buf)
Definition: NetworkPacket.hpp:67
uint16_t get_packet_id()
Definition: NetworkPacket.hpp:57
void set_packet_id(uint16_t id)
Definition: NetworkPacket.hpp:56
virtual ~NetworkPacket()
Definition: NetworkPacket.hpp:51
ByteBuffer & buf()
Definition: NetworkPacket.hpp:59
Definition: Element.hpp:7