Example #1
0
        /// <summary>
        ///     Handles incoming data on the pipe.
        ///     Feeds the data into an instance of JsonLineProtocol.
        ///     Loops over any packets that were decoded and sends them to Command
        /// </summary>
        private void Recv()
        {
            var recvBytes = new byte[256];
            var proto     = new JsonLineProtocol();

            Socket.Receive(recvBytes);

            var msgs = proto.Feed(Encoding.ASCII.GetString(recvBytes));

            foreach (var packet in msgs)
            {
                try
                {
                    Command(packet);
                }
                catch (TimeoutException)
                {
                    Socket = null;
                    OnDisconnect?.Invoke();
                    throw;
                }
                catch (SocketException ex)
                {
                    Socket = null;
                    OnDisconnect?.Invoke();
                    throw new ConnectionException("Connection is in an error state.", ex);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Listens for Vehicle Broadcasts using UPD broadcasts on specified port using local machine network settings for broardcast IP.
        /// Raises on event on vehicle broardcast received
        /// </summary>
        /// <param name="ep">IP address of interface and port to listen on.</param>
        // http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx
        public void Run(IPEndPoint ep)
        {
            _listener = new UdpClient(ep);
            var groupEp = new IPEndPoint(IPAddress.Any, ep.Port);

            try
            {
                while (true)
                {
                    // Waiting for broadcast
                    var bytes = _listener.Receive(ref groupEp);
                    var msgs  = _proto.Feed(Encoding.ASCII.GetString(bytes));

                    foreach (var received in msgs)
                    {
                        try
                        {
                            string name           = received.name;
                            ushort connectionPort = received.port;
                            string video          = received.video;

                            var vehicleIpEndPoint = new IPEndPoint(groupEp.Address, connectionPort);

                            OnBroadcastReceived?.Invoke(new Broadcast(vehicleIpEndPoint, name, video));
                        }
                        catch (RuntimeBinderException) { }                         // caught a broadcast that is not formatted correctly (not from a vehicle)
                    }
                }
            }
            catch (SocketException ex)
            {
                // Thread was killed
                if (ex.ErrorCode == 10004)
                {
                    return;
                }

                throw new Exceptions.ConnectionException("Error in protocol.", ex);
            }
        }