@@ -360,11 +360,15 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
360
360
using ( Stream stream = httpState . Request . EndGetRequestStream ( ar ) )
361
361
{
362
362
stream . Write ( httpState . OutgoingData , 0 , httpState . OutgoingData . Length ) ;
363
+ // We must re-send the data when connection error is not fatal.
364
+ // So, don't set here: httpState.OutgoingData = null;
363
365
}
364
366
365
367
WebResponse response = httpState . Request . GetResponse ( ) ;
366
368
httpState . Request = null ;
367
369
370
+ #region Read Headers
371
+
368
372
lock ( SyncObject )
369
373
{
370
374
foreach ( string header in response . Headers . AllKeys )
@@ -387,9 +391,11 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
387
391
case "SessionID" :
388
392
SessionID = elements [ 1 ] ;
389
393
break ;
394
+
390
395
case "GW-IP" :
391
396
GatewayIP = elements [ 1 ] ;
392
397
break ;
398
+
393
399
case "Session" :
394
400
if ( "close" == elements [ 1 ] )
395
401
{
@@ -399,6 +405,7 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
399
405
connected = false ;
400
406
}
401
407
break ;
408
+
402
409
case "Action" :
403
410
break ;
404
411
}
@@ -408,22 +415,32 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
408
415
}
409
416
}
410
417
418
+ #endregion
411
419
412
- Stream responseStream = response . GetResponseStream ( ) ;
413
420
MemoryStream responseReadStream = new MemoryStream ( ) ;
414
- byte [ ] readBuffer = new byte [ 8192 ] ;
415
421
int read = 0 ;
416
422
try
417
423
{
418
- do
424
+ using ( Stream responseStream = response . GetResponseStream ( ) )
419
425
{
420
- read = responseStream . Read ( readBuffer , 0 , readBuffer . Length ) ;
421
- if ( read > 0 )
426
+ byte [ ] readBuffer = new byte [ 8192 ] ;
427
+ do
422
428
{
423
- responseReadStream . Write ( readBuffer , 0 , read ) ;
429
+ read = responseStream . Read ( readBuffer , 0 , readBuffer . Length ) ;
430
+ if ( read > 0 )
431
+ {
432
+ responseReadStream . Write ( readBuffer , 0 , read ) ;
433
+ }
424
434
}
435
+ while ( read > 0 ) ;
436
+
437
+ // We read all incoming data and no error occured.
438
+ // Now, it is time to set null to not resend the data.
439
+ httpState . OutgoingData = null ;
440
+
441
+ response . Close ( ) ;
442
+ response = null ;
425
443
}
426
- while ( read > 0 ) ;
427
444
}
428
445
catch ( IOException ioe )
429
446
{
@@ -432,31 +449,9 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
432
449
433
450
throw ioe ;
434
451
}
435
- catch ( WebException we )
436
- {
437
- HandleWebException ( we , httpState ) ;
438
- }
439
452
finally
440
453
{
441
- if ( read == 0 )
442
- {
443
- isWebRequestInProcess = false ;
444
- try
445
- {
446
- responseStream . Close ( ) ;
447
- response . Close ( ) ;
448
- }
449
- catch ( Exception exc )
450
- {
451
- Trace . WriteLineIf ( Settings . TraceSwitch . TraceWarning ,
452
- "HTTP Response Stream close error: " + exc . StackTrace , GetType ( ) . Name ) ;
453
- }
454
- finally
455
- {
456
- responseStream = null ;
457
- response = null ;
458
- }
459
- }
454
+ isWebRequestInProcess = false ;
460
455
}
461
456
462
457
OnAfterRawDataSent ( httpState . UserState ) ;
@@ -467,7 +462,13 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
467
462
DispatchRawData ( rawData ) ;
468
463
responseReadStream . Close ( ) ;
469
464
responseReadStream = null ;
470
-
465
+ }
466
+ catch ( WebException we )
467
+ {
468
+ HandleWebException ( we , httpState ) ;
469
+ }
470
+ finally
471
+ {
471
472
lock ( SyncObject )
472
473
{
473
474
if ( connected && ( ! isWebRequestInProcess ) && ( ! pollTimer . Enabled ) )
@@ -481,45 +482,38 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
481
482
}
482
483
}
483
484
}
484
- catch ( WebException we )
485
- {
486
- HandleWebException ( we , httpState ) ;
487
- }
488
485
}
489
486
490
- private bool HandleWebException ( WebException we , HttpState httpState )
487
+ private void HandleWebException ( WebException we , HttpState httpState )
491
488
{
492
489
HttpStatusCode statusCode = GetErrorCode ( we . Response as HttpWebResponse ) ;
493
490
491
+ // Don't send httpState.OutgoingData again... Just reconnect.
492
+ if ( statusCode == HttpStatusCode . BadRequest ||
493
+ httpState . PollAction == HttpPollAction . Open )
494
+ {
495
+ Disconnect ( ) ;
496
+ Connect ( ) ;
497
+ return ;
498
+ }
499
+
494
500
switch ( we . Status )
495
501
{
496
502
case WebExceptionStatus . KeepAliveFailure :
497
503
case WebExceptionStatus . ProtocolError :
498
504
case WebExceptionStatus . Timeout :
499
505
{
500
- if ( statusCode == HttpStatusCode . BadRequest )
501
- {
502
- Disconnect ( ) ;
503
- return false ;
504
- }
505
-
506
- // When this happens, re-send the last packet.
506
+ // When this happened, re-send the last packet.
507
507
isWebRequestInProcess = false ;
508
508
509
- if ( httpState . PollAction == HttpPollAction . Open )
510
- {
511
- Disconnect ( ) ;
512
- Connect ( ) ;
513
- }
514
-
515
509
if ( httpState . OutgoingData != null && httpState . OutgoingData . Length > 0 )
516
510
{
517
511
Send ( httpState . OutgoingData , httpState . UserState ) ;
518
512
}
519
513
else
520
514
{
521
515
// It seems outgoing data was sent, but an error occured while reading http response.
522
- return true ;
516
+ return ;
523
517
}
524
518
}
525
519
break ;
@@ -559,8 +553,6 @@ private bool HandleWebException(WebException we, HttpState httpState)
559
553
break ;
560
554
}
561
555
}
562
-
563
- return false ;
564
556
}
565
557
566
558
private HttpStatusCode GetErrorCode ( HttpWebResponse wr )
0 commit comments