Пример #1
0
 private static void Queue_ReceiveReady(object sender, NetMQQueueEventArgs <byte[]> e)
 {
     if (e.Queue.TryDequeue(out byte[] result, TimeSpan.FromSeconds(1.0)))
     {
         push.SendFrame(result);
     }
 }
Пример #2
0
        //Executes on client poller thread to avoid tying up the dealer socket poller thread
        private void ReceivedQueue_ReceiveReady(object sender, NetMQQueueEventArgs <List <byte[]> > e)
        {
            List <byte[]> frames;

            if (e.Queue.TryDequeue(out frames, new TimeSpan(1000)))
            {
                if (frames[0].IsEqualTo(MessageHeader.HeartBeat))
                {
                    ProcessHeartBeatResponse(frames);
                }
                else if (_throwOnSend && null != _session && null == _session.Crypto)
                {
                    if (IsHandshakeReply(frames))
                    {
                        ProcessProtocolExchange(frames);
                    }
                    else
                    {
                        OnMessageReceivedFailure(frames);
                    }
                }
                else
                {
                    ProcessRegularMessage(frames);
                }
            }
        }
Пример #3
0
        //Executes on same poller thread as dealer socket, so we can send directly
        private void SendQueue_ReceiveReady(object sender, NetMQQueueEventArgs <List <byte[]> > e)
        {
            var message = new NetMQMessage();

            message.AppendEmptyFrame();
            List <byte[]> frames;

            if (e.Queue.TryDequeue(out frames, new TimeSpan(1000)))
            {
                if (null != _session && null != _session.Crypto)
                {
                    //do not encrypt heartbeat message
                    if (frames.Count > 1 || !frames[0].IsEqualTo(MessageHeader.HeartBeat))
                    {
                        //encrypt message frames of regular messages but not heartbeat messages
                        for (int i = 0; i < frames.Count; i++)
                        {
                            frames[i] = _session.Crypto.Encrypt(frames[i]);
                        }
                    }
                }

                foreach (var frame in frames)
                {
                    message.Append(frame);
                }
                _dealerSocket.SendMultipartMessage(message);
                _logger.Debug("Message sent. Frame count: {0}", message.FrameCount);
            }
        }
Пример #4
0
 private void MessageQueue_ReceiveReady(object sender, NetMQQueueEventArgs <NetMQMessage> e)
 {
     while (e.Queue.TryDequeue(out NetMQMessage messageToServer, TimeSpan.FromMilliseconds(10)))
     {
         client.SendMultipartMessage(messageToServer);
     }
 }
