ServerEvents

This commit is contained in:
2025-04-15 00:35:31 -05:00
parent ba8d31335e
commit f94cb10412
7 changed files with 123 additions and 19 deletions

View File

@@ -0,0 +1,45 @@
#include "Archimedes.h"
struct Client {
unsigned int id;
};
class RecievedEvent : public Archimedes::Event {
public:
RecievedEvent(Client c, void* buf, unsigned int s) : client(c), buffer(buf), size(s) {}
Client client;
void* buffer;
unsigned int size;
};
class SentEvent : public Archimedes::Event {
public:
SentEvent(Client c, void* buf, unsigned int s) : client(c), buffer(buf), size(s) {}
Client client;
void* buffer;
unsigned int size;
};
class ConnectionStatusChangedEvent : public Archimedes::Event {
public:
ConnectionStatusChangedEvent(Client c, unsigned int s) : client(c), status(s) {}
Client client;
unsigned int status;
};

View File

@@ -10,12 +10,44 @@ ServerModule::~ServerModule() {
void ServerModule::onLoad() {
SteamDatagramErrMsg errMsg;
if ( !GameNetworkingSockets_Init( nullptr, errMsg ) ) {
//FatalError( "GameNetworkingSockets_Init failed. %s", errMsg );
if ( !GameNetworkingSockets_Init( nullptr, errMsg ) ) {
std::cerr << "GameNetworkingSockets_Init() Failed: " << errMsg << std::endl;
}
//g_logTimeZero = SteamNetworkingUtils()->GetLocalTimestamp();
//SteamNetworkingUtils()->SetDebugOutputFunction( k_ESteamNetworkingSocketsDebugOutputType_Msg, DebugOutput );
interface = SteamNetworkingSockets();
SteamNetworkingIPAddr serverLocalAddr;
serverLocalAddr.Clear();
serverLocalAddr.m_port = port;
SteamNetworkingConfigValue_t opt;
opt.SetPtr(k_ESteamNetworkingConfig_Callback_ConnectionStatusChanged, (void*)SteamNetConnectionStatusChangedCallback);
listenSock = interface->CreateListenSocketIP( serverLocalAddr, 1, &opt );
if ( listenSock == k_HSteamListenSocket_Invalid )
std::cerr << "Failed to listen on port " << port << std::endl;
pollGroup = interface->CreatePollGroup();
if ( pollGroup == k_HSteamNetPollGroup_Invalid )
std::cerr << "Failed to listen on port " << port << std::endl;
//Printf( "Server listening on port %d\n", port );
}
void ServerModule::run() {
if(running) {
PollIncomingMessages();
PollConnectionStateChanges();
PollLocalUserInput();
} else if(port >= 0) {
interface->CloseListenSocket(listenSock);
listenSock = k_HSteamListenSocket_Invalid;
interface->DestroyPollGroup(pollGroup);
pollGroup = k_HSteamNetPollGroup_Invalid;
app->stopModule(getName());
port = -1; //just in case
}
}

View File

@@ -3,16 +3,44 @@
#include <steam/steamnetworkingsockets.h>
#include <steam/isteamnetworkingutils.h>
#include "ServerEvents.h"
class ServerModule : public Archimedes::Module {
public:
ServerModule(Archimedes::App*, void*);
~ServerModule();
void onLoad();
bool onEvent(const Archimedes::Event&);
void run();
private:
void startServer(int);
void stopServer();
private:
bool running = false;
int port = -1;
unsigned int numClients = 0;
HSteamListenSocket listenSock;
HSteamNetPollGroup pollGroup;
ISteamNetworkingSockets* interface;
inline static ServerModule *callbackInstance = nullptr;
static void SteamNetConnectionStatusChangedCallback( SteamNetConnectionStatusChangedCallback_t *pInfo ) {
callbackInstance->OnSteamNetConnectionStatusChanged( pInfo );
}
void OnSteamNetConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t *pInfo );
std::map<HSteamNetConnection, Client> clients;
void PollIncomingMessages();
void PollConnectionStateChanges();
void PollLocalUserInput();
};
#ifdef SERVERMODULE_DYNAMIC