- Home /
NetworkTransport LLAPI DataEvent Recieving First Packet Repeatedly
I'm working on writing a script to synchronize the position of holograms between two Hololens and I'm using the Low Level NetworkTrasnport API . I've written a short testing script to test ways to transfer the spatial anchors which are ~160 MB at a reasonable speed (a few seconds). Right now I'm sending junk data between a client and a server. The Server chops up the data into packs of 1400 bytes (the biggest batch I could get it to accept) and sends it to each client. The client receives the first packet and then continues to receive the same packet again and again. It shows no signs of stopping or receiving a different packet although the events slow down over time. I let it run for around half an hour to see if it every moved on or stopped and get to around 20,000 packets received, all duplicates of the first packet sent by the Host.
I'm using a FSM structure that clears and pulls a new Receive event each update.
private void Update()
{
recData = new NetworkEventType();
recData = NetworkTransport.Receive(out recHostId, out recConnectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (m_state)
{
case States.ListenForHost:
ListenForHost();
break;
case States.ConnectToHost:
ConnectToHost();
break;
case States.BecomeHost:
BecomeHost();
break;
case States.ListenForEvents:
ListenForEvents();
break;
case States.PlayerConnect:
PlayerConnect();
break;
case States.PlayerDisconnect:
PlayerDisconnect();
break;
case States.DataRecieved:
DataRecieved();
break;
case States.HostMigration:
HostMigration();
break;
case States.SendingData:
SendingData();
break;
}
discoveryTimer = discoveryTimer - Time.deltaTime;
StatusCheck();
}
The receiving side swaps between listening for events and receiving data each update.
private void ListenForEvents()
{
switch (recData)
{
case NetworkEventType.ConnectEvent:
m_state = States.PlayerConnect;
Debug.Log("PlayerConnected: " + +recConnectionId);
break;
case NetworkEventType.DisconnectEvent:
m_state = States.PlayerDisconnect;
Debug.Log("PlayerDisconnected" + recConnectionId);
break;
case NetworkEventType.DataEvent:
m_state = States.DataRecieved;
Debug.Log("DataRecieved: " + recConnectionId);
break;
default:
break;
}
}
private void DataRecieved()
{
NetDebugFormat("Data Recieved {0}, size {1}", EventsRecieved, dataSize);
EventsRecieved++;
m_state = States.ListenForEvents;
Debug.Log("ListenForEvents");
}
The Host sends data by generating an array full of junk data and calling a coroutine to break it into small pieces to be sent to the clients.
private void SendingData()
{
Debug.Log(connectionId);
System.Random rnd = new System.Random();
byte[] stuff = new byte[1500 * 10];
rnd.NextBytes(stuff);
Debug.Log("Starting Send Coroutine");
StartCoroutine(IncrementalSend(stuff));
m_state = States.ListenForEvents;
Debug.Log("ListenForEvents");
}
IEnumerator IncrementalSend(byte[] data)
{
// NetworkTransport.Send can not handle arbitrarily large messages.
// Break up data to send into chunks of at most kMaxSendSize bytes.
int remainingLength = data.Length;
int srcOffset = 0;
System.Random sendSizeRNG = new System.Random();
while (remainingLength > 0)
{
// randomize amount of data being sent
int sendLength = sendSizeRNG.Next(kMaxSendSize / 2, kMaxSendSize);
// determine how many bytes to send in this chunk and copy to beginning of temporary sendBuffer.
sendLength = Math.Min(remainingLength, sendLength);
//NetDebugFormat("Send Length: {0} Status: {1}", sendLength, (NetworkError)error);
Array.Copy(data, srcOffset, sendBuffer, 0, sendLength);
// send chunk to all connections
foreach (int id in connectionIds)
{
SendCounter++;
NetworkTransport.Send(hostId, id, reliableChannel, sendBuffer, sendLength, out error);
NetDebugFormat("Send {0} bytes every {1} until {2} bytes have been sent. Have sent {3}", sendLength, data.Length, (NetworkError)error, SendCounter);
}
remainingLength -= sendLength;
srcOffset += sendLength;
yield return new WaitForSeconds(.1f);
}
byte[] data2 = Encoding.ASCII.GetBytes("Finished");
foreach (int id in connectionIds)
{
NetworkTransport.Send(hostId, id, reliableChannel, data2, data2.Length, out error);
}
NetDebug("Finished" + (NetworkError)error);
SendCounter = 0;
Assert.IsTrue(remainingLength == 0);
}
I can't find any logic errors in my code that could cause it to keep receiving the same Data again and again. The client continues to receive Data Events even after the Host has disconnected. The only thing I can think of is that something is happening inside the NetworkTransport API that I'm not aware of and the documentation doesn't provide much detail on this topic. Does anyone have any experience or suggestions for this issue?
Answer by tjmarshall · Aug 03, 2017 at 07:27 PM
In the time it took to get moderator approval, I resolved the issue, sort of at least. The problem seemed to be that the packet size was on the upper limit of what it could handle. When I sent a 1500 byte packet i would get an MessageToLong error so I reduced the packet size to 1400. I stopped getting any NetworkErrors but the first message-received-looping-started. I lowered it to 1000 bytes and it started working fine. I guess it was just small enough not to throw an error but big enough to cause issues when being received.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I transfer large file sizes over the LLAPI NetworkTransport? 1 Answer
Unity's LLAPI for Raspberry Pi Server? 0 Answers
Desync between host and client. 0 Answers