public byte[] RecieveSegment(Segment segment)
{
try
{
// Request body...
Write("BODY "+ segment.addr +'\n');
Assert(Response(), "222");
NetworkStream stream = GetStream();
stream.ReadTimeout = 10000;
byte[] buffer = new byte[segment.bytes];
int read = 0;
bool done = false;
int chunk;
while (!done && (chunk = stream.Read(buffer, read, buffer.Length - read)) > 0 && keepAlive)
{
read += chunk;
segment.parent.parent.ProgressKB += (ulong)chunk;
segment.progress += chunk;
lock (this)
{
speed += (uint)chunk;
}
if (read == buffer.Length)
{
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
buffer = newBuffer;
}
mainGui.UpdateSingleThreadInfo(this);
// Looks for .\r\n (End of message)
if (read > 2 && buffer[read - 3] == '.' && buffer[read - 2] == '\r' && buffer[read - 1] == '\n')
{
// Check that it is in the beginning of a new line
if (read < 4 || buffer[read - 5] == '\r' && buffer[read - 4] == '\n')
{
break;
}
}
}
if (!keepAlive)
{
throw new Exception("KeepAlive is false"); ;
}
byte[] returnarray = new byte[read - 3];
Array.Copy(buffer, returnarray, read - 3);
segment.bytes = read;
Logging.Instance.Log("(NNTPConnection(" + id + ")) SEGMENT Complete: id=" + segment.id + " bytes=" + read);
return returnarray;
}
catch (IOException ex)
{
throw;
}
}