예제 #1
0
        private void Listen()
        {
            //while (!StopToken.IsCancellationRequested && missedListenAttempts < 60)
            while (missedListenAttempts < 60)
            {
                try
                {
                    // Get the next inbound message
                    var data    = ReadString(inStream, outStream);
                    var message = Message.FromCharStream(socket.RemoteDevice.Address, data);

                    if (message.Type == MsgType.Ack)
                    {
                        Log.Info(_tag, $"Received server ack of {message.Content}");
                        lock (messagesAwaitingReply)
                        {
                            if (messagesAwaitingReply.ContainsKey(message.ID))
                            {
                                messagesAwaitingReply[message.ID].TrySetResult();
                            }
                        }
                    }
                    else
                    {
                        OnMessageReceived.Raise(message);
                        if (DoACK)
                        {
                            SendMessage(new Message(MsgType.Ack, $"{message.Type}: {message.Content}"));
                        }
                    }
                }
                catch (Java.IO.IOException)  // Signals one end or the other of the pipe being closed when we go to read from it.
                {
                    //Disconnect();
                    TallyMissedAttempt();
                }
                catch (System.AggregateException e)
                {
                    if (e.InnerExceptions.All(ex => ex is Java.IO.IOException || ex.InnerException is Java.IO.IOException))
                    {
                        TallyMissedAttempt();                                                                                                     //Disconnect();
                    }
                    else
                    {
                        throw e;
                    }
                }
                catch (Exception e)
                {
                    if (e.InnerException is Java.IO.IOException)
                    {
                        TallyMissedAttempt();                                          // Disconnect();
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
            Log.Debug(_tag, $"Reached end of Listen() loop - about to close connection.");
        }