work on ClientModule, add ChatServer
This commit is contained in:
@@ -19,7 +19,7 @@ namespace SMEvent {
|
||||
|
||||
DataRecievedEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||
|
||||
operator std::string() const { return "DataRecievedEvent"; }
|
||||
operator std::string() const { return "SMEvent::DataRecievedEvent"; }
|
||||
|
||||
ISteamNetworkingMessage* msg;
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace SMEvent {
|
||||
|
||||
DataSentEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||
|
||||
operator std::string() const { return "DataSentEvent"; }
|
||||
operator std::string() const { return "SMEvent::DataSentEvent"; }
|
||||
|
||||
ISteamNetworkingMessage* msg;
|
||||
};
|
||||
@@ -51,7 +51,7 @@ namespace SMEvent {
|
||||
|
||||
ConnectionStatusChangedEvent(SteamNetConnectionStatusChangedCallback_t* i) : info(i) {}
|
||||
|
||||
operator std::string() const { return "ConnectionStatusChangedEvent"; }
|
||||
operator std::string() const { return "SMEvent::ConnectionStatusChangedEvent"; }
|
||||
|
||||
SteamNetConnectionStatusChangedCallback_t* info;
|
||||
};
|
||||
|
||||
@@ -5,18 +5,20 @@ ServerModule::ServerModule(Archimedes::App* a, void* h) : Archimedes::Module(a,
|
||||
}
|
||||
|
||||
ServerModule::~ServerModule() {
|
||||
app->removeEventType(SMEvent::DataRecievedEvent());
|
||||
app->removeEventType(SMEvent::DataSentEvent());
|
||||
app->removeEventType(SMEvent::ConnectionStatusChangedEvent());
|
||||
if(app) {
|
||||
app->unregisterEvent(SMEvent::DataRecievedEvent());
|
||||
app->unregisterEvent(SMEvent::DataSentEvent());
|
||||
app->unregisterEvent(SMEvent::ConnectionStatusChangedEvent());
|
||||
|
||||
GameNetworkingSockets_Kill();
|
||||
GameNetworkingSockets_Kill();
|
||||
}
|
||||
}
|
||||
|
||||
void ServerModule::onLoad() {
|
||||
|
||||
app->addEventType(SMEvent::DataRecievedEvent());
|
||||
app->addEventType(SMEvent::DataSentEvent());
|
||||
app->addEventType(SMEvent::ConnectionStatusChangedEvent());
|
||||
app->registerEvent(SMEvent::DataRecievedEvent());
|
||||
app->registerEvent(SMEvent::DataSentEvent());
|
||||
app->registerEvent(SMEvent::ConnectionStatusChangedEvent());
|
||||
|
||||
SteamDatagramErrMsg errMsg;
|
||||
|
||||
@@ -28,26 +30,38 @@ void ServerModule::onLoad() {
|
||||
|
||||
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::startServer(int p) {
|
||||
if(!running) {
|
||||
port = p;
|
||||
|
||||
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 );
|
||||
|
||||
running = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerModule::stopServer() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
void ServerModule::run() {
|
||||
if(running) {
|
||||
pollIncomingData();
|
||||
PollConnectionStateChanges();
|
||||
PollLocalUserInput();
|
||||
} else if(port >= 0) {
|
||||
|
||||
interface->CloseListenSocket(listenSock);
|
||||
@@ -56,18 +70,16 @@ void ServerModule::run() {
|
||||
interface->DestroyPollGroup(pollGroup);
|
||||
pollGroup = k_HSteamNetPollGroup_Invalid;
|
||||
|
||||
app->stopModule(getName());
|
||||
port = -1; //just in case
|
||||
port = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerModule::OnSteamNetConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t *pInfo ) {
|
||||
app->emitEvent(new SMEvent::ConnectionStatusChangedEvent(pInfo));
|
||||
}
|
||||
|
||||
bool ServerModule::onEvent(const Archimedes::Event& event) {
|
||||
|
||||
if(eventsToHandle & SMEventEnum::ConnectionStatusChanged && app->getEventType(event) == app->getEventType("ConnectionStatusChangedEvent")) {
|
||||
unsigned int type = app->getEventType(event);
|
||||
|
||||
if(eventsToHandle & SMEventEnum::ConnectionStatusChanged && type == app->getEventType("ConnectionStatusChangedEvent")) {
|
||||
|
||||
SMEvent::ConnectionStatusChangedEvent& e = (SMEvent::ConnectionStatusChangedEvent&) event;
|
||||
|
||||
@@ -93,18 +105,18 @@ bool ServerModule::onEvent(const Archimedes::Event& event) {
|
||||
|
||||
// Select appropriate log messages
|
||||
//const char *pszDebugLogAction;
|
||||
if ( e.info->m_info.m_eState == k_ESteamNetworkingConnectionState_ProblemDetectedLocally )
|
||||
/*if ( e.info->m_info.m_eState == k_ESteamNetworkingConnectionState_ProblemDetectedLocally )
|
||||
{
|
||||
//pszDebugLogAction = "problem detected locally";
|
||||
//sprintf( temp, "Alas, %s hath fallen into shadow. (%s)", itClient->second.name.c_str(), e.info->m_info.m_szEndDebug );
|
||||
pszDebugLogAction = "problem detected locally";
|
||||
sprintf( temp, "Alas, %s hath fallen into shadow. (%s)", itClient->second.name.c_str(), e.info->m_info.m_szEndDebug );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Note that here we could check the reason code to see if
|
||||
// it was a "usual" connection or an "unusual" one.
|
||||
//pszDebugLogAction = "closed by peer"; (void)pszDebugLogAction;
|
||||
//sprintf( temp, "%s hath departed", itClient->second.name.c_str() );
|
||||
}
|
||||
pszDebugLogAction = "closed by peer"; (void)pszDebugLogAction;
|
||||
sprintf( temp, "%s hath departed", itClient->second.name.c_str() );
|
||||
}*/
|
||||
|
||||
// Spew something to our own log. Note that because we put their nick
|
||||
// as the connection description, it will show up, along with their
|
||||
@@ -180,9 +192,9 @@ bool ServerModule::onEvent(const Archimedes::Event& event) {
|
||||
}
|
||||
return true;
|
||||
|
||||
} else if(eventsToHandle & SMEventEnum::DataRecieved && app->getEventType(event) == app->getEventType("DataRecievedEvent")) {
|
||||
} else if(eventsToHandle & SMEventEnum::DataRecieved && type == app->getEventType("DataRecievedEvent")) {
|
||||
return true;
|
||||
} else if(eventsToHandle & SMEventEnum::DataSent && app->getEventType(event) == app->getEventType("DataSentEvent")) {
|
||||
} else if(eventsToHandle & SMEventEnum::DataSent && type == app->getEventType("DataSentEvent")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifndef SERVERMODULE_H
|
||||
#define SERVERMODULE_H
|
||||
|
||||
#include "Archimedes.h"
|
||||
|
||||
#include <steam/steamnetworkingsockets.h>
|
||||
@@ -9,6 +12,9 @@ class ServerModule : public Archimedes::Module {
|
||||
|
||||
public:
|
||||
ServerModule(Archimedes::App*, void*);
|
||||
|
||||
ServerModule() { name = "ServerModule"; }
|
||||
|
||||
~ServerModule();
|
||||
void onLoad();
|
||||
bool onEvent(const Archimedes::Event&);
|
||||
@@ -26,7 +32,11 @@ class ServerModule : public Archimedes::Module {
|
||||
|
||||
void shouldHandleEvents(unsigned int events) { eventsToHandle = events; }
|
||||
|
||||
void sendData() {}
|
||||
void sendReliable(HSteamNetConnection client, const void* data, uint32 byteCount) {
|
||||
interface->SendMessageToConnection(client, data, byteCount, k_nSteamNetworkingSend_Reliable, nullptr);
|
||||
|
||||
}
|
||||
|
||||
void pollIncomingData();
|
||||
|
||||
private:
|
||||
@@ -48,16 +58,21 @@ class ServerModule : public Archimedes::Module {
|
||||
callbackInstance->OnSteamNetConnectionStatusChanged( pInfo );
|
||||
}
|
||||
|
||||
void OnSteamNetConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t *pInfo );
|
||||
|
||||
void OnSteamNetConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t *pInfo ) {
|
||||
app->emitEvent(new SMEvent::ConnectionStatusChangedEvent(pInfo));
|
||||
}
|
||||
|
||||
std::map<HSteamNetConnection, unsigned int> clients;
|
||||
|
||||
void PollConnectionStateChanges();
|
||||
void PollLocalUserInput();
|
||||
void PollConnectionStateChanges() {
|
||||
callbackInstance = this;
|
||||
interface->RunCallbacks();
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef SERVERMODULE_DYNAMIC
|
||||
#define MODULE_TYPE ServerModule
|
||||
#include "endModule.h"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user