@@ -10,6 +10,8 @@ namespace GodSharp.Sockets
1010{
1111 public sealed class TcpServer : NetBase < ITcpConnection , NetServerEventArgs > , ITcpServer , IDisposable
1212 {
13+ private readonly object ConnectionsLock = new object ( ) ;
14+
1315 private bool stopping = false ;
1416
1517 private bool running ;
@@ -18,9 +20,11 @@ public sealed class TcpServer :NetBase<ITcpConnection, NetServerEventArgs>, ITcp
1820 public Socket Instance { get ; private set ; }
1921
2022 public IPEndPoint LocalEndPoint { get ; private set ; }
21-
23+
2224 public IDictionary < string , ITcpConnection > Connections { get ; private set ; } = new Dictionary < string , ITcpConnection > ( ) ;
2325
26+ public SocketEventHandler < NetServerEventArgs > OnServerException { get ; set ; }
27+
2428 public TcpServer ( TcpServerOptions options ) => OnConstructing ( options ) ;
2529
2630 public TcpServer ( int port = 7788 , string host = null , int backlog = int . MaxValue , AddressFamily family = AddressFamily . InterNetwork , string name = null , int id = 0 )
@@ -33,6 +37,7 @@ public TcpServer(int port = 7788, string host = null, int backlog = int.MaxValue
3337 }
3438 catch ( Exception ex )
3539 {
40+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) { Exception = ex } ) ;
3641 throw ex ;
3742 }
3843 }
@@ -78,6 +83,7 @@ private void OnConstructing(TcpServerOptions options)
7883 }
7984 catch ( Exception ex )
8085 {
86+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) { Exception = ex } ) ;
8187 throw ex ;
8288 }
8389 }
@@ -96,7 +102,7 @@ public override void Start()
96102 }
97103 catch ( Exception ex )
98104 {
99- throw ex ;
105+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) { Exception = ex } ) ;
100106 }
101107 }
102108
@@ -112,7 +118,7 @@ public override void Stop()
112118 }
113119 catch ( Exception ex )
114120 {
115- throw ex ;
121+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) { Exception = ex } ) ;
116122 }
117123 finally
118124 {
@@ -121,7 +127,7 @@ public override void Stop()
121127
122128 running = false ;
123129 stopping = false ;
124-
130+
125131 OnStopped ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) ) ;
126132 }
127133
@@ -133,12 +139,23 @@ protected override void OnDisconnectedHandler(NetClientEventArgs<ITcpConnection>
133139
134140 private void BeginAccept ( )
135141 {
136- Instance . BeginAccept ( AcceptCallback , null ) ;
142+ try
143+ {
144+ Instance . BeginAccept ( AcceptCallback , null ) ;
145+ }
146+ catch ( Exception ex )
147+ {
148+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) { Exception = ex } ) ;
149+
150+ Stop ( ) ;
151+ }
137152 }
138153
139154 private void AcceptCallback ( IAsyncResult result )
140155 {
141- bool error = false ;
156+ bool accept = false ;
157+ ITcpConnection connection = null ;
158+
142159 try
143160 {
144161 if ( ! Running || stopping ) return ;
@@ -148,35 +165,38 @@ private void AcceptCallback(IAsyncResult result)
148165 if ( stopping ) return ;
149166
150167 BeginAccept ( ) ;
168+ accept = true ;
151169
152- ITcpConnection connection = new TcpConnection ( socket ) { OnConnected = OnConnectedHandler , OnReceived = OnReceivedHandler , OnDisconnected = OnDisconnectedHandler , OnException = OnExceptionHandler } ;
170+ connection = new TcpConnection ( socket ) { OnConnected = OnConnectedHandler , OnReceived = OnReceivedHandler , OnDisconnected = OnDisconnectedHandler , OnException = OnExceptionHandler } ;
153171 connection . Start ( ) ;
154172
155173 RemoveListener ( connection . Key ) ;
156174
157- Connections . Add ( connection . Key , connection ) ;
175+ lock ( ConnectionsLock )
176+ {
177+ Connections . Add ( connection . Key , connection ) ;
178+ }
158179
159180 OnConnected ? . Invoke ( new NetClientEventArgs < ITcpConnection > ( connection ) ) ;
160181 }
161- catch ( SocketException ex )
162- {
163- error = true ;
164- throw ex ;
165- }
166182 catch ( Exception ex )
167183 {
168- error = true ;
169- throw ex ;
184+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint , connection ) { Exception = ex } ) ;
170185 }
171186 finally
172187 {
173- if ( error ) Stop ( ) ;
188+ if ( Running && ! accept && ! stopping ) BeginAccept ( ) ;
174189 }
175190 }
176191
177192 private void RemoveListener ( string key )
178193 {
179- bool existed = Connections ? . ContainsKey ( key ) == true ;
194+ bool existed = false ;
195+
196+ lock ( ConnectionsLock )
197+ {
198+ existed = Connections ? . ContainsKey ( key ) == true ;
199+ }
180200
181201 try
182202 {
@@ -188,11 +208,17 @@ private void RemoveListener(string key)
188208 }
189209 catch ( Exception ex )
190210 {
191- Console . WriteLine ( ex . Message ) ;
211+ OnServerException ? . Invoke ( new NetServerEventArgs ( this , LocalEndPoint ) { Exception = ex } ) ;
192212 }
193213 finally
194214 {
195- if ( existed ) Connections ? . Remove ( key ) ;
215+ if ( existed )
216+ {
217+ lock ( ConnectionsLock )
218+ {
219+ Connections ? . Remove ( key ) ;
220+ }
221+ }
196222 }
197223 }
198224
0 commit comments