Example #1
0
        private void MessageCallBack(IAsyncResult result)
        {
            byte[] receivedData = { };
            try
            {
                receivedData = client.EndReceive(result, ref remoteEP);
            }
            catch (Exception ex)
            {
                //if (ex.SocketErrorCode == SocketError.ConnectionReset) //connection was forcibly closed by the remote host
                if (ex is SocketException)
                {
                    OnLeave(new EventArgs());
                    return;
                }
                else if (ex is ObjectDisposedException)
                {
                    // TODO: somehow purge all async taskes rather than waiting for it to error out
                    return;                     // udpClient has been closed
                }
                else
                {
                    throw;
                }
            }
            if (remoteEP.Address.Equals(localEP.Address))
            {
                return; // some loopback weirdness -- ignore it
            }

            RemoteMessage message = new RemoteMessage(receivedData);

            message.Sender = this;

            // begin receving as soon as possible
            client.BeginReceive(new AsyncCallback(MessageCallBack), null);

            switch (message.Event)
            {
            case Event.Leave:
                OnLeave(new EventArgs());
                break;

            case Event.Message:
                NewMessageEventArgs args = new NewMessageEventArgs();
                args.message = message;
                OnMessage(args);                         // BIG ERROR HERE
                break;
                // what if a client sends us a private join or discovery message..? they dumb dumb
            }
        }
Example #2
0
        protected virtual void OnMessage(NewMessageEventArgs e)
        {
            EventHandler <NewMessageEventArgs> handler = Message;

            handler?.Invoke(this, e);
        }
Example #3
0
        // We don't touch this, okay?
        #region Client Events

        // called when a NORMAL message is received
        public void OnClientMessage(object sender, NewMessageEventArgs e) =>
        Dispatcher.BeginInvoke(new Action(() =>
                                          Client_Message((RemoteClient)sender, e.message)
                                          ));