Skip to content

Commit

Permalink
Don't try to clear synchronously. Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Aug 30, 2013
1 parent b4fd91d commit 3b9b2d3
Showing 1 changed file with 5 additions and 31 deletions.
36 changes: 5 additions & 31 deletions Syndll2/Receiver.cs
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace Syndll2
Expand Down Expand Up @@ -69,41 +69,15 @@ private void OnDataReceived(IAsyncResult asyncResult)
// Copy the raw data read into the full receive buffer.
_receiveBuffer.AddRange(_rawReceiveBuffer.Take(bytesRead));

// Read anything else on the stream to make sure the line is clear.
ReadFromStreamUntilTimeout(200);

// Read packets from the buffer
ReadFromBuffer();
// Read packets from the buffer, unless there's still more on the stream to read.
var networkStream = _stream as NetworkStream;
if (networkStream == null || !networkStream.DataAvailable)
ReadFromBuffer();

// Repeat, to continually watch the stream for incoming data.
WatchStream();
}

[DebuggerNonUserCode]
private void ReadFromStreamUntilTimeout(int timeoutMilliseconds)
{
_stream.ReadTimeout = timeoutMilliseconds;
try
{
int bytesRead;
do
{
// Read synchronously
bytesRead = _stream.Read(_rawReceiveBuffer, 0, _rawReceiveBuffer.Length);
if (bytesRead > 0)
{
// Copy the raw data read into the full receive buffer.
_receiveBuffer.AddRange(_rawReceiveBuffer.Take(bytesRead));
}
} while (bytesRead > 0);
}
catch (IOException)
{
// An IOException occurs when the read timeout is reached.
// The [DebuggerNonUserCode] attribute prevents it from being shown as a first chance excption.
}
}

private void ReadFromBuffer()
{
// See if there is an EOT in the read buffer
Expand Down

0 comments on commit 3b9b2d3

Please sign in to comment.