@@ -33,7 +33,6 @@ internal sealed partial class SocketConnection : TransportConnection
33
33
private readonly TaskCompletionSource _waitForConnectionClosedTcs = new TaskCompletionSource ( ) ;
34
34
private bool _connectionClosed ;
35
35
private readonly bool _waitForData ;
36
- private int _connectionStarted ;
37
36
38
37
internal SocketConnection ( Socket socket ,
39
38
MemoryPool < byte > memoryPool ,
@@ -68,32 +67,31 @@ internal SocketConnection(Socket socket,
68
67
69
68
var pair = DuplexPipe . CreateConnectionPair ( inputOptions , outputOptions ) ;
70
69
71
- _originalTransport = pair . Transport ;
70
+ // Set the transport and connection id
71
+ Transport = _originalTransport = pair . Transport ;
72
72
Application = pair . Application ;
73
73
74
- Transport = new SocketDuplexPipe ( this ) ;
75
-
76
74
InitializeFeatures ( ) ;
77
75
}
78
76
79
- public IDuplexPipe InnerTransport => _originalTransport ;
80
-
81
77
public PipeWriter Input => Application . Output ;
82
78
83
79
public PipeReader Output => Application . Input ;
84
80
85
81
public override MemoryPool < byte > MemoryPool { get ; }
86
82
87
- private void EnsureStarted ( )
83
+ public void Start ( )
88
84
{
89
- if ( _connectionStarted == 1 || Interlocked . CompareExchange ( ref _connectionStarted , 1 , 0 ) == 1 )
85
+ try
90
86
{
91
- return ;
87
+ // Spawn send and receive logic
88
+ _receivingTask = DoReceive ( ) ;
89
+ _sendingTask = DoSend ( ) ;
90
+ }
91
+ catch ( Exception ex )
92
+ {
93
+ _trace . LogError ( 0 , ex , $ "Unexpected exception in { nameof ( SocketConnection ) } .{ nameof ( Start ) } .") ;
92
94
}
93
-
94
- // Offload these to avoid potentially blocking the first read/write/flush
95
- _receivingTask = Task . Run ( DoReceive ) ;
96
- _sendingTask = Task . Run ( DoSend ) ;
97
95
}
98
96
99
97
public override void Abort ( ConnectionAbortedException abortReason )
@@ -108,9 +106,6 @@ public override void Abort(ConnectionAbortedException abortReason)
108
106
// Only called after connection middleware is complete which means the ConnectionClosed token has fired.
109
107
public override async ValueTask DisposeAsync ( )
110
108
{
111
- // Just in case we haven't started the connection, start it here so we can clean up properly.
112
- EnsureStarted ( ) ;
113
-
114
109
_originalTransport . Input . Complete ( ) ;
115
110
_originalTransport . Output . Complete ( ) ;
116
111
@@ -130,7 +125,7 @@ public override async ValueTask DisposeAsync()
130
125
}
131
126
catch ( Exception ex )
132
127
{
133
- _trace . LogError ( 0 , ex , $ "Unexpected exception in { nameof ( SocketConnection ) } .") ;
128
+ _trace . LogError ( 0 , ex , $ "Unexpected exception in { nameof ( SocketConnection ) } .{ nameof ( Start ) } . ") ;
134
129
}
135
130
finally
136
131
{
0 commit comments