/// <summary> /// This method is called when ever one of the chatters /// ChatEventHandler delegates is invoked. When this method /// is called it will examine the events ChatEventArgs to see /// what type of message is being broadcast, and will then /// call the correspoding method on the clients callback interface /// </summary> /// <param name="sender">the sender, which is not used</param> /// <param name="e">The ChatEventArgs</param> private void MyEventHandler(object sender, GameEventArgs e) { try { _Callback.Receive(e.Action); } catch { Leave(); } }
private void BroadcastMessage(GameAction action, XmlUser skipUser) { GameEventArgs e = new GameEventArgs(); e.Person = action.Sender; e.Action = action; BroadcastMessage(e, skipUser); }
/// <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> private void BroadcastMessage(GameEventArgs e, XmlUser skipUser) { GameEventHandler 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) { GameEventHandler skipHandler = null; if (skipUser != null) { skipHandler = (from KeyValuePair<XmlUser, GameEventHandler> u in _Users where u.Key.Name.Equals( skipUser.Name, StringComparison.OrdinalIgnoreCase) select u.Value).Single(); } int num = 0; foreach (GameEventHandler handler in temp.GetInvocationList()) { if (!(skipHandler == handler)) { handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); num++; } } Console.WriteLine("broadcasting msg to {0} players of type {1}", num, e.Action.GetType().ToString()); } }
/// <summary> /// Broadcasts the input msg parameter to all the <see cref="Common.Person"> /// Person</see> whos name matches the to input parameter /// by looking up the person from the internal list of chatters /// and invoking their ChatEventHandler delegate asynchronously. /// Where the MyEventHandler() method is called at the start of the /// asynch call, and the EndAsync() method at the end of the asynch call /// </summary> /// <param name="to">The persons name to send the message to</param> /// <param name="msg">The message to broadcast to all chatters</param> public void Whisper(int userID, GameAction action) { GameEventArgs e = new GameEventArgs(); e.Person = _ServerUser.ID; e.Action = action; try { GameEventHandler chatterTo; //carry out a critical section, that attempts to find the //correct Person in the list of chatters. //if a person match is found, the matched chatters //ChatEventHandler delegate is invoked asynchronously lock (threadSync) { chatterTo = GetPersonHandler(userID); if (chatterTo == null) { throw new KeyNotFoundException("The person \" " + GetUser(userID).Name + "\" could not be found"); } } //do a async invoke on the chatter (call the MyEventHandler() method, and the //EndAsync() method at the end of the asynch call chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } catch (KeyNotFoundException) { Console.WriteLine("whispering failed, could not find the player with ID " + e.Person); } }
/// <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() { if (_CurrentPerson == null) return; //get the chatters ChatEventHandler delegate GameEventHandler chatterToRemove = GetPersonHandler(_CurrentPerson.ID); //carry out a critical section, that removes the chatter from the //internal list of chatters lock (threadSync) { _Users.Remove(_CurrentPerson); } //unwire the chatters delegate from the multicast delegate, so that //it no longer gets invoked by globally broadcasted methods ChatEvent -= chatterToRemove; GameEventArgs e = new GameEventArgs() { Person = _CurrentPerson.ID, Action = new UserLeftAction() { Sender = _CurrentPerson.ID, DateTime = DateTime.Now } }; _CurrentPerson = null; //broadcast this leave message to all other remaining connected //chatters BroadcastMessage(e, null); }
/// <summary> /// This method is called when ever one of the chatters /// ChatEventHandler delegates is invoked. When this method /// is called it will examine the events ChatEventArgs to see /// what type of message is being broadcast, and will then /// call the correspoding method on the clients callback interface /// </summary> /// <param name="sender">the sender, which is not used</param> /// <param name="e">The ChatEventArgs</param> private void MyEventHandler(object sender, GameEventArgs e) { try { switch (e.MessageType) { case MessageType.Receive: callback.Receive(e.Person, e.Action); break; case MessageType.ReceiveWhisper: callback.ReceiveWhisper(e.Person, e.Action); break; case MessageType.UserEnter: //callback.UserEnter(e.User); break; case MessageType.UserLeave: callback.UserLeave(e.Person); break; } } catch { Leave(); } }
/// <summary> /// Broadcasts the input msg parameter to all the <see cref="Common.Person"> /// Person</see> whos name matches the to input parameter /// by looking up the person from the internal list of chatters /// and invoking their ChatEventHandler delegate asynchronously. /// Where the MyEventHandler() method is called at the start of the /// asynch call, and the EndAsync() method at the end of the asynch call /// </summary> /// <param name="to">The persons name to send the message to</param> /// <param name="msg">The message to broadcast to all chatters</param> public void Whisper(string to, GameAction action) { GameEventArgs e = new GameEventArgs(); e.MessageType = MessageType.ReceiveWhisper; e.Person = _ServerUser; e.Action = action; try { GameEventHandler chatterTo; //carry out a critical section, that attempts to find the //correct Person in the list of chatters. //if a person match is found, the matched chatters //ChatEventHandler delegate is invoked asynchronously lock (syncObj) { chatterTo = getPersonHandler(to); if (chatterTo == null) { throw new KeyNotFoundException("The person whos name is " + to + " could not be found"); } } //do a async invoke on the chatter (call the MyEventHandler() method, and the //EndAsync() method at the end of the asynch call chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } catch (KeyNotFoundException) { Console.WriteLine("whispering failed, could not find the player with name" + e.Person); } }
/// <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() { if (this._CurrentPerson == null) return; //get the chatters ChatEventHandler delegate GameEventHandler chatterToRemove = getPersonHandler(this._CurrentPerson.Name); //carry out a critical section, that removes the chatter from the //internal list of chatters lock (syncObj) { _Users.Remove(this._CurrentPerson); } //unwire the chatters delegate from the multicast delegate, so that //it no longer gets invokes by globally broadcasted methods ChatEvent -= chatterToRemove; GameEventArgs e = new GameEventArgs(); e.MessageType = MessageType.UserLeave; e.Person = this._CurrentPerson; this._CurrentPerson = null; //broadcast this leave message to all other remaining connected //chatters BroadcastMessage(e, null); }