@@ -84,6 +84,16 @@ private void Init()
8484 Task . Factory . StartNew ( InitInternal ) ;
8585 }
8686
87+ private void CloseSocket ( )
88+ {
89+ if ( _socket != null )
90+ {
91+ _socket . Dispose ( ) ;
92+ }
93+
94+ _socket = null ;
95+ }
96+
8797 private void InitInternal ( )
8898 {
8999 if ( Interlocked . CompareExchange ( ref _isIniting , 1 , 0 ) == 1 )
@@ -126,8 +136,7 @@ private void InitInternal()
126136 var recLength = 0 ;
127137
128138 // killing old socket
129- if ( _socket != null )
130- _socket . Dispose ( ) ;
139+ CloseSocket ( ) ;
131140 _socket = new Socket ( AddressFamily . InterNetwork , SocketType . Dgram , ProtocolType . Udp ) ;
132141 _socket . Connect ( destEP ) ;
133142
@@ -170,7 +179,7 @@ private void InitInternal()
170179 LogHelper . Log . WriteLine ( "No response from server " + destEP ) ;
171180 if ( DateTime . UtcNow . Subtract ( _lastInitRequest ) . TotalSeconds > 60 )
172181 {
173- LogHelper . Log . WriteLine ( "Stopping connect atteptions to " + destEP + " until another request will occure " ) ;
182+ LogHelper . Log . WriteLine ( "Stopping connect atteptions to " + destEP + " until another request will occur " ) ;
174183 }
175184 }
176185
@@ -205,36 +214,42 @@ private void InitInternal()
205214 }
206215 }
207216
217+ private void DropInit ( )
218+ {
219+ _isInited = false ;
220+ if ( _config . KeepAlive )
221+ Init ( ) ;
222+ }
223+
208224 private void PingCycle ( )
209225 {
210- DateTime lastReceiveLast = DateTime . MinValue ;
226+ DateTime lastPingDate = DateTime . MinValue ;
227+ var pingSpan = TimeSpan . FromSeconds ( _config . PingInterval ) ;
228+
211229 while ( ! _disposed )
212230 {
213231 if ( _isInited )
214232 {
215233 // problem with server? no answers, dropping connection
216- if ( Session . LastActivity == lastReceiveLast )
234+ if ( Session . SendReceiveDifference > TimeSpan . FromSeconds ( _config . PingInterval * 2 ) )
217235 {
218- _isInited = false ;
219- lastReceiveLast = DateTime . MinValue ;
220- if ( _config . KeepAlive )
221- Init ( ) ;
236+ DropInit ( ) ;
222237 }
223238
224- if ( _config . KeepAlive || _lastSend . Subtract ( Session . LastActivity ) . TotalSeconds > _config . PingInterval )
239+ if ( ( _config . KeepAlive && DateTime . UtcNow . Subtract ( lastPingDate ) > pingSpan ) || Session . SendReceiveDifference > pingSpan )
225240 {
226- lastReceiveLast = Session . LastActivity ;
227241 _socket . Send ( new byte [ ] { ( byte ) StateFlags . Ping , 0 , 0 , 0 } , 4 , SocketFlags . None ) ;
228- _lastSend = DateTime . UtcNow ;
242+ lastPingDate = DateTime . UtcNow ;
229243 }
230244 }
231245 else
232246 {
233- // force renew connection attepmt
234- _lastInitRequest = DateTime . UtcNow ;
247+ // force renew connection attempt
248+ if ( _config . KeepAlive )
249+ _lastInitRequest = DateTime . UtcNow ;
235250 }
236251
237- Thread . Sleep ( _config . PingInterval * 1000 ) ;
252+ Thread . Sleep ( 1000 ) ;
238253 }
239254 }
240255
@@ -251,15 +266,13 @@ protected override void Send(byte[] packet, int packetLen)
251266 {
252267 var lenToSend = _encryptHelper . Encrypt ( packet , packetLen ) ;
253268 var packetToSend = _encryptHelper . InnerBuf ;
254- _lastSend = DateTime . UtcNow ;
269+ Session . UpdateReceiveActivity ( ) ;
255270 _socket . Send ( packetToSend , lenToSend , SocketFlags . None ) ;
256271 }
257272 }
258273
259274 private readonly byte [ ] _receiveBuffer = new byte [ 65536 ] ;
260275
261- private DateTime _lastSend ;
262-
263276 private void ReceiveCycle ( )
264277 {
265278 byte [ ] buf = _receiveBuffer ;
@@ -276,18 +289,14 @@ private void ReceiveCycle()
276289 {
277290 len = _socket . Receive ( buf ) ;
278291 }
279- catch ( Exception ex )
292+ catch ( Exception /* ex*/ )
280293 {
281294 if ( _isIniting == 1 && _isInited )
282295 {
283296 LogHelper . Log . WriteLine ( "Receive data error" ) ;
284297 _isInited = false ;
285298 // LogHelper.Log.WriteLine(ex);
286- if ( _socket != null )
287- {
288- _socket . Dispose ( ) ;
289- _socket = null ;
290- }
299+ CloseSocket ( ) ;
291300
292301 if ( _config . KeepAlive ) Init ( ) ;
293302 }
@@ -303,14 +312,12 @@ private void ReceiveCycle()
303312 if ( buf [ 0 ] != ( byte ) StateFlags . Pong )
304313 {
305314 LogHelper . Log . WriteLine ( "Received an error flag from " + _socket . RemoteEndPoint ) ;
306- _isInited = false ;
307- // failed data
308- Init ( ) ;
315+ DropInit ( ) ;
309316 continue ;
310317 }
311318 }
312319
313- Session . UpdateLastActivity ( ) ;
320+ Session . UpdateReceiveActivity ( ) ;
314321
315322 var decryptHelper = Session . Decryptor ;
316323 var decLen = decryptHelper . Decrypt ( buf , 0 ) ;
@@ -323,7 +330,7 @@ public override void Dispose()
323330 base . Dispose ( ) ;
324331 _disposed = true ;
325332 _initingEvent . Set ( ) ;
326- _socket . Dispose ( ) ;
333+ CloseSocket ( ) ;
327334 }
328335 }
329336}
0 commit comments