FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/tests/UdpTests.cs at master · include76/NetCoreServer · GitHub
include76
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
tests
/
UdpTests.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
201 lines (177 loc) · 6.99 KB
Breadcrumbs
NetCoreServer
/
tests
/
UdpTests.cs
Copy path
File metadata and controls
201 lines (177 loc) · 6.99 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
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Net
;
using
System
.
Net
.
Sockets
;
using
System
.
Threading
;
using
NetCoreServer
;
using
Xunit
;
namespace
tests
{
class
EchoUdpClient
:
NetCoreServer
.
UdpClient
{
public
bool
Connected
{
get
;
set
;
}
public
bool
Disconnected
{
get
;
set
;
}
public
bool
Errors
{
get
;
set
;
}
public
EchoUdpClient
(
string
address
,
int
port
)
:
base
(
address
,
port
)
{
}
protected
override
void
OnConnected
(
)
{
Connected
=
true
;
ReceiveAsync
(
)
;
}
protected
override
void
OnDisconnected
(
)
{
Disconnected
=
true
;
}
protected
override
void
OnReceived
(
EndPoint
endpoint
,
byte
[
]
buffer
,
long
offset
,
long
size
)
{
ReceiveAsync
(
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Errors
=
true
;
}
}
class
EchoUdpServer
:
UdpServer
{
public
bool
Started
{
get
;
set
;
}
public
bool
Stopped
{
get
;
set
;
}
public
bool
Errors
{
get
;
set
;
}
public
EchoUdpServer
(
IPAddress
address
,
int
port
)
:
base
(
address
,
port
)
{
}
protected
override
void
OnStarted
(
)
{
Started
=
true
;
ReceiveAsync
(
)
;
}
protected
override
void
OnStopped
(
)
{
Stopped
=
true
;
}
protected
override
void
OnReceived
(
EndPoint
endpoint
,
byte
[
]
buffer
,
long
offset
,
long
size
)
{
SendAsync
(
endpoint
,
buffer
,
offset
,
size
)
;
}
protected
override
void
OnSent
(
EndPoint
endpoint
,
long
sent
)
{
ThreadPool
.
QueueUserWorkItem
(
o
=>
{
ReceiveAsync
(
)
;
}
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Errors
=
true
;
}
}
public
class
UdpTests
{
[
Fact
(
DisplayName
=
"UDP server test"
)
]
public
void
UdpServerTest
(
)
{
string
address
=
"127.0.0.1"
;
int
port
=
3333
;
// Create and start Echo server
var
server
=
new
EchoUdpServer
(
IPAddress
.
Any
,
port
)
;
Assert
.
True
(
server
.
Start
(
)
)
;
while
(
!
server
.
IsStarted
)
Thread
.
Yield
(
)
;
// Create and connect Echo client
var
client
=
new
EchoUdpClient
(
address
,
port
)
;
Assert
.
True
(
client
.
Connect
(
)
)
;
while
(
!
client
.
IsConnected
)
Thread
.
Yield
(
)
;
// Send a message to the Echo server
client
.
Send
(
"test"
)
;
// Wait for all data processed...
while
(
client
.
BytesReceived
!=
4
)
Thread
.
Yield
(
)
;
// Disconnect the Echo client
Assert
.
True
(
client
.
Disconnect
(
)
)
;
while
(
client
.
IsConnected
)
Thread
.
Yield
(
)
;
// Stop the Echo server
Assert
.
True
(
server
.
Stop
(
)
)
;
while
(
server
.
IsStarted
)
Thread
.
Yield
(
)
;
// Check the Echo server state
Assert
.
True
(
server
.
Started
)
;
Assert
.
True
(
server
.
Stopped
)
;
Assert
.
True
(
server
.
BytesSent
==
4
)
;
Assert
.
True
(
server
.
BytesReceived
==
4
)
;
Assert
.
True
(
!
server
.
Errors
)
;
// Check the Echo client state
Assert
.
True
(
client
.
Connected
)
;
Assert
.
True
(
client
.
Disconnected
)
;
Assert
.
True
(
client
.
BytesSent
==
4
)
;
Assert
.
True
(
client
.
BytesReceived
==
4
)
;
Assert
.
True
(
!
client
.
Errors
)
;
}
[
Fact
(
DisplayName
=
"UDP server random test"
)
]
public
void
UdpServerRandomTest
(
)
{
string
address
=
"127.0.0.1"
;
int
port
=
3334
;
// Create and start Echo server
var
server
=
new
EchoUdpServer
(
IPAddress
.
Any
,
port
)
;
Assert
.
True
(
server
.
Start
(
)
)
;
while
(
!
server
.
IsStarted
)
Thread
.
Yield
(
)
;
// Test duration in seconds
int
duration
=
10
;
// Clients collection
var
clients
=
new
List
<
EchoUdpClient
>
(
)
;
// Start random test
var
rand
=
new
Random
(
)
;
var
start
=
DateTime
.
UtcNow
;
while
(
(
DateTime
.
UtcNow
-
start
)
.
TotalSeconds
<
duration
)
{
// Create a new client and connect
if
(
(
rand
.
Next
(
)
%
100
)
==
0
)
{
if
(
clients
.
Count
<
100
)
{
// Create and connect Echo client
var
client
=
new
EchoUdpClient
(
address
,
port
)
;
clients
.
Add
(
client
)
;
client
.
Connect
(
)
;
while
(
!
client
.
IsConnected
)
Thread
.
Yield
(
)
;
}
}
// Connect/Disconnect the random client
else
if
(
(
rand
.
Next
(
)
%
100
)
==
0
)
{
if
(
clients
.
Count
>
0
)
{
int
index
=
rand
.
Next
(
)
%
clients
.
Count
;
var
client
=
clients
[
index
]
;
if
(
client
.
IsConnected
)
{
client
.
Disconnect
(
)
;
while
(
client
.
IsConnected
)
Thread
.
Yield
(
)
;
}
else
{
client
.
Connect
(
)
;
while
(
!
client
.
IsConnected
)
Thread
.
Yield
(
)
;
}
}
}
// Reconnect the random client
else
if
(
(
rand
.
Next
(
)
%
100
)
==
0
)
{
if
(
clients
.
Count
>
0
)
{
int
index
=
rand
.
Next
(
)
%
clients
.
Count
;
var
client
=
clients
[
index
]
;
if
(
client
.
IsConnected
)
{
client
.
Reconnect
(
)
;
while
(
!
client
.
IsConnected
)
Thread
.
Yield
(
)
;
}
}
}
// Send a message from the random client
else
if
(
(
rand
.
Next
(
)
%
1
)
==
0
)
{
if
(
clients
.
Count
>
0
)
{
int
index
=
rand
.
Next
(
)
%
clients
.
Count
;
var
client
=
clients
[
index
]
;
if
(
client
.
IsConnected
)
client
.
Send
(
"test"
)
;
}
}
// Sleep for a while...
Thread
.
Sleep
(
1
)
;
}
// Disconnect clients
foreach
(
var
client
in
clients
)
{
client
.
Disconnect
(
)
;
while
(
client
.
IsConnected
)
Thread
.
Yield
(
)
;
}
// Stop the Echo server
Assert
.
True
(
server
.
Stop
(
)
)
;
while
(
server
.
IsStarted
)
Thread
.
Yield
(
)
;
// Check the Echo server state
Assert
.
True
(
server
.
Started
)
;
Assert
.
True
(
server
.
Stopped
)
;
Assert
.
True
(
server
.
BytesSent
>
0
)
;
Assert
.
True
(
server
.
BytesReceived
>
0
)
;
Assert
.
True
(
!
server
.
Errors
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL