Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit aa891cf

Browse files
committed
Clients are now removed properly
1 parent 64a4520 commit aa891cf

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

RCONServerLib.Tests/ConnectionTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ public void TestAuthFail()
1212
server.StartListening();
1313

1414
var client = new RemoteConClient();
15-
client.OnAuthResult += Assert.False;
15+
client.OnAuthResult += success =>
16+
{
17+
Assert.False(success);
18+
19+
client.Disconnect();
20+
server.StopListening();
21+
};
1622

1723
client.Connect("127.0.0.1", 27015);
1824
client.Authenticate("unitfail");
19-
20-
client.Disconnect();
21-
server.StopListening();
2225
}
2326

2427
[Fact]

RCONServerLib/RemoteConServer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Net;
55
using System.Net.Sockets;
6+
using System.Threading;
67
using RCONServerLib.Utils;
78

89
namespace RCONServerLib
@@ -148,7 +149,11 @@ public void StartListening()
148149

149150
public void StopListening()
150151
{
151-
_listener.Stop();
152+
if (!_listener.Server.IsBound)
153+
return;
154+
155+
_listener.Server.Shutdown(SocketShutdown.Both);
156+
_listener.Server.Close(0);
152157
}
153158

154159
private void OnAccept(IAsyncResult result)
@@ -223,5 +228,10 @@ internal void LogDebug(string message)
223228
System.Diagnostics.Debug.WriteLine(message);
224229
Console.WriteLine(message);
225230
}
231+
232+
internal void RemoveClient(TcpClient client)
233+
{
234+
_clients.Remove(client);
235+
}
226236
}
227237
}

RCONServerLib/RemoteConTcpClient.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private void CloseConnection()
9999
{
100100
if (_isUnitTest)
101101
return;
102+
_remoteConServer.RemoveClient(_tcp);
102103
_remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " connection closed.");
103104

104105
_connected = false;
@@ -167,10 +168,7 @@ private void OnPacket(IAsyncResult result)
167168

168169
ParsePacket(_buffer);
169170

170-
if (!_tcp.Connected)
171-
return;
172-
173-
if (!_connected)
171+
if (!_connected || !_tcp.Connected)
174172
{
175173
_remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " lost connection.");
176174
CloseConnection();
@@ -182,8 +180,8 @@ private void OnPacket(IAsyncResult result)
182180
}
183181
catch (IOException)
184182
{
185-
_connected = false;
186183
_remoteConServer.LogDebug(((IPEndPoint) _tcp.Client.RemoteEndPoint).Address + " lost connection.");
184+
CloseConnection();
187185
}
188186
catch (RconServerException)
189187
{

0 commit comments

Comments
 (0)