Exemplo n.º 1
0
 void client_OnDisconnect(StompServerClient client)
 {
     lock (this)
     {
         RemoveClient(client);
     }
 }
Exemplo n.º 2
0
        private void OnStompCommand_Subscribe(StompServerClient client, StompMessage message)
        {
            StompPath path        = null;
            String    destination = message["destination"];

            if (StompLogger.CanLogDebug)
            {
                StompLogger.LogDebug(client.ToString() + " subscribes to " + destination);
            }

            lock (this)
            {
                if (_paths.ContainsKey(destination))
                {
                    path = _paths[destination];
                }
                else
                {
                    path = new StompPath(destination);
                    path.OnLastClientRemoved += new StompPath.OnLastClientRemovedDelegate(path_OnLastClientRemoved);
                    _paths[destination]       = path;
                }
            }

            path.AddClient(client);
        }
Exemplo n.º 3
0
        void client_OnMessageReceived(StompServerClient client, StompMessage message)
        {
            if (_actionMap.ContainsKey(message.Command))
            {
                try
                {
                    if (StompLogger.CanLogDebug)
                    {
                        StompLogger.LogDebug(client.ToString() + " sent a command " + message.Command);
                    }

                    _actionMap[message.Command].DynamicInvoke(client, message);
                }
                catch (Exception ex)
                {
                    if (StompLogger.CanLogException)
                    {
                        StompLogger.LogException(client.ToString() + " sent a command " + message.Command + " which caused an exception", ex);
                    }
                }
            }
            else
            {
                if (StompLogger.CanLogWarning)
                {
                    StompLogger.LogWarning(client.ToString() + " sent an unhandled command " + message.Command);
                }
            }
        }
Exemplo n.º 4
0
        public void OnAcceptTcpClient(IAsyncResult ar)
        {
            try
            {
                Socket socket = _listener.EndAcceptSocket(ar);
                StompServerClient client = new StompServerClient(socket);

                if (OnConnect != null)
                {
                    OnConnect(client);
                }
            }
            catch (Exception ex)
            {
                if (StompLogger.CanLogException)
                    StompLogger.LogException("Listener failed to initialize the client", ex);
            }

            try
            {
                _listener.BeginAcceptTcpClient(new AsyncCallback(OnAcceptTcpClient), null);
            }
            catch (Exception ex)
            {
                if (StompLogger.CanLogException)
                    StompLogger.LogException("Failed to begin accept", ex);
                _isListening = false;
            }
        }
Exemplo n.º 5
0
        void _listener_OnConnect(StompServerClient client)
        {
            lock (this)
            {
                if (_clients.Count + 1 > StompConfiguration.MaxClients)
                {
                    if (StompLogger.CanLogWarning)
                    {
                        StompLogger.LogWarning("Maximum clients (" + StompConfiguration.MaxClients + ") reached. Disconnecting " + client.ToString());
                    }
                    client.Stop();
                    return;
                }

                _clients.Add(client);
            }

            InitClientsEvents(client);

            StompStatistics.AddConnectedClient();

            client.Start();

            if (StompLogger.CanLogDebug)
            {
                StompLogger.LogDebug(client.ToString() + " connected");
            }
        }
Exemplo n.º 6
0
        public void OnAcceptTcpClient(IAsyncResult ar)
        {
            try
            {
                Socket            socket = _listener.EndAcceptSocket(ar);
                StompServerClient client = new StompServerClient(socket);

                if (OnConnect != null)
                {
                    OnConnect(client);
                }
            }
            catch (Exception ex)
            {
                if (StompLogger.CanLogException)
                {
                    StompLogger.LogException("Listener failed to initialize the client", ex);
                }
            }

            try
            {
                _listener.BeginAcceptTcpClient(new AsyncCallback(OnAcceptTcpClient), null);
            }
            catch (Exception ex)
            {
                if (StompLogger.CanLogException)
                {
                    StompLogger.LogException("Failed to begin accept", ex);
                }
                _isListening = false;
            }
        }
Exemplo n.º 7
0
 public void AddClient(StompServerClient client)
 {
     lock (this)
     {
         client.OnDisconnect += new StompServerClient.OnDisconnectDelegate(client_OnDisconnect);
         _clients.Add(client);
     }
 }
