public void Run() { var remoteIpe = _sock.RemoteEndPoint; var netStream = new NetworkStream(_sock, FileAccess.Read, false); try { netStream.FixedCopyTo(_writer, _receiveLength - _bytesReceived, recv => { _writer.Flush(); _bytesReceived += recv; App.LogDebug("[ReceiveChunk{0}] {1} {2}/{3}.", Thread.CurrentThread.ManagedThreadId, remoteIpe, _bytesReceived, _receiveLength); return(_sock.Connected); }); } catch (SocketException ex) { if (ex.ErrorCode == 10054) { return; } throw; } finally { _writer.Unlock(_offset, _count); _writer.Dispose(); _sock.Close(); _isCompleted = true; } }
public static bool Receive <T>(this Socket client, out T packModel) { byte[] data = new byte[4]; int recv = client.Receive(data, 0, 4, SocketFlags.None); if (recv == 0) { packModel = default(T); return(false); } int length = BitConverter.ToInt32(data, 0); var stream = new MemoryStream(); var netStream = new NetworkStream(client, FileAccess.ReadWrite, false); netStream.FixedCopyTo(stream, length); stream.Position = 0L; packModel = (T)Serializer.Deserialize(stream); return(true); }