-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServerSocket.cs
More file actions
146 lines (119 loc) · 3.81 KB
/
ServerSocket.cs
File metadata and controls
146 lines (119 loc) · 3.81 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Atlasd.Daemon;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Atlasd.Battlenet
{
class ServerSocket : IDisposable
{
private bool IsDisposing = false;
public bool IsListening { get; private set; }
public IPEndPoint LocalEndPoint { get; private set; }
public Socket Socket { get; private set; }
public ServerSocket()
{
IsListening = false;
LocalEndPoint = null;
Socket = null;
}
public ServerSocket(IPEndPoint localEndPoint)
{
IsListening = false;
Socket = null;
SetLocalEndPoint(localEndPoint);
}
public void Dispose() /* part of IDisposable */
{
if (IsDisposing) return;
IsDisposing = true;
if (IsListening)
{
Stop();
}
if (Socket != null)
{
Socket = null;
}
if (LocalEndPoint != null)
{
LocalEndPoint = null;
}
IsDisposing = false;
}
private void ProcessAccept(SocketAsyncEventArgs e)
{
var clientState = new ClientState(e.AcceptSocket);
// Start the read loop on a new stack
Task.Run(() =>
{
clientState.ReceiveAsync();
});
// Accept the next connection request
StartAccept(e);
}
public void SetLocalEndPoint(IPEndPoint localEndPoint)
{
if (IsListening)
{
throw new NotSupportedException("Cannot set LocalEndPoint while socket is listening");
}
LocalEndPoint = localEndPoint;
if (Socket != null)
{
Socket.Close();
}
Socket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
{
ExclusiveAddressUse = true,
NoDelay = Daemon.Common.TcpNoDelay,
};
}
public void Start(int backlog = 100)
{
if (IsListening)
{
Stop();
}
if (LocalEndPoint == null)
{
throw new NullReferenceException("LocalEndPoint must be set to an instance of IPEndPoint");
}
Logging.WriteLine(Logging.LogLevel.Info, Logging.LogType.Server, $"Starting TCP listener on [{LocalEndPoint}]");
Socket.Bind(LocalEndPoint);
Socket.Listen(backlog);
IsListening = true;
StartAccept(null);
}
private void StartAccept(SocketAsyncEventArgs acceptEventArg)
{
if (acceptEventArg == null)
{
acceptEventArg = new SocketAsyncEventArgs();
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
}
else
{
// socket must be cleared since the context object is being reused
acceptEventArg.AcceptSocket = null;
}
bool willRaiseEvent = Socket.AcceptAsync(acceptEventArg);
if (!willRaiseEvent)
{
ProcessAccept(acceptEventArg);
}
}
void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
ProcessAccept(e);
}
public void Stop()
{
if (!IsListening) return;
Logging.WriteLine(Logging.LogLevel.Info, Logging.LogType.Server, $"Stopping TCP listener on [{Socket.LocalEndPoint}]");
Socket.Close();
Socket = null;
IsListening = false;
}
}
}