Skip to content

Commit

Permalink
Fix wrong receive byte count (the way benchmarkNet get the data lengt…
Browse files Browse the repository at this point in the history
…h is not good, so Ive got to copy the data to fix the issue.) Can be improved by using reader.Length instead.

Remove useless SwitchQueue
Remove the
  • Loading branch information
Danis Joyal committed Jan 30, 2018
1 parent 69e8244 commit 104fbf2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 110 deletions.
33 changes: 12 additions & 21 deletions LiteNetLib/NetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,11 @@ private void UpdateLogic()
}

//Process acks
_peers.UpdateClone();
lock (_peers.PeersArrayClone)
lock (_peers)
{
NetPeer[] arrayPeers = _peers.PeersArrayClone;
int peersCount = _peers.CloneCount;
for (int i = 0; i < peersCount; i++)
for (int i = 0; i < _peers.Count; i++)
{
NetPeer netPeer = arrayPeers[i];
NetPeer netPeer = _peers[i];
if (netPeer.ConnectionState != ConnectionState.Disconnected)
{
netPeer.Update(nextUpdateTime);
Expand Down Expand Up @@ -819,13 +816,11 @@ public void SendToAll(byte[] data, DeliveryMethod options, int channel = 0)
/// <param name="channel">Set the channel wanted. See NetConstants.MultiChannelSize</param>
public void SendToAll(byte[] data, int start, int length, DeliveryMethod options, int channel = 0)
{
_peers.UpdateClone();
lock (_peers.PeersArrayClone)
lock (_peers)
{
NetPeer[] arrayPeers = _peers.PeersArrayClone;
for (int i = 0; i < _peers.CloneCount; i++)
for (int i = 0; i < _peers.Count; i++)
{
arrayPeers[i].Send(data, start, length, options);
_peers[i].Send(data, start, length, options);
}
}
}
Expand Down Expand Up @@ -865,13 +860,11 @@ public void SendToAll(byte[] data, DeliveryMethod options, NetPeer excludePeer,
/// <param name="channel">Set the channel wanted. See NetConstants.MultiChannelSize</param>
public void SendToAll(byte[] data, int start, int length, DeliveryMethod options, NetPeer excludePeer, int channel = 0)
{
_peers.UpdateClone();
lock (_peers.PeersArrayClone)
lock (_peers)
{
NetPeer[] arrayPeers = _peers.PeersArrayClone;
for (int i = 0; i < _peers.CloneCount; i++)
for (int i = 0; i < _peers.Count; i++)
{
var netPeer = arrayPeers[i];
var netPeer = _peers[i];
if (netPeer != excludePeer)
{
netPeer.Send(data, start, length, options);
Expand Down Expand Up @@ -1011,13 +1004,11 @@ public bool SendDiscoveryResponse(byte[] data, int start, int length, NetEndPoin
/// </summary>
public void Flush()
{
_peers.UpdateClone();
lock (_peers.PeersArrayClone)
lock (_peers)
{
NetPeer[] arrayPeers = _peers.PeersArrayClone;
for (int i = 0; i < _peers.CloneCount; i++)
for (int i = 0; i < _peers.Count; i++)
{
arrayPeers[i].Flush();
_peers[i].Flush();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions LiteNetLib/NetPacketPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private NetPacket GetPacket(int size, bool clear)
//clear in not reallocated
Array.Clear(packet.RawData, 0, size);
}
packet.Size = size;
}
return packet;
}
Expand Down
28 changes: 0 additions & 28 deletions LiteNetLib/NetPeerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ internal sealed class NetPeerCollection
{
private readonly Dictionary<NetEndPoint, NetPeer> _peersDict;
private readonly NetPeer[] _peersArray;
private bool _peersArrayHasChanged;
public NetPeer[] PeersArrayClone;
public int CloneCount;
public int Count;

public NetPeer this[int index]
Expand All @@ -20,9 +17,7 @@ internal sealed class NetPeerCollection
public NetPeerCollection(int maxPeers)
{
_peersArray = new NetPeer[maxPeers];
PeersArrayClone = new NetPeer[maxPeers];
_peersDict = new Dictionary<NetEndPoint, NetPeer>();
_peersArrayHasChanged = false;
}

public bool TryGetValue(NetEndPoint endPoint, out NetPeer peer)
Expand All @@ -32,10 +27,8 @@ public bool TryGetValue(NetEndPoint endPoint, out NetPeer peer)

public void Clear()
{
Array.Clear(PeersArrayClone, 0, Count);
Array.Clear(_peersArray, 0, Count);
_peersDict.Clear();
CloneCount = Count = 0;
}

public void Add(NetEndPoint endPoint, NetPeer peer)
Expand All @@ -45,7 +38,6 @@ public void Add(NetEndPoint endPoint, NetPeer peer)
_peersDict.Add(endPoint, peer);
_peersArray[Count] = peer;
Count++;
_peersArrayHasChanged = true;
}
}

Expand All @@ -54,24 +46,6 @@ public bool ContainsAddress(NetEndPoint endPoint)
return _peersDict.ContainsKey(endPoint);
}

public int UpdateClone()
{
if(_peersArrayHasChanged == true)
{
lock (PeersArrayClone)
{
lock (_peersArray)
{
CloneCount = Count;
Array.Copy(_peersArray, 0, PeersArrayClone, 0, CloneCount);
_peersArrayHasChanged = false;
return CloneCount;
}
}
}
return CloneCount;
}

public NetPeer[] ToArray()
{
NetPeer[] result = new NetPeer[Count];
Expand All @@ -89,7 +63,6 @@ public void Remove(NetPeer peer)
_peersArray[idx] = _peersArray[Count - 1];
_peersArray[Count - 1] = null;
Count--;
_peersArrayHasChanged = true;
break;
}
}
Expand All @@ -101,7 +74,6 @@ public void RemoveAt(int idx)
_peersArray[idx] = _peersArray[Count - 1];
_peersArray[Count - 1] = null;
Count--;
_peersArrayHasChanged = true;
}
}
}
63 changes: 15 additions & 48 deletions LiteNetLib/NetSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ internal sealed class NetSocket
private NetEndPoint _bufferNetEndPointv4;

private Socket _udpSocketv6;
private Thread _threadv4;
private Thread _threadv6;
private bool _running;
private readonly object _receiveLock = new object();
private EndPoint _bufferEndPointv6;
private NetEndPoint _bufferNetEndPointv6;

private readonly NetManager.OnMessageReceived _onMessageReceived;

private static readonly IPAddress MulticastAddressV6 = IPAddress.Parse (NetConstants.MulticastGroupIPv6);
Expand All @@ -37,26 +36,19 @@ public NetSocket(NetManager.OnMessageReceived onMessageReceived)
_onMessageReceived = onMessageReceived;
}

private void ReceiveLogic(object state)
{
Socket socket = (Socket)state;
byte[] receiveBuffer = new byte[NetConstants.SocketBufferSize];

while(_running)
Receive(false, receiveBuffer);
}

public void Receive(bool ipV6, byte[] receiveBuffer)
{
Socket socket = _udpSocketv4;
EndPoint bufferEndPoint = _bufferEndPointv4;
NetEndPoint bufferNetEndPoint = _bufferNetEndPointv4;
int result;

//if (ipV6 == true)
//{
//socket = _udpSocketv6;
//}
if (ipV6 == true)
{
socket = _udpSocketv6;
bufferEndPoint = _bufferEndPointv6;
bufferNetEndPoint = _bufferNetEndPointv6;
}

while (true)
{
Expand Down Expand Up @@ -90,20 +82,14 @@ public void Receive(bool ipV6, byte[] receiveBuffer)
return;
}
NetUtils.DebugWriteError("[R]Error code: {0} - {1}", (int)ex.SocketErrorCode, ex.ToString());
lock (_receiveLock)
{
_onMessageReceived(null, 0, (int)ex.SocketErrorCode, bufferNetEndPoint);
}
_onMessageReceived(null, 0, (int)ex.SocketErrorCode, bufferNetEndPoint);

return;
}

//All ok!
NetUtils.DebugWrite(ConsoleColor.Blue, "[R]Received data from {0}, result: {1}", bufferNetEndPoint.ToString(), result);
lock (_receiveLock)
{
_onMessageReceived(receiveBuffer, result, 0, bufferNetEndPoint);
}
_onMessageReceived(receiveBuffer, result, 0, bufferNetEndPoint);
}
}

