4
4
// The code below is licensed under the MIT license as
5
5
// of the date of its publication, available at
6
6
//
7
- // File name: CadenteHttpListener .cs
7
+ // File name: HttpHost .cs
8
8
// Repository: https://github.com/sisk-http/core
9
9
10
10
using System . Net ;
@@ -17,10 +17,9 @@ namespace Sisk.Cadente;
17
17
/// <summary>
18
18
/// Represents an HTTP host that listens for incoming TCP connections and handles HTTP requests.
19
19
/// </summary>
20
- public sealed class CadenteHttpListener : IDisposable {
20
+ public sealed class HttpHost : IDisposable {
21
21
22
- // defines the connection queue size (worker connections)
23
- const int QUEUE_SIZE = 512 ;
22
+ const int QUEUE_SIZE = 256 ;
24
23
25
24
private readonly TcpListener _listener ;
26
25
private readonly Channel < TcpClient > clientQueue ;
@@ -31,32 +30,32 @@ public sealed class CadenteHttpListener : IDisposable {
31
30
private bool disposedValue ;
32
31
33
32
/// <summary>
34
- /// Gets the action handler for processing HTTP actions .
33
+ /// Gets or sets the action handler for HTTP requests .
35
34
/// </summary>
36
35
public HttpAction ActionHandler { get ; }
37
36
38
37
/// <summary>
39
- /// Gets a value indicating whether the host has been disposed.
38
+ /// Gets a value indicating whether this <see cref="HttpHost"/> has been disposed.
40
39
/// </summary>
41
40
public bool IsDisposed { get => this . disposedValue ; }
42
41
43
42
/// <summary>
44
- /// Gets or sets the port on which the HTTP host listens for incoming connections.
45
- /// Default is 8080.
43
+ /// Gets or sets the port number to listen on.
46
44
/// </summary>
47
45
public int Port { get ; set ; } = 8080 ;
48
46
49
47
/// <summary>
50
- /// Gets or sets the options for HTTPS configuration.
48
+ /// Gets or sets the HTTPS options for secure connections. Setting an <see cref="Sisk.Cadente.HttpsOptions"/> object in this
49
+ /// property, the <see cref="Sisk.Cadente.HttpHost"/> will use HTTPS instead of HTTP.
51
50
/// </summary>
52
51
public HttpsOptions ? HttpsOptions { get ; set ; }
53
52
54
53
/// <summary>
55
- /// Initializes a new instance of the <see cref="CadenteHttpListener "/> class with the specified port and action handler .
54
+ /// Initializes a new instance of the <see cref="HttpHost "/> class.
56
55
/// </summary>
57
- /// <param name="port">The port on which the host will listen for incoming connections .</param>
58
- /// <param name="actionHandler">The action handler for processing HTTP actions .</param>
59
- public CadenteHttpListener ( int port , HttpAction actionHandler ) {
56
+ /// <param name="port">The port number to listen on .</param>
57
+ /// <param name="actionHandler">The action handler for HTTP requests .</param>
58
+ public HttpHost ( int port , HttpAction actionHandler ) {
60
59
this . _listener = new TcpListener ( new IPEndPoint ( IPAddress . Any , port ) ) ;
61
60
this . channelConsumerThread = new Thread ( this . ConsumerJobThread ) ;
62
61
this . clientQueue = Channel . CreateBounded < TcpClient > (
@@ -67,12 +66,12 @@ public CadenteHttpListener ( int port, HttpAction actionHandler ) {
67
66
}
68
67
69
68
/// <summary>
70
- /// Starts the Cadente HTTP host, beginning to listen for incoming connections.
69
+ /// Starts the HTTP host and begins listening for incoming connections.
71
70
/// </summary>
72
71
public void Start ( ) {
73
72
ObjectDisposedException . ThrowIf ( this . disposedValue , this ) ;
74
73
75
- this . _listener . Server . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveInterval , 3 ) ;
74
+ this . _listener . Server . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveInterval , 1 ) ;
76
75
this . _listener . Server . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveTime , 120 ) ;
77
76
this . _listener . Server . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveRetryCount , 3 ) ;
78
77
this . _listener . Server . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , true ) ;
@@ -96,8 +95,8 @@ private async Task HandleTcpClient ( TcpClient client ) {
96
95
{ // setup the tcpclient
97
96
client . NoDelay = true ;
98
97
99
- client . ReceiveTimeout = 5_000 ;
100
- client . SendTimeout = 5_000 ;
98
+ client . ReceiveTimeout = ( int ) TimeSpan . FromSeconds ( 5 ) . TotalMilliseconds ;
99
+ client . SendTimeout = ( int ) TimeSpan . FromSeconds ( 5 ) . TotalMilliseconds ;
101
100
102
101
client . ReceiveBufferSize = HttpConnection . REQUEST_BUFFER_SIZE ;
103
102
client . SendBufferSize = HttpConnection . RESPONSE_BUFFER_SIZE ;
@@ -126,14 +125,16 @@ await sslStream.AuthenticateAsServerAsync (
126
125
checkCertificateRevocation : this . HttpsOptions . CheckCertificateRevocation ,
127
126
enabledSslProtocols : this . HttpsOptions . AllowedProtocols ) ;
128
127
}
129
- catch ( Exception ) {
128
+ catch ( Exception ex ) {
130
129
//Logger.LogInformation ( $"[{connection.Id}] Failed SSL authenticate: {ex.Message}" );
131
- // TODO drop connection with 401
132
130
}
133
131
}
134
132
133
+ //Logger.LogInformation ( $"[{connection.Id}] Begin handle connection" );
135
134
var state = await connection . HandleConnectionEvents ( ) ;
136
135
136
+ //Logger.LogInformation ( $"[{connection.Id}] Ended handling connection with state {state}" );
137
+
137
138
}
138
139
}
139
140
finally {
0 commit comments