11
11
using System . Net . Security ;
12
12
using System . Net . Sockets ;
13
13
using System . Runtime . CompilerServices ;
14
- using System . Threading . Channels ;
15
14
16
15
namespace Sisk . Cadente ;
17
16
@@ -20,19 +19,19 @@ namespace Sisk.Cadente;
20
19
/// </summary>
21
20
public sealed class HttpHost : IDisposable {
22
21
23
- const int MAX_WORKERS = 65536 ;
24
-
25
22
private readonly TcpListener _listener ;
26
- private readonly Channel < TcpClient > clientQueue ;
27
- private readonly ChannelWriter < TcpClient > writerQueue ;
28
- private readonly ChannelReader < TcpClient > readerQueue ;
29
- private readonly Thread channelConsumerThread ;
30
23
31
24
// internal readonly SemaphoreSlim HostLimiter = new SemaphoreSlim ( 64 );
32
25
private readonly LingerOption tcpLingerOption = new LingerOption ( true , 0 ) ;
33
26
34
27
private bool disposedValue ;
35
28
29
+ /// <summary>
30
+ /// Gets or sets the client queue size of all <see cref="HttpHost"/> instances. This value indicates how many
31
+ /// connections the server can maintain simultaneously before queueing other connections attempts.
32
+ /// </summary>
33
+ public static int QueueSize { get ; set ; } = 1024 ;
34
+
36
35
/// <summary>
37
36
/// Gets or sets the action handler for HTTP requests.
38
37
/// </summary>
@@ -60,12 +59,6 @@ public sealed class HttpHost : IDisposable {
60
59
/// <param name="endpoint">The <see cref="IPEndPoint"/> to listen on.</param>
61
60
public HttpHost ( IPEndPoint endpoint ) {
62
61
this . _listener = new TcpListener ( endpoint ) ;
63
- this . channelConsumerThread = new Thread ( this . ConsumerJobThread ) ;
64
- this . clientQueue = Channel . CreateBounded < TcpClient > (
65
- new BoundedChannelOptions ( MAX_WORKERS ) { SingleReader = true , SingleWriter = true , AllowSynchronousContinuations = true } ) ;
66
-
67
- this . readerQueue = this . clientQueue . Reader ;
68
- this . writerQueue = this . clientQueue . Writer ;
69
62
}
70
63
71
64
/// <summary>
@@ -86,27 +79,25 @@ public void Start () {
86
79
this . _listener . Server . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveRetryCount , 3 ) ;
87
80
this . _listener . Server . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , true ) ;
88
81
89
- this . _listener . Start ( ) ;
82
+ this . _listener . Start ( QueueSize ) ;
90
83
this . _listener . BeginAcceptTcpClient ( this . ReceiveClient , null ) ;
91
-
92
- this . channelConsumerThread . Start ( ) ;
93
84
}
94
85
95
86
private async void ReceiveClient ( IAsyncResult result ) {
96
87
97
88
this . _listener . BeginAcceptTcpClient ( this . ReceiveClient , null ) ;
98
89
var client = this . _listener . EndAcceptTcpClient ( result ) ;
99
90
100
- await this . writerQueue . WriteAsync ( client ) ;
91
+ await this . HandleTcpClient ( client ) ;
101
92
}
102
93
103
94
private async Task HandleTcpClient ( TcpClient client ) {
104
95
try {
105
96
{ // setup the tcpclient
106
97
client . NoDelay = true ;
107
98
108
- // client.ReceiveTimeout = this.TimeoutManager._ClientReadTimeoutSeconds;
109
- // client.SendTimeout = this.TimeoutManager._ClientWriteTimeoutSeconds;
99
+ client . ReceiveTimeout = this . TimeoutManager . _ClientReadTimeoutSeconds ;
100
+ client . SendTimeout = this . TimeoutManager . _ClientWriteTimeoutSeconds ;
110
101
111
102
client . ReceiveBufferSize = HttpConnection . REQUEST_BUFFER_SIZE ;
112
103
client . SendBufferSize = HttpConnection . RESPONSE_BUFFER_SIZE ;
@@ -148,19 +139,10 @@ await sslStream.AuthenticateAsServerAsync (
148
139
}
149
140
}
150
141
151
- async void ConsumerJobThread ( ) {
152
- while ( ! this . disposedValue && await this . readerQueue . WaitToReadAsync ( ) ) {
153
- while ( ! this . disposedValue && this . readerQueue . TryRead ( out var client ) ) {
154
- _ = this . HandleTcpClient ( client ) ;
155
- }
156
- }
157
- }
158
-
159
142
private void Dispose ( bool disposing ) {
160
143
if ( ! this . disposedValue ) {
161
144
if ( disposing ) {
162
145
this . _listener . Dispose ( ) ;
163
- this . channelConsumerThread . Join ( ) ;
164
146
}
165
147
166
148
this . disposedValue = true ;
0 commit comments