FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/performance/SslMulticastServer/Program.cs at master · devdev22plus/NetCoreServer · GitHub
devdev22plus
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
performance
/
SslMulticastServer
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
155 lines (131 loc) · 4.86 KB
Breadcrumbs
NetCoreServer
/
performance
/
SslMulticastServer
/
Program.cs
Copy path
File metadata and controls
155 lines (131 loc) · 4.86 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
using
System
;
using
System
.
Net
;
using
System
.
Net
.
Sockets
;
using
System
.
Security
.
Authentication
;
using
System
.
Security
.
Cryptography
.
X509Certificates
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
using
NetCoreServer
;
using
NDesk
.
Options
;
namespace
SslMulticastServer
{
class
MulticastSession
:
SslSession
{
public
MulticastSession
(
SslServer
server
)
:
base
(
server
)
{
}
public
override
bool
SendAsync
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
// Limit session send buffer to 1 megabyte
const
long
limit
=
1
*
1024
*
1024
;
long
pending
=
BytesPending
+
BytesSending
;
if
(
(
pending
+
size
)
>
limit
)
return
false
;
if
(
size
>
(
limit
-
pending
)
)
size
=
limit
-
pending
;
return
base
.
SendAsync
(
buffer
,
offset
,
size
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Console
.
WriteLine
(
$
"Session caught an error with code
{
error
}
"
)
;
}
}
class
MulticastServer
:
SslServer
{
public
MulticastServer
(
SslContext
context
,
IPAddress
address
,
int
port
)
:
base
(
context
,
address
,
port
)
{
}
protected
override
SslSession
CreateSession
(
)
{
return
new
MulticastSession
(
this
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Console
.
WriteLine
(
$
"Server caught an error with code
{
error
}
"
)
;
}
}
class
Program
{
static
void
Main
(
string
[
]
args
)
{
bool
help
=
false
;
int
port
=
2222
;
int
messagesRate
=
1000000
;
int
messageSize
=
32
;
var
options
=
new
OptionSet
(
)
{
{
"h|?|help"
,
v
=>
help
=
v
!=
null
}
,
{
"p|port="
,
v
=>
port
=
int
.
Parse
(
v
)
}
,
{
"m|messages="
,
v
=>
messagesRate
=
int
.
Parse
(
v
)
}
,
{
"s|size="
,
v
=>
messageSize
=
int
.
Parse
(
v
)
}
}
;
try
{
options
.
Parse
(
args
)
;
}
catch
(
OptionException
e
)
{
Console
.
Write
(
"Command line error: "
)
;
Console
.
WriteLine
(
e
.
Message
)
;
Console
.
WriteLine
(
"Try `--help' to get usage information."
)
;
return
;
}
if
(
help
)
{
Console
.
WriteLine
(
"Usage:"
)
;
options
.
WriteOptionDescriptions
(
Console
.
Out
)
;
return
;
}
Console
.
WriteLine
(
$
"Server port:
{
port
}
"
)
;
Console
.
WriteLine
(
$
"Messages rate:
{
messagesRate
}
"
)
;
Console
.
WriteLine
(
$
"Message size:
{
messageSize
}
"
)
;
Console
.
WriteLine
(
)
;
// Create and prepare a new SSL server context
var
context
=
new
SslContext
(
SslProtocols
.
Tls12
,
new
X509Certificate2
(
"server.pfx"
,
"qwerty"
)
)
;
// Create a new echo server
var
server
=
new
MulticastServer
(
context
,
IPAddress
.
Any
,
port
)
;
// server.OptionNoDelay = true;
server
.
OptionReuseAddress
=
true
;
// Start the server
Console
.
Write
(
"Server starting..."
)
;
server
.
Start
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
// Start the multicasting thread
bool
multicasting
=
true
;
var
multicaster
=
Task
.
Factory
.
StartNew
(
(
)
=>
{
// Prepare message to multicast
byte
[
]
message
=
new
byte
[
messageSize
]
;
// Multicasting loop
while
(
multicasting
)
{
var
start
=
DateTime
.
UtcNow
;
for
(
int
i
=
0
;
i
<
messagesRate
;
i
++
)
server
.
Multicast
(
message
)
;
var
end
=
DateTime
.
UtcNow
;
// Sleep for remaining time or yield
var
milliseconds
=
(
int
)
(
end
-
start
)
.
TotalMilliseconds
;
if
(
milliseconds
<
1000
)
Thread
.
Sleep
(
1000
-
milliseconds
)
;
else
Thread
.
Yield
(
)
;
}
}
)
;
Console
.
WriteLine
(
"Press Enter to stop the server or '!' to restart the server..."
)
;
// Perform text input
for
(
;
;
)
{
string
line
=
Console
.
ReadLine
(
)
;
if
(
string
.
IsNullOrEmpty
(
line
)
)
break
;
// Restart the server
if
(
line
==
"!"
)
{
Console
.
Write
(
"Server restarting..."
)
;
server
.
Restart
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
}
}
// Stop the multicasting thread
multicasting
=
false
;
multicaster
.
Wait
(
)
;
// Stop the server
Console
.
Write
(
"Server stopping..."
)
;
server
.
Stop
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL