Exemplo n.º 1
0
        public void StartListener()
        {
            _keepReading = true;
            IsListening = false;
            try
            {
                if (_listener != null)
                    _listener.Close();
                _listener = new UdpClient(new IPEndPoint(IPAddress.Any, _listenPort));

            }
            catch (Exception exception)
            {
                var errorMessage = string.Format("Couldn't listen to Port {0}. Exception: {1}", _listenPort,
                    exception);
                Console.WriteLine(errorMessage);
                if (OnConnectionStateChanged != null)
                {
                    var connectionStateMessage = new BleConnectionStateMessage
                    {
                        Receiver = _receiver,
                        ConnectionState = BleConnectionState.Disconnected,
                        Message = errorMessage
                    };
                    OnConnectionStateChanged(connectionStateMessage);
                }
                return;

            }
            IsListening = true;
            if (OnConnectionStateChanged != null)
            {
                var connectionStateMessage = new BleConnectionStateMessage
                {
                    Receiver = _receiver,
                    ConnectionState = BleConnectionState.Connected,
                    Message = string.Format("Listening to port {0}",_receiver.IncomingPort)
                };
                OnConnectionStateChanged(connectionStateMessage);
            }
            Task.Run(() => ListenerLoop());

        }
Exemplo n.º 2
0
 public void StartListener()
 {
     _keepReading = true;
     IsListening  = false;
     try
     {
         if (_listener != null)
         {
             _listener.Close();
         }
         _listener = new UdpClient(new IPEndPoint(IPAddress.Any, _listenPort));
     }
     catch (Exception exception)
     {
         var errorMessage = string.Format("Couldn't listen to Port {0}. Exception: {1}", _listenPort,
                                          exception);
         Console.WriteLine(errorMessage);
         if (OnConnectionStateChanged != null)
         {
             var connectionStateMessage = new BleConnectionStateMessage
             {
                 Receiver        = _receiver,
                 ConnectionState = BleConnectionState.Disconnected,
                 Message         = errorMessage
             };
             OnConnectionStateChanged(connectionStateMessage);
         }
         return;
     }
     IsListening = true;
     if (OnConnectionStateChanged != null)
     {
         var connectionStateMessage = new BleConnectionStateMessage
         {
             Receiver        = _receiver,
             ConnectionState = BleConnectionState.Connected,
             Message         = string.Format("Listening to port {0}", _receiver.IncomingPort)
         };
         OnConnectionStateChanged(connectionStateMessage);
     }
     Task.Run(() => ListenerLoop());
 }
Exemplo n.º 3
0
 public void Stop()
 {
     IsListening  = false;
     _keepReading = false;
     if (_listener == null)
     {
         return;
     }
     _listener.Close();
     if (OnConnectionStateChanged != null)
     {
         var connectionStateMessage = new BleConnectionStateMessage
         {
             Receiver        = _receiver,
             ConnectionState = BleConnectionState.Disconnected,
             Message         = string.Format("Stopped listening to port {0}", _receiver.IncomingPort)
         };
         OnConnectionStateChanged(connectionStateMessage);
     }
 }
Exemplo n.º 4
0
        private async Task ListenerLoop()
        {
            try
            {
                while (_keepReading)
                {
                    //await Task.Delay(100);


                    //Console.WriteLine("Waiting for broadcast");
                    var bufferResult = await _listener.ReceiveAsync();

                    if (bufferResult.Buffer.IsNullOrEmpty())
                    {
                        continue;
                    }

                    var message = Encoding.ASCII.GetString(bufferResult.Buffer);
                    //Console.Write(
                    //    message);
                    MessageParser.ProcessMessage(message);
                }
            }
            catch (Exception exception)
            {
                var errorMessage = string.Format("Exception occured in ListenerLoop. Not listening to Port {0}. Exception: {1}", _listenPort,
                                                 exception);
                Console.WriteLine(errorMessage);
                if (OnConnectionStateChanged != null)
                {
                    var connectionStateMessage = new BleConnectionStateMessage
                    {
                        Receiver        = _receiver,
                        ConnectionState = BleConnectionState.Disconnected,
                        Message         = errorMessage
                    };
                    OnConnectionStateChanged(connectionStateMessage);
                }
            }
        }
Exemplo n.º 5
0
        private async Task ListenerLoop()
        {
            try
            {
                while (_keepReading)
                {
                    //await Task.Delay(100);


                    //Console.WriteLine("Waiting for broadcast");
                    var bufferResult = await _listener.ReceiveAsync();
                    if (bufferResult.Buffer.IsNullOrEmpty())
                        continue;

                    var message = Encoding.ASCII.GetString(bufferResult.Buffer);
                    //Console.Write(
                    //    message);
                    MessageParser.ProcessMessage(message);
                }

            }
            catch (Exception exception)
            {
                var errorMessage = string.Format("Exception occured in ListenerLoop. Not listening to Port {0}. Exception: {1}", _listenPort,
                    exception);
                Console.WriteLine(errorMessage);
                if (OnConnectionStateChanged != null)
                {
                    var connectionStateMessage = new BleConnectionStateMessage
                    {
                        Receiver = _receiver,
                        ConnectionState = BleConnectionState.Disconnected,
                        Message = errorMessage
                    };
                    OnConnectionStateChanged(connectionStateMessage);
                }
            }

        }
Exemplo n.º 6
0
 public void Stop()
 {
     IsListening = false;
     _keepReading = false;
     if (_listener == null)
         return;
     _listener.Close();
     if (OnConnectionStateChanged != null)
     {
         var connectionStateMessage = new BleConnectionStateMessage
         {
             Receiver = _receiver,
             ConnectionState = BleConnectionState.Disconnected,
             Message = string.Format("Stopped listening to port {0}", _receiver.IncomingPort)
         };
         OnConnectionStateChanged(connectionStateMessage);
     }
 }