work on ClientModule, add ChatServer
This commit is contained in:
@@ -14,7 +14,7 @@ namespace CMEvent {
|
||||
|
||||
DataRecievedEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||
|
||||
operator std::string() const { return "DataRecievedEvent"; }
|
||||
operator std::string() const { return "CMEvent::DataRecievedEvent"; }
|
||||
|
||||
ISteamNetworkingMessage* msg;
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace CMEvent {
|
||||
|
||||
DataSentEvent(ISteamNetworkingMessage* m) : msg(m) {}
|
||||
|
||||
operator std::string() const { return "DataSentEvent"; }
|
||||
operator std::string() const { return "CMEvent::DataSentEvent"; }
|
||||
|
||||
ISteamNetworkingMessage* msg;
|
||||
};
|
||||
@@ -41,7 +41,7 @@ namespace CMEvent {
|
||||
|
||||
ConnectionStatusChangedEvent(SteamNetConnectionStatusChangedCallback_t* i) : info(i) {}
|
||||
|
||||
operator std::string() const { return "ConnectionStatusChangedEvent"; }
|
||||
operator std::string() const { return "CMEvent::ConnectionStatusChangedEvent"; }
|
||||
|
||||
SteamNetConnectionStatusChangedCallback_t* info;
|
||||
};
|
||||
|
||||
@@ -5,17 +5,137 @@ ClientModule::ClientModule(Archimedes::App* a, void* h) : Archimedes::Module(a,
|
||||
}
|
||||
|
||||
ClientModule::~ClientModule() {
|
||||
GameNetworkingSockets_Kill();
|
||||
if(app) {
|
||||
app->unregisterEvent(CMEvent::DataRecievedEvent());
|
||||
app->unregisterEvent(CMEvent::DataSentEvent());
|
||||
app->unregisterEvent(CMEvent::ConnectionStatusChangedEvent());
|
||||
|
||||
GameNetworkingSockets_Kill();
|
||||
}
|
||||
}
|
||||
|
||||
void ClientModule::onLoad() {
|
||||
|
||||
app->registerEvent(CMEvent::DataSentEvent());
|
||||
app->registerEvent(CMEvent::DataRecievedEvent());
|
||||
app->registerEvent(CMEvent::ConnectionStatusChangedEvent());
|
||||
|
||||
SteamDatagramErrMsg errMsg;
|
||||
if ( !GameNetworkingSockets_Init( nullptr, errMsg ) ) {
|
||||
//FatalError( "GameNetworkingSockets_Init failed. %s", errMsg );
|
||||
std::cerr << "GameNetworkingSockets_Init() Failed: " << errMsg << std::endl;
|
||||
}
|
||||
|
||||
//g_logTimeZero = SteamNetworkingUtils()->GetLocalTimestamp();
|
||||
|
||||
//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/isteamnetworkingutils.h>
|
||||
|
||||
#include "ClientEvents.h"
|
||||
|
||||
class ClientModule : public Archimedes::Module {
|
||||
|
||||
public:
|
||||
@@ -10,9 +12,49 @@ class ClientModule : public Archimedes::Module {
|
||||
~ClientModule();
|
||||
void onLoad();
|
||||
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:
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user