-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
…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
There are no files selected for viewing
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(); | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.