Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 KB | None | 0 0
  1. /// <summary>
  2.         /// Klasa reprezentująca sesję klienta
  3.         /// </summary>
  4.         public class ClientConnection : EventArgs
  5.         {
  6.             /// <summary>
  7.             /// Tworzy nowy obiekt
  8.             /// </summary>
  9.             /// <param name="Context">Kontekst operacyjny (Tunel połączeniowy)</param>
  10.             public ClientConnection(OperationContext Context)
  11.             {
  12.                 this.Context = Context;
  13.                 Context.GetCallbackChannel<IDuplexContextChannel>().Closed += ClientConnection_Closed;
  14.                 Context.GetCallbackChannel<IDuplexContextChannel>().Faulted += ClientConnection_Faulted;
  15.             }
  16.  
  17.             private void ClientConnection_Faulted(object sender, EventArgs e)
  18.             {
  19.                 Context.GetCallbackChannel<IDuplexContextChannel>().Closed -= ClientConnection_Closed;
  20.                 ClientConnections.ServiceEvents.Fault(this);
  21.             }
  22.             private void ClientConnection_Closed(object sender, EventArgs e)
  23.             {
  24.                 Context.GetCallbackChannel<IDuplexContextChannel>().Faulted -= ClientConnection_Faulted;
  25.                 ClientConnections.ServiceEvents.Close(this);
  26.             }
  27.  
  28.             /// <summary>
  29.             /// Kontekst operacyjny połączenia
  30.             /// </summary>
  31.             public OperationContext Context { get; private set; }
  32.  
  33.             /// <summary>
  34.             /// Porównuje dwa połączenia na podstawie identyfikatora sesji
  35.             /// </summary>
  36.             /// <param name="Connection1">Połączenie 1</param>
  37.             /// <param name="Connection2">Połączenie 2</param>
  38.             /// <returns>Zwraca true, jeżeli sesje zgadzają się</returns>
  39.             public static bool Compare(ClientConnection Connection1, ClientConnection Connection2)
  40.             {
  41.                 if (Connection1 == null || Connection2 == null)
  42.                     return false;
  43.                 return (Connection1.Context.SessionId == Connection2.Context.SessionId) ? true : false;
  44.             }
  45.             /// <summary>
  46.             /// Porównuje dwie sesje
  47.             /// </summary>
  48.             /// <param name="Session1">Sesja 1</param>
  49.             /// <param name="Session2">Sesja 2</param>
  50.             /// <returns>Zwraca true, jeżeli sesje zgadzają się</returns>
  51.             public static bool Compare(string Session1, string Session2)
  52.             {
  53.                 return (Session1 == Session2) ? true : false;
  54.             }
  55.         }
  56.         public class ClientConnections
  57.         {
  58.             /// <summary>
  59.             /// Lista aktywnych sesji
  60.             /// </summary>
  61.             public static List<ClientConnection> Connections = new List<ClientConnection>();
  62.             /// <summary>
  63.             /// Wyszukuje połączenie w kolekcji <see cref="Connections"/>
  64.             /// </summary>
  65.             /// <param name="Connection">Szukane połączenie</param>
  66.             /// <returns>Zwraca połączenie lub null, jeżeli nie znaleziono</returns>
  67.             public static ClientConnection Find(ClientConnection Connection)
  68.             {
  69.                 return Connections.FirstOrDefault(x => ClientConnection.Compare(x, Connection));
  70.             }
  71.             /// <summary>
  72.             /// Wyszukuje połączenie w kolekcji <see cref="Connections"/>
  73.             /// </summary>
  74.             /// <param name="SessionID">Identyfikator sesji</param>
  75.             /// <returns>Zwraca połączenie lub null, jeżeli nie znaleziono</returns>
  76.             public static ClientConnection Find(string SessionID)
  77.             {
  78.                 return Connections.FirstOrDefault(x => ClientConnection.Compare(x.Context.SessionId, SessionID));
  79.             }
  80.             /// <summary>
  81.             /// Dodaje połączenie do kolekcji <see cref="Connections"/>. Po pomyślnym dodaniu uwalnia zdarzenie <see cref="SessionCreated"/>
  82.             /// </summary>
  83.             /// <param name="Connection">Dodawane połączenie</param>
  84.             /// <returns>Zwraca dodane połączenie lub null, jeżeli nie dodano</returns>
  85.             public static ClientConnection Add(ClientConnection Connection)
  86.             {
  87.                 if(Find(Connection) == null)
  88.                 {
  89.                     Connections.Add(Connection);
  90.                     SessionCreated?.Invoke(null, Connection);
  91.                     return Connection;
  92.                 }
  93.                 return null;
  94.             }
  95.             /// <summary>
  96.             /// Usuwa połączenie z kolekcji <see cref="Connections"/>. Po pomyślnym usunięciu uwalnia zdarzenie <see cref="SessionDeleted"/>
  97.             /// </summary>
  98.             /// <param name="Connection">Usuwane połączenie</param>
  99.             /// <returns>Zwraca usunięte połączenie lub null, jeżeli nie znaleziono połączenia</returns>
  100.             public static ClientConnection Erase(ClientConnection Connection)
  101.             {
  102.                 ClientConnection Finded = Find(Connection);
  103.                 if (Finded != null)
  104.                 {
  105.                     Connections.Remove(Finded);
  106.                     SessionDeleted?.Invoke(null, Finded);
  107.                     return Connection;
  108.                 }
  109.                 return null;
  110.             }
  111.            
  112.             /// <summary>
  113.             /// Zdarzenia obsługiwane przez serwis
  114.             /// </summary>
  115.             public static class ServiceEvents
  116.             {
  117.                 public static event EventHandler<ClientConnection> Faulted;
  118.                 public static event EventHandler<ClientConnection> Closed;
  119.                 public static event EventHandler<ClientConnection> Opened;
  120.                 public static void Fault(ClientConnection connection)
  121.                 {
  122.                     Faulted?.Invoke(null, connection);
  123.                 }
  124.                 public static void Close(ClientConnection connection)
  125.                 {
  126.                     Closed?.Invoke(null, connection);
  127.                 }
  128.                 public static void Open(ClientConnection connection)
  129.                 {
  130.                     Opened?.Invoke(null, connection);
  131.                 }
  132.             }
  133.  
  134.             /// <summary>
  135.             /// Zdarzenie uwalniane po dodaniu połączenia do kolekcji <see cref="Connections"/>
  136.             /// </summary>
  137.             public static event EventHandler<ClientConnection> SessionCreated;
  138.             /// <summary>
  139.             /// Zdarzenie uwalniane po usunięciu połączenia z kolekcji <see cref="Connections"/>
  140.             /// </summary>
  141.             public static event EventHandler<ClientConnection> SessionDeleted;
  142.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement