101 lines
2.8 KiB
C++
101 lines
2.8 KiB
C++
#ifndef SERVERMODULE_H
|
|
#define SERVERMODULE_H
|
|
|
|
#include "Archimedes.h"
|
|
|
|
#include <steam/steamnetworkingsockets.h>
|
|
#include <steam/isteamnetworkingutils.h>
|
|
|
|
#include "utils/Events/NetworkEvents.h"
|
|
|
|
class ServerModule : public Archimedes::Module {
|
|
|
|
public:
|
|
ServerModule(Archimedes::App*, void*);
|
|
|
|
ServerModule() { name = "ServerModule"; }
|
|
|
|
~ServerModule();
|
|
void onLoad();
|
|
bool onEvent(const Archimedes::Event&);
|
|
void run();
|
|
|
|
void startServer(int);
|
|
void stopServer();
|
|
|
|
enum SMEventEnum {
|
|
None = 0,
|
|
ConnectionStatusChanged = 1 << 0,
|
|
DataRecieved = 1 << 1,
|
|
DataSent = 1 << 2
|
|
};
|
|
|
|
bool isRunning() const { return running; }
|
|
|
|
void shouldHandleEvents(unsigned int events) { eventsToHandle = events; }
|
|
|
|
void sendReliable(HSteamNetConnection client, const void* data, uint32 byteCount) {
|
|
interface->SendMessageToConnection(client, data, byteCount, k_nSteamNetworkingSend_Reliable, nullptr);
|
|
|
|
}
|
|
|
|
void disconnectClient(HSteamNetConnection c) {
|
|
if(clients.find(c) != clients.end()) {
|
|
interface->CloseConnection(c, 0, nullptr, false);
|
|
clients.erase(c);
|
|
}
|
|
}
|
|
|
|
void disconnectAllClients() {
|
|
while(!clients.empty()) {
|
|
disconnectClient(clients.begin()->first);
|
|
}
|
|
}
|
|
|
|
std::map<HSteamNetConnection, unsigned int> getClients(unsigned int* num = nullptr) const {
|
|
if(num) {
|
|
*num = numClients;
|
|
}
|
|
return clients;
|
|
}
|
|
|
|
void pollIncomingData();
|
|
|
|
private:
|
|
//handle all events by default
|
|
unsigned int eventsToHandle = SMEventEnum::ConnectionStatusChanged | SMEventEnum::DataSent | SMEventEnum::DataRecieved;
|
|
|
|
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 ) {
|
|
app->emitEvent(new Archimedes::ConnectionStatusChangedEvent(pInfo));
|
|
}
|
|
|
|
std::map<HSteamNetConnection, unsigned int> clients;
|
|
|
|
void PollConnectionStateChanges() {
|
|
callbackInstance = this;
|
|
interface->RunCallbacks();
|
|
}
|
|
};
|
|
|
|
#ifdef SERVERMODULE_DYNAMIC
|
|
typedef ServerModule mtype;
|
|
#include "endModule.h"
|
|
#endif
|
|
|
|
#endif
|