Example #1
0
        public void BuildAckMessage()
        {
            "创建的Ack信息可以被解析出创建传入的Ack值".Test(() =>
            {
                // 返回的信息包含当前的 ack 值
                var buildAckMessage    = AckManager.BuildAckMessage(100);
                using var memoryStream = new MemoryStream(buildAckMessage, false);
                var isAck = AckManager.IsAckMessage(memoryStream, out var ack);

                Assert.AreEqual(true, isAck);
                // 100ul 就是 ulong 100 的意思,我担心你看懂,所以特别加了 ulong 的转换,让你以为 ul 是一个有趣的后缀
                Assert.AreEqual((ulong)100ul, ack.Value);
            });

            "传入不属于 Ack 的信息,可以返回不是 Ack 信息".Test(() =>
            {
                using var memoryStream = new MemoryStream();
                var streamWriter       = new StreamWriter(memoryStream);
                streamWriter.WriteLine("林德熙是逗比");
                const long position   = 2;
                memoryStream.Position = position;

                var isAck = AckManager.IsAckMessage(memoryStream, out var ack);
                Assert.AreEqual(false, isAck);
                // 如果读取返回不是 Ack 那么将 Stream 设置回传入的 Position 的值
                Assert.AreEqual(position, memoryStream.Position);
            });
        }
Example #2
0
 private async Task QueueWriteAsync(Func <Ack, Task> task)
 {
     await DoubleBufferTask.AddTaskAsync(async() =>
     {
         await AckManager.DoWillReceivedAck(task, PeerName, TimeSpan.FromSeconds(3), maxRetryCount: 10);
     });
 }
Example #3
0
        public void BuildAckMessage()
        {
            "创建的Ack信息可以被解析出创建传入的Ack值".Test(() =>
            {
                // 返回的信息包含当前的 ack 值
                var buildAckMessage    = AckManager.BuildAckMessage(100);
                using var memoryStream = new MemoryStream(buildAckMessage, false);
                var isAck = AckManager.IsAckMessage(memoryStream, out var ack);

                Assert.AreEqual(true, isAck);
                Assert.AreEqual(100, ack.Value);
            });
        }
Example #4
0
        public void RegisterAckTask()
        {
            "将消息注册,此时设置收到回复,注册的消息可以完成".Test(() =>
            {
                // 注册的消息可以完成
                var clientName           = "lindexi";
                Ack ack                  = 2;
                var taskCompletionSource = new TaskCompletionSource <bool>();

                var ackTask = new AckTask(clientName, ack, taskCompletionSource);
                AckManager.RegisterAckTask(ackTask);
                AckManager.OnAckReceived(this, new AckArgs(clientName, ack));
                //Debug.Assert(taskCompletionSource.Task.IsCompleted);
                Assert.AreEqual(true, taskCompletionSource.Task.IsCompleted);
            });
        }
Example #5
0
        /// <summary>
        /// 向服务端发送消息
        /// </summary>
        /// <remarks>
        /// 框架层使用的
        /// </remarks>
        internal async Task WriteMessageAsync(IpcBufferMessageContext ipcBufferMessageContext)
        {
            await DoubleBufferTask.AddTaskAsync(WriteMessageAsyncInner);

            async Task WriteMessageAsyncInner()
            {
                await IpcMessageConverter.WriteAsync
                (
                    NamedPipeClientStream,
                    IpcConfiguration.MessageHeader,
                    AckManager.GetAck(),
                    ipcBufferMessageContext,
                    Logger
                );

                await NamedPipeClientStream.FlushAsync();
            }
        }
Example #6
0
        public void RegisterAckTask()
        {
            "重复注册相同编号的消息,提示错误".Test(() =>
            {
                var clientName           = "lindexi";
                Ack ack                  = 20;
                var taskCompletionSource = new TaskCompletionSource <bool>();

                var ackTask = new AckTask(clientName, ack, taskCompletionSource, "调试");
                AckManager.RegisterAckTask(ackTask);
                Assert.ThrowsException <ArgumentException>(() => { AckManager.RegisterAckTask(ackTask); });
            });

            "将消息注册,如果没有收到回复,那么注册的任务依然没有完成".Test(() =>
            {
                // 注册的消息可以完成
                var clientName           = "lindexi";
                Ack ack                  = 2;
                var taskCompletionSource = new TaskCompletionSource <bool>();

                var ackTask = new AckTask(clientName, ack, taskCompletionSource, "调试");
                AckManager.RegisterAckTask(ackTask);
                Assert.AreEqual(false, taskCompletionSource.Task.IsCompleted);
            });

            "将消息注册,此时设置收到回复,注册的消息可以完成".Test(() =>
            {
                // 注册的消息可以完成
                var clientName           = "lindexi";
                Ack ack                  = 2;
                var taskCompletionSource = new TaskCompletionSource <bool>();

                var ackTask = new AckTask(clientName, ack, taskCompletionSource, "调试");
                AckManager.RegisterAckTask(ackTask);
                AckManager.OnAckReceived(this, new AckArgs(clientName, ack));
                //Debug.Assert(taskCompletionSource.Task.IsCompleted);
                Assert.AreEqual(true, taskCompletionSource.Task.IsCompleted);
            });
        }
Example #7
0
        /// <summary>
        /// 向服务端发送消息
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <param name="summary">这一次写入的是什么内容,用于调试</param>
        /// <returns></returns>
        /// <remarks>
        /// 业务层使用的
        /// </remarks>
        public async Task WriteMessageAsync(byte[] buffer, int offset, int count,
                                            [CallerMemberName] string summary = null !)
        {
            await DoubleBufferTask.AddTaskAsync(WriteMessageAsyncInner);

            async Task WriteMessageAsyncInner()
            {
                await IpcMessageConverter.WriteAsync
                (
                    NamedPipeClientStream,
                    IpcConfiguration.MessageHeader,
                    AckManager.GetAck(),
                    // 表示这是业务层的消息
                    IpcMessageCommandType.Business,
                    buffer,
                    offset,
                    count,
                    summary,
                    Logger
                );

                await NamedPipeClientStream.FlushAsync();
            }
        }
Example #8
0
        public async Task SendAckAsync(Ack receivedAck)
        {
            var ackMessage = AckManager.BuildAckMessage(receivedAck);

            await WriteMessageAsync(ackMessage, 0, ackMessage.Length);
        }