ServerEvents
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include "utils/Module/Module.h"
|
||||
#include "utils/App/App.h"
|
||||
|
||||
#include "utils/Events/Event.h"
|
||||
|
||||
#include "entryPoint.h"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <future>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <chrono>
|
||||
|
||||
@@ -53,17 +53,26 @@ namespace Archimedes {
|
||||
|
||||
void end() { done = true; }
|
||||
|
||||
bool isDone() const { return done; }
|
||||
|
||||
std::unordered_map<std::string, unsigned int> getEventTypes() const { return eventTypes; }
|
||||
|
||||
void addEventType(std::string type) { eventTypes[type] = nextEventType++; }
|
||||
|
||||
private:
|
||||
|
||||
std::list<std::string>::iterator roInsert;
|
||||
|
||||
inline static App* instance = nullptr;
|
||||
|
||||
unsigned int nextEventType = 0;
|
||||
protected:
|
||||
|
||||
bool done = false;
|
||||
|
||||
std::unordered_map<std::string, Module*> modules;
|
||||
std::unordered_map<std::string, unsigned int> eventTypes;
|
||||
|
||||
std::list<std::string> runOrder;
|
||||
|
||||
std::list<std::string> toClose;
|
||||
|
||||
@@ -9,21 +9,8 @@ namespace Archimedes {
|
||||
|
||||
public:
|
||||
|
||||
enum class Type : unsigned int {
|
||||
None = 0,
|
||||
WindowEvent = 1 << 0,
|
||||
KeyEvent = 1 << 1,
|
||||
MouseMoveEvent = 1 << 2,
|
||||
MouseScrollEvent = 1 << 3,
|
||||
MouseButtonEvent = 1 << 4,
|
||||
PressedEvent = 1 << 5,
|
||||
ReleasedEvent = 1 << 6,
|
||||
WindowCloseEvent = 1 << 7,
|
||||
WindowResizeEvent = 1 << 8,
|
||||
};
|
||||
|
||||
unsigned int type;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
45
modules/ServerModule/src/ServerEvents.h
Normal file
45
modules/ServerModule/src/ServerEvents.h
Normal 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;
|
||||
|
||||
};
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user