Hi guys. I’m developing a multiplayer game with Google Play Services and Cocos2d-x. I already set up communication between Java and C*+ using JNI
, and can run processes like Sign-in, Create room, Invitation… Everything is fine until I need to send some struct
to another players. When receive data from other, an error has occurred. Here’s the structs:
<pre>
typedef enum
{
MESSAGE_TYPE_PING = 1,>NewByteArray; //env is a static JNIEnv* in this class; 16 since my PING_MESSAGE struct is 16 bytes in size;
MESSAGE_TYPE_PING_BACK = 0,
MESSAGE_TYPE_RTT = 3
} MESSAGE_TYPE;
typedef struct
{
MESSAGE_TYPE messType;
} MESSAGE;
typedef struct
{
MESSAGE mess;
timeval sendTime;
const char* text;
} PING_MESSAGE;
</pre>
And in this code snippet I convert struct
and send it to another players:
<pre>
void MyClass::sendData
{
// Send package
PING_MESSAGE ping;
ping.mess.messType = MESSAGE_TYPE_PING;
ping.sendTime = startTime;
ping.text = “Ohhhhhh”;
char byte[sizeof];
memcpy);
GCHelper::sendReliableRealTimeMessage;
}
// In GCHelper’s implementation
void GCHelper::sendReliableRealTimeMessage
{
// Convert char* to jbyteArray
jbyteArray bArray = env
env~~>SetByteArrayRegionbyteMess);
// Make Java call
env~~>CallStaticVoidMethod;
methodInfo.env~~>DeleteLocalRef;
methodInfo.env~~>DeleteLocalRef;
}
</pre>
Now, Java code take responsibility to send the byte array to participants in room. At receiver side, I’m continue send received data to C**. And error occured here when I convert jbyteArray
back to struct
:
<pre>
void Java_package_name_TestMulti_nativeOnRealTimeMessageReceived
{
CCLOG (“Called from java”);
jboolean isCopy;
jbyte * pCData = env~~>GetByteArrayElements;
jsize size = env~~>GetArrayLength;
const char* sd = pCData;
PING_MESSAGE result;
memcpy);
CCLOG (“TEST size: %d”, size); // This log out: “TEST size: 16”
CCLOG (“TEST type: %d”, result.mess.messType); // This log out: “TEST type: –1”
CCLOG (“TEST text: %s”, result.text); // AND ERROR!
cocos2d::CCByteArray* data = cocos2d::CCByteArray::createWithData;
cocos2d::CCNotificationCenter::sharedNotificationCenter~~>postNotification;
if
{
env~~>ReleaseByteArrayElements;
}
}
</pre>
I’m not understand here. If I don’t send byte array to another players yet send that array back to C*+ by calling nativeOnRealTimeMessageReceived()
method from Java side, it runs fine and logs correctly. It’s mean that with the same byte[]
package converted from char*
in C**, if I just pass it back to C**, it’s correct, but if I send it through Google Play Game Services, it goes wrong. What does this mean?
Thank you.