forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientManager.cs
More file actions
75 lines (60 loc) · 2.2 KB
/
Copy pathClientManager.cs
File metadata and controls
75 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using L2dotNET.Logging.Abstraction;
using L2dotNET.Network;
using L2dotNET.Services.Contracts;
namespace L2dotNET
{
public class ClientManager
{
private static readonly ILog log = LogProvider.GetCurrentClassLogger();
private readonly IPlayerService _playerService;
protected SortedList<string, DateTime> Flood = new SortedList<string, DateTime>();
protected NetworkBlock Banned;
public SortedList<string, GameClient> Clients = new SortedList<string, GameClient>();
private readonly GamePacketHandler _gamePacketHandler;
public ClientManager(IPlayerService playerService, GamePacketHandler gamePacketHandler)
{
_playerService = playerService;
_gamePacketHandler = gamePacketHandler;
}
public void AddClient(TcpClient client)
{
if (Banned == null)
Banned = NetworkBlock.Instance;
string ip = client.Client.RemoteEndPoint.ToString().Split(':')[0];
lock (Flood)
{
if (Flood.ContainsKey(ip))
{
if (Flood[ip].CompareTo(DateTime.Now) == 1)
{
log.Warn($"Active flooder: {ip}");
client.Close();
return;
}
lock (Flood)
Flood.Remove(ip);
}
}
Flood.Add(ip, DateTime.Now.AddMilliseconds(3000));
if (!Banned.Allowed(ip))
{
client.Close();
log.Error($"Connection attempt failed. {ip} banned.");
return;
}
GameClient gc = new GameClient(_playerService, this, client, _gamePacketHandler);
lock (Clients)
Clients.Add(gc.Address.ToString(), gc);
log.Info($"{Clients.Count} active connections");
}
public void Terminate(string sock)
{
lock (Clients)
Clients.Remove(sock);
log.Info($"{Clients.Count} active connections");
}
}
}