Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Klasa reprezentująca sesję klienta
- /// </summary>
- public class ClientConnection : EventArgs
- {
- /// <summary>
- /// Tworzy nowy obiekt
- /// </summary>
- /// <param name="Context">Kontekst operacyjny (Tunel połączeniowy)</param>
- public ClientConnection(OperationContext Context)
- {
- this.Context = Context;
- Context.GetCallbackChannel<IDuplexContextChannel>().Closed += ClientConnection_Closed;
- Context.GetCallbackChannel<IDuplexContextChannel>().Faulted += ClientConnection_Faulted;
- }
- private void ClientConnection_Faulted(object sender, EventArgs e)
- {
- Context.GetCallbackChannel<IDuplexContextChannel>().Closed -= ClientConnection_Closed;
- ClientConnections.ServiceEvents.Fault(this);
- }
- private void ClientConnection_Closed(object sender, EventArgs e)
- {
- Context.GetCallbackChannel<IDuplexContextChannel>().Faulted -= ClientConnection_Faulted;
- ClientConnections.ServiceEvents.Close(this);
- }
- /// <summary>
- /// Kontekst operacyjny połączenia
- /// </summary>
- public OperationContext Context { get; private set; }
- /// <summary>
- /// Porównuje dwa połączenia na podstawie identyfikatora sesji
- /// </summary>
- /// <param name="Connection1">Połączenie 1</param>
- /// <param name="Connection2">Połączenie 2</param>
- /// <returns>Zwraca true, jeżeli sesje zgadzają się</returns>
- public static bool Compare(ClientConnection Connection1, ClientConnection Connection2)
- {
- if (Connection1 == null || Connection2 == null)
- return false;
- return (Connection1.Context.SessionId == Connection2.Context.SessionId) ? true : false;
- }
- /// <summary>
- /// Porównuje dwie sesje
- /// </summary>
- /// <param name="Session1">Sesja 1</param>
- /// <param name="Session2">Sesja 2</param>
- /// <returns>Zwraca true, jeżeli sesje zgadzają się</returns>
- public static bool Compare(string Session1, string Session2)
- {
- return (Session1 == Session2) ? true : false;
- }
- }
- public class ClientConnections
- {
- /// <summary>
- /// Lista aktywnych sesji
- /// </summary>
- public static List<ClientConnection> Connections = new List<ClientConnection>();
- /// <summary>
- /// Wyszukuje połączenie w kolekcji <see cref="Connections"/>
- /// </summary>
- /// <param name="Connection">Szukane połączenie</param>
- /// <returns>Zwraca połączenie lub null, jeżeli nie znaleziono</returns>
- public static ClientConnection Find(ClientConnection Connection)
- {
- return Connections.FirstOrDefault(x => ClientConnection.Compare(x, Connection));
- }
- /// <summary>
- /// Wyszukuje połączenie w kolekcji <see cref="Connections"/>
- /// </summary>
- /// <param name="SessionID">Identyfikator sesji</param>
- /// <returns>Zwraca połączenie lub null, jeżeli nie znaleziono</returns>
- public static ClientConnection Find(string SessionID)
- {
- return Connections.FirstOrDefault(x => ClientConnection.Compare(x.Context.SessionId, SessionID));
- }
- /// <summary>
- /// Dodaje połączenie do kolekcji <see cref="Connections"/>. Po pomyślnym dodaniu uwalnia zdarzenie <see cref="SessionCreated"/>
- /// </summary>
- /// <param name="Connection">Dodawane połączenie</param>
- /// <returns>Zwraca dodane połączenie lub null, jeżeli nie dodano</returns>
- public static ClientConnection Add(ClientConnection Connection)
- {
- if(Find(Connection) == null)
- {
- Connections.Add(Connection);
- SessionCreated?.Invoke(null, Connection);
- return Connection;
- }
- return null;
- }
- /// <summary>
- /// Usuwa połączenie z kolekcji <see cref="Connections"/>. Po pomyślnym usunięciu uwalnia zdarzenie <see cref="SessionDeleted"/>
- /// </summary>
- /// <param name="Connection">Usuwane połączenie</param>
- /// <returns>Zwraca usunięte połączenie lub null, jeżeli nie znaleziono połączenia</returns>
- public static ClientConnection Erase(ClientConnection Connection)
- {
- ClientConnection Finded = Find(Connection);
- if (Finded != null)
- {
- Connections.Remove(Finded);
- SessionDeleted?.Invoke(null, Finded);
- return Connection;
- }
- return null;
- }
- /// <summary>
- /// Zdarzenia obsługiwane przez serwis
- /// </summary>
- public static class ServiceEvents
- {
- public static event EventHandler<ClientConnection> Faulted;
- public static event EventHandler<ClientConnection> Closed;
- public static event EventHandler<ClientConnection> Opened;
- public static void Fault(ClientConnection connection)
- {
- Faulted?.Invoke(null, connection);
- }
- public static void Close(ClientConnection connection)
- {
- Closed?.Invoke(null, connection);
- }
- public static void Open(ClientConnection connection)
- {
- Opened?.Invoke(null, connection);
- }
- }
- /// <summary>
- /// Zdarzenie uwalniane po dodaniu połączenia do kolekcji <see cref="Connections"/>
- /// </summary>
- public static event EventHandler<ClientConnection> SessionCreated;
- /// <summary>
- /// Zdarzenie uwalniane po usunięciu połączenia z kolekcji <see cref="Connections"/>
- /// </summary>
- public static event EventHandler<ClientConnection> SessionDeleted;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement