work on ClientModule, add ChatServer
This commit is contained in:
@@ -60,13 +60,13 @@ namespace Archimedes {
|
|||||||
|
|
||||||
unsigned int getEventType(std::string event) { return eventTypes[event]; }
|
unsigned int getEventType(std::string event) { return eventTypes[event]; }
|
||||||
|
|
||||||
void addEventType(std::string type) {
|
void registerEvent(std::string type) {
|
||||||
//only add each type once
|
//only add each type once
|
||||||
if(eventTypes.find(type) == eventTypes.end())
|
if(eventTypes.find(type) == eventTypes.end())
|
||||||
eventTypes[type] = nextEventType++;
|
eventTypes[type] = nextEventType++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeEventType(std::string type) {
|
void unregisterEvent(std::string type) {
|
||||||
//only erase registered types
|
//only erase registered types
|
||||||
auto it = eventTypes.find(type);
|
auto it = eventTypes.find(type);
|
||||||
if(it != eventTypes.end())
|
if(it != eventTypes.end())
|
||||||
|
|||||||
@@ -19,13 +19,15 @@ namespace Archimedes {
|
|||||||
|
|
||||||
Module(App* a, void* h) : app(a), handle(h) {}
|
Module(App* a, void* h) : app(a), handle(h) {}
|
||||||
|
|
||||||
|
Module() : app(nullptr), handle(nullptr) {}
|
||||||
|
|
||||||
virtual ~Module() {}
|
virtual ~Module() {}
|
||||||
|
|
||||||
virtual void run() {}
|
virtual void run() {}
|
||||||
virtual bool onEvent(const Event& e) { return false; }
|
virtual bool onEvent(const Event& e) { return false; }
|
||||||
virtual void onLoad() {};
|
virtual void onLoad() {};
|
||||||
|
|
||||||
std::string getName() const { return name; }
|
operator std::string() const { return name; }
|
||||||
void* getHandle() { return handle; }
|
void* getHandle() { return handle; }
|
||||||
|
|
||||||
//std::any getData(std::string s) { return data[s.c_str()]; };
|
//std::any getData(std::string s) { return data[s.c_str()]; };
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace CMEvent {
|
|||||||
|
|
||||||
DataRecievedEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
DataRecievedEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||||
|
|
||||||
operator std::string() const { return "DataRecievedEvent"; }
|
operator std::string() const { return "CMEvent::DataRecievedEvent"; }
|
||||||
|
|
||||||
ISteamNetworkingMessage* msg;
|
ISteamNetworkingMessage* msg;
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ namespace CMEvent {
|
|||||||
|
|
||||||
DataSentEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
DataSentEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||||
|
|
||||||
operator std::string() const { return "DataSentEvent"; }
|
operator std::string() const { return "CMEvent::DataSentEvent"; }
|
||||||
|
|
||||||
ISteamNetworkingMessage* msg;
|
ISteamNetworkingMessage* msg;
|
||||||
};
|
};
|
||||||
@@ -41,7 +41,7 @@ namespace CMEvent {
|
|||||||
|
|
||||||
ConnectionStatusChangedEvent(SteamNetConnectionStatusChangedCallback_t* i) : info(i) {}
|
ConnectionStatusChangedEvent(SteamNetConnectionStatusChangedCallback_t* i) : info(i) {}
|
||||||
|
|
||||||
operator std::string() const { return "ConnectionStatusChangedEvent"; }
|
operator std::string() const { return "CMEvent::ConnectionStatusChangedEvent"; }
|
||||||
|
|
||||||
SteamNetConnectionStatusChangedCallback_t* info;
|
SteamNetConnectionStatusChangedCallback_t* info;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,17 +5,137 @@ ClientModule::ClientModule(Archimedes::App* a, void* h) : Archimedes::Module(a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ClientModule::~ClientModule() {
|
ClientModule::~ClientModule() {
|
||||||
GameNetworkingSockets_Kill();
|
if(app) {
|
||||||
|
app->unregisterEvent(CMEvent::DataRecievedEvent());
|
||||||
|
app->unregisterEvent(CMEvent::DataSentEvent());
|
||||||
|
app->unregisterEvent(CMEvent::ConnectionStatusChangedEvent());
|
||||||
|
|
||||||
|
GameNetworkingSockets_Kill();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientModule::onLoad() {
|
void ClientModule::onLoad() {
|
||||||
|
|
||||||
|
app->registerEvent(CMEvent::DataSentEvent());
|
||||||
|
app->registerEvent(CMEvent::DataRecievedEvent());
|
||||||
|
app->registerEvent(CMEvent::ConnectionStatusChangedEvent());
|
||||||
|
|
||||||
SteamDatagramErrMsg errMsg;
|
SteamDatagramErrMsg errMsg;
|
||||||
if ( !GameNetworkingSockets_Init( nullptr, errMsg ) ) {
|
if ( !GameNetworkingSockets_Init( nullptr, errMsg ) ) {
|
||||||
//FatalError( "GameNetworkingSockets_Init failed. %s", errMsg );
|
//FatalError( "GameNetworkingSockets_Init failed. %s", errMsg );
|
||||||
std::cerr << "GameNetworkingSockets_Init() Failed: " << errMsg << std::endl;
|
std::cerr << "GameNetworkingSockets_Init() Failed: " << errMsg << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
//g_logTimeZero = SteamNetworkingUtils()->GetLocalTimestamp();
|
|
||||||
|
|
||||||
//SteamNetworkingUtils()->SetDebugOutputFunction( k_ESteamNetworkingSocketsDebugOutputType_Msg, DebugOutput );
|
//SteamNetworkingUtils()->SetDebugOutputFunction( k_ESteamNetworkingSocketsDebugOutputType_Msg, DebugOutput );
|
||||||
|
|
||||||
|
interface = SteamNetworkingSockets();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientModule::startClient(SteamNetworkingIPAddr& serverAddr) {
|
||||||
|
if(!running) {
|
||||||
|
// Start connecting
|
||||||
|
char szAddr[ SteamNetworkingIPAddr::k_cchMaxString ];
|
||||||
|
serverAddr.ToString( szAddr, sizeof(szAddr), true );
|
||||||
|
//Printf( "Connecting to chat server at %s", szAddr );
|
||||||
|
SteamNetworkingConfigValue_t opt;
|
||||||
|
opt.SetPtr( k_ESteamNetworkingConfig_Callback_ConnectionStatusChanged, (void*)SteamNetConnectionStatusChangedCallback );
|
||||||
|
connection = interface->ConnectByIPAddress( serverAddr, 1, &opt );
|
||||||
|
if ( connection == k_HSteamNetConnection_Invalid )
|
||||||
|
std::cerr << "Failed to create connection\n";
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientModule::stopClient() { running = false; }
|
||||||
|
|
||||||
|
void ClientModule::run() {
|
||||||
|
|
||||||
|
if(running) {
|
||||||
|
pollIncomingData();
|
||||||
|
PollConnectionStateChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClientModule::onEvent(const Archimedes::Event& event) {
|
||||||
|
|
||||||
|
unsigned int type = app->getEventType(event);
|
||||||
|
|
||||||
|
if(eventsToHandle & CMEventEnum::ConnectionStatusChanged && type == app->getEventType("ConnectionStatusChangedEvent")) {
|
||||||
|
|
||||||
|
CMEvent::ConnectionStatusChangedEvent& e = (CMEvent::ConnectionStatusChangedEvent&) event;
|
||||||
|
|
||||||
|
switch(e.info->m_info.m_eState) {
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_None:
|
||||||
|
// NOTE: We will get callbacks here when we destroy connections. You can ignore these.
|
||||||
|
break;
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_ClosedByPeer:
|
||||||
|
case k_ESteamNetworkingConnectionState_ProblemDetectedLocally:
|
||||||
|
{
|
||||||
|
// Ignore if they were not previously connected. (If they disconnected
|
||||||
|
// before we accepted the connection.)
|
||||||
|
if ( e.info->m_eOldState == k_ESteamNetworkingConnectionState_Connected )
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
assert( e.info->m_eOldState == k_ESteamNetworkingConnectionState_Connecting );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up the connection. This is important!
|
||||||
|
// The connection is "closed" in the network sense, but
|
||||||
|
// it has not been destroyed. We must close it on our end, too
|
||||||
|
// to finish up. The reason information do not matter in this case,
|
||||||
|
// and we cannot linger because it's already closed on the other end,
|
||||||
|
// so we just pass 0's.
|
||||||
|
//
|
||||||
|
// OnDisconnect
|
||||||
|
interface->CloseConnection( e.info->m_hConn, 0, nullptr, false );
|
||||||
|
connection = k_HSteamNetConnection_Invalid;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_Connecting:
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_Connected:
|
||||||
|
//OnConnect
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Silences -Wswitch
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} else if(eventsToHandle & CMEventEnum::DataRecieved && type == app->getEventType("DataRecievedEvent")) {
|
||||||
|
return true;
|
||||||
|
} else if(eventsToHandle & CMEventEnum::DataSent && type == app->getEventType("DataSentEvent")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientModule::pollIncomingData() {
|
||||||
|
|
||||||
|
while ( running )
|
||||||
|
{
|
||||||
|
ISteamNetworkingMessage *pIncomingMsg = nullptr;
|
||||||
|
int numMsgs = interface->ReceiveMessagesOnConnection( connection, &pIncomingMsg, 1 );
|
||||||
|
if ( numMsgs == 0 )
|
||||||
|
break;
|
||||||
|
if ( numMsgs < 0 )
|
||||||
|
std::cerr << "Error checking for messages" << std::endl;
|
||||||
|
assert( numMsgs == 1 && pIncomingMsg );
|
||||||
|
assert( pIncomingMsg->m_conn == connection );
|
||||||
|
|
||||||
|
app->emitEvent(new CMEvent::DataRecievedEvent(pIncomingMsg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include <steam/steamnetworkingsockets.h>
|
#include <steam/steamnetworkingsockets.h>
|
||||||
#include <steam/isteamnetworkingutils.h>
|
#include <steam/isteamnetworkingutils.h>
|
||||||
|
|
||||||
|
#include "ClientEvents.h"
|
||||||
|
|
||||||
class ClientModule : public Archimedes::Module {
|
class ClientModule : public Archimedes::Module {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -10,9 +12,49 @@ class ClientModule : public Archimedes::Module {
|
|||||||
~ClientModule();
|
~ClientModule();
|
||||||
void onLoad();
|
void onLoad();
|
||||||
void run();
|
void run();
|
||||||
|
bool onEvent(const Archimedes::Event&);
|
||||||
|
|
||||||
|
void startClient(SteamNetworkingIPAddr&);
|
||||||
|
void stopClient();
|
||||||
|
|
||||||
|
void sendReliable(const void* data, uint32 byteCount) {
|
||||||
|
interface->SendMessageToConnection(connection, data, byteCount, k_nSteamNetworkingSend_Reliable, nullptr);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void pollIncomingData();
|
||||||
|
|
||||||
|
void shouldHandleEvents(unsigned int events) { eventsToHandle = events; }
|
||||||
|
|
||||||
|
enum CMEventEnum {
|
||||||
|
None = 0,
|
||||||
|
ConnectionStatusChanged = 1 << 0,
|
||||||
|
DataRecieved = 1 << 1,
|
||||||
|
DataSent = 1 << 2
|
||||||
|
};
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
unsigned int eventsToHandle = CMEventEnum::ConnectionStatusChanged | CMEventEnum::DataSent | CMEventEnum::DataRecieved;
|
||||||
|
|
||||||
|
bool running = false;
|
||||||
|
|
||||||
|
ISteamNetworkingSockets* interface;
|
||||||
|
HSteamNetConnection connection;
|
||||||
|
|
||||||
|
inline static ClientModule* callbackInstance = nullptr;
|
||||||
|
|
||||||
|
static void SteamNetConnectionStatusChangedCallback( SteamNetConnectionStatusChangedCallback_t *pInfo ) {
|
||||||
|
callbackInstance->OnSteamNetConnectionStatusChanged( pInfo );
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnSteamNetConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t *pInfo ) {
|
||||||
|
app->emitEvent(new CMEvent::ConnectionStatusChangedEvent(pInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollConnectionStateChanges() {
|
||||||
|
callbackInstance = this;
|
||||||
|
interface->RunCallbacks();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef CLIENTMODULE_DYNAMIC
|
#ifdef CLIENTMODULE_DYNAMIC
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace SMEvent {
|
|||||||
|
|
||||||
DataRecievedEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
DataRecievedEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||||
|
|
||||||
operator std::string() const { return "DataRecievedEvent"; }
|
operator std::string() const { return "SMEvent::DataRecievedEvent"; }
|
||||||
|
|
||||||
ISteamNetworkingMessage* msg;
|
ISteamNetworkingMessage* msg;
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ namespace SMEvent {
|
|||||||
|
|
||||||
DataSentEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
DataSentEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||||
|
|
||||||
operator std::string() const { return "DataSentEvent"; }
|
operator std::string() const { return "SMEvent::DataSentEvent"; }
|
||||||
|
|
||||||
ISteamNetworkingMessage* msg;
|
ISteamNetworkingMessage* msg;
|
||||||
};
|
};
|
||||||
@@ -51,7 +51,7 @@ namespace SMEvent {
|
|||||||
|
|
||||||
ConnectionStatusChangedEvent(SteamNetConnectionStatusChangedCallback_t* i) : info(i) {}
|
ConnectionStatusChangedEvent(SteamNetConnectionStatusChangedCallback_t* i) : info(i) {}
|
||||||
|
|
||||||
operator std::string() const { return "ConnectionStatusChangedEvent"; }
|
operator std::string() const { return "SMEvent::ConnectionStatusChangedEvent"; }
|
||||||
|
|
||||||
SteamNetConnectionStatusChangedCallback_t* info;
|
SteamNetConnectionStatusChangedCallback_t* info;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,18 +5,20 @@ ServerModule::ServerModule(Archimedes::App* a, void* h) : Archimedes::Module(a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServerModule::~ServerModule() {
|
ServerModule::~ServerModule() {
|
||||||
app->removeEventType(SMEvent::DataRecievedEvent());
|
if(app) {
|
||||||
app->removeEventType(SMEvent::DataSentEvent());
|
app->unregisterEvent(SMEvent::DataRecievedEvent());
|
||||||
app->removeEventType(SMEvent::ConnectionStatusChangedEvent());
|
app->unregisterEvent(SMEvent::DataSentEvent());
|
||||||
|
app->unregisterEvent(SMEvent::ConnectionStatusChangedEvent());
|
||||||
|
|
||||||
GameNetworkingSockets_Kill();
|
GameNetworkingSockets_Kill();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerModule::onLoad() {
|
void ServerModule::onLoad() {
|
||||||
|
|
||||||
app->addEventType(SMEvent::DataRecievedEvent());
|
app->registerEvent(SMEvent::DataRecievedEvent());
|
||||||
app->addEventType(SMEvent::DataSentEvent());
|
app->registerEvent(SMEvent::DataSentEvent());
|
||||||
app->addEventType(SMEvent::ConnectionStatusChangedEvent());
|
app->registerEvent(SMEvent::ConnectionStatusChangedEvent());
|
||||||
|
|
||||||
SteamDatagramErrMsg errMsg;
|
SteamDatagramErrMsg errMsg;
|
||||||
|
|
||||||
@@ -28,26 +30,38 @@ void ServerModule::onLoad() {
|
|||||||
|
|
||||||
interface = SteamNetworkingSockets();
|
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 );
|
void ServerModule::startServer(int p) {
|
||||||
if ( listenSock == k_HSteamListenSocket_Invalid )
|
if(!running) {
|
||||||
std::cerr << "Failed to listen on port " << port << std::endl;
|
port = p;
|
||||||
pollGroup = interface->CreatePollGroup();
|
|
||||||
if ( pollGroup == k_HSteamNetPollGroup_Invalid )
|
SteamNetworkingIPAddr serverLocalAddr;
|
||||||
std::cerr << "Failed to listen on port " << port << std::endl;
|
serverLocalAddr.Clear();
|
||||||
//Printf( "Server listening on port %d\n", port );
|
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() {
|
void ServerModule::run() {
|
||||||
if(running) {
|
if(running) {
|
||||||
pollIncomingData();
|
pollIncomingData();
|
||||||
PollConnectionStateChanges();
|
PollConnectionStateChanges();
|
||||||
PollLocalUserInput();
|
|
||||||
} else if(port >= 0) {
|
} else if(port >= 0) {
|
||||||
|
|
||||||
interface->CloseListenSocket(listenSock);
|
interface->CloseListenSocket(listenSock);
|
||||||
@@ -56,18 +70,16 @@ void ServerModule::run() {
|
|||||||
interface->DestroyPollGroup(pollGroup);
|
interface->DestroyPollGroup(pollGroup);
|
||||||
pollGroup = k_HSteamNetPollGroup_Invalid;
|
pollGroup = k_HSteamNetPollGroup_Invalid;
|
||||||
|
|
||||||
app->stopModule(getName());
|
port = -1;
|
||||||
port = -1; //just in case
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerModule::OnSteamNetConnectionStatusChanged( SteamNetConnectionStatusChangedCallback_t *pInfo ) {
|
|
||||||
app->emitEvent(new SMEvent::ConnectionStatusChangedEvent(pInfo));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ServerModule::onEvent(const Archimedes::Event& event) {
|
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;
|
SMEvent::ConnectionStatusChangedEvent& e = (SMEvent::ConnectionStatusChangedEvent&) event;
|
||||||
|
|
||||||
@@ -93,18 +105,18 @@ bool ServerModule::onEvent(const Archimedes::Event& event) {
|
|||||||
|
|
||||||
// Select appropriate log messages
|
// Select appropriate log messages
|
||||||
//const char *pszDebugLogAction;
|
//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";
|
pszDebugLogAction = "problem detected locally";
|
||||||
//sprintf( temp, "Alas, %s hath fallen into shadow. (%s)", itClient->second.name.c_str(), e.info->m_info.m_szEndDebug );
|
sprintf( temp, "Alas, %s hath fallen into shadow. (%s)", itClient->second.name.c_str(), e.info->m_info.m_szEndDebug );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Note that here we could check the reason code to see if
|
// Note that here we could check the reason code to see if
|
||||||
// it was a "usual" connection or an "unusual" one.
|
// it was a "usual" connection or an "unusual" one.
|
||||||
//pszDebugLogAction = "closed by peer"; (void)pszDebugLogAction;
|
pszDebugLogAction = "closed by peer"; (void)pszDebugLogAction;
|
||||||
//sprintf( temp, "%s hath departed", itClient->second.name.c_str() );
|
sprintf( temp, "%s hath departed", itClient->second.name.c_str() );
|
||||||
}
|
}*/
|
||||||
|
|
||||||
// Spew something to our own log. Note that because we put their nick
|
// Spew something to our own log. Note that because we put their nick
|
||||||
// as the connection description, it will show up, along with their
|
// as the connection description, it will show up, along with their
|
||||||
@@ -180,9 +192,9 @@ bool ServerModule::onEvent(const Archimedes::Event& event) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else if(eventsToHandle & SMEventEnum::DataRecieved && app->getEventType(event) == app->getEventType("DataRecievedEvent")) {
|
} else if(eventsToHandle & SMEventEnum::DataRecieved && type == app->getEventType("DataRecievedEvent")) {
|
||||||
return true;
|
return true;
|
||||||
} else if(eventsToHandle & SMEventEnum::DataSent && app->getEventType(event) == app->getEventType("DataSentEvent")) {
|
} else if(eventsToHandle & SMEventEnum::DataSent && type == app->getEventType("DataSentEvent")) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#ifndef SERVERMODULE_H
|
||||||
|
#define SERVERMODULE_H
|
||||||
|
|
||||||
#include "Archimedes.h"
|
#include "Archimedes.h"
|
||||||
|
|
||||||
#include <steam/steamnetworkingsockets.h>
|
#include <steam/steamnetworkingsockets.h>
|
||||||
@@ -9,6 +12,9 @@ class ServerModule : public Archimedes::Module {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
ServerModule(Archimedes::App*, void*);
|
ServerModule(Archimedes::App*, void*);
|
||||||
|
|
||||||
|
ServerModule() { name = "ServerModule"; }
|
||||||
|
|
||||||
~ServerModule();
|
~ServerModule();
|
||||||
void onLoad();
|
void onLoad();
|
||||||
bool onEvent(const Archimedes::Event&);
|
bool onEvent(const Archimedes::Event&);
|
||||||
@@ -26,7 +32,11 @@ class ServerModule : public Archimedes::Module {
|
|||||||
|
|
||||||
void shouldHandleEvents(unsigned int events) { eventsToHandle = events; }
|
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();
|
void pollIncomingData();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -48,16 +58,21 @@ class ServerModule : public Archimedes::Module {
|
|||||||
callbackInstance->OnSteamNetConnectionStatusChanged( pInfo );
|
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;
|
std::map<HSteamNetConnection, unsigned int> clients;
|
||||||
|
|
||||||
void PollConnectionStateChanges();
|
void PollConnectionStateChanges() {
|
||||||
void PollLocalUserInput();
|
callbackInstance = this;
|
||||||
|
interface->RunCallbacks();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef SERVERMODULE_DYNAMIC
|
#ifdef SERVERMODULE_DYNAMIC
|
||||||
#define MODULE_TYPE ServerModule
|
#define MODULE_TYPE ServerModule
|
||||||
#include "endModule.h"
|
#include "endModule.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ Terminal::Terminal(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
|||||||
name = "Terminal";
|
name = "Terminal";
|
||||||
|
|
||||||
ImguiModule* im = new ImguiModule(a, h);
|
ImguiModule* im = new ImguiModule(a, h);
|
||||||
deps[im->getName()] = im;
|
deps[*im] = im;
|
||||||
}
|
}
|
||||||
|
|
||||||
Terminal::~Terminal() {
|
Terminal::~Terminal() {
|
||||||
@@ -22,7 +22,7 @@ Terminal::~Terminal() {
|
|||||||
|
|
||||||
void Terminal::onLoad() {
|
void Terminal::onLoad() {
|
||||||
|
|
||||||
ImguiModule* im = (ImguiModule*) moduleInstances["ImguiModule"];
|
ImguiModule* im = (ImguiModule*) moduleInstances[ImguiModule()];
|
||||||
|
|
||||||
if(!im) {
|
if(!im) {
|
||||||
std::cout << "No ImguiModule for Terminal!\n";
|
std::cout << "No ImguiModule for Terminal!\n";
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class WindowModule : public Archimedes::Module {
|
|||||||
name = "WindowModule";
|
name = "WindowModule";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WindowModule() { name = "WindowModule"; }
|
||||||
|
|
||||||
~WindowModule();
|
~WindowModule();
|
||||||
|
|
||||||
|
|||||||
40
modules/examples/ChatServer/src/ChatServer.cpp
Normal file
40
modules/examples/ChatServer/src/ChatServer.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include "ChatServer.h"
|
||||||
|
|
||||||
|
//void ChatServer::onLoad() {}
|
||||||
|
|
||||||
|
//void ChatServer::run() {}
|
||||||
|
|
||||||
|
bool ChatServer::onEvent(const Archimedes::Event& event) {
|
||||||
|
|
||||||
|
unsigned int type = app->getEventType(event);
|
||||||
|
|
||||||
|
|
||||||
|
/*if(type == app->getEventType("DataSentEvent")) {
|
||||||
|
//we did this?
|
||||||
|
return true;
|
||||||
|
} else */
|
||||||
|
|
||||||
|
if(type == app->getEventType("DataRecievedEvent")) {
|
||||||
|
|
||||||
|
static ServerModule* sm; { sm = (ServerModule*) moduleInstances[ServerModule()]; }
|
||||||
|
|
||||||
|
SMEvent::DataRecievedEvent& e = (SMEvent::DataRecievedEvent&) event;
|
||||||
|
|
||||||
|
static std::string s; s = std::string((const char*)e.msg->m_pData, e.msg->m_cbSize);
|
||||||
|
|
||||||
|
std::cerr << "Data Recieved: " << s << std::endl;
|
||||||
|
|
||||||
|
static std::string res; res = "Message Recieved: " + s;
|
||||||
|
|
||||||
|
sm->sendReliable(e.msg->m_conn, res.c_str(), res.length());
|
||||||
|
|
||||||
|
} /*else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
||||||
|
|
||||||
|
//SMEvent::ConnectionStatusChangedEvent& e = (SMEvent::ConnectionStatusChangedEvent&) event;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
29
modules/examples/ChatServer/src/ChatServer.h
Normal file
29
modules/examples/ChatServer/src/ChatServer.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "Archimedes.h"
|
||||||
|
|
||||||
|
#include "modules/ServerModule/src/ServerModule.h"
|
||||||
|
|
||||||
|
class ChatServer : public Archimedes::Module {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
ChatServer(Archimedes::App* a, void* h) : Module(a, h) {
|
||||||
|
|
||||||
|
name = "ChatServer";
|
||||||
|
|
||||||
|
ServerModule* sm = new ServerModule(a, h);
|
||||||
|
deps[*sm] = sm;
|
||||||
|
sm->shouldHandleEvents(ServerModule::SMEventEnum::ConnectionStatusChanged | ServerModule::SMEventEnum::DataSent);
|
||||||
|
}
|
||||||
|
|
||||||
|
ChatServer() { name = "ChatServer"; }
|
||||||
|
|
||||||
|
~ChatServer() {
|
||||||
|
if(app) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
//void onLoad();
|
||||||
|
|
||||||
|
//void run();
|
||||||
|
|
||||||
|
bool onEvent(const Archimedes::Event&);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user