Exemplo n.º 1
0
        public void Start()
        {
            subThread         = new BackgroundWorker();
            subThread.DoWork += new DoWorkEventHandler(subThread_DoWork);
            subThread.RunWorkerAsync();

            using (var ctx = ZContext.Create())
            {
                using (var socket = new ZSocket(ctx, ZSocketType.PUB))
                {
                    socket.Bind("tcp://127.0.0.1:5000");
                    while (true)
                    {
                        Thread.Sleep(1000);
                        // Create a ZmqMessage containing 3 frames
                        ZMessage zmqMessage = new ZMessage();
                        zmqMessage.Append(new ZFrame(Encoding.UTF8.GetBytes("My Frame 01")));
                        zmqMessage.Append(new ZFrame(Encoding.UTF8.GetBytes("My Frame 02")));
                        zmqMessage.Append(new ZFrame(Encoding.UTF8.GetBytes("My Frame 03")));

                        Console.WriteLine("PUB; publishing: ");
                        foreach (var msg in zmqMessage)
                        {
                            Console.WriteLine("\t" + msg.ReadString(Encoding.UTF8));
                        }
                        socket.SendMessage(zmqMessage);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Publish(Type type, byte[] message, Dictionary<string, string> headers = null)
        {
            Dictionary<string, object> messageHeaders = GetHeaders(type, headers, _transportSettings.QueueName, "Publish");
            var serializedHeaders = JsonConvert.SerializeObject(messageHeaders);

            var msg = new ZMessage();
            msg.Append(new ZFrame(type.FullName.Replace(".", string.Empty)));
            msg.Append(new ZFrame(serializedHeaders));
            msg.Append(new ZFrame(message));

            _publisher.SendMessage(msg);
        }
Exemplo n.º 3
0
        public void Start()
        {
            _subThread         = new BackgroundWorker();
            _subThread.DoWork += new DoWorkEventHandler(subThread_DoWork);
            _subThread.RunWorkerAsync();

            using (var ctx = ZContext.Create())
            {
                using (var socket = new ZSocket(ctx, ZSocketType.PUB))
                {
                    socket.Bind("tcp://127.0.0.1:5000");

                    while (true)
                    {
                        Thread.Sleep(1000);

                        var shoppingBasket = new ShoppingBasket()
                        {
                            StoreName     = "Fruits City",
                            ShoppingItems = new List <ShoppingItem>()
                            {
                                new ShoppingItem()
                                {
                                    Description = "Orange", Weight = 0.5f
                                },
                                new ShoppingItem()
                                {
                                    Description = "Apple", Weight = 1.4f
                                },
                                new ShoppingItem()
                                {
                                    Description = "Banana", Weight = 0.75f
                                }
                            }
                        };

                        ZMessage zmqMessage = new ZMessage();
                        var      msg1       = "Shopping Basket";
                        var      msg2       = JsonSerializer.SerializeToString <ShoppingBasket>(shoppingBasket);
                        zmqMessage.Append(new ZFrame(msg1, Encoding.UTF8));
                        zmqMessage.Append(JsonFrame.Serialize <ShoppingBasket>(shoppingBasket));

                        Console.WriteLine("PUB; publishing: ");
                        Console.WriteLine("\t" + msg1);
                        Console.WriteLine("\t" + msg2);

                        socket.SendMessage(zmqMessage);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void Send(Type type, byte[] message, Dictionary<string, string> headers = null)
        {
            IList<string> endPoints = _queueMappings[type.FullName];

            foreach (string endPoint in endPoints)
            {
                Dictionary<string, object> messageHeaders = GetHeaders(type, headers, endPoint, "Send");
                var serializedHeaders = JsonConvert.SerializeObject(messageHeaders);

                var msg = new ZMessage();
                msg.Append(new ZFrame(serializedHeaders));
                msg.Append(new ZFrame(message));

                // Send
                _sender.SendMessage(msg);
            }
        }
Exemplo n.º 5
0
        internal void OnRecvMsg(BaseZmqWorker worker, ZMessage recvMsg)
        {
            using (recvMsg)
            {
                var startTime = DateTime.UtcNow;
                using (recvMsg)
                {
                    var clientAddr     = recvMsg[0];
                    var clientData     = recvMsg[2].Read();
                    var baseRequestMsg = MsgPackTool.GetMsg <BaseRequestMsg>(clientData);
                    var requestDataMsg = baseRequestMsg.Data;

                    var responseTask = ProcessRequest(requestDataMsg);
                    responseTask.Wait();
                    var responseMsg = responseTask.Result;

                    // if no session key, generate new
                    var sessionToken = baseRequestMsg.SessionToken;
                    if (string.IsNullOrEmpty(sessionToken))
                    {
                        sessionToken = GenerateSessionKey();
                    }
                    var baseResponseMsg = new BaseResponseMsg()
                    {
                        SessionToken = sessionToken,
                        RequestToken = baseRequestMsg.RequestToken,
                        Data         = responseMsg,
                    };

                    var sendData = MsgPackTool.GetBytes(baseResponseMsg);

                    var messageToServer = new ZMessage();
                    messageToServer.Append(clientAddr);
                    messageToServer.Append(ZFrame.CreateEmpty());
                    messageToServer.Append(new ZFrame(sendData));
                    worker.workerSocket.SendMessage(messageToServer);
                }

                Logger.Trace("Receive Msg and Send used Time: {0:F5}s", (DateTime.UtcNow - startTime).TotalSeconds);
            }
        }
Exemplo n.º 6
0
        public void Send(string endPoint, Type type, byte[] message, Dictionary<string, string> headers = null)
        {
            lock (_lock)
            {
                Dictionary<string, object> messageHeaders = GetHeaders(type, headers, endPoint, "Send");
                var serializedHeaders = JsonConvert.SerializeObject(messageHeaders);

                var msg = new ZMessage();
                msg.Append(new ZFrame(serializedHeaders));
                msg.Append(new ZFrame(message));

                // Reconnect if enpoint changed since the last message was sent.
                //if (_senderEndpoint != endPoint)
                //{
                    _senderEndpoint = endPoint;
                    _senderContext = new ZContext();
                    _sender = new ZSocket(_senderContext, ZSocketType.REQ);
                    
                    _sender.Connect(endPoint);
                //}

                // Send
                var poll = ZPollItem.CreateReceiver();
                var timeout = TimeSpan.FromMilliseconds(1);
                ZError error;
                int retriesLeft = 3;

                while (retriesLeft > 0)
                {
                    // Outgoing
                    if (!_sender.SendMessage(msg, out error))
                    {
                        if (error == ZError.ETERM)
                            return;    // Interrupted
                        throw new ZException(error);
                    }

                    // Incoming
                    ZMessage incoming;
                    while (true)
                    {
                        if (_sender.PollIn(poll, out incoming, out error, timeout))
                        {
                            using (incoming)
                            {
                                retriesLeft = 0;
                                break;
                            }
                        }
                        else
                        {
                            if (error == ZError.EAGAIN)
                            {
                                if (--retriesLeft == 0)
                                {
                                    Logger.Error("Server seems to be offline, abandoning");
                                    break;
                                }

                                Logger.Warn("No response from server, retrying…");

                                // Old socket is confused; close it and open a new one
                                _sender.Dispose();
                                _sender = new ZSocket(_senderContext, ZSocketType.REQ);
                                _sender.Connect(endPoint);

                                Logger.Info("I: reconnected");

                                // Send request again, on new socket
                                if (!_sender.SendMessage(msg, out error))
                                {
                                    if (error == ZError.ETERM)
                                        return;    // Interrupted
                                    throw new ZException(error);
                                }
 
                                continue;
                            }

                            if (error == ZError.ETERM)
                                return;    // Interrupted
                            throw new ZException(error);
                        }
                    }
                }

                _sender.Dispose();
                _senderContext.Dispose();
            }            
        }
Exemplo n.º 7
0
        internal void OnRecvMsg(BaseZmqWorker worker, ZMessage recvMsg)
        {
            using (recvMsg)
            {
                var startTime = DateTime.UtcNow;
                using (recvMsg)
                {
                    var clientAddr = recvMsg[0];
                    var clientData = recvMsg[2].Read();
                    var baseRequestMsg = MsgPackTool.GetMsg<BaseRequestMsg>(clientData);
                    var requestDataMsg = baseRequestMsg.Data;

                    var responseTask = ProcessRequest(requestDataMsg);
                    responseTask.Wait();
                    var responseMsg = responseTask.Result;

                    // if no session key, generate new
                    var sessionToken = baseRequestMsg.SessionToken;
                    if (string.IsNullOrEmpty(sessionToken))
                    {
                        sessionToken = GenerateSessionKey();
                    }
                    var baseResponseMsg = new BaseResponseMsg()
                    {
                        SessionToken = sessionToken,
                        RequestToken = baseRequestMsg.RequestToken,
                        Data = responseMsg,
                    };

                    var sendData = MsgPackTool.GetBytes(baseResponseMsg);

                    var messageToServer = new ZMessage();
                    messageToServer.Append(clientAddr);
                    messageToServer.Append(ZFrame.CreateEmpty());
                    messageToServer.Append(new ZFrame(sendData));
                    worker.workerSocket.SendMessage(messageToServer);
                }

                Logger.Trace("Receive Msg and Send used Time: {0:F5}s", (DateTime.UtcNow - startTime).TotalSeconds);
            }
        }