private static async Task <bool> GetHeader(Stream stream, byte[] messageHeader, ISharedArrayPool sharedArrayPool) { var binaryReader = new AsyncBinaryReader(stream); var messageHeaderLength = await binaryReader.ReadUInt16Async(); Debug.Assert(messageHeaderLength == messageHeader.Length); if (messageHeaderLength != messageHeader.Length) { // 消息不对,忽略 return(false); } var messageHeaderBuffer = sharedArrayPool.Rent(messageHeader.Length); try { var readCount = await ReadBufferAsync(stream, messageHeaderBuffer, messageHeader.Length); Debug.Assert(readCount == messageHeader.Length); if (ByteListExtension.Equals(messageHeaderBuffer, messageHeader, readCount)) { // 读对了 return(true); } else { // 发过来的消息是出错的 return(false); } } finally { sharedArrayPool.Return(messageHeaderBuffer); } }
public ByteListMessageStream(byte[] buffer, int count, ISharedArrayPool sharedArrayPool) : base(buffer, 0, count, false) { _sharedArrayPool = sharedArrayPool; Buffer = buffer; }
public static async Task <IpcMessageResult> ReadAsync(Stream stream, byte[] messageHeader, ISharedArrayPool sharedArrayPool) { /* * UInt16 Message Header Length 消息头的长度 * byte[] Message Header 消息头的内容 * UInt32 Version 当前IPC服务的版本 * UInt64 Ack 用于给对方确认收到消息使用 * UInt32 Empty 给以后版本使用的值 * UInt16 Command Type 命令类型,业务端的值将会是 0 而框架层采用其他值 * UInt32 Content Length 这条消息的内容长度 * byte[] Content 实际的内容 */ if (!await GetHeader(stream, messageHeader, sharedArrayPool)) { // 消息不对,忽略 return(new IpcMessageResult("Message Header no match")); } var binaryReader = new AsyncBinaryReader(stream); // UInt32 Version 当前IPC服务的版本 var version = await binaryReader.ReadUInt32Async(); Debug.Assert(version == 1); if (version == 0) { // 这是上个版本的,但是不兼容了 return(new IpcMessageResult("收到版本为 0 的旧版本消息,但是不兼容此版本")); } // UInt64 Ack 用于给对方确认收到消息使用 var ack = await binaryReader.ReadReadUInt64Async(); // UInt32 Empty 给以后版本使用的值 var empty = await binaryReader.ReadUInt32Async(); Debug.Assert(empty == 0); // UInt16 Command Type 命令类型,业务端的值将会是大于 0 而框架层采用其他值 var commandType = (IpcMessageCommandType)await binaryReader.ReadUInt16Async(); // UInt32 Content Length 这条消息的内容长度 var messageLength = await binaryReader.ReadUInt32Async(); if (messageLength > IpcConfiguration.MaxMessageLength) { // 太长了 return(new IpcMessageResult( $"Message Length too long MessageLength={messageLength} MaxMessageLength={IpcConfiguration.MaxMessageLength}. {DebugContext.OverMaxMessageLength}")); } var messageBuffer = sharedArrayPool.Rent((int)messageLength); // byte[] Content 实际的内容 var readCount = await ReadBufferAsync(stream, messageBuffer, (int)messageLength); Debug.Assert(readCount == messageLength); var ipcMessageContext = new IpcMessageContext(ack, messageBuffer, messageLength, sharedArrayPool); return(new IpcMessageResult(success: true, ipcMessageContext, commandType)); }
public static async Task <(bool success, IpcMessageContext ipcMessageContext)> ReadAsync(Stream stream, byte[] messageHeader, ISharedArrayPool sharedArrayPool, int maxMessageLength = ushort.MaxValue *byte.MaxValue) { try { /* * UInt16 Message Header Length * byte[] Message Header * UInt32 Version * UInt64 Ack * UInt32 Empty * UInt32 Content Length * byte[] Content */ if (!await GetHeader(stream, messageHeader, sharedArrayPool)) { // 消息不对,忽略 return((false, default) !);