/// <summary> /// Continuously calls the <see cref="TcpClient.GetStream().Read"/> method of the <see cref="TcpClient"/> while the socket is connected /// </summary> /// <remarks>This is a blocking call. When exiting this call the socket will be already closed</remarks> public void Run() { var recvBuffer = _bufferManager.Allocate(); try { while (_client.Connected) { //blocking call int read = _client.GetStream().Read(recvBuffer, 0, recvBuffer.Length); if (read > 0) { this.UpperLayer.Receive(new ByteArray(recvBuffer, read)); } } } catch (Exception ex) { this.OnRunError(new UnhandledExceptionEventArgs(ex, false)); } finally { _bufferManager.Free(recvBuffer); _client.Close(); } Console.WriteLine("Tcp layer loop completed"); }
public int ReadBuffer(BufferManager<byte> bufferManager, FileStream sourceFileStream) { int bytesRead = 0; var buffer = bufferManager.Allocate(); try { bytesRead = sourceFileStream.Read(buffer.Data, 0, buffer.Length); buffer.UsedLength = bytesRead; } catch { bufferManager.Free(buffer); throw; } bufferManager.Enqueue(buffer); return bytesRead; }