FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/examples/ProtoClient/Program.cs at master · PerkProject/NetCoreServer · GitHub
PerkProject
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
examples
/
ProtoClient
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
374 lines (290 loc) · 11.6 KB
Breadcrumbs
NetCoreServer
/
examples
/
ProtoClient
/
Program.cs
Copy path
File metadata and controls
374 lines (290 loc) · 11.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
using
System
.
Net
.
Sockets
;
using
System
.
Threading
;
using
TcpClient
=
NetCoreServer
.
TcpClient
;
using
com
.
chronoxor
.
simple
;
using
com
.
chronoxor
.
simple
.
FBE
;
namespace
ProtoClient
{
public
class
TcpProtoClient
:
TcpClient
{
public
TcpProtoClient
(
string
address
,
int
port
)
:
base
(
address
,
port
)
{
}
public
bool
ConnectAndStart
(
)
{
Console
.
WriteLine
(
$
"TCP protocol client starting a new session with Id '
{
Id
}
'..."
)
;
StartReconnectTimer
(
)
;
return
ConnectAsync
(
)
;
}
public
bool
DisconnectAndStop
(
)
{
Console
.
WriteLine
(
$
"TCP protocol client stopping the session with Id '
{
Id
}
'..."
)
;
StopReconnectTimer
(
)
;
DisconnectAsync
(
)
;
return
true
;
}
public
override
bool
Reconnect
(
)
{
return
ReconnectAsync
(
)
;
}
private
Timer
_reconnectTimer
;
public
void
StartReconnectTimer
(
)
{
// Start the reconnect timer
_reconnectTimer
=
new
Timer
(
state
=>
{
Console
.
WriteLine
(
$
"TCP reconnect timer connecting the client session with Id '
{
Id
}
'..."
)
;
ConnectAsync
(
)
;
}
,
null
,
Timeout
.
InfiniteTimeSpan
,
Timeout
.
InfiniteTimeSpan
)
;
}
public
void
StopReconnectTimer
(
)
{
// Stop the reconnect timer
_reconnectTimer
?
.
Dispose
(
)
;
_reconnectTimer
=
null
;
}
public
delegate
void
ConnectedHandler
(
)
;
public
event
ConnectedHandler
Connected
=
(
)
=>
{
}
;
protected
override
void
OnConnected
(
)
{
Console
.
WriteLine
(
$
"TCP protocol client connected a new session with Id '
{
Id
}
' to remote address '
{
Address
}
' and port
{
Port
}
"
)
;
Connected
?
.
Invoke
(
)
;
}
public
delegate
void
DisconnectedHandler
(
)
;
public
event
DisconnectedHandler
Disconnected
=
(
)
=>
{
}
;
protected
override
void
OnDisconnected
(
)
{
Console
.
WriteLine
(
$
"TCP protocol client disconnected the session with Id '
{
Id
}
'"
)
;
// Setup and asynchronously wait for the reconnect timer
_reconnectTimer
?
.
Change
(
TimeSpan
.
FromSeconds
(
1
)
,
Timeout
.
InfiniteTimeSpan
)
;
Disconnected
?
.
Invoke
(
)
;
}
public
delegate
void
ReceivedHandler
(
byte
[
]
buffer
,
long
offset
,
long
size
)
;
public
event
ReceivedHandler
Received
=
(
buffer
,
offset
,
size
)
=>
{
}
;
protected
override
void
OnReceived
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
Received
?
.
Invoke
(
buffer
,
offset
,
size
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Console
.
WriteLine
(
$
"TCP protocol client caught a socket error:
{
error
}
"
)
;
}
#region IDisposable implementation
// Disposed flag.
private
bool
_disposed
;
protected
override
void
Dispose
(
bool
disposingManagedResources
)
{
if
(
!
_disposed
)
{
if
(
disposingManagedResources
)
{
// Dispose managed resources here...
StopReconnectTimer
(
)
;
}
// Dispose unmanaged resources here...
// Set large fields to null here...
// Mark as disposed.
_disposed
=
true
;
}
// Call Dispose in the base class.
base
.
Dispose
(
disposingManagedResources
)
;
}
// The derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits
// them from the base class.
#endregion
}
public
class
SimpleProtoClient
:
Client
,
ISenderListener
,
IReceiverListener
,
IDisposable
{
private
readonly
TcpProtoClient
_tcpProtoClient
;
public
Guid
Id
=>
_tcpProtoClient
.
Id
;
public
bool
IsConnected
=>
_tcpProtoClient
.
IsConnected
;
public
SimpleProtoClient
(
string
address
,
int
port
)
{
_tcpProtoClient
=
new
TcpProtoClient
(
address
,
port
)
;
_tcpProtoClient
.
Connected
+=
OnConnected
;
_tcpProtoClient
.
Disconnected
+=
OnDisconnected
;
_tcpProtoClient
.
Received
+=
OnReceived
;
ReceivedResponse_DisconnectRequest
+=
HandleDisconnectRequest
;
ReceivedResponse_SimpleResponse
+=
HandleSimpleResponse
;
ReceivedResponse_SimpleReject
+=
HandleSimpleReject
;
ReceivedResponse_SimpleNotify
+=
HandleSimpleNotify
;
}
private
void
DisposeClient
(
)
{
_tcpProtoClient
.
Connected
-=
OnConnected
;
_tcpProtoClient
.
Connected
-=
OnDisconnected
;
_tcpProtoClient
.
Received
-=
OnReceived
;
ReceivedResponse_DisconnectRequest
-=
HandleDisconnectRequest
;
ReceivedResponse_SimpleResponse
-=
HandleSimpleResponse
;
ReceivedResponse_SimpleReject
-=
HandleSimpleReject
;
ReceivedResponse_SimpleNotify
-=
HandleSimpleNotify
;
_tcpProtoClient
.
Dispose
(
)
;
}
public
bool
ConnectAndStart
(
)
{
return
_tcpProtoClient
.
ConnectAndStart
(
)
;
}
public
bool
DisconnectAndStop
(
)
{
return
_tcpProtoClient
.
DisconnectAndStop
(
)
;
}
public
bool
Reconnect
(
)
{
return
_tcpProtoClient
.
Reconnect
(
)
;
}
private
bool
_watchdog
;
private
Thread
_watchdogThread
;
public
bool
StartWatchdog
(
)
{
if
(
_watchdog
)
return
false
;
Console
.
WriteLine
(
"Watchdog thread starting..."
)
;
// Start the watchdog thread
_watchdog
=
true
;
_watchdogThread
=
new
Thread
(
WatchdogThread
)
;
Console
.
WriteLine
(
"Watchdog thread started!"
)
;
return
true
;
}
public
bool
StopWatchdog
(
)
{
if
(
!
_watchdog
)
return
false
;
Console
.
WriteLine
(
"Watchdog thread stopping..."
)
;
// Stop the watchdog thread
_watchdog
=
false
;
_watchdogThread
.
Join
(
)
;
Console
.
WriteLine
(
"Watchdog thread stopped!"
)
;
return
true
;
}
public
static
void
WatchdogThread
(
object
obj
)
{
var
instance
=
obj
as
SimpleProtoClient
;
if
(
instance
==
null
)
return
;
try
{
// Watchdog loop...
while
(
instance
.
_watchdog
)
{
var
utc
=
DateTime
.
UtcNow
;
// Watchdog the client
instance
.
Watchdog
(
utc
)
;
// Sleep for a while...
Thread
.
Sleep
(
1000
)
;
}
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$
"Config client watchdog thread terminated:
{
e
}
"
)
;
}
}
#region Connection handlers
public
delegate
void
ConnectedHandler
(
)
;
public
event
ConnectedHandler
Connected
=
(
)
=>
{
}
;
private
void
OnConnected
(
)
{
// Reset FBE protocol buffers
Reset
(
)
;
Connected
?
.
Invoke
(
)
;
}
public
delegate
void
DisconnectedHandler
(
)
;
public
event
DisconnectedHandler
Disconnected
=
(
)
=>
{
}
;
private
void
OnDisconnected
(
)
{
Disconnected
?
.
Invoke
(
)
;
}
public
long
OnSend
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
return
_tcpProtoClient
.
SendAsync
(
buffer
,
offset
,
size
)
?
size
:
0
;
}
public
void
OnReceived
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
Receive
(
buffer
,
offset
,
size
)
;
}
#endregion
#region Protocol handlers
private
void
HandleDisconnectRequest
(
DisconnectRequest
request
)
{
Console
.
WriteLine
(
$
"Received:
{
request
}
"
)
;
_tcpProtoClient
.
DisconnectAsync
(
)
;
}
private
void
HandleSimpleResponse
(
SimpleResponse
response
)
{
Console
.
WriteLine
(
$
"Received:
{
response
}
"
)
;
}
private
void
HandleSimpleReject
(
SimpleReject
reject
)
{
Console
.
WriteLine
(
$
"Received:
{
reject
}
"
)
;
}
private
void
HandleSimpleNotify
(
SimpleNotify
notify
)
{
Console
.
WriteLine
(
$
"Received:
{
notify
}
"
)
;
}
#endregion
#region IDisposable implementation
// Disposed flag.
private
bool
_disposed
;
// Implement IDisposable.
public
void
Dispose
(
)
{
Dispose
(
true
)
;
GC
.
SuppressFinalize
(
this
)
;
}
protected
virtual
void
Dispose
(
bool
disposingManagedResources
)
{
// The idea here is that Dispose(Boolean) knows whether it is
// being called to do explicit cleanup (the Boolean is true)
// versus being called due to a garbage collection (the Boolean
// is false). This distinction is useful because, when being
// disposed explicitly, the Dispose(Boolean) method can safely
// execute code using reference type fields that refer to other
// objects knowing for sure that these other objects have not been
// finalized or disposed of yet. When the Boolean is false,
// the Dispose(Boolean) method should not execute code that
// refer to reference type fields because those objects may
// have already been finalized."
if
(
!
_disposed
)
{
if
(
disposingManagedResources
)
{
// Dispose managed resources here...
DisposeClient
(
)
;
}
// Dispose unmanaged resources here...
// Set large fields to null here...
// Mark as disposed.
_disposed
=
true
;
}
}
#endregion
}
class
Program
{
static
void
Main
(
string
[
]
args
)
{
// Simple protocol server address
string
address
=
"127.0.0.1"
;
if
(
args
.
Length
>
0
)
address
=
args
[
0
]
;
// Simple protocol server port
int
port
=
4444
;
if
(
args
.
Length
>
1
)
port
=
int
.
Parse
(
args
[
1
]
)
;
Console
.
WriteLine
(
$
"Simple protocol server address:
{
address
}
"
)
;
Console
.
WriteLine
(
$
"Simple protocol server port:
{
port
}
"
)
;
Console
.
WriteLine
(
)
;
// Create a new simple protocol chat client
var
client
=
new
SimpleProtoClient
(
address
,
port
)
;
// Connect the client
Console
.
Write
(
"Client connecting..."
)
;
client
.
ConnectAndStart
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
client
.
StartWatchdog
(
)
;
Console
.
WriteLine
(
"Press Enter to stop the client or '!' to reconnect the client..."
)
;
// Perform text input
for
(
;
;
)
{
string
line
=
Console
.
ReadLine
(
)
;
if
(
string
.
IsNullOrEmpty
(
line
)
)
break
;
// Disconnect the client
if
(
line
==
"!"
)
{
Console
.
Write
(
"Client disconnecting..."
)
;
client
.
Reconnect
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
continue
;
}
// Send request to the simple protocol server
SimpleRequest
request
=
SimpleRequest
.
Default
;
request
.
Message
=
line
;
var
response
=
client
.
Request
(
request
)
.
Result
;
// Show string hash calculation result
Console
.
WriteLine
(
$
"Hash of '
{
line
}
' = 0x
{
response
.
Hash
:
X8
}
"
)
;
}
client
.
StopWatchdog
(
)
;
// Disconnect the client
Console
.
Write
(
"Client disconnecting..."
)
;
client
.
DisconnectAndStop
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL