Beispiel #1
0
        // which actually calls this
        private void Broadcast_Join(object sender, NewBroadcastEventArgs e)
        {
            try
            {
                addClient(e.message.Sender);

                RemoteMessage msg = new RemoteMessage(nameTextBox.Text);
                msg.Event = Event.Discovery;
                broadcast.Send(msg);

                messages.Add(new InternalMessage($"{e.message.Sender.Name} joined"));
            }
            catch (Exception ex) // Just for debugging
            {
                MessageBox.Show(ex.Message);
                throw; // for the debugger
            }
        }
Beispiel #2
0
        //This is called when a message is received (before any events are called)
        private void OnBroadcastMessage(IAsyncResult res)
        {
            byte[] received = client.EndReceive(res, ref remoteEP);

            // Begin receiving A$AP
            client.BeginReceive(new AsyncCallback(OnBroadcastMessage), null);

            //Process the message
            if (!remoteEP.Address.Equals(localEP.Address)) // message is from someone else
            {
                NewBroadcastEventArgs args = new NewBroadcastEventArgs();
                args.message        = new RemoteMessage(received);
                args.message.Sender = new RemoteClient(localEP, remoteEP);

                switch (args.message.Event)
                {
                case Event.Join:
                    args.message.Sender.Name = args.message.Words;
                    OnJoin(args);
                    break;

                case Event.Discovery:
                    args.message.Sender.Name = args.message.Words;
                    OnDiscovery(args);
                    break;

                case Event.Leave:
                    OnLeave(args);
                    break;

                case Event.Message:
                    OnMessage(args);
                    break;
                }
            }
        }
Beispiel #3
0
        // which actually calls this
        private void Broadcast_Message(object sender, NewBroadcastEventArgs e)
        {
            messages.Add(e.message);

            //messages.Add(new InternalMessage($"{e.message.Sender.Name} joined"));
        }
Beispiel #4
0
 // called when new broadcast arrives
 public void OnMessage(object sender, NewBroadcastEventArgs e) =>
 Dispatcher.BeginInvoke(new Action(() =>
                                   Broadcast_Message(sender, e)
                                   ));
Beispiel #5
0
 // called upon a client leaving
 private void Broadcast_Leave(object sender, NewBroadcastEventArgs e) =>
 Dispatcher.BeginInvoke(new Action(() =>
                                   clients.Remove(e.message.Sender)
                                   ));
Beispiel #6
0
 // response to my join message (other people exist)
 public void OnBroadcastDiscovery(object sender, NewBroadcastEventArgs e) =>
 Dispatcher.BeginInvoke(new Action(() =>
                                   addClient(e.message.Sender)
                                   ));
Beispiel #7
0
        protected virtual void OnMessage(NewBroadcastEventArgs e)
        {
            EventHandler <NewBroadcastEventArgs> handler = Message;

            handler?.Invoke(this, e);
        }
Beispiel #8
0
        protected virtual void OnDiscovery(NewBroadcastEventArgs e)
        {
            EventHandler <NewBroadcastEventArgs> handler = Discovery;

            handler?.Invoke(this, e);
        }
Beispiel #9
0
        // EventThing
        protected virtual void OnJoin(NewBroadcastEventArgs e)
        {
            EventHandler <NewBroadcastEventArgs> handler = Join;

            handler?.Invoke(this, e);
        }