Skip to content

Commit

Permalink
more secure close connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed May 9, 2016
1 parent 32abe3c commit 087d12d
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions LiteNetLib/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public sealed class NetClient : NetBase
private int _connectTimer;
private ulong _connectId;
private readonly string _connectKey;
private readonly object _connectionCloseLock = new object();

public NetClient(INetEventListener listener, string connectKey) : base(listener)
{
Expand Down Expand Up @@ -51,29 +52,36 @@ public bool IsConnected

private void CloseConnection(bool force, string info)
{
//Close threads
base.Stop();

//Send goodbye
if (_peer != null && !force && _connected)
lock (_connectionCloseLock)
{
//Send disconnect data
var disconnectPacket = NetPacket.CreateRawPacket(PacketProperty.Disconnect, 8);
FastBitConverter.GetBytes(disconnectPacket, 1, _connectId);
_peer.SendRawData(disconnectPacket);
}
//Nothing to do
if (!IsRunning)
return;

//Clear data
_peer = null;
_connected = false;
_connectTimer = 0;
_connectAttempts = 0;
SocketClearPeers();
//Send goodbye
if (_peer != null && !force && _connected)
{
//Send disconnect data
var disconnectPacket = NetPacket.CreateRawPacket(PacketProperty.Disconnect, 8);
FastBitConverter.GetBytes(disconnectPacket, 1, _connectId);
_peer.SendRawData(disconnectPacket);
}

//Send event to Listener
var netEvent = CreateEvent(NetEventType.Disconnect);
netEvent.AdditionalInfo = info;
EnqueueEvent(netEvent);
//Close threads and socket close
base.Stop();

//Clear data
_peer = null;
_connected = false;
_connectTimer = 0;
_connectAttempts = 0;
SocketClearPeers();

//Send event to Listener
var netEvent = CreateEvent(NetEventType.Disconnect);
netEvent.AdditionalInfo = info;
EnqueueEvent(netEvent);
}
}

/// <summary>
Expand Down

0 comments on commit 087d12d

Please sign in to comment.