11using Microsoft . AspNetCore . Http ;
2- using Microsoft . Extensions . Logging ;
32using Quick . Protocol . Utils ;
43using System ;
54using System . Collections ;
@@ -12,7 +11,7 @@ namespace Quick.Protocol.WebSocket.Server.AspNetCore
1211 public class QpWebSocketServer : QpServer
1312 {
1413 private Queue < WebSocketContext > webSocketContextQueue = new Queue < WebSocketContext > ( ) ;
15- private AutoResetEvent waitForConnectionAutoResetEvent ;
14+ private bool isStarted = false ;
1615
1716 private class WebSocketContext
1817 {
@@ -32,14 +31,18 @@ public QpWebSocketServer(QpWebSocketServerOptions options) : base(options) { }
3231
3332 public override void Start ( )
3433 {
35- waitForConnectionAutoResetEvent = new AutoResetEvent ( false ) ;
34+ isStarted = true ;
3635 lock ( webSocketContextQueue )
3736 webSocketContextQueue . Clear ( ) ;
3837 base . Start ( ) ;
3938 }
4039
4140 public Task OnNewConnection ( System . Net . WebSockets . WebSocket webSocket , ConnectionInfo connectionInfo )
4241 {
42+ //如果还没有开始接收,则直接关闭
43+ if ( ! isStarted )
44+ return webSocket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . NormalClosure , "" , CancellationToken . None ) ;
45+
4346 var connectionInfoStr = $ "WebSocket:{ connectionInfo . RemoteIpAddress } :{ connectionInfo . RemotePort } ";
4447 var cts = new CancellationTokenSource ( ) ;
4548 lock ( webSocketContextQueue )
@@ -48,7 +51,6 @@ public Task OnNewConnection(System.Net.WebSockets.WebSocket webSocket, Connectio
4851 connectionInfoStr ,
4952 webSocket ,
5053 cts ) ) ;
51- waitForConnectionAutoResetEvent . Set ( ) ;
5254 return Task . Delay ( - 1 , cts . Token ) . ContinueWith ( t =>
5355 {
5456 if ( LogUtils . LogConnection )
@@ -58,40 +60,42 @@ public Task OnNewConnection(System.Net.WebSockets.WebSocket webSocket, Connectio
5860
5961 public override void Stop ( )
6062 {
63+ isStarted = false ;
6164 lock ( webSocketContextQueue )
6265 webSocketContextQueue . Clear ( ) ;
63- waitForConnectionAutoResetEvent ? . Dispose ( ) ;
6466 base . Stop ( ) ;
6567 }
6668
67- protected override Task InnerAcceptAsync ( CancellationToken token )
69+ protected override async Task InnerAcceptAsync ( CancellationToken token )
6870 {
69- return Task . Run ( ( ) =>
71+ WebSocketContext [ ] webSocketContexts = null ;
72+ lock ( webSocketContextQueue )
73+ {
74+ webSocketContexts = webSocketContextQueue . ToArray ( ) ;
75+ webSocketContextQueue . Clear ( ) ;
76+ }
77+ //如果当前没有WebSocket连接,则等待0.1秒后再返回
78+ if ( webSocketContexts == null || webSocketContexts . Length == 0 )
79+ {
80+ await Task . Delay ( 100 ) ;
81+ return ;
82+ }
83+ foreach ( var context in webSocketContexts )
7084 {
71- waitForConnectionAutoResetEvent . WaitOne ( ) ;
72- WebSocketContext [ ] webSocketContexts = null ;
73- lock ( webSocketContextQueue )
85+ try
7486 {
75- webSocketContexts = webSocketContextQueue . ToArray ( ) ;
76- webSocketContextQueue . Clear ( ) ;
87+ if ( LogUtils . LogConnection )
88+ Console . WriteLine ( "[Connection]{0} connected." , context . ConnectionInfo ) ;
89+ OnNewChannelConnected ( new WebSocketServerStream ( context . WebSocket , context . Cts ) , context . ConnectionInfo , token ) ;
7790 }
78- foreach ( var context in webSocketContexts )
91+ catch ( Exception ex )
7992 {
80- try
81- {
82- if ( LogUtils . LogConnection )
83- Console . WriteLine ( "[Connection]{0} connected." , context . ConnectionInfo ) ;
84- OnNewChannelConnected ( new WebSocketServerStream ( context . WebSocket , context . Cts ) , context . ConnectionInfo , token ) ;
85- }
86- catch ( Exception ex )
87- {
88- if ( LogUtils . LogConnection )
89- Console . WriteLine ( "[Connection]Init&Start Channel error,reason:{0}" , ex . ToString ( ) ) ;
90- try { context . WebSocket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . InternalServerError , ex . Message , CancellationToken . None ) ; }
91- catch { }
92- }
93+ if ( LogUtils . LogConnection )
94+ Console . WriteLine ( "[Connection]Init&Start Channel error,reason:{0}" , ex . ToString ( ) ) ;
95+ try { await context . WebSocket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . InternalServerError , ex . Message , CancellationToken . None ) ; }
96+ catch { }
9397 }
94- } ) ;
98+ }
9599 }
96100 }
97101}
0 commit comments