Expand Down Expand Up @@ -133,11 +119,6 @@ public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool re
return false;
}
LocalPort = ((IPEndPoint) _udpSocketv4.LocalEndPoint).Port;
_running = true;
//_threadv4 = new Thread(ReceiveLogic);
//_threadv4.Name = "SocketThreadv4(" + LocalPort + ")";
//_threadv4.IsBackground = true;
//_threadv4.Start(_udpSocketv4);

_bufferEndPointv4 = new IPEndPoint(_udpSocketv4.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0);
_bufferNetEndPointv4 = new NetEndPoint((IPEndPoint)_bufferEndPointv4);
Expand All @@ -148,7 +129,7 @@ public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool re
return true;

_udpSocketv6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
_udpSocketv6.Blocking = true;
_udpSocketv6.Blocking = false;
_udpSocketv6.ReceiveBufferSize = NetConstants.SocketBufferSize;
_udpSocketv6.SendBufferSize = NetConstants.SocketBufferSize;
//_udpSocketv6.Ttl = NetConstants.SocketTTL;
Expand All @@ -171,13 +152,11 @@ public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool re
{
// Unity3d throws exception - ignored
}

_threadv6 = new Thread(ReceiveLogic);
_threadv6.Name = "SocketThreadv6(" + LocalPort + ")";
_threadv6.IsBackground = true;
_threadv6.Start(_udpSocketv6);
}

