Initial Commit

This commit is contained in:
HikikoMarmy
2024-12-15 09:09:50 +00:00
parent d94096c147
commit 8b154e614f
49 changed files with 4315 additions and 1 deletions

11
game/client.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "..\global_define.h"
CClient::CClient()
{
}
CClient::~CClient()
{
}

15
game/client.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include "../misc/Encryptor.h"
class CClient {
public:
CClient();
~CClient();
sptr_socket socket;
Encryptor encryptor;
};
typedef std::shared_ptr< CClient > sptr_client;

48
game/client_manager.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include "../global_define.h"
// Spawn a new client
bool CClientManager::spawn_new( sptr_socket socket, sptr_client &ret )
{
sptr_client client = std::make_shared< CClient >();
client->socket = socket;
// The result if the 'insert'
std::pair< std::map< SOCKET, sptr_client >::iterator, bool > result;
// Insert the client into the map - keyed by its socket.
result = client_map.insert( std::make_pair( socket->fd, client ) );
ret = ( *result.first ).second;
return result.second;
}
// Find an existing client in the service
bool CClientManager::get_client( sptr_socket socket, sptr_client &ret )
{
std::map< SOCKET, sptr_client >::iterator it;
if( client_map.end() == ( it = client_map.find( socket->fd ) ) ) return false;
ret = ( *it ).second;
return true;
}
// Find and remove a client by its socket
void CClientManager::remove_client( sptr_socket socket )
{
std::map< SOCKET, sptr_client >::iterator it;
if( client_map.end() == ( it = client_map.find( socket->fd ) ) ) return;
client_map.erase( it );
}
// Remove a client and mark the socket for disconnection
void CClientManager::disconnect( sptr_client c )
{
std::map< SOCKET, sptr_client >::iterator it;
if( client_map.end() == ( it = client_map.find( c->socket->fd ) ) ) return;
c->socket->flag.disconnected = 1;
client_map.erase( it );
}

29
game/client_manager.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <map>
class CClientManager {
public:
CClientManager()
{
client_map.clear();
}
~CClientManager()
{
client_map.clear();
}
// Add a new client
bool spawn_new( sptr_socket socket, sptr_client &ret );
// Fine an existing client in the service
bool get_client( sptr_socket socket, sptr_client &ret );
// Remove an existing client in the service
void remove_client( sptr_socket socket );
void disconnect( sptr_client client );
std::map< SOCKET, sptr_client > client_map;
};