Exemplo n.º 8
0
        void client_OnDisconnect(StompServerClient client)
        {
            lock (this)
            {
                if (StompLogger.CanLogDebug)
                    StompLogger.LogDebug(client.ToString() + " quitted");
                _clients.Remove(client);
            }

            StompStatistics.RemoveConnectedClient();
        }
Exemplo n.º 9
0
        void client_OnDisconnect(StompServerClient client)
        {
            lock (this)
            {
                if (StompLogger.CanLogDebug)
                {
                    StompLogger.LogDebug(client.ToString() + " quitted");
                }
                _clients.Remove(client);
            }

            StompStatistics.RemoveConnectedClient();
        }
Exemplo n.º 10
0
        private void OnStompCommand_Connect(StompServerClient client, StompMessage message)
        {
            if (StompLogger.CanLogDebug)
            {
                StompLogger.LogDebug(client.ToString() + " connected with session-id " + client.SessionId.ToString());
            }

            StompMessage result = new StompMessage("CONNECTED");

            result["session-id"] = client.SessionId.ToString();

            client.Send(result);
        }
Exemplo n.º 11
0
        public void RemoveClient(StompServerClient client)
        {
            lock (this)
            {
                client.OnDisconnect -= new StompServerClient.OnDisconnectDelegate(client_OnDisconnect);
                _clients.Remove(client);

                if (_clients.Count == 0 && OnLastClientRemoved != null)
                {
                    try
                    {
                        OnLastClientRemoved(this);
                    }
                    catch (Exception) { }
                }
            }
        }
