Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
RARunnable.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
9
10#include "Common.h"
11#include "Config.h"
12#include "Log.h"
14#include "RARunnable.h"
16#include "World.h"
17
18#include "RASocket.h"
19
20#include <boost/asio/error.hpp>
21#include <boost/asio/steady_timer.hpp>
22#include <boost/system/error_code.hpp>
23#include <chrono>
24#include <memory>
25#include <utility>
26
27namespace
28{
29 void AsyncAcceptRA(std::shared_ptr<Skyfire::Asio::IoContextExecutor> const& executor, std::shared_ptr<boost::asio::ip::tcp::acceptor> const& acceptor);
30 void ScheduleStopCheck(std::shared_ptr<Skyfire::Asio::IoContextExecutor> const& executor, std::shared_ptr<boost::asio::ip::tcp::acceptor> const& acceptor,
31 std::shared_ptr<boost::asio::steady_timer> const& timer);
32}
33
35{
36 if (!sConfigMgr->GetBoolDefault("Ra.Enable", false))
37 return;
38
39 uint16 raPort = uint16(sConfigMgr->GetIntDefault("Ra.Port", 3443));
40 std::string stringIp = sConfigMgr->GetStringDefault("Ra.IP", "0.0.0.0");
41
42 std::shared_ptr<Skyfire::Asio::IoContextExecutor> executor(new Skyfire::Asio::IoContextExecutor);
43 std::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor(new boost::asio::ip::tcp::acceptor(executor->GetIoContext()));
44 if (!Skyfire::Net::OpenTcpAcceptor(executor->GetIoContext(), *acceptor, raPort, stringIp, "server.worldserver", "Skyfire RA"))
45 return;
46
47 SF_LOG_INFO("server.worldserver", "Starting Skyfire RA on port %d on %s", raPort, stringIp.c_str());
48
49 AsyncAcceptRA(executor, acceptor);
50
51 std::shared_ptr<boost::asio::steady_timer> stopTimer(new boost::asio::steady_timer(executor->GetIoContext()));
52 ScheduleStopCheck(executor, acceptor, stopTimer);
53
54 executor->Run();
55
57 Skyfire::Net::CancelTimer(*stopTimer);
58
59 SF_LOG_DEBUG("server.worldserver", "Skyfire RA thread exiting");
60}
61
62namespace
63{
64 void AsyncAcceptRA(std::shared_ptr<Skyfire::Asio::IoContextExecutor> const& executor, std::shared_ptr<boost::asio::ip::tcp::acceptor> const& acceptor)
65 {
66 if (!acceptor->is_open())
67 return;
68
69 std::shared_ptr<RASocketHandle> clientSocket(new RASocketHandle(executor->GetIoContext()));
70 acceptor->async_accept(*clientSocket,
71 [executor, acceptor, clientSocket](boost::system::error_code const& error)
72 {
73 if (error)
74 {
75 if (error != boost::asio::error::operation_aborted)
76 SF_LOG_ERROR("commands.ra", "Skyfire RA failed to accept socket, error %d", error.value());
77 }
78 else
79 {
80 boost::system::error_code endpointError;
81 boost::asio::ip::tcp::endpoint remoteEndpoint = clientSocket->remote_endpoint(endpointError);
82 std::string remote = endpointError ? std::string("<unknown>") : remoteEndpoint.address().to_string();
83
84 SF_LOG_INFO("commands.ra", "Incoming connection from %s", remote.c_str());
85
86 std::unique_ptr<RASocketHandle> socketHandle(new RASocketHandle(std::move(*clientSocket)));
87 std::make_shared<RASocket>(executor, std::move(socketHandle), remote)->start();
88 }
89
90 if (!World::IsStopped())
91 AsyncAcceptRA(executor, acceptor);
92 });
93 }
94
95 void ScheduleStopCheck(std::shared_ptr<Skyfire::Asio::IoContextExecutor> const& executor, std::shared_ptr<boost::asio::ip::tcp::acceptor> const& acceptor,
96 std::shared_ptr<boost::asio::steady_timer> const& timer)
97 {
98 timer->expires_after(std::chrono::milliseconds(100));
99 timer->async_wait(
100 [executor, acceptor, timer](boost::system::error_code const& error)
101 {
102 if (error == boost::asio::error::operation_aborted)
103 return;
104
105 if (World::IsStopped())
106 {
108 executor->Stop();
109 return;
110 }
111
112 ScheduleStopCheck(executor, acceptor, timer);
113 });
114 }
115}
#define sConfigMgr
Definition Config.h:64
std::uint16_t uint16
Definition Define.h:78
#define SF_LOG_DEBUG(filterType__,...)
Definition Log.h:134
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
#define SF_LOG_INFO(filterType__,...)
Definition Log.h:137
static bool IsStopped()
Definition World.h:675
boost::asio::ip::tcp::socket RASocketHandle
Definition RASocket.h:28
void CancelTimer(Timer &timer)
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)