-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerPlus.cs
259 lines (257 loc) · 9.49 KB
/
ServerPlus.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//==============================================================
// The source from the Tony Lee edited and updated, without
// the author's permission is prohibited acts on the source
// code for commercial purposes or other reproduced and explained.
//==============================================================
// Copyright(C) 2014 - 2015 Tony Lee
// All rights reserved
// Input Item: ServerPlus
// PC Name: PC0525
// Name Space: TerumoMIS.CoreLibrary.Net.TCP
// File Name: ServerPlus
// User name: C1400008
// Location Time: 2015/7/16 15:23:50
// Tony Lee [mailto:liting5828424@gmail.com]
//==============================================================
// Update History :
// CLRVersion : 4.0.30319.18408
//==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TerumoMIS.CoreLibrary.Net.TCP
{
/// <summary>
/// TCP调用服务端基类
/// </summary>
public abstract class ServerPlus : IDisposable
{
/// <summary>
/// 配置信息
/// </summary>
protected internal fastCSharp.code.cSharp.tcpServer attribute;
/// <summary>
/// 服务名称
/// </summary>
public string ServiceName
{
get { return attribute.ServiceName; }
}
/// <summary>
/// TCP注册服务 客户端
/// </summary>
private fastCSharp.net.tcp.tcpRegister.client tcpRegisterClient;
/// <summary>
/// TCP监听服务器端套接字
/// </summary>
private Socket socket;
/// <summary>
/// 待处理的客户端集合
/// </summary>
private Socket[] newClients;
/// <summary>
/// 待处理的客户端数量
/// </summary>
private int newClientCount;
/// <summary>
/// 是否正在处理客户端集合
/// </summary>
private int isNewClientThread;
/// <summary>
/// 待处理的客户端集合访问锁
/// </summary>
private int newClientLock;
/// <summary>
/// 处理待处理客户端请求
/// </summary>
private Action waitSocketHandle;
/// <summary>
/// 是否已启动服务
/// </summary>
protected int isStart;
/// <summary>
/// 是否已启动服务
/// </summary>
public bool IsStart
{
get { return isStart != 0; }
}
/// <summary>
/// TCP调用服务端
/// </summary>
/// <param name="attribute">配置信息</param>
protected server(fastCSharp.code.cSharp.tcpServer attribute)
{
if (attribute == null) log.Error.Throw(log.exceptionType.Null);
if (attribute.TcpRegisterName != null)
{
tcpRegisterClient = fastCSharp.net.tcp.tcpRegister.client.Get(attribute.TcpRegisterName);
if (tcpRegisterClient == null) log.Error.Throw("TCP注册服务 " + attribute.TcpRegisterName + " 链接失败", true, false);
fastCSharp.net.tcp.tcpRegister.registerState state = tcpRegisterClient.Register(attribute);
if (state != fastCSharp.net.tcp.tcpRegister.registerState.Success) log.Error.Throw("TCP服务注册 " + attribute.ServiceName + " 失败 " + state.ToString(), true, false);
log.Default.Add(attribute.ServiceName + " 注册 " + attribute.Host + ":" + attribute.Port.toString(), false, false);
}
if (!attribute.IsServer) log.Default.Add("配置未指明的TCP服务端 " + attribute.ServiceName, true, false);
this.attribute = attribute;
}
/// <summary>
/// 停止服务事件
/// </summary>
public event Action OnDisposed;
/// <summary>
/// 停止服务
/// </summary>
public virtual void Dispose()
{
if (Interlocked.CompareExchange(ref isStart, 0, 1) == 1)
{
log.Default.Add("停止服务 " + attribute.ServiceName + "[" + attribute.Host + ":" + attribute.Port.toString() + "]", true, false);
fastCSharp.domainUnload.Remove(Dispose, false);
if (tcpRegisterClient != null)
{
tcpRegisterClient.RemoveRegister(attribute);
tcpRegisterClient = null;
}
pub.Dispose(ref this.socket);
Socket[] sockets = null;
interlocked.NoCheckCompareSetSleep0(ref newClientLock);
if (newClients == null || newClientCount == 0) newClientLock = 0;
else
{
try
{
Array.Copy(newClients, sockets = new Socket[newClientCount], newClientCount);
Array.Clear(newClients, 0, newClientCount);
newClientCount = 0;
}
finally { newClientLock = 0; }
if (sockets != null)
{
foreach (Socket socket in sockets) socket.shutdown();
}
}
if (OnDisposed != null) OnDisposed();
}
}
/// <summary>
/// 启动服务
/// </summary>
/// <returns>是否成功</returns>
protected bool start()
{
if (Interlocked.CompareExchange(ref isStart, 1, 0) == 0)
{
try
{
socket = new Socket(attribute.IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(attribute.IpAddress, attribute.Port));
socket.Listen(int.MaxValue);
newClients = new Socket[16];
waitSocketHandle = waitSocket;
}
catch (Exception error)
{
Dispose();
log.Error.ThrowReal(error, GetType().FullName + "服务器端口 " + attribute.Host + ":" + attribute.Port.toString() + " TCP连接失败)", false);
}
return isStart != 0;
}
return false;
}
/// <summary>
/// 启动服务
/// </summary>
/// <returns>是否成功</returns>
public bool Start()
{
if (start())
{
threadPool.TinyPool.FastStart(getSocket, null, null);
Thread.Sleep(0);
fastCSharp.domainUnload.Add(Dispose);
return true;
}
return false;
}
/// <summary>
/// 获取客户端请求
/// </summary>
protected abstract void getSocket();
/// <summary>
/// 获取客户端请求
/// </summary>
protected void acceptSocket()
{
while (isStart != 0)
{
try
{
while (isStart != 0)
{
Socket socket = this.socket.Accept();
interlocked.NoCheckCompareSetSleep0(ref newClientLock);
int isNewClientThread = this.isNewClientThread;
if (newClientCount == this.newClients.Length)
{
try
{
Socket[] newClients = new Socket[newClientCount << 1];
this.newClients.CopyTo(newClients, 0);
newClients[newClientCount] = socket;
this.newClients = newClients;
this.isNewClientThread = 1;
++newClientCount;
}
finally { newClientLock = 0; }
}
else
{
newClients[newClientCount] = socket;
this.isNewClientThread = 1;
++newClientCount;
newClientLock = 0;
}
if (isNewClientThread == 0) threadPool.TinyPool.FastStart(waitSocketHandle, null, null);
}
}
catch (Exception error)
{
if (isStart != 0)
{
log.Error.Add(error, null, false);
Thread.Sleep(1);
}
}
}
}
/// <summary>
/// 处理待处理客户端请求
/// </summary>
private unsafe void waitSocket()
{
while (isStart != 0)
{
interlocked.NoCheckCompareSetSleep0(ref newClientLock);
if (newClientCount == 0)
{
isNewClientThread = 0;
newClientLock = 0;
return;
}
Socket socket = newClients[--newClientCount];
newClientLock = 0;
newSocket(socket);
}
}
/// <summary>
/// 客户端请求处理
/// </summary>
/// <param name="socket">客户端套接字</param>
protected abstract void newSocket(Socket socket);
}
}