home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Chat / Chat.cs next >
Encoding:
Text File  |  2000-06-23  |  7.0 KB  |  210 lines

  1. /*=====================================================================
  2.   File:      Chat.cs
  3.  
  4.   Summary:   Demonstrates how to use delegates and events.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft COM+ 2.0 SDK Code Samples.
  8.  
  9.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20.  
  21. // Add the classes in the following namespaces to our namespace
  22. using System;
  23.  
  24.  
  25. ///////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. class DChatServer {
  29.     // Declare a multicast (because return type is void) delegate type
  30.     public delegate void OnMsgArrived(String message);
  31.  
  32.     // Declare a reference to an OnGetString delegate
  33.     private static OnMsgArrived onMsgArrived;
  34.  
  35.     // Private to prevent instances of this type from being instantiated.
  36.     private DChatServer() {}
  37.  
  38.     // This function is necessary because we are not using an event
  39.     public static void ClientConnect(OnMsgArrived onMsgArrived) {
  40.         DChatServer.onMsgArrived = (OnMsgArrived)
  41.             Delegate.Combine(DChatServer.onMsgArrived, onMsgArrived);
  42.     }
  43.  
  44.     // This function is necessary because we are not using an event
  45.     public static void ClientDisconnect(OnMsgArrived onMsgArrived) {
  46.         DChatServer.onMsgArrived = (OnMsgArrived)
  47.             Delegate.Remove(DChatServer.onMsgArrived, onMsgArrived);
  48.     }
  49.  
  50.     public static void SendMsg(String msg) {
  51.         // Send message to ALL clients
  52.         SendMsg(msg, null);
  53.     }
  54.  
  55.     public static void SendMsg(String msg, Object excludeClient) {
  56.         // Send message to all clients except 'excludeClient'
  57.         if (excludeClient == null) {
  58.             onMsgArrived(msg);
  59.         } else {
  60.             Delegate[] DelegateList = onMsgArrived.GetInvocationList();
  61.             for (int i = 0; i < DelegateList.Length; i++) {
  62.                 if (DelegateList[i].Target != excludeClient) {
  63.                     ((OnMsgArrived) DelegateList[i])(msg);
  64.                 }
  65.             }            
  66.         }        
  67.     }    
  68. }
  69.  
  70.  
  71. ///////////////////////////////////////////////////////////////////////////////
  72.  
  73.  
  74. class DChatClient {
  75.     private void onMsgArrived(String msg) {
  76.         Console.WriteLine("Msg arrived (Client {0}): {1}", clientName, msg);
  77.     }
  78.  
  79.     private String clientName;
  80.  
  81.     public DChatClient(String clientName) {
  82.         this.clientName = clientName;
  83.         DChatServer.ClientConnect(new DChatServer.OnMsgArrived(onMsgArrived));
  84.     }
  85.  
  86.     public void Dispose() {
  87.         Finalize();
  88.         GC.SuppressFinalize(this);
  89.     }
  90.  
  91.     protected override void Finalize() {
  92.         DChatServer.ClientDisconnect(new DChatServer.OnMsgArrived(onMsgArrived));
  93.     }
  94. }
  95.  
  96.  
  97. ///////////////////////////////////////////////////////////////////////////////
  98.  
  99.  
  100. class EChatServer {
  101.     // Declare a multicast (because return type is void) delegate type
  102.     public delegate void OnMsgArrived(String message);
  103.  
  104.     // Declaring an event causes the compiler to generate a PRIVATE field 
  105.     // (onMsgArrived) that references the tail of an OnMsgArrived delegate 
  106.     // linked-list. The compiler also generates two PUBLIC methods, 
  107.     // add_onMsgArrived and remove_onMsgArrived which are called when the 
  108.     // += and -= operators are applied to the event's delegate.
  109.     public static event OnMsgArrived onMsgArrived;
  110.  
  111.     // Private to prevent instances of this type from being instantiated.
  112.     private EChatServer() {}
  113.  
  114.     public static void SendMsg(String msg) {
  115.         // Send message to ALL clients
  116.         SendMsg(msg, null);
  117.     }
  118.  
  119.     public static void SendMsg(String msg, Object excludeClient) {
  120.         // Send message to all clients except 'excludeClient'
  121.         if (excludeClient == null) {
  122.             onMsgArrived(msg);
  123.         } else {
  124.             Delegate[] DelegateList = onMsgArrived.GetInvocationList();
  125.             for (int i = 0; i < DelegateList.Length; i++) {
  126.                 if (DelegateList[i].Target != excludeClient) {
  127.                     ((OnMsgArrived) DelegateList[i])(msg);
  128.                 }
  129.             }            
  130.         }        
  131.     }    
  132. }
  133.  
  134.  
  135. ///////////////////////////////////////////////////////////////////////////////
  136.  
  137.  
  138. class EChatClient {
  139.     private void onMsgArrived(String msg) {
  140.         Console.WriteLine("Msg arrived (Client {0}): {1}", clientName, msg);
  141.     }
  142.  
  143.     private String clientName;
  144.  
  145.     public EChatClient(String clientName) {
  146.         this.clientName = clientName;
  147.         EChatServer.onMsgArrived += new EChatServer.OnMsgArrived(onMsgArrived);
  148.     }
  149.  
  150.     public void Dispose() {
  151.         Finalize();
  152.         GC.SuppressFinalize(this);
  153.     }
  154.  
  155.     protected override void Finalize() {
  156.         EChatServer.onMsgArrived -= new EChatServer.OnMsgArrived(onMsgArrived);
  157.     }
  158. }
  159.  
  160.  
  161. ///////////////////////////////////////////////////////////////////////////////
  162.  
  163.  
  164. class Application {
  165.     private static void DelegateChatServerDemo() {
  166.         Console.WriteLine("Demo start: Delegate Chat Server.");
  167.  
  168.         DChatClient cc1 = new DChatClient("1");
  169.         DChatClient cc2 = new DChatClient("2");
  170.         DChatClient cc3 = new DChatClient("3");
  171.  
  172.         DChatServer.SendMsg("Hi to all clients");
  173.         DChatServer.SendMsg("Hi to all clients except client 2", cc2);
  174.  
  175.         // Explicitly disconnect the clients from the chat server.
  176.         // If we didn't do this, the clients' memory could not be 
  177.         // reclaimed until the server is collected (app shutdown time).
  178.         cc1.Dispose();
  179.         cc2.Dispose();
  180.         cc3.Dispose();
  181.         Console.WriteLine("Demo stop: Delegate Chat Server.");
  182.     }
  183.  
  184.     private static void EventChatServerDemo() {
  185.         Console.WriteLine("\n\nDemo start: Event Chat Server.");
  186.         EChatClient cc1 = new EChatClient("1");
  187.         EChatClient cc2 = new EChatClient("2");
  188.         EChatClient cc3 = new EChatClient("3");
  189.  
  190.         EChatServer.SendMsg("Hi to all clients");
  191.         EChatServer.SendMsg("Hi to all clients except client 2", cc2);
  192.  
  193.         // Explicitly disconnect the clients from the chat server.
  194.         // If we didn't do this, the clients' memory could not be 
  195.         // reclaimed until the server is collected (app shutdown time).
  196.         cc1.Dispose();
  197.         cc2.Dispose();
  198.         cc3.Dispose();
  199.         Console.WriteLine("Demo stop: Event Chat Server.");
  200.     }
  201.  
  202.     public static void Main() {
  203.         DelegateChatServerDemo();
  204.         EventChatServerDemo();
  205.     }
  206. }
  207.  
  208.  
  209. ///////////////////////////////// End of File /////////////////////////////////
  210.