Пример #1
0
 public void Send(string message)
 {
     if (_outgoing.IsDisposed)
     {
         return;
     }
     _outgoing.TrySendFrame(_interval, message);
 }
Пример #2
0
 protected bool TrySendFrameBytes(byte[] frame)
 {
     if (Socket.TrySendFrame(SendReceiveTimeout, frame))
     {
         return(true);
     }
     else
     {
         HandleError(new SendTimeoutException());
         return(false);
     }
 }
Пример #3
0
        public static bool Send(Message message, NetMQSocket socket)
        {
            if (_signatureValidator == null)
            {
                throw new InvalidOperationException($"{nameof(MessageSender)} has not been initialized with a {nameof(SignatureValidator)}");
            }

            var hmac = _signatureValidator.CreateSignature(message);

            if (message.Identifiers.Count > 0)
            {
                // Send ZMQ identifiers from the message we're responding to.
                // This is important when we're dealing with ROUTER sockets, like the shell socket,
                // because the message won't be sent unless we manually include these.
                foreach (var ident in message.Identifiers)
                {
                    socket.TrySendFrame(ident, true);
                }
            }
            else
            {
                // This is just a normal message so send the UUID
                Send(message.UUID, socket);
            }

            Send(Constants.Delimiter, socket);
            Send(hmac, socket);
            Send(JsonSerializer.Serialize(message.Header), socket);
            Send(JsonSerializer.Serialize(message.ParentHeader), socket);
            Send(JsonSerializer.Serialize(message.Metadata), socket);
            Send(message.Content, socket, false);

            return(true);
        }
Пример #4
0
        public bool Send(Message message, NetMQSocket socket)
        {
            string hmac = this._signatureValidator.CreateSignature(message);

            if (message.Identifiers.Count > 0)
            {
                // Send ZMQ identifiers from the message we're responding to.
                // This is important when we're dealing with ROUTER sockets, like the shell socket,
                // because the message won't be sent unless we manually include these.
                foreach (var ident in message.Identifiers)
                {
                    socket.TrySendFrame(ident, true);
                }
            }
            else
            {
                // This is just a normal message so send the UUID
                Send(message.UUID, socket);
            }

            Send(Constants.DELIMITER, socket);
            Send(hmac, socket);
            Send(JsonSerializer.Serialize(message.Header), socket);
            Send(JsonSerializer.Serialize(message.ParentHeader), socket);
            Send(JsonSerializer.Serialize(message.MetaData), socket);
            Send(message.Content, socket, false);

            return(true);
        }
Пример #5
0
 private void SendAllRequests(NetMQSocket client)
 {
     while (requestQueue.Count > 0)
     {
         client.TrySendFrame(requestQueue.Dequeue());
     }
 }
Пример #6
0
    private void SendNextRequest(NetMQSocket client)
    {
        bool success = client.TrySendFrame(TimeSpan.FromSeconds(5), requestQueue.Dequeue());

        if (success == false)
        {
            throw new FaultException();
        }
    }
Пример #7
0
        public static bool SendMessage(this NetMQSocket socket, Message message)
        {
            Logger?.LogTrace("Sending Message: {0}", JsonConvert.SerializeObject(message));
            if (message.Header.MessageType == MessageType.Input)
            {
                lastParentHeader = message.ParentHeader;
            }
            if (string.IsNullOrEmpty(message.UUID))
            {
                message.UUID = SessionId;
            }

            if (string.IsNullOrEmpty(message.Header.Session))
            {
                message.Header.Session = SessionId;
                message.ParentHeader   = lastParentHeader;
            }

            var messageFrames = new[] {
                JsonConvert.SerializeObject(message.Header),
                JsonConvert.SerializeObject(message.ParentHeader),
                JsonConvert.SerializeObject(message.MetaData),
                JsonConvert.SerializeObject(message.Content)
            };
            string hmac = Validator.CreateSignature(messageFrames);

            if (message.Identifiers != null && message.Identifiers.Count > 0)
            {
                // Send ZMQ identifiers from the message we're responding to.
                // This is important when we're dealing with ROUTER sockets, like the shell socket,
                // because the message won't be sent unless we manually include these.
                foreach (var ident in message.Identifiers)
                {
                    socket.TrySendFrame(ident, true);
                }
            }
            else
            {
                // This is just a normal message so send the UUID
                socket.SendFrame(message.UUID, true);
            }

            socket.SendFrame(Constants.DELIMITER, true);
            socket.SendFrame(hmac, true);
            socket.SendFrame(messageFrames[0], true);
            socket.SendFrame(messageFrames[1], true);
            socket.SendFrame(messageFrames[2], true);
            socket.SendFrame(messageFrames[3], false);

            return(true);
        }
Пример #8
0
    void Update()
    {
        //sender.TrySendFrame("work");
        //string temp;
        //if (receiver.TryReceiveFrameString(out temp)) {
        //    Debug.Log(temp);
        //}
        string temp2;

        if (response.TryReceiveFrameString(out temp2))
        {
            Debug.Log(temp2);
            response.TrySendFrame("ok");
        }
    }
Пример #9
0
        public bool Send(Message message, NetMQSocket socket)
        {
            string hmac = this._signatureValidator.CreateSignature(message);

            foreach (var ident in message.Identifiers)
            {
                socket.TrySendFrame(ident, true);
            }

            Send(Constants.DELIMITER, socket);
            Send(hmac, socket);
            Send(JsonSerializer.Serialize(message.Header), socket);
            Send(JsonSerializer.Serialize(message.ParentHeader), socket);
            Send(JsonSerializer.Serialize(message.MetaData), socket);
            Send(JsonSerializer.Serialize(message.Content), socket, false);

            return(true);
        }
