Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
WorldSocketAcceptor.cpp
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
6#include "Log.h"
9#include "WorldSocketMgr.h"
10#include <boost/asio/error.hpp>
11#include <boost/system/error_code.hpp>
12#include <memory>
13#include <utility>
14
21
26
27bool WorldSocketAcceptor::Open(uint16 port, const char* address)
28{
29 if (!Skyfire::Net::OpenTcpAcceptor(m_ThreadGroup.GetIoContext(), m_Acceptor, port, address, "network", "world"))
30 return false;
31
32 m_Closed = false;
34
35 if (m_ThreadGroup.Start(1) == -1)
36 {
37 Close();
38 return false;
39 }
40
41 return true;
42}
43
45{
46 bool expected = false;
47 if (!m_Closed.compare_exchange_strong(expected, true))
48 return;
49
51 m_ThreadGroup.StopAndJoin();
52}
53
57
59{
60 if (!m_Acceptor.is_open())
61 return;
62
63 ReactorRunnable* reactor = sWorldSocketMgr->SelectNetworkThread();
64 std::shared_ptr<boost::asio::ip::tcp::endpoint> remoteEndpoint(new boost::asio::ip::tcp::endpoint);
65
66 m_Acceptor.async_accept(sWorldSocketMgr->GetNetworkIoContext(reactor), *remoteEndpoint,
67 [this, reactor, remoteEndpoint](boost::system::error_code const& error, auto socket) mutable
68 {
69 HandleAccept(reactor, std::move(socket), *remoteEndpoint, error);
70 });
71}
72
73void WorldSocketAcceptor::HandleAccept(ReactorRunnable* reactor, WorldSocketHandle socket, boost::asio::ip::tcp::endpoint const& remoteEndpoint,
74 boost::system::error_code const& error)
75{
76 if (m_Closed)
77 return;
78
79 if (error)
80 {
81 if (error != boost::asio::error::operation_aborted)
82 SF_LOG_ERROR("network", "Failed to accept world socket, error %d", error.value());
83 }
84 else
85 {
86 std::string remoteAddress = remoteEndpoint.address().to_string();
87
88 std::unique_ptr<WorldSocketHandle> socketHandle(new WorldSocketHandle(std::move(socket)));
89 std::unique_ptr<WorldSocket> worldSocket(new WorldSocket(std::move(socketHandle), remoteAddress));
90 if (sWorldSocketMgr->OnSocketOpen(worldSocket.get(), reactor) == -1)
91 worldSocket->CloseSocket();
92 else
93 worldSocket.release();
94 }
95
97}
std::uint16_t uint16
Definition Define.h:78
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
bool Open(uint16 port, const char *address)
boost::asio::ip::tcp::acceptor m_Acceptor
Skyfire::Asio::IoContextThreadGroup m_ThreadGroup
std::atomic< bool > m_Closed
void HandleAccept(ReactorRunnable *reactor, WorldSocketHandle socket, boost::asio::ip::tcp::endpoint const &remoteEndpoint, boost::system::error_code const &error)
Handler that can communicate over stream sockets.
Definition WorldSocket.h:51
#define sWorldSocketMgr
boost::asio::ip::tcp::socket WorldSocketHandle
Definition WorldSocket.h:31
void CloseTcpAcceptor(boost::asio::ip::tcp::acceptor &acceptor)
bool OpenTcpAcceptor(boost::asio::io_context &ioContext, boost::asio::ip::tcp::acceptor &acceptor, uint16 port, std::string const &bindAddress, char const *logFilter, char const *logName)