How to properly serialize / deserialize an array?
I'm trying to get basic networking working again with Photon 3.x, and I'm having a devil of a time trying to figure out how to stuff data into an array of bytes and send it -- the Photon SDK seems to have become much less intuitive since the 6.x version.
I'm using Marmalade and C++ on iOS.
Here is how I am attempting to broadcast the data:
void BroadcastData( std::vector<byte> const & data )
{
EG_HashTable hash;
EG_HashTable_initialize( &hash );
byte * data_copy = new byte[ data.size() ];
for ( uint i = 0; i < data.size(); ++i )
{
data_copy[ i ] = data[ i ];
}
TSerialData::const_iterator data_begin = data.begin();
EG_Object * array_data = EG_Object_createFromArray( (void*)data_copy, EG_BYTE, data.size(), true );
int key = 0;
EG_HashTable_addEntry( &hash, EG_Object_create( &key, EG_INTEGER, true), array_data );
LitePeer_opRaiseEvent( m_PhotonPeer, true, &hash, skEventCodeBase, 0, NULL, 0, EVC_DO_NOT_CACHE, RG_OTHERS );
EG_HashTable_clear(&hash);
delete[] data_copy;
}
This *seems* to work fine, but then in my "OnEvent" callback, I have the following:
void Callback_OnEvent( void* pUser, CEventData* eventData )
{
switch ( eventData->code )
{
case skEventCodeBase:
{
EG_HashTable * hashTable = EventData_getParameters( eventData );
if ( hashTable != NULL )
{
int key = 0;
EG_Object * object = EG_HashTable_getValFromKey( hashTable, &key, EG_INTEGER, NULL );
if ( object != NULL )
{
g_GameplayModeObject->ReceiveDataFromNetwork( object->obj_data, *( object->size )
}
Unfortunately, "object" is always NULL. Why would this be? I stuffed it into the original hash table with a key of 0.
I'm using Marmalade and C++ on iOS.
Here is how I am attempting to broadcast the data:
void BroadcastData( std::vector<byte> const & data )
{
EG_HashTable hash;
EG_HashTable_initialize( &hash );
byte * data_copy = new byte[ data.size() ];
for ( uint i = 0; i < data.size(); ++i )
{
data_copy[ i ] = data[ i ];
}
TSerialData::const_iterator data_begin = data.begin();
EG_Object * array_data = EG_Object_createFromArray( (void*)data_copy, EG_BYTE, data.size(), true );
int key = 0;
EG_HashTable_addEntry( &hash, EG_Object_create( &key, EG_INTEGER, true), array_data );
LitePeer_opRaiseEvent( m_PhotonPeer, true, &hash, skEventCodeBase, 0, NULL, 0, EVC_DO_NOT_CACHE, RG_OTHERS );
EG_HashTable_clear(&hash);
delete[] data_copy;
}
This *seems* to work fine, but then in my "OnEvent" callback, I have the following:
void Callback_OnEvent( void* pUser, CEventData* eventData )
{
switch ( eventData->code )
{
case skEventCodeBase:
{
EG_HashTable * hashTable = EventData_getParameters( eventData );
if ( hashTable != NULL )
{
int key = 0;
EG_Object * object = EG_HashTable_getValFromKey( hashTable, &key, EG_INTEGER, NULL );
if ( object != NULL )
{
g_GameplayModeObject->ReceiveDataFromNetwork( object->obj_data, *( object->size )
}
Unfortunately, "object" is always NULL. Why would this be? I stuffed it into the original hash table with a key of 0.
0
Comments
-
PaulT wrote:I'm using Marmalade and C++ on iOS.
I would recommend to use the C++ one in that case.
Have you already tried EG_HashTable_toString() to check, what the hash actually contains? Have you checked the size of the hash, to be sure, that it contains the expected amount of parameters?
You code looks fine on a first view. I will come back to you tomorrow, after I have tested you code.0 -
Your onEvent has to look like this:
[code2=cpp]void Callback_OnEvent( void* pUser, CEventData* eventData )
{
switch ( eventData->code )
{
case skEventCodeBase:
{
EG_HashTable * hashTable = EventData_getParameters( eventData );
if ( hashTable != NULL )
{
nByte key0 = EV_RT_KEY_DATA;
objHash = EG_HashTable_getValFromKey(hashTable, &key0, EG_BYTE, NULL);
if(objHash)
{
EG_HashTable* evDataContent = (EG_HashTable*)objHash->obj_data;
int key = 0;
EG_Object * object = EG_HashTable_getValFromKey( evDataContent, &key, EG_INTEGER, NULL );
if ( object != NULL )
{
g_GameplayModeObject->ReceiveDataFromNetwork( object->obj_data, *( object->size )
}
}
}
}
}
}[/code2]
So, you have to exrac the actual payload hashtable from its wrapper hash first, like its demonstrated in the demo. I have totally overseen yesterday, that you are not doing that.0