/// <summary> /// Called by .NET Remoting when an async call is finished. /// </summary> /// <param name="ar"></param> public static void OurAsyncCallbackHandler(IAsyncResult ar) { AsyncCallBackData asyncCallBackData = (AsyncCallBackData)ar.AsyncState; try { object result = asyncCallBackData.RemoteAsyncDelegate.EndInvoke(ar); } catch (Exception ex) { Console.WriteLine("Client call failed: {0}.", ex.Message); // remove the failed client from our list lock (_clients) { _clients.Remove(RemotingServices.GetObjectUri(asyncCallBackData.MbrBeingCalled)); } } }
/// <summary> /// Sends the message to all clients. /// </summary> /// <param name="message">Message to be sent.</param> /// <returns>Number of clients having received this message.</returns> public void SendMessage(string message) { lock (_clients) { Console.WriteLine("\"{0}\" message will be sent to all clients.", message); AsyncCallback asyncCallback = new AsyncCallback(OurAsyncCallbackHandler); foreach (DictionaryEntry entry in _clients) { IChatClient iChatClient = (IChatClient)entry.Value; ReceiveMessageEventHandler remoteAsyncDelegate = new ReceiveMessageEventHandler(iChatClient.ReceiveMessage); AsyncCallBackData asyncCallBackData = new AsyncCallBackData(); asyncCallBackData.RemoteAsyncDelegate = remoteAsyncDelegate; asyncCallBackData.MbrBeingCalled = (MarshalByRefObject)iChatClient; IAsyncResult RemAr = remoteAsyncDelegate.BeginInvoke(message, asyncCallback, asyncCallBackData); } } }