/
SocketConnectionListener.cs
148 lines (125 loc) · 4.97 KB
/
SocketConnectionListener.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
{
internal sealed class SocketConnectionListener : IConnectionListener
{
private readonly MemoryPool<byte> _memoryPool;
private readonly int _numSchedulers;
private readonly PipeScheduler[] _schedulers;
private readonly ISocketsTrace _trace;
private Socket _listenSocket;
private int _schedulerIndex;
private readonly SocketTransportOptions _options;
public EndPoint EndPoint { get; private set; }
internal SocketConnectionListener(
EndPoint endpoint,
SocketTransportOptions options,
ISocketsTrace trace)
{
EndPoint = endpoint;
_trace = trace;
_options = options;
_memoryPool = _options.MemoryPoolFactory();
var ioQueueCount = options.IOQueueCount;
if (ioQueueCount > 0)
{
_numSchedulers = ioQueueCount;
_schedulers = new IOQueue[_numSchedulers];
for (var i = 0; i < _numSchedulers; i++)
{
_schedulers[i] = new IOQueue();
}
}
else
{
var directScheduler = new PipeScheduler[] { PipeScheduler.ThreadPool };
_numSchedulers = directScheduler.Length;
_schedulers = directScheduler;
}
}
internal void Bind()
{
if (_listenSocket != null)
{
throw new InvalidOperationException(SocketsStrings.TransportAlreadyBound);
}
Socket listenSocket;
// Unix domain sockets are unspecified
var protocolType = EndPoint is UnixDomainSocketEndPoint ? ProtocolType.Unspecified : ProtocolType.Tcp;
listenSocket = new Socket(EndPoint.AddressFamily, SocketType.Stream, protocolType);
// Kestrel expects IPv6Any to bind to both IPv6 and IPv4
if (EndPoint is IPEndPoint ip && ip.Address == IPAddress.IPv6Any)
{
listenSocket.DualMode = true;
}
try
{
listenSocket.Bind(EndPoint);
}
catch (SocketException e) when (e.SocketErrorCode == SocketError.AddressAlreadyInUse)
{
throw new AddressInUseException(e.Message, e);
}
EndPoint = listenSocket.LocalEndPoint;
listenSocket.Listen(512);
_listenSocket = listenSocket;
}
public async ValueTask<ConnectionContext> AcceptAsync(CancellationToken cancellationToken = default)
{
while (true)
{
try
{
var acceptSocket = await _listenSocket.AcceptAsync();
// Only apply no delay to Tcp based endpoints
if (acceptSocket.LocalEndPoint is IPEndPoint)
{
acceptSocket.NoDelay = _options.NoDelay;
}
var connection = new SocketConnection(acceptSocket, _memoryPool, _schedulers[_schedulerIndex], _trace, _options.MaxReadBufferSize, _options.MaxWriteBufferSize);
connection.Start();
_schedulerIndex = (_schedulerIndex + 1) % _numSchedulers;
return connection;
}
catch (ObjectDisposedException)
{
// A call was made to UnbindAsync/DisposeAsync just return null which signals we're done
return null;
}
catch (SocketException e) when (e.SocketErrorCode == SocketError.OperationAborted)
{
// A call was made to UnbindAsync/DisposeAsync just return null which signals we're done
return null;
}
catch (SocketException)
{
// The connection got reset while it was in the backlog, so we try again.
_trace.ConnectionReset(connectionId: "(null)");
}
}
}
public ValueTask UnbindAsync(CancellationToken cancellationToken = default)
{
_listenSocket?.Dispose();
return default;
}
public ValueTask DisposeAsync()
{
_listenSocket?.Dispose();
// Dispose the memory pool
_memoryPool.Dispose();
return default;
}
}
}