_bufferEndPointv6 = new IPEndPoint(_udpSocketv4.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0);
_bufferNetEndPointv6 = new NetEndPoint((IPEndPoint)_bufferEndPointv6);

return true;
}

Expand Down Expand Up @@ -282,19 +261,12 @@ private void CloseSocket(Socket s)

public void Close()
{
_running = false;

//Close IPv4
if (_udpSocketv4 != null)
{
CloseSocket(_udpSocketv4);
_udpSocketv4 = null;
}
if (Thread.CurrentThread != _threadv4)
{
_threadv4.Join();
}
_threadv4 = null;

//No ipv6
if (_udpSocketv6 == null)
Expand All @@ -306,11 +278,6 @@ public void Close()
CloseSocket(_udpSocketv6);
_udpSocketv6 = null;
}
if (Thread.CurrentThread != _threadv6)
{
_threadv6.Join();
}
_threadv6 = null;
}
}
}
11 changes: 5 additions & 6 deletions LiteNetLib/SequencedChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ internal sealed class SequencedChannel
{
private int _localSequence;
private int _remoteSequence;
private readonly SwitchQueue<NetPacket> _outgoingPackets;
private readonly Queue<NetPacket> _outgoingPackets;
private readonly NetPeer _peer;
private readonly int _channel;

public SequencedChannel(NetPeer peer, int channel)
{
_outgoingPackets = new SwitchQueue<NetPacket>();
_outgoingPackets = new Queue<NetPacket>();
_peer = peer;
_channel = channel;
}

public void AddToQueue(NetPacket packet)
{
_outgoingPackets.Push(packet);
_outgoingPackets.Enqueue(packet);
}

public void SendNextPackets()
{
_outgoingPackets.Switch();
while (_outgoingPackets.Empty() != true)
while (_outgoingPackets.Count > 0)
{
NetPacket packet = _outgoingPackets.Pop();
NetPacket packet = _outgoingPackets.Dequeue();
_localSequence = (_localSequence + 1) % NetConstants.MaxSequence;
packet.Sequence = (ushort)_localSequence;
_peer.SendRawData(packet);
Expand Down
11 changes: 5 additions & 6 deletions LiteNetLib/SimpleChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@ namespace LiteNetLib
{
internal sealed class SimpleChannel
{
private readonly SwitchQueue<NetPacket> _outgoingPackets;
private readonly Queue<NetPacket> _outgoingPackets;
private readonly NetPeer _peer;
private readonly int _channel;

public SimpleChannel(NetPeer peer, int channel)
{
_outgoingPackets = new SwitchQueue<NetPacket>();
_outgoingPackets = new Queue<NetPacket>();
_peer = peer;
_channel = channel;
}

public void AddToQueue(NetPacket packet)
{
_outgoingPackets.Push(packet);
_outgoingPackets.Enqueue(packet);
}

public void SendNextPackets()
{
NetPacket packet;
_outgoingPackets.Switch();
while (_outgoingPackets.Empty() != true)
while (_outgoingPackets.Count > 0)
{
packet = _outgoingPackets.Pop();
packet = _outgoingPackets.Dequeue();
_peer.SendRawData(packet);
packet.Recycle();
}
Expand Down
10 changes: 9 additions & 1 deletion LiteNetLib/Utils/NetDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public byte[] Data
{
get { return _data; }
}

public int Length { get { return _dataSize; } }

public int Position
{
Expand Down Expand Up @@ -69,7 +71,13 @@ public void SetSource(byte[] source, int offset, int maxSize)
internal void SetSource(NetPacket packet)
{
Clear();
_data = packet.RawData;
if (packet.GetDataSize() > 0)
{
// Temp size on BenchmarkNet
_data = new byte[packet.GetDataSize()];
Array.Copy(packet.RawData, _data, packet.GetDataSize());
}
//_data = packet.RawData;
_position = 0;
_dataSize = packet.GetDataSize();
_packet = packet;
Expand Down

0 comments on commit 104fbf2

Please sign in to comment.