Tasharen Entertainment Forum

Support => TNet 3 Support => Topic started by: raw on February 17, 2014, 06:04:35 PM

Title: ArgumentOutOfRangeException: size must be >= 0
Post by: raw on February 17, 2014, 06:04:35 PM
i have a problem sending packets using UDP, getting two errors on the client side, the first one is *sometimes* (-> not always) happening and not critical:
  1. NullReferenceException: Object reference not set to an instance of an object
  2. TNet.GameClient.EndSend (Boolean reliable) (at Assets/TNet/Client/TNGameClient.cs:391)
  3. TNManager.EndSend (Boolean reliable) (at Assets/TNet/Client/TNManager.cs:620)
  4. RawPlayerSync.Update () (at Assets/Scripts/Player/RawPlayerSync.cs:43)
  5.  

and this one, always occurring, kills the connection:
  1. ArgumentOutOfRangeException: size must be >= 0
  2. Parameter name: size
  3. System.Net.Sockets.Socket.BeginSendTo (System.Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, System.Net.EndPoint remote_end, System.AsyncCallback callback, System.Object state)
  4. TNet.UdpProtocol.Send (TNet.Buffer buffer, System.Net.IPEndPoint ip) (at Assets/TNet/Common/TNUdpProtocol.cs:255)
  5. TNet.GameClient.EndSend (Boolean reliable) (at Assets/TNet/Client/TNGameClient.cs:404)
  6. TNManager.EndSend (Boolean reliable) (at Assets/TNet/Client/TNManager.cs:620)
  7. RawPlayerSync.Update () (at Assets/Scripts/Player/RawPlayerSync.cs:43)

The Update-Function contains basicly this, i also tried the FixedUpdate:
  1. BinaryWriter writer = TNManager.BeginSend(Packet.Test);
  2. TNManager.EndSend(false);
i know that Update() means a lot of packets, the error is occurring after some time, like one minute, sometimes less, sometines more, so it is working in general.
The server just shows the client has disconnected after this error. i can then just reconnect and move around for a some seconds to a minute until it happens again.

I followed the Custom Packets tutorial, using the standalone server on localhost. The above code is using my custom "Test" packet. I boiled it down to this after it happend with some content in the packet in the first place. This it is the only packet using UDP. Other Update-Functions send packets using TCP, while also receiving a lot of data. If i silence the other traffic, the error seems not to happen, so it is maybe a threading/async io/locking issue?

I'am using unity 4.3.3 and the latest TNet on Windows 8.1.

Could there be an error on my side? How can i debug it? is it maybe a known error?
Thank you.
Title: Re: ArgumentOutOfRangeException: size must be >= 0
Post by: ArenMook on February 18, 2014, 11:14:05 AM
Hmm... Try changing TNGameClient.EndSend functions to these:
  1.         /// <summary>
  2.         /// Send the outgoing buffer.
  3.         /// </summary>
  4.  
  5.         public void EndSend (bool reliable)
  6.         {
  7.                 mBuffer.EndPacket();
  8. #if UNITY_WEBPLAYER
  9.                 mTcp.SendTcpPacket(mBuffer);
  10. #else
  11.                 if (reliable || !mUdpIsUsable || mServerUdpEndPoint == null || !mUdp.isActive)
  12.                 {
  13.                         mTcp.SendTcpPacket(mBuffer);
  14.                 }
  15.                 else mUdp.Send(mBuffer, mServerUdpEndPoint);
  16. #endif
  17.                 mBuffer.Recycle();
  18.                 mBuffer = null;
  19.         }
  20.  
  21.         /// <summary>
  22.         /// Broadcast the outgoing buffer to the entire LAN via UDP.
  23.         /// </summary>
  24.  
  25.         public void EndSend (int port)
  26.         {
  27.                 mBuffer.EndPacket();
  28. #if !UNITY_WEBPLAYER
  29.                 mUdp.Broadcast(mBuffer, port);
  30. #endif
  31.                 mBuffer.Recycle();
  32.                 mBuffer = null;
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Send this packet to a remote UDP listener.
  37.         /// </summary>
  38.  
  39.         public void EndSend (IPEndPoint target)
  40.         {
  41.                 mBuffer.EndPacket();
  42. #if !UNITY_WEBPLAYER
  43.                 mUdp.Send(mBuffer, target);
  44. #endif
  45.                 mBuffer.Recycle();
  46.                 mBuffer = null;
  47.         }
Title: Re: ArgumentOutOfRangeException: size must be >= 0
Post by: raw on February 20, 2014, 04:32:04 PM
thank you for your response!
im sorry to report that it did not solve it.

  1. Assets/TNet/Client/TNGameClient.cs(395,26): error CS0103: The name `mUdpIsUsable' does not exist in the current context

after removing this parameter from the or list, its still the same problem.

  1. NullReferenceException: Object reference not set to an instance of an object
  2. TNet.GameClient.EndSend (Boolean reliable) (at Assets/TNet/Client/TNGameClient.cs:401)
  3. TNManager.EndSend (Boolean reliable) (at Assets/TNet/Client/TNManager.cs:620)
  4. RawPlayerSync.FixedUpdate () (at Assets/Scripts/Player/RawPlayerSync.cs:43)
  5.  
where line 401 of TNGameClient.cs is this one:
  1. mBuffer.Recycle();

the 2nd error that causes the disconnect now looks like this:
  1. ArgumentOutOfRangeException: size must be >= 0
  2. Parameter name: size
  3. System.Net.Sockets.Socket.BeginSendTo (System.Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, System.Net.EndPoint remote_end, System.AsyncCallback callback, System.Object state)
  4. TNet.UdpProtocol.Send (TNet.Buffer buffer, System.Net.IPEndPoint ip) (at Assets/TNet/Common/TNUdpProtocol.cs:255)
  5. TNet.GameClient.EndSend (Boolean reliable) (at Assets/TNet/Client/TNGameClient.cs:399)
  6. TNManager.EndSend (Boolean reliable) (at Assets/TNet/Client/TNManager.cs:620)
  7. RawPlayerSync.FixedUpdate () (at Assets/Scripts/Player/RawPlayerSync.cs:43)
  8.  

while rolling your change back and forth, i came to the conclusion that both errors are happening by a factor of two more frequently now.

if you need more informations, let me know. thank you for your help.
Title: Re: ArgumentOutOfRangeException: size must be >= 0
Post by: ArenMook on February 21, 2014, 11:21:00 AM
I noticed you're doing your logic in Fixed Update... I wonder if it's related. These kind of errors can be caused by doing things from different threads, but as far as I know Unity's FixedUpdate() and Update() run on the same thread. Maybe not anymore? All TNet stuff should generally be in the Update instead.

The "mUdpIsUsable" flag is one of the changes in the Pro repository. It might make sense for me to give you the latest beta to try out. I can't post it here though -- skype would be best -- "arenmook".
Title: Re: ArgumentOutOfRangeException: size must be >= 0
Post by: raw on February 22, 2014, 08:04:23 AM
no, it is not related, no difference if i use Update or FixedUpdate.
added you on skype.