コード例 #1
0
        public void SendMessage(IMessage message)
        {
            var multipart = new MultipartMessage((Message)message);
            var frames    = (IList <byte[]>)multipart.Frames;
            var msg       = new Msg();

            try
            {
                var framesCount = frames.Count;
                for (var i = 0; i < framesCount;)
                {
                    var buffer = frames[i];
                    msg.InitGC(buffer, buffer.Length);
                    var sendingTimeout1 = config.SendTimeout;
                    if (!socket.TrySend(ref msg, sendingTimeout1, ++i < framesCount))
                    {
                        throw new TimeoutException($"Sending timed out after {sendingTimeout1.TotalMilliseconds} ms!");
                    }
                }
                SendRate?.Increment();
            }
            finally
            {
                msg.Close();
            }
        }
コード例 #2
0
        public static bool SendNetworkMsg(this NetMQSocket socket, NetworkMessageBase message,
                                          uint timeoutMilliSeconds = GlobalConstants.StandardSendingTimeout)
        {
            var encodedMessage = NetworkMessageCoding.Encode(message);

            var outMsg = new Msg();

            outMsg.InitPool(Encoding.GetByteCount(encodedMessage));
            Encoding.GetBytes(encodedMessage, 0, encodedMessage.Length, outMsg.Data, 0);

            bool sendSuccessful;

            try
            {
                sendSuccessful = socket.TrySend(ref outMsg, TimeSpan.FromMilliseconds(timeoutMilliSeconds), false);

                Debug.WriteLine("[T >>>] " + encodedMessage);
            }
            catch (Exception)
            {
                sendSuccessful = false;
            }

            outMsg.Close();
            return(sendSuccessful);
        }
コード例 #3
0
    public bool sendAndReceive(ref NetMQ.Msg req_msg, ref NetMQ.Msg resp_msg)
    {
        if (socket == null)
        {
            reconnect();
        }

        StdMessage m = new StdMessage(req_msg.Data[1], req_msg.Data[2]);

        NetMQ.Msg copy = new NetMQ.Msg();
        bool      ok   = false;

        for (uint i = 0; i < retries; i++)
        {
            copy.Copy(ref req_msg);

            // send
            if (!socket.TrySend(ref copy, TimeSpan.FromMilliseconds(min_trysend_ms), false))
            {
                ok = false;
                UnityEngine.Debug.Log("ReliableExternalClient: could not send");
                break;

                //TODO: clear enqueued messages when server is offline
            }
            //UnityEngine.Debug.Log("ReliableExternalClient: request sent " + m.to_string());

            // receive
            if (socket.TryReceive(ref resp_msg, timeout))
            {
                ok = true;
                // UnityEngine.Debug.Log("ReliableExternalClient: response received "
                //+ new StdMessage(resp_msg.Data[1], resp_msg.Data[2]).to_string());
                break;
            }

            //UnityEngine.Debug.Log(String.Format("ReliableExternalClient: no response from server {0}. Retrying", srvAddress));
            reconnect();
        }

        if (!ok)
        {
            UnityEngine.Debug.Log(String.Format("ReliableExternalClient: server {0} seems to be offline. Abandoning", srvAddress));
        }

        copy.Close();
        socket.Dispose();  //call Dispose on all sockets before cleanup the netmq context
        socket.Close();

        socket = null;

        if (socket == null)
        {
            UnityEngine.Debug.Log("ReliableExternalClient: socket closed and null");
        }

        return(ok);
    }