public void ReceiveData(SocketAsyncEventArgs args) { if (Buffer == null) { if (args.BytesTransferred > 10) { byte[] bytes = new byte[10]; args.CopyTo(bytes, 0, 10); string str = Encoding.UTF8.GetString(bytes).TrimStart('0'); int len = 0; if (int.TryParse(str, out len)) { Buffer = new byte[len]; System.Buffer.BlockCopy(args.Buffer, args.Offset + 10, Buffer, _offset, args.BytesTransferred - 10); _offset = args.BytesTransferred - 10; } else { throw new NotSupportedException("Message format error."); } } else { throw new NotSupportedException("Message format error."); } } else { args.CopyTo(Buffer, _offset); _offset += args.BytesTransferred; } IsCompleted = _offset >= Buffer.Length; }
public static void CopyTo(this SocketAsyncEventArgs args, byte[] bytes) { args.CopyTo(bytes, 0, args.BytesTransferred); }