Support for RTA and game regions via gateway sockets.

This commit is contained in:
HikikoMarmy
2025-06-15 00:18:08 +01:00
parent cb1daf15a6
commit 63a0978cbe
2 changed files with 177 additions and 66 deletions

View File

@@ -5,17 +5,24 @@
// ║ ║ ║╠╩╗╠╩╗╚╦╝ ╚═╗║╣ ╠╦╝╚╗╔╝║╣ ╠╦╝ // ║ ║ ║╠╩╗╠╩╗╚╦╝ ╚═╗║╣ ╠╦╝╚╗╔╝║╣ ╠╦╝
// ╩═╝╚═╝╚═╝╚═╝ ╩ ╚═╝╚═╝╩╚═ ╚╝ ╚═╝╩╚═ // ╩═╝╚═╝╚═╝╚═╝ ╩ ╚═╝╚═╝╩╚═ ╚╝ ╚═╝╩╚═
#include "../global_define.h"
#include "../Network/Events.h"
#include "LobbyServer.h" #include "LobbyServer.h"
#include "../Game/RealmUserManager.h"
#include "../Network/Events.h"
#include "../../logging.h"
LobbyServer::LobbyServer() LobbyServer::LobbyServer()
{ {
m_running = false; m_running = false;
m_conSocket = INVALID_SOCKET; m_conSocket = INVALID_SOCKET;
m_rtaSocket = INVALID_SOCKET; m_rtaSocket = INVALID_SOCKET;
for( int i = 0; i < 8; i++ )
{
m_conGatewaySocket[ i ] = INVALID_SOCKET;
m_rtaGatewaySocket[ i ] = INVALID_SOCKET;
}
m_clientSockets.clear(); m_clientSockets.clear();
m_recvBuffer.resize( 1024 ); m_recvBuffer.resize( 1024 );
} }
@@ -23,27 +30,47 @@ LobbyServer::LobbyServer()
LobbyServer::~LobbyServer() LobbyServer::~LobbyServer()
{ {
Log::Info( "Lobby Server stopped." ); Log::Info( "Lobby Server stopped." );
if( m_conSocket != INVALID_SOCKET )
for( auto &socket : m_clientSockets )
{ {
closesocket( m_conSocket ); if( socket->fd != INVALID_SOCKET )
m_conSocket = INVALID_SOCKET; {
closesocket( socket->fd );
}
} }
if( m_rtaSocket != INVALID_SOCKET )
for( auto &sock : m_conGatewaySocket )
{ {
closesocket( m_rtaSocket ); if( sock != INVALID_SOCKET )
m_rtaSocket = INVALID_SOCKET; {
closesocket( sock );
}
}
for( auto &sock : m_rtaGatewaySocket )
{
if( sock != INVALID_SOCKET )
{
closesocket( sock );
}
} }
} }
void LobbyServer::Start( std::string ip ) void LobbyServer::Start( std::string ip )
{ {
m_conSocket = OpenNetworkSocket( ip, 40801 ); for( int i = 0; i < 8; i++ )
if( m_conSocket == INVALID_SOCKET ) {
return; m_conGatewaySocket[ i ] = OpenNetworkSocket( ip, 40800 + i );
if( m_conGatewaySocket[ i ] == INVALID_SOCKET )
return;
m_rtaSocket = OpenNetworkSocket( ip, 40810 ); m_rtaGatewaySocket[ i ] = OpenNetworkSocket( ip, 40810 + i );
if( m_rtaSocket == INVALID_SOCKET ) if( m_rtaGatewaySocket[ i ] == INVALID_SOCKET )
return; return;
}
m_conSocket = OpenNetworkSocket( ip, 40900 );
m_rtaSocket = OpenNetworkSocket( ip, 40910 );
m_running = true; m_running = true;
m_thread = std::thread( &LobbyServer::Run, this ); m_thread = std::thread( &LobbyServer::Run, this );
@@ -74,11 +101,20 @@ SOCKET LobbyServer::OpenNetworkSocket( std::string ip, int32_t port )
service.sin_port = htons( port ); service.sin_port = htons( port );
if( ip == "0.0.0.0" ) if( ip == "0.0.0.0" )
service.sin_addr.s_addr = ADDR_ANY; {
service.sin_addr.s_addr = INADDR_ANY;
}
else else
service.sin_addr.s_addr = inet_addr( ip.c_str() ); {
if( InetPtonA( AF_INET, ip.c_str(), &service.sin_addr ) != 1 )
{
Log::Error( "Invalid IP address format: %s", ip.c_str() );
closesocket( sock );
return INVALID_SOCKET;
}
}
if( bind( sock, ( SOCKADDR * )&service, sizeof( service ) ) == SOCKET_ERROR ) if( bind( sock, reinterpret_cast< SOCKADDR * >( &service ), sizeof( service ) ) == SOCKET_ERROR )
{ {
Log::Error( "bind() failed on port %u", port ); Log::Error( "bind() failed on port %u", port );
closesocket( sock ); closesocket( sock );
@@ -112,20 +148,31 @@ void LobbyServer::Run()
FD_SET( m_conSocket, &readSet ); FD_SET( m_conSocket, &readSet );
FD_SET( m_rtaSocket, &readSet ); FD_SET( m_rtaSocket, &readSet );
CheckSocketProblem(); for( auto &sock : m_conGatewaySocket )
{
if( sock != INVALID_SOCKET )
{
FD_SET( sock, &readSet );
}
}
auto maxfd = std::max< SOCKET >( m_conSocket, m_rtaSocket ); for( auto &sock : m_rtaGatewaySocket )
{
if( sock != INVALID_SOCKET )
{
FD_SET( sock, &readSet );
}
}
CheckSocketProblem();
for( const auto &client : m_clientSockets ) for( const auto &client : m_clientSockets )
{ {
FD_SET( client->fd, &readSet ); FD_SET( client->fd, &readSet );
FD_SET( client->fd, &writeSet ); FD_SET( client->fd, &writeSet );
if( client->fd > maxfd )
maxfd = client->fd;
} }
auto result = select( static_cast< int >( maxfd + 1 ), &readSet, &writeSet, NULL, &timeout ); auto result = select( 0, &readSet, &writeSet, NULL, &timeout );
if( result == SOCKET_ERROR ) if( result == SOCKET_ERROR )
{ {
@@ -133,14 +180,30 @@ void LobbyServer::Run()
continue; continue;
} }
for( auto &sock : m_conGatewaySocket )
{
if( sock != INVALID_SOCKET && FD_ISSET( sock, &readSet ) )
{
AcceptGateway( sock, RealmGameType::CHAMPIONS_OF_NORRATH );
}
}
for( auto &sock : m_rtaGatewaySocket )
{
if( sock != INVALID_SOCKET && FD_ISSET( sock, &readSet ) )
{
AcceptGateway( sock, RealmGameType::RETURN_TO_ARMS );
}
}
if( FD_ISSET( m_conSocket, &readSet ) ) if( FD_ISSET( m_conSocket, &readSet ) )
{ {
AcceptNewClient( m_conSocket, RealmClientType::CHAMPIONS_OF_NORRATH ); AcceptClient( m_conSocket, RealmGameType::CHAMPIONS_OF_NORRATH );
} }
if( FD_ISSET( m_rtaSocket, &readSet ) ) if( FD_ISSET( m_rtaSocket, &readSet ) )
{ {
AcceptNewClient( m_rtaSocket, RealmClientType::RETURN_TO_ARMS ); AcceptClient( m_rtaSocket, RealmGameType::RETURN_TO_ARMS );
} }
for( auto &client : m_clientSockets ) for( auto &client : m_clientSockets )
@@ -161,20 +224,29 @@ void LobbyServer::Run()
void LobbyServer::CheckSocketProblem() void LobbyServer::CheckSocketProblem()
{ {
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
for( auto it = m_clientSockets.begin(); it != m_clientSockets.end(); ) for( auto it = m_clientSockets.begin(); it != m_clientSockets.end(); )
{ {
auto duration = std::chrono::duration_cast< std::chrono::seconds >( now - ( *it )->last_recv_time ); auto &socket = *it;
auto elapsed = std::chrono::duration_cast< std::chrono::seconds >( now - socket->last_recv_time );
if( duration.count() > 30 ) // Check for client timeouts
if( elapsed.count() > 30 )
{ {
( *it )->flag.disconnected = true; socket->flag.disconnected_forced = true;
Log::Info( "[LOBBY] Client Timeout : (%s)", ( *it )->remote_ip.c_str() ); Log::Info( "[LOBBY] Client Timeout : (%s)", socket->remote_ip.c_str() );
} }
if( ( *it )->flag.disconnected ) // Check if we're waiting to disconnect after sending buffered data
if( socket->flag.disconnected_wait && socket->m_pendingWriteBuffer.empty() )
{ {
RealmUserManager::Get().RemoveUser( ( *it ) ); socket->flag.disconnected_forced = true;
Log::Info( "[LOBBY] Client Disconnected : (%s)", ( *it )->remote_ip.c_str() ); }
if( socket->flag.disconnected_forced )
{
RealmUserManager::Get().RemoveUser( socket );
Log::Info( "[LOBBY] Client Disconnected : (%s)", socket->remote_ip.c_str() );
it = m_clientSockets.erase( it ); it = m_clientSockets.erase( it );
} }
else else
@@ -184,7 +256,29 @@ void LobbyServer::CheckSocketProblem()
} }
} }
void LobbyServer::AcceptNewClient( SOCKET socket, RealmClientType clientType ) void LobbyServer::AcceptGateway( SOCKET socket, RealmGameType clientType )
{
sockaddr_in clientInfo{};
int32_t addrSize = sizeof( clientInfo );
SOCKET clientSocket = accept( socket, ( SOCKADDR * )&clientInfo, &addrSize );
if( clientSocket == INVALID_SOCKET )
{
Log::Error( "accept() failed" );
return;
}
auto new_socket = std::make_shared< RealmSocket >();
new_socket->fd = clientSocket;
new_socket->remote_addr = clientInfo;
new_socket->remote_ip = inet_ntoa( clientInfo.sin_addr );
new_socket->remote_port = ntohs( clientInfo.sin_port );
m_clientSockets.push_back( new_socket );
Log::Info( "New Gateway Client Connected : (%s)", new_socket->remote_ip.c_str() );
}
void LobbyServer::AcceptClient( SOCKET socket, RealmGameType clientType )
{ {
sockaddr_in clientInfo{}; sockaddr_in clientInfo{};
int32_t addrSize = sizeof( clientInfo ); int32_t addrSize = sizeof( clientInfo );
@@ -206,12 +300,12 @@ void LobbyServer::AcceptNewClient( SOCKET socket, RealmClientType clientType )
RealmUserManager::Get().CreateUser( new_socket, clientType ); RealmUserManager::Get().CreateUser( new_socket, clientType );
Log::Info( "[LOBBY] New Client Connected : (%s)", new_socket->remote_ip.c_str() ); Log::Info( "New Client Connected : (%s)", new_socket->remote_ip.c_str() );
} }
void LobbyServer::ReadSocket( sptr_socket socket ) void LobbyServer::ReadSocket( sptr_socket socket )
{ {
if( socket->flag.disconnected ) if( socket->flag.disconnected_wait )
{ {
return; return;
} }
@@ -221,13 +315,13 @@ void LobbyServer::ReadSocket( sptr_socket socket )
if( bytesReceived == SOCKET_ERROR ) if( bytesReceived == SOCKET_ERROR )
{ {
Log::Info( "Socket Error [%d].", WSAGetLastError() ); Log::Info( "Socket Error [%d].", WSAGetLastError() );
socket->flag.disconnected = true; socket->flag.disconnected_wait = true;
return; return;
} }
if( bytesReceived == 0 ) if( bytesReceived == 0 )
{ {
socket->flag.disconnected = true; socket->flag.disconnected_wait = true;
return; return;
} }
@@ -248,7 +342,7 @@ void LobbyServer::ReadSocket( sptr_socket socket )
if( packetSize <= 0 || packetSize > 2048 ) if( packetSize <= 0 || packetSize > 2048 )
{ {
Log::Error( "Invalid packet size: %d. Disconnecting client.", packetSize ); Log::Error( "Invalid packet size: %d. Disconnecting client.", packetSize );
socket->flag.disconnected = true; socket->flag.disconnected_wait = true;
break; break;
} }
@@ -257,7 +351,7 @@ void LobbyServer::ReadSocket( sptr_socket socket )
break; break;
} }
auto stream = std::make_shared< ByteStream >( socket->m_pendingReadBuffer.data() + 4, packetSize - 4 ); auto stream = std::make_shared< ByteBuffer >( socket->m_pendingReadBuffer.data() + 4, packetSize - 4 );
// Erase the packet from the buffer // Erase the packet from the buffer
socket->m_pendingReadBuffer.erase( socket->m_pendingReadBuffer.erase(
@@ -265,7 +359,7 @@ void LobbyServer::ReadSocket( sptr_socket socket )
socket->m_pendingReadBuffer.begin() + packetSize socket->m_pendingReadBuffer.begin() + packetSize
); );
if (stream->data.size() == 0) break; if( stream->m_buffer.size() == 0 ) break;
// Process the packet // Process the packet
HandleRequest( socket, stream ); HandleRequest( socket, stream );
@@ -274,7 +368,7 @@ void LobbyServer::ReadSocket( sptr_socket socket )
void LobbyServer::WriteSocket( sptr_socket socket ) void LobbyServer::WriteSocket( sptr_socket socket )
{ {
if( socket->flag.disconnected || socket->m_pendingWriteBuffer.empty() ) if( socket->flag.disconnected_wait || socket->m_pendingWriteBuffer.empty() )
return; return;
socket->last_send_time = std::chrono::steady_clock::now(); socket->last_send_time = std::chrono::steady_clock::now();
@@ -301,13 +395,13 @@ void LobbyServer::WriteSocket( sptr_socket socket )
break; break;
Log::Error( "Send failed: %d", err ); Log::Error( "Send failed: %d", err );
socket->flag.disconnected = true; socket->flag.disconnected_wait = true;
return; return;
} }
if( bytesSent == 0 ) if( bytesSent == 0 )
{ {
socket->flag.disconnected = true; socket->flag.disconnected_wait = true;
return; return;
} }
@@ -319,6 +413,8 @@ void LobbyServer::WriteSocket( sptr_socket socket )
} }
} }
std::lock_guard< std::mutex > lock( socket->write_mutex );
socket->m_pendingWriteBuffer.erase( socket->m_pendingWriteBuffer.erase(
socket->m_pendingWriteBuffer.begin(), socket->m_pendingWriteBuffer.begin(),
socket->m_pendingWriteBuffer.begin() + totalBytesSent socket->m_pendingWriteBuffer.begin() + totalBytesSent
@@ -331,27 +427,28 @@ void LobbyServer::HandleRequest( sptr_socket socket, sptr_byte_stream stream )
stream->set_position( 0 ); stream->set_position( 0 );
Log::Debug( "Event Request %04X", packetId ); Log::Debug( "Event Request %04X", packetId );
Log::Packet( stream->data, stream->data.size(), false );
//Log::Packet( stream->m_buffer, stream->m_buffer.size(), false );
auto it = REQUEST_EVENT.find( packetId ); auto it = REQUEST_EVENT.find( packetId );
if( it == REQUEST_EVENT.end() ) if( it == REQUEST_EVENT.end() )
{ {
Log::Error( "[LOBBY] Unknown packet id : 0x%04X", packetId ); Log::Error( "[LOBBY] Unknown packet id : 0x%04X", packetId );
Log::Packet( stream->data, stream->data.size(), false ); Log::Packet( stream->m_buffer, stream->m_buffer.size(), false );
return; return;
} }
auto request = it->second(); auto request = it->second();
auto user = RealmUserManager::Get().GetUser( socket ); //auto user = RealmUserManager::Get().FindUserBySocket( socket );
//
//if( user == nullptr )
//{
// Log::Error( "User not found!" );
// socket->flag.disconnected_wait = true;
// return;
//}
if( user == nullptr ) if( auto res = request->ProcessRequest( socket, stream ) )
{
Log::Error( "User not found!" );
socket->flag.disconnected = true;
return;
}
if( auto res = request->ProcessRequest( user, stream ) )
{ {
socket->send( res ); socket->send( res );
} }

View File

@@ -2,26 +2,31 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <thread>
#include <atomic>
#include <vector>
#include <string>
#include <array>
//#define FD_SETSIZE 512
#include <winsock2.h>
#include <ws2tcpip.h>
#include "../Common/Constant.h"
#include "../Common/ByteStream.h"
#include "../Network/RealmSocket.h"
class LobbyServer class LobbyServer
{ {
private: private:
static inline std::unique_ptr< LobbyServer > m_instance;
static inline std::mutex m_mutex;
std::atomic< bool > m_running; std::atomic< bool > m_running;
std::thread m_thread; std::thread m_thread;
public: public:
static LobbyServer& Get() static LobbyServer& Get()
{ {
std::lock_guard< std::mutex > lock( m_mutex ); static LobbyServer instance;
if( m_instance == nullptr ) return instance;
{
m_instance.reset( new LobbyServer() );
}
return *m_instance;
} }
LobbyServer( const LobbyServer & ) = delete; LobbyServer( const LobbyServer & ) = delete;
@@ -37,16 +42,25 @@ public:
} }
private: private:
// These are the main sockets for the lobby server
SOCKET m_conSocket; SOCKET m_conSocket;
SOCKET m_rtaSocket; SOCKET m_rtaSocket;
// Gateway sockets. 1 port per region.
std::array< SOCKET, 8 > m_conGatewaySocket;
std::array< SOCKET, 8 > m_rtaGatewaySocket;
// Connected client sockets
std::vector< sptr_socket > m_clientSockets; std::vector< sptr_socket > m_clientSockets;
std::vector< uint8_t > m_recvBuffer; std::vector< uint8_t > m_recvBuffer;
SOCKET OpenNetworkSocket( std::string ip, int32_t port ); SOCKET OpenNetworkSocket( std::string ip, int32_t port );
void Run(); void Run();
void CheckSocketProblem(); void CheckSocketProblem();
void AcceptNewClient( SOCKET socket, RealmClientType clientType ); void AcceptGateway( SOCKET socket, RealmGameType clientType );
void AcceptClient( SOCKET socket, RealmGameType clientType );
void ReadSocket( sptr_socket socket ); void ReadSocket( sptr_socket socket );
void WriteSocket( sptr_socket socket ); void WriteSocket( sptr_socket socket );
void HandleRequest( sptr_socket socket, sptr_byte_stream stream ); void HandleRequest( sptr_socket socket, sptr_byte_stream stream );