Пример #1
0
        /// <summary>
        ///loop through all connected chatters and invoke their
        ///ChatEventHandler delegate asynchronously, which will firstly call
        ///the MyEventHandler() method and will allow a asynch callback to call
        ///the EndAsync() method on completion of the initial call
        /// </summary>
        /// <param name="e">The ChatEventArgs to use to send to all connected chatters</param>
        public void BroadcastMessage(ChatEventArgs e)
        {
            ChatEventHandler temp = ChatEvent;

            //loop through all connected chatters and invoke their
            //ChatEventHandler delegate asynchronously, which will firstly call
            //the MyEventHandler() method and will allow a asynch callback to call
            //the EndAsync() method on completion of the initial call
            if (temp != null)
            {
                foreach (ChatEventHandler handler in temp.GetInvocationList())
                {
                    handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Takes a <see cref="Common.Person">Person</see> and allows them
        /// to join the chat room, if there is not already a chatter with
        /// the same name
        /// </summary>
        /// <param name="person"><see cref="Common.Person">Person</see> joining</param>
        /// <return
        public bool Join(string name, ChatEventHandler chat_event_handler, out CommPointInfo[] person_list)
        {
            bool userAdded = false;

            person_list = null;

            CommPointInfo person = new CommPointInfo(name);

            //carry out a critical section that checks to see if the new chatter
            //name is already in use, if its not allow the new chatter to be
            //added to the list of chatters, using the person as the key, and the
            //ChatEventHandler delegate as the value, for later invocation
            lock (syncObj)
            {
                if (!checkIfPersonExists(person.name) && person != null)
                {
                    chatters.Add(person, chat_event_handler);
                    userAdded = true;
                }
            }

            //if the new chatter could be successfully added, get a callback instance
            //create a new message, and broadcast it to all other chatters, and then
            //return the list of al chatters such that connected clients may show a
            //list of all the chatters
            if (userAdded)
            {
                ChatEventArgs e = new ChatEventArgs();
                e.msgType = MessageType.UserEnter;
                e.person  = person.name;
                BroadcastMessage(e);

                //add this newly joined chatters ChatEventHandler delegate, to the global
                //multicast delegate for invocation
                ChatEvent += chat_event_handler;
                //carry out a critical section that copy all chatters to a new list
                lock (syncObj)
                {
                    person_list = new CommPointInfo[chatters.Count];
                    chatters.Keys.CopyTo(person_list, 0);
                }
                // Tell the controller we have a new pilot that joined.
                _controller_callback.Join(person.name);
            }

            return(userAdded);
        }
Пример #3
0
        /// <summary>
        /// A request has been made by a client to leave the chat room,
        /// so remove the <see cref="Common.Person">Person </see>from
        /// the internal list of chatters, and unwire the chatters
        /// delegate from the multicast delegate, so that it no longer
        /// gets invokes by globally broadcasted methods
        /// </summary>
        public void Leave(string person)
        {
            if (person == null)
            {
                return;
            }

            bool userRemoved = false;
            //get the chatters ChatEventHandler delegate
            ChatEventHandler chatterToRemove = null;

            //carry out a critical section, that removes the chatter from the
            //internal list of chatters
            lock (syncObj)
            {
                chatterToRemove = getPersonHandler(person);
                if (chatterToRemove != null)
                {
                    userRemoved = true; // Found the user that needs to be removed.
                    CommPointInfo comm_point = getPerson(person);
                    chatters.Remove(comm_point);
                }
            }

            if (userRemoved)
            {
                //unwire the chatters delegate from the multicast delegate, so that
                //it no longer gets invokes by globally broadcasted methods
                ChatEvent -= chatterToRemove;
                ChatEventArgs e = new ChatEventArgs();
                e.msgType = MessageType.UserLeave;
                e.person  = person;
                //broadcast this leave message to all other remaining connected
                //chatters
                BroadcastMessage(e);

                // Tell the controller we have a pilot that left.
                _controller_callback.Leave(person);
            }
        }