Пример #10
0
        public bool Send <T, C>(Message <T> request, C content, string msgType)
        {
            var ioPubMessage = new Message <C>
            {
                Identifiers  = request.Identifiers,
                Delimiter    = request.Delimiter,
                ParentHeader = request.Header,
                Header       = new Header()
                {
                    UserName    = request.Header.UserName,
                    Session     = request.Header.Session,
                    Date        = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                    MessageId   = Guid.NewGuid().ToString(),
                    MessageType = msgType,
                    Version     = request.Header.Version
                },
                Metadata = request.Metadata,
                Content  = content
            };



            Console.WriteLine($"{msgType}: [{JsonConvert.SerializeObject(ioPubMessage.Content)}]");

            var           encoder   = new UTF8Encoding();
            List <string> messages  = new List <string>();
            var           signature = Sign(_key, ioPubMessage, messages, _iopub);

            // send
            foreach (var id in request.Identifiers)
            {
                _iopub.TrySendFrame(id, true);
            }

            _iopub.SendFrame(ioPubMessage.Delimiter, true);
            _iopub.SendFrame(signature, true);

            for (int i = 0; i < messages.Count; i++)
            {
                _iopub.SendFrame(messages[i], i < messages.Count - 1);
            }

            return(true);
        }
Пример #11
0
        private static void RepOnReceiveReady(object sender, NetMQSocketEventArgs args)
        {
            try
            {
                NetMQSocket rep = args.Socket;

                byte[] message = rep.ReceiveFrameBytes();

                //Thread.Sleep(1000); //  Simulate 'work'

                byte[] response =
                    Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(message) + " World from worker " + Encoding.Unicode.GetString(rep.Options.Identity));

                rep.TrySendFrame(response, response.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception on RepOnReceiveReady: {0}", ex.Message);
                throw;
            }
        }
Пример #12
0
        public void OnDataReady()
        {
            switch (m_state)
            {
            case WebSocketClientState.Closed:
                m_state = WebSocketClientState.Handshake;
                string clientHandshake = m_streamSocket.ReceiveFrameString();

                string[] lines = clientHandshake.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                string key;

                if (ValidateClientHandshake(lines, out key))
                {
                    string acceptKey = GenerateAcceptKey(key);

                    try
                    {
                        if (m_streamSocket.TrySendFrame(Identity, Identity.Length, true) &&
                            m_streamSocket.TrySendFrame("HTTP/1.1 101 Switching Protocols\r\n" +
                                                        "Upgrade: websocket\r\n" +
                                                        "Connection: Upgrade\r\n" +
                                                        "Sec-WebSocket-Accept: " + acceptKey + "\r\n" +
                                                        "Sec-WebSocket-Protocol: WSNetMQ\r\n\r\n"))
                        {
                            m_decoder          = new Decoder();
                            m_decoder.Message += OnMessage;
                            m_state            = WebSocketClientState.Ready;
                        }
                        else
                        {
                            m_state = WebSocketClientState.Closed;
                        }
                    }
                    catch (NetMQException)
                    {
                        m_state = WebSocketClientState.Closed;
                    }
                }
                else
                {
                    m_state = WebSocketClientState.Closed;

                    if (m_streamSocket.TrySendFrame(Identity, Identity.Length, true))
                    {
                        m_streamSocket.TrySendFrame("HTTP/1.1 400 Bad Request\r\nSec-WebSocket-Version: 13\r\n");
                    }

                    // invalid request, close the socket and raise closed event
                    if (m_streamSocket.TrySendFrame(Identity, Identity.Length, true))
                    {
                        m_streamSocket.TrySendFrame("");
                    }
                }

                break;

            case WebSocketClientState.Ready:
                byte[] message = m_streamSocket.ReceiveFrameBytes();
                m_decoder.Process(message);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #13
0
        public static void SendMessageAndData(NetMQSocket socket, MmiMessage message)
        {
            message.TimeStamp = DateTime.Now;

            var values = message.Values;
            if (values != null)
            {
                if (message.Shape == null)
                {
                    var shape = new int[values.Rank];
                    for (var i = 0; i < values.Rank; i++)
                    {
                        shape[i] = values.GetLength(i);
                    }
                    message.Shape = shape;
                }

                if (string.IsNullOrEmpty(message.DataType))
                {
                    message.DataType = GetDataTypeName(values.GetValue(0).GetType());
                }
            }

            var json = message.ToJson();

            if (values == null)
            {
                lock (socket)
                {
                    if (!socket.TrySendFrame(Timeout, json))
                    {
                        throw new NetMQException("Timeout during send");
                    }
                }
            }
            else
            {
                lock (socket)
                {
                    var bytes = ArrayToBytes(values);

                    if (!socket.TrySendFrame(Timeout, json, true))
                    {
                        throw new NetMQException("Timeout during send");
                    }

                    if (!socket.TrySendFrame(Timeout, bytes))
                    {
                        throw new NetMQException("Timeout during send");
                    }
                }
            }
        }
Пример #14
0
        public static void SendMessage(NetMQSocket socket, object o, Array values = null)
        {
            var json = JsonConvert.SerializeObject(o);

            lock (socket)
            {
                if (values == null)
                {
                    if (!socket.TrySendFrame(Timeout, json))
                    {
                        throw new NetMQException("Timeout during send");
                    }
                }
                else
                {
                    var bytes = ArrayToBytes(values);

                    if (!socket.TrySendFrame(Timeout, json, true))
                    {
                        throw new NetMQException("Timeout during send");
                    }

                    if (!socket.TrySendFrame(Timeout, bytes))
                    {
                        throw new NetMQException("Timeout during send");
                    }
                }
            }
        }