-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListener.cs
89 lines (67 loc) · 2.22 KB
/
Listener.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace oftc_ircd_cs
{
[Flags]
public enum ListenerFlag
{
SSL = 0x1
}
public class Listener
{
private static readonly List<Listener> Listeners = new List<Listener>();
private ListenerFlag flags;
private Socket listener;
public Listener(string host, ushort port, ListenerFlag flags)
{
Host = host;
Port = port;
this.flags = flags;
}
public static ListenerSection Conf { get; set; }
public string Host { get; private set; }
public ushort Port { get; private set; }
public void Start()
{
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = String.IsNullOrEmpty(Host) ? IPAddress.Any : IPAddress.Parse(Host);
listener.Bind(new IPEndPoint(address, Port));
listener.Listen(100);
listener.BeginAccept(AcceptCallback, this);
}
public void Connected(IAsyncResult ar)
{
Socket handler = listener.EndAccept(ar);
var connection = new Connection();
Connection.Add(connection);
connection.Client = new Client { Connection = connection, FirstSeen = DateTime.Now, Server = Server.Me };
BaseClient.AddUnregistered(connection.Client);
connection.Accept(handler);
}
public static void AcceptCallback(IAsyncResult ar)
{
var listener = (Listener) ar.AsyncState;
listener.Connected(ar);
}
public static void Init()
{
Conf = new ListenerSection();
Config.AddSection("listeners", Conf);
}
public static Listener Create(string host, ushort port, ListenerFlag flags)
{
var listener = new Listener(host, port, flags);
Listeners.Add(listener);
return listener;
}
public static void StartListeners()
{
foreach (Listener listener in Listeners)
{
listener.Start();
}
}
}
}