Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
WorldSocket.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
11
12#ifndef SF_WORLDSOCKET_H
13#define SF_WORLDSOCKET_H
14
15#include "AuthCrypt.h"
16#include "Common.h"
17#include "Platform/Threading.h"
18#include "SharedDefines.h"
20#include <boost/asio/ip/tcp.hpp>
21#include <boost/system/error_code.hpp>
22#include <array>
23#include <atomic>
24#include <chrono>
25#include <deque>
26#include <functional>
27#include <memory>
28#include <mutex>
29#include <vector>
30
31typedef boost::asio::ip::tcp::socket WorldSocketHandle;
32
33namespace Skyfire
34{
35namespace Net
36{
37 template<class AsyncWriteStream>
39}
40}
41
42class WorldPacket;
43class WorldSession;
44namespace Skyfire
45{
46struct PacketLogServerSessionInfo;
47}
48
51{
52public:
53 WorldSocket(std::unique_ptr<WorldSocketHandle> socket, std::string remoteAddress);
54 ~WorldSocket(void);
55
56 friend class WorldSocketMgr;
57 friend class ReactorRunnable;
58
61 typedef std::unique_lock<LockType> GuardType;
62
64 bool IsClosed(void) const;
65
67 void CloseSocket(void);
68
70 const std::string& GetRemoteAddress(void) const;
71
75 int SendPacket(const WorldPacket& pct);
76
78 void LogPacketMarker(std::string const& marker);
79
82
84 long AddReference(void);
85
87 long RemoveReference(void);
88
90 int Initialize(void);
91
93 void Start(std::function<void(WorldSocket*)> closeHandler);
94
96 void DetachSession(WorldSession* session);
97
99 bool HasPendingOutput(void) const;
100
101private:
103 int handle_input_header(void);
104 int handle_input_payload(void);
105 int handle_input_missing_data(char const* data, size_t length);
106
109 int ProcessIncoming(WorldPacket* new_pct);
110
112 int HandleAuthSession(WorldPacket& recvPacket);
113
115 int HandlePing(WorldPacket& recvPacket);
116
119
120private:
122 bool IsValidSocket(void) const;
123 void StartAsyncRead();
124 void QueueSerializedPacket(std::vector<char> data);
125 void HandleAsyncRead(boost::system::error_code const& error, size_t transferredBytes);
126 void HandleAsyncWrite(boost::system::error_code const& error, size_t transferredBytes);
127 void NotifyClosed();
129
131 std::chrono::steady_clock::time_point m_LastPingTime;
133
136
138 std::string m_Address;
139
142
145
148
150 std::vector<uint8> m_Header;
152 std::vector<uint8> m_WorldHeader;
154
157
159 std::deque<std::vector<char>> m_PendingOutput;
160
164
165 std::array<uint8, 4> m_Seed;
166 std::array<char, 4096> m_ReadBuffer;
167
168 std::unique_ptr<WorldSocketHandle> m_Socket;
169 std::unique_ptr<Skyfire::Net::BoostAsioWriteQueue<WorldSocketHandle>> m_WriteQueue;
170 std::function<void(WorldSocket*)> m_CloseHandler;
171 std::atomic<long> m_ReferenceCount;
172 std::atomic<bool> m_Closed;
173 std::atomic<bool> m_CloseNotified;
174
175 WorldSocket(WorldSocket const& right) = delete;
176 WorldSocket& operator=(WorldSocket const& right) = delete;
177};
178
179#endif /* _WORLDSOCKET_H */
180
std::uint32_t uint32
Definition Define.h:77
ResponseCodes
Player session in the World.
size_t m_OutBufferSize
Configured output buffer size retained for network option compatibility.
bool IsClosed(void) const
Check if socket is closed.
size_t m_WorldHeaderRead
void StartAsyncRead()
WorldSocket(std::unique_ptr< WorldSocketHandle > socket, std::string remoteAddress)
void HandleAsyncWrite(boost::system::error_code const &error, size_t transferredBytes)
long RemoveReference(void)
Remove reference to this object.
std::unique_ptr< WorldSocketHandle > m_Socket
std::atomic< bool > m_CloseNotified
std::vector< uint8 > m_WorldHeader
friend class WorldSocketMgr
Definition WorldSocket.h:56
void SendAuthResponseError(ResponseCodes code)
WorldPacket * m_RecvWPct
here are stored the fragments of the received data
std::atomic< long > m_ReferenceCount
Skyfire::Mutex LockType
Mutex type used for various synchronizations.
Definition WorldSocket.h:60
WorldSocketSessionState m_SessionState
Session to which received packets are routed.
long AddReference(void)
Add reference to this object.
std::chrono::steady_clock::time_point m_LastPingTime
Time in which the last ping was received.
friend class ReactorRunnable
Definition WorldSocket.h:57
int ProcessIncoming(WorldPacket *new_pct)
size_t m_RecvPctRead
WorldSocket & operator=(WorldSocket const &right)=delete
AuthCrypt m_Crypt
Class used for managing encryption of the headers.
std::unique_ptr< Skyfire::Net::BoostAsioWriteQueue< WorldSocketHandle > > m_WriteQueue
int HandleSendAuthSession()
Called by MSG_VERIFY_CONNECTIVITY_RESPONSE.
uint32 m_OverSpeedPings
Keep track of over-speed pings, to prevent ping flood.
LockType m_SendLock
Mutex for protecting packet serialization and pre-start output.
void RefreshPacketLogSessionInfo()
Refreshes packet log metadata after a session gains character context.
int handle_input_header(void)
Helper functions for processing incoming data.
bool HasPendingOutput(void) const
Returns true when outgoing data is waiting to be flushed.
void Start(std::function< void(WorldSocket *)> closeHandler)
Starts asynchronous socket processing.
int HandleAuthSession(WorldPacket &recvPacket)
Called by ProcessIncoming() on CMSG_AUTH_SESSION.
void CloseSocket(void)
Close the socket.
void HandleAsyncRead(boost::system::error_code const &error, size_t transferredBytes)
void DetachSession(WorldSession *session)
Detaches the owning world session when the session is being removed.
int handle_input_payload(void)
std::atomic< bool > m_Closed
std::vector< uint8 > m_Header
std::unique_lock< LockType > GuardType
Definition WorldSocket.h:61
int Initialize(void)
Called after socket accept and manager setup.
const std::string & GetRemoteAddress(void) const
Get address of connected peer.
int HandlePing(WorldPacket &recvPacket)
Called by ProcessIncoming() on CMSG_PING.
Skyfire::PacketLogServerSessionInfo BuildPacketLogSessionInfo() const
bool IsValidSocket(void) const
void LogPacketMarker(std::string const &marker)
Writes a readable marker into the per-session packet log.
size_t m_HeaderRead
std::function< void(WorldSocket *)> m_CloseHandler
WorldSocket(WorldSocket const &right)=delete
std::array< uint8, 4 > m_Seed
std::deque< std::vector< char > > m_PendingOutput
Queue used before the socket manager starts asynchronous processing.
std::array< char, 4096 > m_ReadBuffer
void QueueSerializedPacket(std::vector< char > data)
bool m_HasLastPingTime
std::string m_Address
Address of the remote peer.
int SendPacket(const WorldPacket &pct)
int handle_input_missing_data(char const *data, size_t length)
void NotifyClosed()
boost::asio::ip::tcp::socket WorldSocketHandle
Definition WorldSocket.h:31