コード例 #1
0
ファイル: Context.cs プロジェクト: dsjzazs/Tomato
        public bool Response <T>(Header header, T body) where T : IProtocol
        {
            if (_answer)
            {
                throw new Exception("该请求只能响应一次消息!");
            }

            NetMQ.NetMQMessage mqms = new NetMQMessage();
            header.SendTime = DateTime.Now;
            //数据包类型
            mqms.Append(new NetMQFrame(BitConverter.GetBytes((UInt32)body.MessageType)));
            using (var ms = new System.IO.MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms, header);//数据包头
                mqms.Append(ms.ToArray());
            }
            using (var ms2 = new System.IO.MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms2, body);//对象
                mqms.Append(ms2.ToArray());
            }
            //结束帧
            mqms.Append(NetMQFrame.Empty);
            return(_answer = Socket.TrySendMultipartMessage(TimeSpan.FromSeconds(1), mqms));
        }
コード例 #2
0
        public static Task SendMultipartMessageAsync(
            this NetMQSocket socket,
            NetMQMessage message,
            bool more = false,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            if (NetMQRuntime.Current == null)
            {
                throw new InvalidOperationException("NetMQRuntime must be created before calling async functions");
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            socket.AttachToRuntime();

            if (socket.TrySendMultipartMessage(TimeSpan.Zero, message))
            {
                return(Task.CompletedTask);
            }

            TaskCompletionSource <object> source = new TaskCompletionSource <object>();

            cancellationToken.Register(() =>
            {
                socket.SendReady -= Listener;
                source.TrySetCanceled();
            });

            void Listener(object sender, NetMQSocketEventArgs args)
            {
                if (socket.TrySendMultipartMessage(TimeSpan.Zero, message))
                {
                    socket.SendReady -= Listener;
                    source.SetResult(null);
                }
            }

            socket.SendReady += Listener;

            return(source.Task);
        }
コード例 #3
0
ファイル: Context.cs プロジェクト: dsjzazs/Tomato
        public bool Response <T>(Header header, T body) where T : IProtocol
        {
            if (body == null)
            {
                throw new ArgumentNullException("body is Null !!");
            }

            if (header == null)
            {
                throw new ArgumentNullException("header is Null !!");
            }

            if (_answer)
            {
                throw new Exception("该请求只能响应一次消息!");
            }


            NetMQ.NetMQMessage mqms = new NetMQMessage();
            header.SendTime = DateTime.Now;
            //数据包类型
            mqms.Append((int)body.MessageType);
            using (var ms = new System.IO.MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms, header);//数据包头
                mqms.Append(ms.ToArray());
            }
            using (var ms2 = new System.IO.MemoryStream())
            {
#if DEBUG
                Console.WriteLine("Header:");
                Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(header));
                Console.WriteLine("Body:");
                Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(body));
                Console.WriteLine("------------------------------------------");
#endif
                ProtoBuf.Serializer.Serialize(ms2, body);//对象
                mqms.Append(ms2.ToArray());
            }
            //结束帧
            mqms.AppendEmptyFrame();
            return(_answer = Socket.TrySendMultipartMessage(Timeout, mqms));
        }