connection.h

main 134 lines · 3.7 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_CONNECTION_H_FC8E1B4392D24D27A2F129D8B93A6348
21#define FS_CONNECTION_H_FC8E1B4392D24D27A2F129D8B93A6348
22
23#include <unordered_set>
24
25#include "networkmessage.h"
26
27static constexpr int32_t CONNECTION_WRITE_TIMEOUT = 30;
28static constexpr int32_t CONNECTION_READ_TIMEOUT = 30;
29
30class Protocol;
31using Protocol_ptr = std::shared_ptr<Protocol>;
32class OutputMessage;
33using OutputMessage_ptr = std::shared_ptr<OutputMessage>;
34class Connection;
35using Connection_ptr = std::shared_ptr<Connection>;
36using ConnectionWeak_ptr = std::weak_ptr<Connection>;
37class ServiceBase;
38using Service_ptr = std::shared_ptr<ServiceBase>;
39class ServicePort;
40using ServicePort_ptr = std::shared_ptr<ServicePort>;
41using ConstServicePort_ptr = std::shared_ptr<const ServicePort>;
42
43class ConnectionManager
44{
45 public:
46 static ConnectionManager& getInstance() {
47 static ConnectionManager instance;
48 return instance;
49 }
50
51 Connection_ptr createConnection(boost::asio::io_service& io_service, ConstServicePort_ptr servicePort);
52 void releaseConnection(const Connection_ptr& connection);
53 void closeAll();
54
55 private:
56 ConnectionManager() = default;
57
58 std::unordered_set<Connection_ptr> connections;
59 std::mutex connectionManagerLock;
60};
61
62class Connection : public std::enable_shared_from_this<Connection>
63{
64 public:
65 // non-copyable
66 Connection(const Connection&) = delete;
67 Connection& operator=(const Connection&) = delete;
68
69 enum ConnectionState_t {
70 CONNECTION_STATE_OPEN,
71 CONNECTION_STATE_CLOSED,
72 };
73
74 enum { FORCE_CLOSE = true };
75
76 Connection(boost::asio::io_service& io_service,
77 ConstServicePort_ptr service_port) :
78 readTimer(io_service),
79 writeTimer(io_service),
80 service_port(std::move(service_port)),
81 socket(io_service),
82 timeConnected(time(nullptr)) {}
83 ~Connection();
84
85 friend class ConnectionManager;
86
87 void close(bool force = false);
88 // Used by protocols that require server to send first
89 void accept(Protocol_ptr protocol);
90 void accept();
91
92 void send(const OutputMessage_ptr& msg);
93
94 uint32_t getIP();
95
96 private:
97 void parseHeader(const boost::system::error_code& error);
98 void parsePacket(const boost::system::error_code& error);
99
100 void onWriteOperation(const boost::system::error_code& error);
101
102 static void handleTimeout(ConnectionWeak_ptr connectionWeak, const boost::system::error_code& error);
103
104 void closeSocket();
105 void internalSend(const OutputMessage_ptr& msg);
106
107 boost::asio::ip::tcp::socket& getSocket() {
108 return socket;
109 }
110 friend class ServicePort;
111
112 NetworkMessage msg;
113
114 boost::asio::deadline_timer readTimer;
115 boost::asio::deadline_timer writeTimer;
116
117 std::recursive_mutex connectionLock;
118
119 std::list<OutputMessage_ptr> messageQueue;
120
121 ConstServicePort_ptr service_port;
122 Protocol_ptr protocol;
123
124 boost::asio::ip::tcp::socket socket;
125
126 time_t timeConnected;
127 uint32_t packetsSent = 0;
128
129 bool connectionState = CONNECTION_STATE_OPEN;
130 bool receivedFirst = false;
131};
132
133#endif
134
Top