示例#1
0
        MessagePayload ReceiveMessage()
        {
            lock (ReceiveLock)
            {
                lock (AliveLock)
                {
                    if (DeathException != null)
                    {
                        throw DeathException;
                    }
                }

                try
                {
                    var framer  = new MessageFramer(NetworkConfig.Magic);
                    var message = framer.NextMessage(Stream);
                    ReceivedMessages.Add(message);
                    return(message);
                }
                catch
                {
                    lock (AliveLock)
                    {
                        if (DeathException == null)
                        {
                            Alive          = false;
                            DeathException = new ConnectionDeadException("ReceiveMessage threw exception.");
                        }
                    }
                    throw DeathException;
                }
            }
        }
示例#2
0
        public void SendMessage(MessagePayload message)
        {
            lock (SendLock)
            {
                lock (AliveLock)
                {
                    if (DeathException != null)
                    {
                        throw DeathException;
                    }
                }

                try
                {
                    Byte[] data = message.ToBytes();
                    Stream.Write(data, 0, data.Length);
                    SentMessages.Add(message);
                }
                catch
                {
                    lock (AliveLock)
                    {
                        if (DeathException == null)
                        {
                            Alive          = false;
                            DeathException = new ConnectionDeadException("SendMessage threw exception.");
                        }
                    }
                    throw DeathException;
                }
            }
        }