Skip to content

Commit

Permalink
perf: adding native UDP socket (NanoSockets) for supported platforms (#…
Browse files Browse the repository at this point in the history
…860)

Adding native socket (https://github.com/nxrighthere/NanoSockets) that will be used for windows, mac and linux instead of the managed c# version.

This reducing allocations from receiving message and improves cpu performance
  • Loading branch information
phodoval committed Jul 16, 2021
1 parent 0e5cccf commit 3f34863
Show file tree
Hide file tree
Showing 18 changed files with 851 additions and 104 deletions.
53 changes: 53 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using Mirage.SocketLayer;
using NanoSockets;

namespace Mirage.Sockets.Udp {
public sealed class NanoEndPoint : IEndPoint, IEquatable<NanoEndPoint>
{
public Address address;

public NanoEndPoint(string host, ushort port)
{
address = new Address();
address.port = port;
UDP.SetHostName(ref address, host);
}

public NanoEndPoint(Address address)
{
this.address = address;
}

public IEndPoint CreateCopy()
{
return new NanoEndPoint(address);
}

public bool Equals(NanoEndPoint other)
{
return address.Equals(other.address);
}

public override bool Equals(object obj)
{
if (obj is NanoEndPoint endPoint)
{
return address.Equals(endPoint.address);
}

return false;
}

public override int GetHashCode()
{
return address.GetHashCode();
}

public override string ToString()
{
return address.ToString();
}
}

}
11 changes: 11 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoEndPoint.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Mirage.SocketLayer;
using NanoSockets;

namespace Mirage.Sockets.Udp {
public class NanoSocket : ISocket
{
Socket socket;
NanoEndPoint receiveEndPoint;
readonly int bufferSize;

public NanoSocket(UdpSocketFactory factory) {
bufferSize = factory.BufferSize;
}

public void Bind(IEndPoint endPoint)
{
receiveEndPoint = (NanoEndPoint)endPoint;

InitSocket();
UDP.Bind(socket, ref receiveEndPoint.address);
}

public void Close()
{
UDP.Destroy(ref socket);
}

public void Connect(IEndPoint endPoint)
{
receiveEndPoint = (NanoEndPoint)endPoint;

InitSocket();
UDP.Connect(socket, ref receiveEndPoint.address);
}

public bool Poll()
{
return UDP.Poll(socket, 0) > 0;
}

public int Receive(byte[] buffer, out IEndPoint endPoint)
{
int count = UDP.Receive(socket, ref receiveEndPoint.address, buffer, buffer.Length);
endPoint = receiveEndPoint;

return count;
}

public void Send(IEndPoint endPoint, byte[] packet, int length)
{
var nanoEndPoint = (NanoEndPoint)endPoint;
UDP.Send(socket, ref nanoEndPoint.address, packet, length);
}

void InitSocket()
{
socket = UDP.Create(bufferSize, bufferSize);
UDP.SetDontFragment(socket);
UDP.SetNonBlocking(socket);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoSocket.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoSockets.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoSockets/Plugins.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

0 comments on commit 3f34863

Please sign in to comment.