Beispiel #1
0
        private void RunReceiverProcessCallback(object state, CancellationToken token)
        {
            buffer = HelperTools.RentBuffer(HelperTools.SIZE_BYTES);
            Socket socket = (Socket)state;
            //EndPoint e = new IPEndPoint(IPAddress.Any, networkPort);
            long time;
            int  n_bytes;

            while (!cancelToken.IsCancellationRequested)
            {
                if (socket != null)
                {
                    try
                    {
                        n_bytes = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                        time    = HelperTools.GetLocalMicrosTime();
                        if (n_bytes > 0)
                        {
                            // Fire Event
                            DataReadyEvent?.Invoke(networkIp, networkPort, time, buffer, 0, n_bytes, ID, ipChunks);
                        }
                    }
                    catch (Exception ee) { }
                }
            }
            HelperTools.ReturnBuffer(buffer);
        }
Beispiel #2
0
        private void OnCameraImageReceived(MeshDataUpdates meshes, Texture2D camTexture)
        {
            meshes.WriteCameraImageColors(_camera, camTexture);
            var data = MeshSerializer.GenerateMeshData(meshes);

            DataReadyEvent?.Invoke(data);
        }
        /// <summary>
        /// method that will be called when theres data waiting in the buffer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string msg;

            //determine the mode the user selected (binary/string)
            switch (CurrentTransmissionType)
            {
            //user chose string
            case TransmissionType.Text:
                //read data waiting in the buffer
                //msg = comPort.ReadExisting().Trim();
                msg = comPort.ReadLine().Trim();
                //display the data to the user
#if false
                if (msg.Length > 0)
                {
                    DataReadyEvent.Set();
                    RcvdMsg = msg;
                    DisplayData(MessageType.Incoming, msg + "\n");
                }
#else
                // Don't check for 0 length - ReadLine() reads until NewLine anyway, and Trim() removes trailing white space
                DataReadyEvent.Set();
                RcvdMsg = msg;
                DisplayData(MessageType.Incoming, msg + "\n");
#endif
                break;

            //user chose binary
            case TransmissionType.Hex:
                //retrieve number of bytes in the buffer
                int bytes = comPort.BytesToRead;
                //create a byte array to hold the awaiting data
                byte[] comBuffer = new byte[bytes];
                //read the data and store it
                comPort.Read(comBuffer, 0, bytes);
                //display the data to the user
                if (bytes > 0)
                {
                    RcvdMsg = ByteToHex(comBuffer);
                    DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
                }
                break;

            default:
                //read data waiting in the buffer
                //msg = comPort.ReadExisting().Trim();
                msg = comPort.ReadLine().Trim();
                //display the data to the user
                if (msg.Length > 0)
                {
                    RcvdMsg = msg;
                    DisplayData(MessageType.Incoming, msg + "\n");
                }
                break;
            }
        }
Beispiel #4
0
        /// <summary>
        /// When a connection to the server is established and we can start reading the data, this will be called.
        /// </summary>
        /// <param name="asyncInfo">Info about the connection.</param>
        /// <param name="status">Status of the connection</param>
        private async void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
        {
            // Status completed is successful.
            if (status == AsyncStatus.Completed)
            {
                DataReader networkDataReader;

                // Since we are connected, we can read the data being sent to us.
                using (networkDataReader = new DataReader(networkConnection.InputStream))
                {
                    // read four bytes to get the size.
                    DataReaderLoadOperation drlo = networkDataReader.LoadAsync(4);
                    while (drlo.Status == AsyncStatus.Started)
                    {
                        // just waiting.
                    }

                    int dataSize = networkDataReader.ReadInt32();
                    if (dataSize < 0)
                    {
                        Debug.Log("Super bad super big data size");
                    }

                    // Need to allocate a new buffer with the dataSize.
                    mostRecentDataBuffer = new byte[dataSize];

                    // Read the data.
                    await networkDataReader.LoadAsync((uint)dataSize);

                    networkDataReader.ReadBytes(mostRecentDataBuffer);

                    // And fire our data ready event.
                    DataReadyEvent?.Invoke(mostRecentDataBuffer);
                }
            }
            else
            {
                Debug.Log("Failed to establish connection for rcv. Error Code: " + asyncInfo.ErrorCode);
                // In the failure case we'll requeue the data and wait before trying again.


                // And set the defer time so the update loop can do the 'Unity things'
                // on the main Unity thread.
                DeferredActionQueue.Enqueue(() =>
                {
                    Invoke("RequestDataRetry", timeToDeferFailedConnections);
                });
            }

            networkConnection.Dispose();
            waitingForConnection = false;
        }
