1
0
forked from 0ad/0ad

Add test for network simulation message serialization.

Fix network simulation message serialization.

This was SVN commit r7586.
This commit is contained in:
Ykkrosh 2010-05-25 20:48:06 +00:00
parent 159b96c1a1
commit 42bcb175ff
6 changed files with 62 additions and 5 deletions

View File

@ -144,7 +144,8 @@ CStr CNetMessage::ToString( void ) const
// Desc: Creates the appropriate message based on the given data
//-----------------------------------------------------------------------------
CNetMessage* CNetMessageFactory::CreateMessage(const void* pData,
size_t dataSize )
size_t dataSize,
ScriptInterface& scriptInterface)
{
CNetMessage* pNewMessage = NULL;
CNetMessage header;
@ -222,7 +223,7 @@ CNetMessage* CNetMessageFactory::CreateMessage(const void* pData,
break;
case NMT_SIMULATION_COMMAND:
pNewMessage = new CSimulationMessage(g_Game->GetSimulation2()->GetScriptInterface());
pNewMessage = new CSimulationMessage(scriptInterface);
break;
default:

View File

@ -154,9 +154,10 @@ public:
*
* @param pData Data buffer
* @param dataSize Size of data buffer
* @param scriptInterface Script instance to use when constructing scripted messages
* @return The new message created
*/
static CNetMessage* CreateMessage( const void* pData, size_t dataSize );
static CNetMessage* CreateMessage( const void* pData, size_t dataSize, ScriptInterface& scriptInterface );
private:

View File

@ -60,6 +60,11 @@ public:
class CLengthBinarySerializerImpl
{
public:
CLengthBinarySerializerImpl() :
m_Length(0)
{
}
void Put(const char* UNUSED(name), const u8* UNUSED(data), size_t len)
{
m_Length += len;

View File

@ -27,6 +27,7 @@
#include "precompiled.h"
#include "NetSession.h"
#include "NetLog.h"
#include "simulation2/Simulation2.h"
// DECLARATIONS
@ -283,7 +284,7 @@ bool CNetHost::Poll( void )
bool ok = false;
// Create message from raw data
CNetMessage* pNewMessage = CNetMessageFactory::CreateMessage( event.packet->data, event.packet->dataLength );
CNetMessage* pNewMessage = CNetMessageFactory::CreateMessage( event.packet->data, event.packet->dataLength, g_Game->GetSimulation2()->GetScriptInterface() );
if ( pNewMessage )
{
NET_LOG4( "Message %s of size %lu was received from %p", pNewMessage->ToString().c_str(), (unsigned long)pNewMessage->GetSerializedLength(), event.peer->data );
@ -389,7 +390,7 @@ CNetMessage* CNetHost::ReceiveMessage( const CNetSession* pSession )
if ( !pPacket ) return NULL;
// Create new message
return CNetMessageFactory::CreateMessage( pPacket->data, pPacket->dataLength );
return CNetMessageFactory::CreateMessage( pPacket->data, pPacket->dataLength, g_Game->GetSimulation2()->GetScriptInterface() );
}
//-----------------------------------------------------------------------------

View File

@ -0,0 +1,47 @@
/* Copyright (C) 2010 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/self_test.h"
#include "network/NetMessage.h"
#include "scriptinterface/ScriptInterface.h"
class TestNetMessage : public CxxTest::TestSuite
{
public:
void test_sim()
{
ScriptInterface script("Test");
CScriptValRooted val;
script.Eval("[4]", val);
CSimulationMessage msg(script, 1, 2, 3, val.get());
TS_ASSERT_STR_EQUALS(msg.ToString(), "CSimulationMessage { m_Client: 1, m_Player: 2, m_Turn: 3, m_Data: [4] }");
size_t len = msg.GetSerializedLength();
u8* buf = new u8[len+1];
buf[len] = '!';
TS_ASSERT_EQUALS(msg.Serialize(buf) - (buf+len), 0);
TS_ASSERT_EQUALS(buf[len], '!');
CNetMessage* msg2 = CNetMessageFactory::CreateMessage(buf, len, script);
TS_ASSERT_STR_EQUALS(((CSimulationMessage*)msg2)->ToString(), "CSimulationMessage { m_Client: 1, m_Player: 2, m_Turn: 3, m_Data: [4] }");
delete msg2;
delete[] buf;
}
};

View File

@ -88,6 +88,8 @@ public:
TS_ASSERT_EQUALS(str2.Deserialize(buf, buf+len) - (buf+len), 0);
TS_ASSERT_EQUALS(str2.length(), str.length());
TS_ASSERT_EQUALS(str2, str);
delete[] buf;
}
void test_serialize_8()