Exemplo n.º 12
0
        private void OnStompCommand_Send(StompServerClient client, StompMessage message)
        {
            StompPath path        = null;
            String    destination = message["destination"];

            if (StompLogger.CanLogDebug)
            {
                StompLogger.LogDebug(client.ToString() + " sent data to " + destination);
            }

            lock (this)
            {
                if (_paths.ContainsKey(destination))
                {
                    path = _paths[destination];
                }
            }

            if (path != null)
            {
                List <StompServerClient> pathClients = path.Clients;

                message["message-id"] = Guid.NewGuid().ToString();
                message.Command       = "MESSAGE";

                foreach (StompServerClient pathClient in pathClients)
                {
                    try
                    {
                        if (StompLogger.CanLogDebug)
                        {
                            StompLogger.LogDebug("Sending " + message.Command + " to " + pathClient.ToString());
                        }
                        pathClient.Send(message);
                    }
                    catch (Exception ex)
                    {
                        if (StompLogger.CanLogException)
                        {
                            StompLogger.LogException(pathClient.ToString() + " has thrown an exception while sending data", ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        private void OnStompCommand_Unsubscribe(StompServerClient client, StompMessage message)
        {
            StompPath path        = null;
            String    destination = message["destination"];

            if (StompLogger.CanLogDebug)
            {
                StompLogger.LogDebug(client.ToString() + " unsubscribes from " + destination);
            }

            lock (this)
            {
                if (_paths.ContainsKey(destination))
                {
                    path = _paths[destination];
                }
            }

            if (path != null)
            {
                path.RemoveClient(client);
            }
        }
Exemplo n.º 14
0
        private void OnStompCommand_Unsubscribe(StompServerClient client, StompMessage message)
        {
            StompPath path = null;
            String destination = message["destination"];

            if (StompLogger.CanLogDebug)
                StompLogger.LogDebug(client.ToString() + " unsubscribes from " + destination);

            lock (this)
            {
                if (_paths.ContainsKey(destination))
                    path = _paths[destination];
            }

            if (path != null)
                path.RemoveClient(client);
        }
Exemplo n.º 15
0
 public void AddClient(StompServerClient client)
 {
     lock (this)
     {
         client.OnDisconnect += new StompServerClient.OnDisconnectDelegate(client_OnDisconnect);
         _clients.Add(client);
     }
 }
Exemplo n.º 16
0
 void client_OnDisconnect(StompServerClient client)
 {
     lock (this)
     {
         RemoveClient(client);
     }
 }
Exemplo n.º 17
0
        public void RemoveClient(StompServerClient client)
        {
            lock (this)
            {
                client.OnDisconnect -= new StompServerClient.OnDisconnectDelegate(client_OnDisconnect);
                _clients.Remove(client);

                if (_clients.Count == 0 && OnLastClientRemoved != null)
                {
                    try
                    {
                        OnLastClientRemoved(this);
                    }
                    catch (Exception) { }
                }
            }
        }
Exemplo n.º 18
0
        void client_OnMessageReceived(StompServerClient client, StompMessage message)
        {
            if (_actionMap.ContainsKey(message.Command))
            {
                try
                {
                    if (StompLogger.CanLogDebug)
                        StompLogger.LogDebug(client.ToString() + " sent a command " + message.Command);

                    _actionMap[message.Command].DynamicInvoke(client, message);
                }
                catch (Exception ex)
                {
                    if (StompLogger.CanLogException)
                        StompLogger.LogException(client.ToString() + " sent a command " + message.Command + " which caused an exception", ex);
                }
            }
            else
            {
                if (StompLogger.CanLogWarning)
                    StompLogger.LogWarning(client.ToString() + " sent an unhandled command " + message.Command);
            }
        }
Exemplo n.º 19
0
        private void OnStompCommand_Subscribe(StompServerClient client, StompMessage message)
        {
            StompPath path = null;
            String destination = message["destination"];

            if (StompLogger.CanLogDebug)
                StompLogger.LogDebug(client.ToString() + " subscribes to " + destination);

            lock (this)
            {
                if (_paths.ContainsKey(destination))
                    path = _paths[destination];
                else
                {
                    path = new StompPath(destination);
                    path.OnLastClientRemoved += new StompPath.OnLastClientRemovedDelegate(path_OnLastClientRemoved);
                    _paths[destination] = path;
                }
            }

            path.AddClient(client);
        }
Exemplo n.º 20
0
 void InitClientsEvents(StompServerClient client)
 {
     client.OnDisconnect += new StompServerClient.OnDisconnectDelegate(client_OnDisconnect);
     client.OnMessageReceived += new StompServerClient.OnMessageReceivedDelegate(client_OnMessageReceived);
 }
Exemplo n.º 21
0
 void InitClientsEvents(StompServerClient client)
 {
     client.OnDisconnect      += new StompServerClient.OnDisconnectDelegate(client_OnDisconnect);
     client.OnMessageReceived += new StompServerClient.OnMessageReceivedDelegate(client_OnMessageReceived);
 }
Exemplo n.º 22
0
        void _listener_OnConnect(StompServerClient client)
        {
            lock (this)
            {
                if (_clients.Count + 1 > StompConfiguration.MaxClients)
                {
                    if (StompLogger.CanLogWarning)
                        StompLogger.LogWarning("Maximum clients ("+StompConfiguration.MaxClients+") reached. Disconnecting " + client.ToString());
                    client.Stop();
                    return;
                }

                _clients.Add(client);
            }

            InitClientsEvents(client);

            StompStatistics.AddConnectedClient();

            client.Start();

            if (StompLogger.CanLogDebug)
                StompLogger.LogDebug(client.ToString() + " connected");
        }
Exemplo n.º 23
0
        private void OnStompCommand_Connect(StompServerClient client, StompMessage message)
        {
            if (StompLogger.CanLogDebug)
                StompLogger.LogDebug(client.ToString() + " connected with session-id " + client.SessionId.ToString());

            StompMessage result = new StompMessage("CONNECTED");
            result["session-id"] = client.SessionId.ToString();

            client.Send(result);
        }
Exemplo n.º 24
0
        private void OnStompCommand_Send(StompServerClient client, StompMessage message)
        {
            StompPath path = null;
            String destination = message["destination"];

            if (StompLogger.CanLogDebug)
                StompLogger.LogDebug(client.ToString() + " sent data to " + destination);

            lock (this)
            {
                if (_paths.ContainsKey(destination))
                    path = _paths[destination];
            }

            if (path != null)
            {
                List<StompServerClient> pathClients = path.Clients;

                message["message-id"] = Guid.NewGuid().ToString();
                message.Command = "MESSAGE";

                foreach (StompServerClient pathClient in pathClients)
                {
                    try
                    {
                        if (StompLogger.CanLogDebug)
                            StompLogger.LogDebug("Sending " + message.Command + " to " + pathClient.ToString());
                        pathClient.Send(message);
                    }
                    catch (Exception ex)
                    {
                        if (StompLogger.CanLogException)
                            StompLogger.LogException(pathClient.ToString() + " has thrown an exception while sending data", ex);
                    }
                }
            }
        }