Beispiel #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());

        }
Beispiel #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());
 }
Beispiel #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);
     }
 }
Beispiel #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);
                }
            }
        }
Beispiel #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);
                }
            }

        }
Beispiel #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);
     }
 }