Esempio n. 1
0
 protected bool TryReceiveFrameBytes(out byte[] frame)
 {
     if (Socket.TryReceiveFrameBytes(SendReceiveTimeout, out frame))
     {
         return(true);
     }
     else
     {
         HandleError(new ReceiveTimeoutException());
         return(false);
     }
 }
Esempio n. 2
0
        protected override ReceiveStatus SendPointValues()
        {
            byte[]   message         = null;
            bool     recievedmessage = false;
            DateTime lastMessageTime = DateTime.Now;
            int      index           = 0;

            while (true)
            {
                recievedmessage = socket.TryReceiveFrameBytes(out message);
                if (recievedmessage)
                {
                    lastMessageTime = DateTime.Now;
                    Debug.Log("Message Received");
                    //Debug.Log(BitConverter.ToString(message));
                    if (index >= byte_dataList.Count)
                    {
                        Debug.Log("Transfer Finished");
                        socket.SendFrame("PointData Finished");
                        return(ReceiveStatus.SUCCESS);
                    }
                    string convert = System.Text.Encoding.UTF8.GetString(message, 0, message.Length);
                    if (convert == "PointCollumn Echo")
                    {
                        Debug.Log("length of " + index + " : " + dataList[index].Length);

                        Debug.Log("length of " + index + " : " + byte_dataList[index].Length);
                        socket.SendFrame(byte_dataList[index]);
                        lastMessageTime = DateTime.Now;
                        index++;
                    }
                }
                DateTime currentTime = DateTime.Now;
                DateTime stopTime    = lastMessageTime.AddSeconds(TimeLimit);
                if (stopTime < currentTime)
                {
                    return(ReceiveStatus.TIMEOUT);
                }
            }
        }
Esempio n. 3
0
        public void Start(int port, CancellationToken cancellationToken = default)
        {
            if (cancellationToken == default)
            {
                _source           = new CancellationTokenSource();
                cancellationToken = _source.Token;
            }

            _address  = $"tcp://*:{port}";
            _outgoing = new ResponseSocket();

            var options = new SocketOptions(_outgoing);

            _protocol.Configure(options);
            _outgoing.Bind(_address);
            _out?.WriteInfoLine($"{_id}: Bound to {_address}");

            _task = Task.Run(() =>
            {
                EstablishHandshake();

                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        if (_outgoing.IsDisposed)
                        {
                            continue;
                        }
                        if (!_outgoing.TryReceiveFrameBytes(_interval, out var message))
                        {
                            continue;
                        }
                        _protocol.OnMessageReceived(_outgoing, message);
                    }
                    catch (Exception e)
                    {
                        _out?.WriteErrorLine($"{_id}: Error during message receive loop: {e}");
                        break;
                    }
                }

                _stopping.Set();
            }, cancellationToken);
        }
Esempio n. 4
0
        public static MmiMessage ReceiveMessageAndData(NetMQSocket socket)
        {
            MmiMessage message;

            lock (socket)
            {
                string json;
                bool more;
                var msg = new Msg();
                msg.InitEmpty();
                if (!socket.TryReceiveFrameString(Timeout, out json, out more))
                {
                    throw new NetMQException("Timeout during receive");
                }

                message = new MmiMessage { JsonString = json };
                message.FillFromJson(json);

                // receive data
                if (socket.HasIn)
                {
                    byte[] bytes;
                    if (!socket.TryReceiveFrameBytes(Timeout, out bytes))
                    {
                        throw new NetMQException("Timeout during receive bytes");
                    }

                    message.Values = BytesToArray(bytes, message.DataType, message.Shape);
                }
            }

            return message;
        }
Esempio n. 5
0
        protected override ReceiveStatus ReceivePointValues()
        {
            long timeSinceLastMessage;

            while (true)
            {
                socket.SendFrame("PointCollumn Echo");
                byte[] message         = null;
                bool   recievedmessage = false;
                timeSinceLastMessage = 0;

                while (true)
                {
                    recievedmessage = socket.TryReceiveFrameBytes(out message);
                    if (recievedmessage)
                    {
                        timeSinceLastMessage = 0;
                        Debug.Log("Message Received");
                        string convert = System.Text.Encoding.UTF8.GetString(message, 0, message.Length);
                        if (convert == "PointData Finished")
                        {
                            if (dataList.Count < 1)
                            {
                                return(ReceiveStatus.INVALID_FORMAT);
                            }
                            else
                            {
                                return(ReceiveStatus.SUCCESS);
                            }
                        }
                        if (message.Length < 1)
                        {
                            return(ReceiveStatus.INVALID_FORMAT);
                        }

                        var array = new float[message.Length / sizeof(float)];
                        try
                        {
                            Buffer.BlockCopy(message, 0, array, 0, message.Length);
                        }
                        catch
                        {
                            return(ReceiveStatus.BYTE_CONVERSION_ERROR);
                        }
                        if (dataList.Count != 0 && array.Length != dataList[dataList.Count - 1].Length)
                        {
                            return(ReceiveStatus.INVALID_FORMAT);
                        }

                        dataList.Add(array);
                        break;
                    }
                    else
                    {
                        timeSinceLastMessage++;
                    }

                    if (timeSinceLastMessage > timeLimit)
                    {
                        //Doesn't seem very efficient, should probably find another way later.
                        return(ReceiveStatus.TIMEOUT);
                    }
                }
            }
        }