Hello,
For my next online game, I’m developing my network logic out of Unity (as the servers won’t be Unity-based and I want to re-use this work on non-Unity project). In my Visual Studio test project everything work perfectly, but I have a problem when calling my DLL from Unity :
private void ReceiveFunction(object sender, SocketAsyncEventArgs e)
{
// UDP error -> Event
if (e.SocketError != SocketError.Success || e.BytesTransferred == 0)
{
if (Server.Crash != null) Server.Crash(e);
return;
}
// Client identification
IPEndPoint Address = (IPEndPoint) e.RemoteEndPoint;
Client Sender;
lock (Server.Children)
{
if (Server.Children.ContainsKey(Address) == false)
{
// Keep track of the new client
Server.Children.Add(Address, new Client());
}
Sender = Server.Children[Address];
}
// Read and answer the message
Server.WorkOnThatMessage(Server.ReceiveBuffer, e.BytesTransferred, Sender);
// Can receive more UDP message now
e.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); // Receive from anyone
if (Server.Socket.ReceiveFromAsync(e) == false)
{
Server.ReceiveFunction(null, e); //In case of synchronous completion
}
}
When using Socket.ReceiveFromAsync in Unity, SocketAsyncEventArgs.RemoteEndPoint return “0.0.0.0:0” (IPAddress.Any) instead of the sender’s address. This is really problematic as I have no other mean of getting this information, and thus can’t answer any client’s request.
Socket.ReceiveAsync (TCP communication) work fine in Unity …
Socket.BeginReceive (with “ref EndPoint”) work fine in Unity too …
… but I really want to avoid using TCP (the game will be a First Person Shooter and TCP will be too slow) or the Begin/EndReceive methods (as they create a lot of work for the garbage collector).
Thanks for your help.