1
0
forked from 0ad/0ad

NetServer onChat event and NetClient onDisconnect event added

This was SVN commit r1894.
This commit is contained in:
Simon Brenner 2005-02-02 16:57:15 +00:00
parent a6668c1b22
commit 5d9fe41fec
5 changed files with 87 additions and 64 deletions

View File

@ -6,6 +6,7 @@
#include <scripting/JSConversions.h>
#include <scripting/ScriptableObject.h>
#include <Network/Client.h>
#include <Network/JSEvents.h>
#include <CStr.h>
#include <CLogger.h>
#include <CConsole.h>
@ -16,68 +17,6 @@ CNetClient *g_NetClient=NULL;
extern "C" int fps;
extern CConsole *g_Console;
enum CClientEvents
{
CLIENT_EVENT_START_GAME,
CLIENT_EVENT_CHAT,
CLIENT_EVENT_CONNECT_COMPLETE,
CLIENT_EVENT_DISCONNECT,
CLIENT_EVENT_LAST
};
class CStartGameEvent: public CScriptEvent
{
public:
CStartGameEvent():
CScriptEvent(L"startGame", false, CLIENT_EVENT_START_GAME)
{}
};
class CChatEvent: public CScriptEvent
{
CStrW m_Sender;
CStrW m_Message;
public:
CChatEvent(CStrW sender, CStrW message):
CScriptEvent(L"chat", false, CLIENT_EVENT_CHAT),
m_Sender(sender),
m_Message(message)
{
AddReadOnlyProperty(L"sender", &m_Sender);
AddReadOnlyProperty(L"message", &m_Message);
}
};
class CConnectCompleteEvent: public CScriptEvent
{
CStrW m_Message;
bool m_Success;
public:
CConnectCompleteEvent(CStrW message, bool success):
CScriptEvent(L"connectComplete", false, CLIENT_EVENT_CONNECT_COMPLETE),
m_Message(message),
m_Success(success)
{
AddReadOnlyProperty(L"message", &m_Message);
AddReadOnlyProperty(L"success", &m_Success);
}
};
class CDisconnectEvent: public CScriptEvent
{
CStrW m_Message;
public:
CDisconnectEvent(CStrW message):
CScriptEvent(L"disconnect", false, CLIENT_EVENT_DISCONNECT),
m_Message(message)
{
AddReadOnlyProperty(L"message", &m_Message);
}
};
CNetClient::CNetClient(CGame *pGame, CGameAttributes *pGameAttribs):
CNetSession(ConnectHandler),
m_pLocalPlayer(NULL),
@ -98,6 +37,7 @@ CNetClient::CNetClient(CGame *pGame, CGameAttributes *pGameAttribs):
AddProperty(L"onStartGame", &m_OnStartGame);
AddProperty(L"onChat", &m_OnChat);
AddProperty(L"onConnectComplete", &m_OnConnectComplete);
AddProperty(L"onDisconnect", &m_OnDisconnect);
AddProperty(L"password", &m_Password);
AddProperty(L"playerName", &m_Name);

View File

@ -0,0 +1,66 @@
#ifndef _Network_JSEvents_H
#define _Network_JSEvents_H
enum ENetworkJSEvents
{
NET_JS_EVENT_START_GAME,
NET_JS_EVENT_CHAT,
NET_JS_EVENT_CONNECT_COMPLETE,
NET_JS_EVENT_DISCONNECT,
NET_JS_EVENT_LAST
};
class CStartGameEvent: public CScriptEvent
{
public:
CStartGameEvent():
CScriptEvent(L"startGame", false, NET_JS_EVENT_START_GAME)
{}
};
class CChatEvent: public CScriptEvent
{
CStrW m_Sender;
CStrW m_Message;
public:
CChatEvent(CStrW sender, CStrW message):
CScriptEvent(L"chat", false, NET_JS_EVENT_CHAT),
m_Sender(sender),
m_Message(message)
{
AddReadOnlyProperty(L"sender", &m_Sender);
AddReadOnlyProperty(L"message", &m_Message);
}
};
class CConnectCompleteEvent: public CScriptEvent
{
CStrW m_Message;
bool m_Success;
public:
CConnectCompleteEvent(CStrW message, bool success):
CScriptEvent(L"connectComplete", false, NET_JS_EVENT_CONNECT_COMPLETE),
m_Message(message),
m_Success(success)
{
AddReadOnlyProperty(L"message", &m_Message);
AddReadOnlyProperty(L"success", &m_Success);
}
};
class CDisconnectEvent: public CScriptEvent
{
CStrW m_Message;
public:
CDisconnectEvent(CStrW message):
CScriptEvent(L"disconnect", false, NET_JS_EVENT_DISCONNECT),
m_Message(message)
{
AddReadOnlyProperty(L"message", &m_Message);
}
};
#endif

View File

@ -4,6 +4,7 @@
#include "Network/Server.h"
#include "Network/Network.h"
#include "Network/JSEvents.h"
#include "Game.h"
#include "Player.h"
@ -60,6 +61,8 @@ CNetServer::CNetServer(CGame *pGame, CGameAttributes *pGameAttribs):
AddProperty(L"welcomeMessage", &m_WelcomeMessage);
AddProperty(L"port", &m_Port);
AddProperty(L"onChat", &m_OnChat);
m_pGameAttributes->SetUpdateCallback(AttributeUpdate, this);
m_pGameAttributes->SetPlayerUpdateCallback(PlayerAttributeUpdate, this);
@ -326,3 +329,12 @@ void CNetServer::QueueIncomingCommand(CNetMessage *pMsg)
LOG(NORMAL, LOG_CAT_NET, "CNetServer::QueueIncomingCommand(): %s.", pMsg->GetString().c_str());
QueueMessage(2, pMsg);
}
void CNetServer::OnChat(CStrW from, CStrW message)
{
if (m_OnChat.Defined())
{
CChatEvent evt(from, message);
m_OnChat.DispatchEvent(GetScript(), &evt);
}
}

View File

@ -62,6 +62,8 @@ private:
int m_Port;
CScriptObject m_OnChat;
static CGameAttributes::UpdateCallback AttributeUpdate;
static CPlayer::UpdateCallback PlayerAttributeUpdate;
@ -87,6 +89,9 @@ protected:
// by the caller.
void QueueIncomingCommand(CNetMessage *pMsg);
// Call the JS callback for incoming chat messages
void OnChat(CStrW from, CStrW message);
// OVERRIDES FROM CServerSocket
virtual void OnAccept(const CSocketAddress &);

View File

@ -172,8 +172,8 @@ bool CNetServerSession::ChatHandler(CNetMessage *pMsg, CNetSession *pNetSession)
{
CChatMessage *msg=(CChatMessage *)pMsg;
msg->m_Sender=pSession->m_Name;
CStrW wstr=msg->m_Message;
g_Console->ReceivedChatMessage(pSession->GetName().c_str(), wstr.c_str());
g_Console->ReceivedChatMessage(pSession->GetName().c_str(), msg->m_Message.c_str());
pSession->m_pServer->OnChat(msg->m_Sender, msg->m_Message);
pSession->m_pServer->Broadcast(msg);
TAKEN(pMsg);