send messages to different objects

0 votes

hey i am using the viking demo and  i have set up the basic viking to viking communication .but now i need to add another object whose position i want to update over the server.i have made another script for it and it sends bytes  through sendupdatepeers to the listener class,but in the onupdatepeersreceived function where i get the message,how do i differentiate between which object is sending the message and then send it to the respective object's script like the viking or my object in this case.do i need to use another listener script?please help!

code snippets:-

sent using the script

public void sendBytes(byte[] msg, bool useUDP)
{
if(state == 1)
{
if(useUDP == true)
WarpClient.GetInstance().SendUDPUpdatePeers(msg);
else
WarpClient.GetInstance().SendUpdatePeers(msg);
}
}
the fuction which receives:-(maybe a gameobject check here should do?)
public void onUpdatePeersReceived (UpdateEvent eventObj)
{
 
       if(???)
{   
m_apppwarp.onBytes(eventObj.getUpdate());
}
else
otherscript.onBytes(eventObj.getUpdate());
 
}
}
 
 

 

asked Jul 12, 2015 in AppWarp by Mihir Solanki (27 points)

1 Answer

0 votes

Hi Mihir,

In the Viking Demo project, we are already checking this functionality. We are doing this like:

First, we are copying the username along with the position, rotation and velocity of the gameObject to the byte array (which we are passing as an argument in sendBytes method) through these line:

int data_len = (sizeof(float)*9) + (username.Length*sizeof(char));

byte[] data = new byte[data_len];

System.Buffer.BlockCopy(username.ToCharArray(),0,data,sizeof(float)*9,username.Length*sizeof(char));

Now, when we get the response callback in the onBytes method of the appwarp class, we are checking sender like this:

char[] data_c = new char[(msg.Length - (sizeof(float)*9))/sizeof(char)];
System.Buffer.BlockCopy(msg,sizeof(float)*9,data_c,0,msg.Length - (sizeof(float)*9));
string sender = new string(data_c);
 
if(sender != username){
       //I am not the sender
}
Currently the above check is written in onBytes method of the AppWarp class, which is getting called from onUpdatePeersReceived method of the Listener class You can put the same kind of check for the object you are creating either in onUpdatePeersReceived or in onBytes mehod as per your requirement.
 
Let us know if you face any problem.
 
Thanks & Regards,
Sumit
answered Jul 13, 2015 by sumit.balodi (40 points)
um in the line int data_len = (sizeof(float)*9) + (username.Length*sizeof(char));
why are we adding the size of float to the username.isnt adding the size of all its characters enough?
Hi,
The 'data_len' var is the length of byte array, the array which we are sending through 'sendBytes' method. This includes the the total size of the float array and the char array.
The float array in further includes the position, rotation and speed of the object and total length of this array is 9 (you can check the code in the appwarp class), so (sizeof(float)*9) will be its total size. On similar note, the char array includes the object's name only, so its size will be (username.Length*sizeof(char).
And the total will be the data array length, i.e., the data_len variable.
Let us know if you face any problem.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...