Skip to content

Commit 79ab585

Browse files
committed
If an error occurs, re-send the last packet again.
1 parent b94cd91 commit 79ab585

File tree

1 file changed

+44
-52
lines changed

1 file changed

+44
-52
lines changed

MSNPSharp/Core/HttpSocketMessageProcessor.cs

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,15 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
360360
using (Stream stream = httpState.Request.EndGetRequestStream(ar))
361361
{
362362
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;
363365
}
364366

365367
WebResponse response = httpState.Request.GetResponse();
366368
httpState.Request = null;
367369

370+
#region Read Headers
371+
368372
lock (SyncObject)
369373
{
370374
foreach (string header in response.Headers.AllKeys)
@@ -387,9 +391,11 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
387391
case "SessionID":
388392
SessionID = elements[1];
389393
break;
394+
390395
case "GW-IP":
391396
GatewayIP = elements[1];
392397
break;
398+
393399
case "Session":
394400
if ("close" == elements[1])
395401
{
@@ -399,6 +405,7 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
399405
connected = false;
400406
}
401407
break;
408+
402409
case "Action":
403410
break;
404411
}
@@ -408,22 +415,32 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
408415
}
409416
}
410417

418+
#endregion
411419

412-
Stream responseStream = response.GetResponseStream();
413420
MemoryStream responseReadStream = new MemoryStream();
414-
byte[] readBuffer = new byte[8192];
415421
int read = 0;
416422
try
417423
{
418-
do
424+
using (Stream responseStream = response.GetResponseStream())
419425
{
420-
read = responseStream.Read(readBuffer, 0, readBuffer.Length);
421-
if (read > 0)
426+
byte[] readBuffer = new byte[8192];
427+
do
422428
{
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+
}
424434
}
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;
425443
}
426-
while (read > 0);
427444
}
428445
catch (IOException ioe)
429446
{
@@ -432,31 +449,9 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
432449

433450
throw ioe;
434451
}
435-
catch (WebException we)
436-
{
437-
HandleWebException(we, httpState);
438-
}
439452
finally
440453
{
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;
460455
}
461456

462457
OnAfterRawDataSent(httpState.UserState);
@@ -467,7 +462,13 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
467462
DispatchRawData(rawData);
468463
responseReadStream.Close();
469464
responseReadStream = null;
470-
465+
}
466+
catch (WebException we)
467+
{
468+
HandleWebException(we, httpState);
469+
}
470+
finally
471+
{
471472
lock (SyncObject)
472473
{
473474
if (connected && (!isWebRequestInProcess) && (!pollTimer.Enabled))
@@ -481,45 +482,38 @@ private void EndGetRequestStreamCallback(IAsyncResult ar)
481482
}
482483
}
483484
}
484-
catch (WebException we)
485-
{
486-
HandleWebException(we, httpState);
487-
}
488485
}
489486

490-
private bool HandleWebException(WebException we, HttpState httpState)
487+
private void HandleWebException(WebException we, HttpState httpState)
491488
{
492489
HttpStatusCode statusCode = GetErrorCode(we.Response as HttpWebResponse);
493490

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+
494500
switch (we.Status)
495501
{
496502
case WebExceptionStatus.KeepAliveFailure:
497503
case WebExceptionStatus.ProtocolError:
498504
case WebExceptionStatus.Timeout:
499505
{
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.
507507
isWebRequestInProcess = false;
508508

509-
if (httpState.PollAction == HttpPollAction.Open)
510-
{
511-
Disconnect();
512-
Connect();
513-
}
514-
515509
if (httpState.OutgoingData != null && httpState.OutgoingData.Length > 0)
516510
{
517511
Send(httpState.OutgoingData, httpState.UserState);
518512
}
519513
else
520514
{
521515
// It seems outgoing data was sent, but an error occured while reading http response.
522-
return true;
516+
return;
523517
}
524518
}
525519
break;
@@ -559,8 +553,6 @@ private bool HandleWebException(WebException we, HttpState httpState)
559553
break;
560554
}
561555
}
562-
563-
return false;
564556
}
565557

566558
private HttpStatusCode GetErrorCode(HttpWebResponse wr)

0 commit comments

Comments
 (0)