Пример #5
0
        //occurs on host polling thread to allow sending and receiving on a different thread
        private void SendFailureQueue_ReceiveReady(object sender, NetMQQueueEventArgs <MessageFailure> e)
        {
            MessageFailure mf;

            if (e.Queue.TryDequeue(out mf, new TimeSpan(1000)))
            {
                _logger.Error("Send failed {0}, {1}", mf.ErrorCode, mf.ErrorMessage);
                _sentFailureEvent?.Invoke(this, new MessageEventFailureArgs
                {
                    Failure = mf
                });
            }
        }
 private void OnMessageQueued(object sender, NetMQQueueEventArgs <NetMQMessage> e)
 {
     for (var i = 0u; i < 100u; ++i)
     {
         if (e.Queue.TryDequeue(out var message, TimeSpan.FromMilliseconds(10)))
         {
             try
             {
                 OutgoingMessages.Value.SendMultipartMessage(message);
             }
             catch (Exception exception)
             {
                 OnError(exception);
             }
         }
Пример #7
0
        //occurs on host polling thread to allow sending and receiving on a different thread
        private void ReceivedQueue_ReceiveReady(object sender, NetMQQueueEventArgs <Message> e)
        {
            Message message;

            if (e.Queue.TryDequeue(out message, new TimeSpan(1000)))
            {
                if (message.Frames[0].IsEqualTo(MessageHeader.HeartBeat))
                {
                    ProcessHeartBeat(message);
                }
                else if (null == _authRepository)
                {
                    ProcessRegularMessage(message, null);
                }
                else
                {
                    ProcessProtectedMessage(message);
                }
            }
        }
Пример #8
0
        private void OnOutgoingQueueReady(object sender, NetMQQueueEventArgs <OutgoingMessage> e)
        {
            var outgoingMessage = m_outgoingQueue.Dequeue();

            //var bodySegment = m_serializer.Serialize(outgoingMessage.Message);
            //byte[] body = new byte[bodySegment.Count];
            //Buffer.BlockCopy(bodySegment.Array, bodySegment.Offset, body, 0, bodySegment.Count);

            UInt64 messageId = ++m_nextMessageId;

            m_nextMessageId = messageId;

            //string subject = m_serializer.GetObjectSubject(outgoingMessage.Message);
            //Console.WriteLine("Codec.Message.RelatedMessageId: " + messageId + " ; " + Codec.Error.RelatedMessageId);


            Codec.Id = Codec.MessageId.Message;
            Codec.Message.MessageId        = messageId;
            Codec.Message.Service          = outgoingMessage.Service;
            Codec.Message.Subject          = "";
            Codec.Message.Body             = outgoingMessage.Message;
            Codec.Message.RelatedMessageId = 0;

            // one way message
            if (outgoingMessage.Oneway)
            {
                Codec.Message.OneWay = 1;
            }
            else
            {
                Codec.Message.OneWay = 0;
                // add to pending requests dictionary
                // TODO: we might want to create a pending message structure that will not hold reference to the message (can lead to GC second generation)
                var msg = new PendingMessage(messageId, outgoingMessage.TaskCompletionSource);
                m_pendingRequests.Add(messageId, msg);
                GC.KeepAlive(msg);
                GC.KeepAlive(outgoingMessage);
            }

            Codec.Send(m_clientSocket);
        }
Пример #9
0
        //occurs on socket polling thread to assure sending and receiving on same thread
        private void SendQueue_ReceiveReady(object sender, NetMQQueueEventArgs <NetMQMessage> e)
        {
            NetMQMessage message;

            if (e.Queue.TryDequeue(out message, new TimeSpan(1000)))
            {
                try
                {
                    _routerSocket.SendMultipartMessage(message);
                    _logger.Info("Message sent with {0} frames.", message.FrameCount);
                }
                catch (Exception ex) //clientId not found or other error, raise event
                {
                    var unreachable = ex as HostUnreachableException;
                    _logger.Error("Error sending message to {0} with {1} frames.", new Guid(message.First.Buffer), message.FrameCount);
                    _sendFailureQueue.Enqueue(new MessageFailure
                    {
                        Message      = message.ToMessageWithClientFrame(),
                        ErrorCode    = null != unreachable ? unreachable.ErrorCode.ToString() : "Unknown",
                        ErrorMessage = ex.Message
                    });
                }
            }
        }
Пример #10
0
        private void OnAction(NetMQQueueEventArgs <Action> args)
        {
            var action = args.Queue.Dequeue();

            action.Invoke();
        }
Пример #11
0
        public static void ReceiveReadyAction(this IntermediaryServer server, object sender, NetMQQueueEventArgs <IntermediaryMsg> e)
        {
            try
            {
                var Item = e.Queue.Dequeue();
                var msg  = Item.Msg;
                var from = Item.From;
                var to   = Item.To;

                var more = msg.HasMore;
                if (more)
                {
                    var topic = Encoding.Default.GetString(msg.Data);
                    var head  = topic.SplitTrim(":")[0];
                    var host  = topic.SplitTrim(":")[1];
                    // random dispatch
                    if (string.Equals(head, TopicType.SVRR))
                    {
                        // 随机分发
                        if (_randomSendMode && proxyCollection.Count > 0)
                        {
                            var newTopic = proxyCollection.Values.Shuffle().Get(0);
                            var data     = Encoding.Default.GetBytes(newTopic);
                            to.SendFrame(data, more);
                            return;
                        }
                    }
                    // heartbeat logic
                    else if (string.Equals(head, TopicType.ONLINE))
                    {
                        var proxy = $"{TopicType.SVRR}:{host}";
                        proxyCollection.AddOrUpdate(host, proxy, (k, v) => v);
                    }
                    // offline logic
                    else if (string.Equals(head, TopicType.OFFLINE))
                    {
                        proxyCollection.Remove(host);
                    }
                }
                // send frame
                to.Send(ref msg, more);
                msg.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"IntermediaryHandler-ReceiveReadyAction {ex.Message}");
            }
        }
Пример #12
0
 private void OnAction(NetMQQueueEventArgs<Action> args)
 {
     var action = args.Queue.Dequeue();
     action.Invoke();
 }