-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPartialSocket.cs
378 lines (377 loc) · 15.6 KB
/
PartialSocket.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System.Net.Sockets;
namespace TEArts.Networking.AsyncSocketer
{
public class PartialSocket : EventSocketer
{
private EventPool mbrEventAcceptor, mbrEventConnector, mbrEventSender, mbrEventRecevicer, mbrEventDisconnector;
private BufferManager mbrBufferAcceptor, mbrBufferConnector, mbrBufferSender, mbrBufferRecevicer, mbrBufferDisconnector;
private Pooler<EventSocketer> mbrAcceptSocketor;
protected PartialSocket()
{
}
public PartialSocket(SocketConfigure sc)
: base(sc)
{
//mbrBufferRecevicer = new BufferManager(Config.MaxDataConnection);
//mbrBufferSender = new BufferManager(Config.MaxDataConnection);
//mbrBufferConnector = new BufferManager(3);
//Config = sc;
//mbrEventConnector = new EventPool(Config.MaxDataConnection);
//mbrEventRecevicer = new EventPool(Config.MaxDataConnection);
//mbrEventSender = new EventPool(3);
}
public PartialSocket(SocketConfigure sc, Socket skt)
: base(sc, skt)
{
}
protected override ISocketer CreateClientSocket()
{
if (Config.Protocol == ProtocolType.Tcp)
{
return TcpSocketer.CreateSocket(Config);
}
else if (Config.Protocol == ProtocolType.Udp)
{
return UdpSocketer.CreateSocket(Config);
}
return base.CreateClientSocket();
}
protected override ISocketer CreateClientSocket(Socket skt)
{
if (skt != null)
{
if (skt.ProtocolType == ProtocolType.Tcp)
{
return TcpSocketer.CreateSocket(Config, skt);
}
else if (skt.ProtocolType == ProtocolType.Udp)
{
return UdpSocketer.CreateSocket(Config, skt);
}
else return new ISocketer(Config, skt);
}
else if ((Config.SocketType & EventSocketType.Client) == EventSocketType.Client)
{
if (Config.Protocol == ProtocolType.Tcp)
{
return TcpSocketer.CreateSocket(Config, skt);
}
else if (Config.Protocol == ProtocolType.Udp)
{
return UdpSocketer.CreateSocket(Config, skt);
}
}
else
{
if (skt.ProtocolType == ProtocolType.Tcp)
{
return TcpSocketer.CreateSocket(Config, skt);
}
else if (skt.ProtocolType == ProtocolType.Udp)
{
return UdpSocketer.CreateSocket(Config, skt);
}
}
return base.CreateClientSocket(skt);
}
protected override EventPool GetAcceptEventsPooler()
{
if (mbrEventAcceptor == null)
{
mbrEventAcceptor = new EventPool(Config.AsyncSendReceiveEventInstance);
mbrEventAcceptor.PoolerIdentity = mbrAcceptLocker + "_mbrEventAcceptor";
InitEventPooler(mbrEventAcceptor, GetAcceptBuffer(), Config.AsyncSendReceiveEventInstance, OnAccepted);
}
return mbrEventAcceptor;
}
protected override BufferManager GetAcceptBuffer()
{
if (mbrBufferAcceptor == null)
{
mbrBufferAcceptor = new BufferManager(Config.AsyncSendReceiveEventInstance);
mbrBufferAcceptor.ManagerIdentity = mbrAcceptLocker + "_mbrBufferAcceptor";
}
return mbrBufferAcceptor;
}
protected override EventPool GetConnectEventsPooler()
{
if (mbrEventConnector == null)
{
mbrEventConnector = new EventPool(Config.AsyncConnectEventInstance);
mbrEventConnector.PoolerIdentity = mbrReceverLocker + "_mbrEventConnector";
InitEventPooler(mbrEventConnector, GetConnectBuffer(), Config.AsyncConnectEventInstance, OnConnected);
}
#region ///
//if (mbrEventConnector == null)
//{
// mbrEventConnector = new EventPool(Config.MaxDataConnection);
// for (int i = 0; i < Config.MaxDataConnection; i++)
// {
// SocketAsyncEventArgs e = new SocketAsyncEventArgs();
// e.RemoteEndPoint = Config.RemotePoint;
// e.UserToken = new EventToken(mbrEventConnector.NextTokenID, Config);
// e.Completed += (o, x) =>
// {
// if (x.SocketError != SocketError.Success)
// {
// OnError(x);
// if (!Config.OnErrorContinue)
// {
// return;
// }
// }
// else
// {
// OnConnected(x);
// }
// GetConnectBuffer().FreeBuffer(x);
// mbrEventConnector.Push(x);
// };
// GetConnectBuffer().SetBuffer(e);
// //GetConnectBuffer().SetBuffer(e, 4);
// mbrEventConnector.Push(e);
// }
//}
//s = mbrEventConnector.Pop(Config);
//GetConnectBuffer().SetBuffer(s);
#endregion
return mbrEventConnector;
}
protected override BufferManager GetConnectBuffer()
{
if (mbrBufferConnector == null)
{
mbrBufferConnector = new BufferManager(Config.AsyncConnectEventInstance, Config.ConnectBufferSize);
mbrBufferConnector.ManagerIdentity = "mbrBufferConnector";
}
return mbrBufferConnector;
}
protected override EventPool GetReceiveEventsPooler()
{
if (mbrEventRecevicer == null)
{
mbrEventRecevicer = new EventPool(Config.AsyncSendReceiveEventInstance);
mbrEventRecevicer.PoolerIdentity = mbrReceverLocker + "_mbrEventRecevicer";
InitEventPooler(mbrEventRecevicer, GetRecevieBuffer(), Config.AsyncSendReceiveEventInstance, OnReceived);
}
#region ///
//if (mbrEventRecevicer == null)
//{
// mbrEventRecevicer = new EventPool(Config.MaxDataConnection);
// for (int i = 0; i < Config.MaxDataConnection; i++)
// {
// SocketAsyncEventArgs e = new SocketAsyncEventArgs();
// e.RemoteEndPoint = Config.RemotePoint;
// e.UserToken = new EventToken(mbrEventRecevicer.NextTokenID, Config);
// e.Completed += (o, x) =>
// {
// if (x.SocketError != SocketError.Success)
// {
// OnError(x);
// if (!Config.OnErrorContinue)
// {
// return;
// }
// }
// else
// {
// OnReceived(x);
// }
// GetRecevieBuffer().FreeBuffer(x);
// mbrEventRecevicer.Push(x);
// };
// GetRecevieBuffer().SetBuffer(e);
// //GetRecevieBuffer().SetBuffer(e, 5);
// mbrEventRecevicer.Push(e);
// }
//}
//s = mbrEventRecevicer.Pop(Config);
//GetConnectBuffer().SetBuffer(s);
#endregion
return mbrEventRecevicer;
}
protected override BufferManager GetRecevieBuffer()
{
if (mbrBufferRecevicer == null)
{
mbrBufferRecevicer = new BufferManager(Config.AsyncSendReceiveEventInstance);
mbrBufferRecevicer.ManagerIdentity = mbrReceverLocker + "_mbrBufferRecevicer";
}
return mbrBufferRecevicer;
}
protected override EventPool GetSendEventsPooler()
{
if (mbrEventSender == null)
{
mbrEventSender = new EventPool(Config.AsyncSendReceiveEventInstance);
mbrEventSender.PoolerIdentity = mbrSenderLocker + " mbrEventSender";
InitEventPooler(mbrEventSender, GetSendBuffer(), Config.AsyncSendReceiveEventInstance, OnSended);
}
#region ///
//if (mbrEventSender == null)
//{
// mbrEventSender = new EventPool(Config.MaxDataConnection);
// for (int i = 0; i < Config.MaxDataConnection; i++)
// {
// SocketAsyncEventArgs e = new SocketAsyncEventArgs();
// e.RemoteEndPoint = Config.RemotePoint;
// e.UserToken = new EventToken(mbrEventSender.NextTokenID, Config);
// e.Completed += (o, x) =>
// {
// if (x.SocketError != SocketError.Success)
// {
// OnError(x);
// if (!Config.OnErrorContinue)
// {
// return;
// }
// }
// else
// {
// OnSended(x);
// }
// GetSendBuffer().FreeBuffer(x);
// mbrEventSender.Push(x);
// };
// GetSendBuffer().SetBuffer(e);
// //GetSendBuffer().SetBuffer(e, 3);
// mbrEventSender.Push(e);
// }
//}
//s = mbrEventSender.Pop(Config);
//GetConnectBuffer().SetBuffer(s);
#endregion
return mbrEventSender;
}
protected override BufferManager GetSendBuffer()
{
if (mbrBufferSender == null)
{
mbrBufferSender = new BufferManager(Config.AsyncSendReceiveEventInstance);
mbrBufferSender.ManagerIdentity = mbrSenderLocker + " mbrBufferSender";
}
return mbrBufferSender;
}
protected override EventPool GetDisconnectEventsPooler()
{
if (mbrEventDisconnector == null)
{
mbrEventDisconnector = new EventPool(Config.AsyncSendReceiveEventInstance);
mbrEventDisconnector.PoolerIdentity = " mbrEventDisconnector";
InitEventPooler(mbrEventDisconnector, GetDisonnectBuffer(), Config.AsyncConnectEventInstance, OnDisconnected);
}
#region ///
//if (mbrEventDisconnector == null)
//{
// mbrEventDisconnector = new EventPool(Config.MaxDataConnection);
// for (int i = 0; i < Config.MaxDataConnection; i++)
// {
// SocketAsyncEventArgs e = new SocketAsyncEventArgs();
// e.RemoteEndPoint = Config.RemotePoint;
// e.UserToken = new EventToken(mbrEventDisconnector.NextTokenID, Config);
// e.Completed += (o, x) =>
// {
// if (x.SocketError != SocketError.Success)
// {
// OnError(x);
// if (!Config.OnErrorContinue)
// {
// return;
// }
// }
// else
// {
// OnDisconnected(x);
// }
// GetConnectBuffer().FreeBuffer(x);
// mbrEventDisconnector.Push(x);
// };
// GetConnectBuffer().SetBuffer(e);
// //GetConnectBuffer().SetBuffer(e, 4);
// mbrEventDisconnector.Push(e);
// }
//}
//s = mbrEventDisconnector.Pop(Config);
//GetConnectBuffer().SetBuffer(s);
#endregion
return mbrEventDisconnector;
}
protected override BufferManager GetDisonnectBuffer()
{
if (mbrBufferDisconnector == null)
{
mbrBufferDisconnector = new BufferManager(Config.AsyncConnectEventInstance, Config.ConnectBufferSize);
mbrBufferDisconnector.ManagerIdentity = "mbrBufferConnector";
}
return mbrBufferDisconnector;
}
protected override EventSocketer GetAcceptInstance()
{
return this;
}
//protected override Pooler<EventSocketer> GetAcceptInstancePooler()
//{
// if (mbrAcceptSocketor == null)
// {
// mbrAcceptSocketor = new Pooler<EventSocketer>(Config.AsyncSendReceiveEventInstance);
// for (int i = 0; i < Config.AsyncSendReceiveEventInstance; i++)
// {
// PartialSocket p = new PartialSocket(TransferConfig);
// p.Disconnected += (o, e) =>
// {
// DebugInfo("Client disconnected, recylic Socket");
// mbrAcceptSocketor.Pushin(p);
// };
// mbrAcceptSocketor.Pushin(p);
// }
// }
// return mbrAcceptSocketor;
//}
public override EventSocketer New(SocketConfigure sc, Socket accept)
{
//ClientSocket = CreateClientSocket(accept);
//return base.New(sc, accept);
PartialSocket partial = new PartialSocket(sc, accept);
return partial;
}
public override EventSocketer New(Socket accept)
{
return New(Config,accept);
}
//protected override void ResetConnectAsyncEvents()
//{
// if (mbrEventConnector != null)
// {
// mbrEventConnector.ForceClose();
// }
//}
//protected override void ResetDisconnectAsyncEvents()
//{
// if (mbrEventDisconnector != null)
// {
// mbrEventDisconnector.ForceClose();
// }
//}
//protected override void ResetReceiveAsyncEvents()
//{
// if (mbrEventRecevicer != null)
// {
// mbrEventRecevicer.ForceClose();
// }
//}
//protected override void ResetSendAsyncEvents()
//{
// if (mbrEventSender != null)
// {
// mbrEventSender.ForceClose();
// }
//}
#if DEBUG
protected override int EventPoolerSizeConnect { get { return mbrEventDisconnector == null ? -1 : mbrEventDisconnector.Count; } }
protected override int EventPoolerSizeReceive { get { return mbrEventRecevicer == null ? -1 : mbrEventRecevicer.Count; } }
protected override int EventPoolerSizeSend { get { return mbrEventSender == null ? -1 : mbrEventSender.Count; } }
protected override int MessagePoolerSizeReceive { get { return mbrBufferRecevicer == null ? -1 : mbrBufferRecevicer.Count; } }
protected override int MessagePoolerSizeSend { get { return mbrBufferSender == null ? -1 : mbrBufferSender.Count; } }
#endif
}
}