Exemplo n.º 1
0
 /// <summary>
 /// Handles message send failure.
 /// </summary>
 /// <param name="sender">The communications object that raised the error</param>
 /// <param name="args">The message that failed</param>
 private void FailedMessage(object sender, MessageEventArgs args)
 {
     Message message = args.Message;
     if (message.Type == MessageType.Login) {
         if (!LoggedIn) {
             _address = ' ';
         }
     }
     if (message.Type == MessageType.Message) {
         //If 6 attempts haven't gotten through, they probably logged out already
         _identifiedUsers.Remove(message.Address);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Triggers the actionable message event
 /// </summary>
 /// <param name="args">EventArgs object containing the message</param>
 private void OnActionableMessageRecieved(MessageEventArgs args)
 {
     if (ActionableMessageRecieved != null) {
         ActionableMessageRecieved(this, args);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Event handler for recieving a new message.
        /// Passes events up to the UI if neccesary, otherwise responds as appropriate
        /// </summary>
        /// <param name="sender">Communications object that raised the event</param>
        /// <param name="args">MessageEventArgs object containing the recieved message</param>
        public void RecieveMessage(object sender, MessageEventArgs args)
        {
            Message message = args.Message;
            if (_promiscuous || message.Address == Address || message.SenderAddress == Address) {
                if (message.Type == MessageType.Message) {
                    OnActionableMessageRecieved(args);
                }
            }
            if (message.Type == MessageType.Login) {
                if (message.SenderAddress == Address) {
                    if (LoggedIn) {
                        Communications.PassOn(message.Acknowledge);
                    }
                    else {
                        _loggedIn = true;
                    }
                }

                if (!_identifiedUsers.Contains(message.Address)) {
                    _identifiedUsers.Add(message.Address);
                }
                if (message.SenderAddress != Address) {
                    Communications.PassOn(new Message(null, message.Address, this.Address, MessageType.IdentResponse));
                }
                OnActionableMessageRecieved(args);
            }
            if (message.Type == MessageType.Logout) {
                if (message.Address == Address) {
                    _loggedIn = false;
                    _address = ' ';
                }
                _identifiedUsers.Remove(message.Address);
                OnActionableMessageRecieved(args);
            }
            if (message.Type == MessageType.IdentResponse) {
                if (!_identifiedUsers.Contains(message.SenderAddress)) {
                    _identifiedUsers.Add(message.SenderAddress);
                }
                OnActionableMessageRecieved(args);
            }
            if (message.Type == MessageType.Acknowledge) {
                if (!LoggedIn) {
                    _address = ' ';
                    OnActionableMessageRecieved(args);
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Handles sending messages to other clients from a UI
 /// </summary>
 /// <param name="sender">The UI object that requested the send</param>
 /// <param name="args">The MessageEventArgs object representing the message to send</param>
 public void SendMessage(object sender, MessageEventArgs args)
 {
     Communications.Send(args.Message);
     if (args.Message.Address != Address) {
         OnActionableMessageRecieved(args);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Event handler for failed sends
        /// </summary>
        /// <param name="sender">The communications interface that raised the failure</param>
        /// <param name="args">The MessageEventArgs object containing the failed message object</param>
        void MessageSendFailed(object sender, MessageEventArgs args)
        {
            Message message = args.Message;
            switch (message.Type) {
                case MessageType.Login:
                    DisplayStatusMessage("Either ident taken or network failure! Try again!");
                    this.Invoke((Action)(() => RecievedMessagesBox.Enabled = false));
                    break;
                case MessageType.Message:
                    DisplayStatusMessage("Message '{0}' to {1} send failed!".With(message.Payload, message.Recipient));
                    DisplayStatusMessage("Assuming {0} is offline.".With(message.Recipient));
                    this.Invoke((Action) HandleLogin);
                    break;

            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Send a message to all listening clients
 /// </summary>
 /// <param name="message">The message object to be sent.</param>
 private void OnSendMessage(Message message)
 {
     if (SendMessage!=null) {
         MessageEventArgs args = new MessageEventArgs(message);
         SendMessage(this, args);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Event Handler for new displayable message 
        /// </summary>
        /// <param name="sender">The Client object that raised the event</param>
        /// <param name="e">The Message Event Args object containing the message object to be displayed</param>
        void MessageRecieved(object sender, MessageEventArgs e)
        {
            Message message = e.Message;
            switch (message.Type) {
                case MessageType.Message:
                    DisplayRecievedMessage(message);
                    break;
                case MessageType.Login:
                case MessageType.Logout:
                    DisplayLoginMessage(message);
                    this.Invoke((Action)HandleLogin);
                    break;
                case MessageType.IdentResponse:
                    this.Invoke((Action)HandleLogin);
                    DisplayStatusMessage("{0} is online.".With(message.Sender));
                    break;
                case MessageType.Acknowledge:
                    DisplayStatusMessage("Ident taken! Try again.");
                    this.Invoke((Action)(() => RecievedMessagesBox.Enabled = false));
                    break;

            }
            _lastRecievedMessage = e.Message;
            _lastRecievedOn = DateTime.UtcNow;
        }
Exemplo n.º 8
0
        //
        // Event Handlers
        //
        /// <summary>
        /// Event Handler for reciving a message and handling the low-level post-recieve tasks
        /// </summary>
        /// <param name="sender">This comms object</param>
        /// <param name="args">The MessageEventArgs object containing the recieved message</param>
        private void RecieveMessage(object sender, MessageEventArgs args)
        {
            Message message = args.Message;

            switch (message.Type) {
                case MessageType.Login:
                    if (message.Address == _parent.Address && message.SenderAddress == _parent.Address) {
                        Logger.Log("Got my login packet back", _parent.DisplayAddress);
                        lock (this) {
                            _pending = _pending.Where(item => item.Message != message).ToList();
                        }
                    }
                    break;
                case MessageType.Message:
                    if (message.Address == _parent.Address) {
                        Logger.Log("Recieved message '{0}' from {1}".With(message.ToString(), message.Sender), _parent.DisplayAddress);
                        Logger.Log("Responding to '{0}' with Ack".With(message.ToString()), _parent.DisplayAddress);
                        PassOn(message.Acknowledge);
                    }
                    break;
                case MessageType.Acknowledge:
                    if (message.Address == _parent.Address) {
                        char address = message.SenderAddress;
                        Logger.Log("Received Ack from {0}".With(address), _parent.DisplayAddress);
                        lock (this) {
                            try {
                                Message toRemove = _pending.NextTo(address).Message;
                                Logger.Log("Matched this to message '{0}'".With(toRemove.ToString()), _parent.DisplayAddress);
                                _pending = _pending.Where(item => item.Message != toRemove).ToList();
                            }
                            catch (NullReferenceException) {
                                Logger.Log("Recieved Ack for unknown packet from {0}!".With(message.SenderAddress), _parent.DisplayAddress);
                            }
                        }
                    }
                    break;
                case MessageType.NotAcknowledgable:
                    if (message.Address == _parent.Address) {
                        Logger.Log("Recieved Nack from {0}".With(message.SenderAddress), _parent.DisplayAddress);
                        Pending pending = _pending.NextTo(message.SenderAddress);
                        if (pending != null) {
                            Logger.Log("Matched this to '{0}'".With(pending.Message.ToString()), _parent.DisplayAddress);
                            pending.LastSend = DateTime.MinValue;
                        }
                    }
                    break;

            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Raises a Message Recieved event
 /// </summary>
 /// <param name="message">The message object to raise</param>
 private void OnMessageRecieved(Message message)
 {
     MessageEventArgs args = new MessageEventArgs(message);
     OnMessageRecieved(args);
 }
Exemplo n.º 10
0
 //
 //Private Methods and Events
 //
 /// <summary>
 /// Raises a Message Recieved event
 /// </summary>
 /// <param name="message">The MessageEventArgs object wrapping the Message object to be sent</param>
 private void OnMessageRecieved(MessageEventArgs message)
 {
     if (Recieved != null) {
         Recieved(this, message);
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Raises a Message Failed event for when sending was impossible
 /// </summary>
 /// <param name="args">The MessageEventArgs object wrapping the Message that failed</param>
 public void OnMessageFailed(MessageEventArgs args)
 {
     if (Failed != null) {
         Failed(this, args);
     }
 }