Beispiel #5
0
        public void SetViewModel(object context)
        {
            if (context is TViewModel viewModel)
            {
                DataContext = viewModel;
            }
            else
            {
                Her.Warn($"{context} is not matching {typeof(TViewModel)}");
            }

            DataReadyEvent?.Invoke();
        }
Beispiel #6
0
        public void UnsubscribeEventHandlers()
        {
            if (DataReadyEvent != null)
            {
                foreach (var d in DataReadyEvent.GetInvocationList())
                {
                    DataReadyEvent -= (d as DataReadyEventHandler);
                }
            }

            if (ConnectionStateEvent != null)
            {
                foreach (var d in ConnectionStateEvent.GetInvocationList())
                {
                    ConnectionStateEvent -= (d as ConnectionStateDelegate);
                }
            }
        }
        /// <summary>
        /// When a connection to the server is established and we can start reading the data, this will be called.
        /// </summary>
        /// <param name="asyncInfo">Info about the connection.</param>
        /// <param name="status">Status of the connection</param>
        private async void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
        {
            // Status completed is successful.
            if (status == AsyncStatus.Completed)
            {
                DataReader networkDataReader;

                // Since we are connected, we can read the data being sent to us.
                using (networkDataReader = new DataReader(networkConnection.InputStream))
                {
                    // read four bytes to get the size.
                    DataReaderLoadOperation drlo = networkDataReader.LoadAsync(4);
                    while (drlo.Status == AsyncStatus.Started)
                    {
                        // just waiting.
                    }

                    int dataSize = networkDataReader.ReadInt32();
                    if (dataSize < 0)
                    {
                        Debug.Log("Super bad super big datasize");
                    }

                    // Need to allocate a new buffer with the dataSize.
                    mostRecentDataBuffer = new byte[dataSize];

                    // Read the data.
                    await networkDataReader.LoadAsync((uint)dataSize);

                    networkDataReader.ReadBytes(mostRecentDataBuffer);

                    // And fire our data ready event.
                    DataReadyEvent?.Invoke(mostRecentDataBuffer);
                }
            }
            else
            {
                Debug.Log("Failed to establish connection for rcv. Error Code: " + asyncInfo.ErrorCode);
            }

            networkConnection.Dispose();
            waitingForConnection = false;
        }
        private void HandleTransponderDataReady(object sender, RawTransponderDataEventArgs e)
        {
            Tracks = new Track[e.TransponderData.Count];
            char[] separator  = { ';' };
            int    trackIndex = 0;

            foreach (var data in e.TransponderData)
            {
                string[] tokens = data.Split(separator);
                Tracks[trackIndex] = new Track()
                {
                    Tag       = tokens[0],
                    X         = int.Parse(tokens[1]),
                    Y         = int.Parse(tokens[2]),
                    Z         = int.Parse(tokens[3]),
                    Timestamp = DateTime.ParseExact(tokens[4], "yyyyMMddHHmmssfff", null)
                };

                trackIndex++;
            }

            DataReadyEvent?.Invoke(null, new DataReceivedEventArgs(Tracks.ToList()));
        }
Beispiel #9
0
 public void SetViewModel(TViewModel dataContext)
 {
     DataContext = dataContext;
     DataReadyEvent?.Invoke();
 }
Beispiel #10
0
 private void OnCommunicatorData(string ip, int port, long time, byte[] bytes, int offset, int length, string ID, ushort[] ipChunks)
 {
     // Not used normally
     DataReadyEvent?.Invoke(ip, port, time, bytes, offset, length, ID, ipChunks);
 }
Beispiel #11
0
 public virtual void FireDataEvent(string ip, int port, long time, byte[] bytes, int offset, int length, string ID, ushort[] ipChunks = null)
 {
     DataReadyEvent?.Invoke(ip, port, time, bytes, offset, length, ID, ipChunks);
 }