示例#1
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);
                }
            }
        }
示例#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);
        }
示例#3
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);
        }
示例#4
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);
                        }
                    }
                }
            }
        }
示例#5
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);
            }
        }
示例#6
0
        private void OnClientReceive(IAsyncResult ar)
        {
            try
            {
                int readBytes = _socket.EndReceive(ar);

                if (readBytes > 0)
                {
                    _buffer.Cursor += readBytes;

                    int localCursor = 0;

                    for (int i = 0; i < _buffer.Cursor; i++)
                    {
                        if (_buffer.Buffer[i] == '\0') // match
                        {
                            StompMessage message = null;

                            try
                            {
                                message = new StompMessage(_buffer.Buffer, localCursor, i - localCursor);
                                StompStatistics.CountIncomingMessage();

                                if (OnMessageReceived != null)
                                {
                                    try
                                    {
                                        OnMessageReceived(this, message);
                                    }
                                    catch (Exception) { }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (StompLogger.CanLogException)
                                {
                                    StompLogger.LogException("Failed to parse stomp packet", ex);
                                }
                            }


                            localCursor = i + 1;
                        }
                    }

                    if (localCursor > 0)
                    {
                        _buffer.Remove(localCursor);
                    }


                    BeginReceive();
                }
                else
                {
                    throw new Exception("Connection closed.");
                }
            }
            catch (Exception)
            {
                OnInternalDisconnect();
            }
        }
示例#7
0
        public void Send(StompMessage message)
        {
            byte[] buffer = message.ToBuffer();

            Send(buffer, 0, buffer.Length);
        }
        private void OnClientReceive(IAsyncResult ar)
        {
            try
            {
                int readBytes = _socket.EndReceive(ar);

                if (readBytes > 0)
                {
                    _buffer.Cursor += readBytes;

                    int localCursor = 0;

                    for (int i = 0; i < _buffer.Cursor; i++)
                    {
                        if (_buffer.Buffer[i] == '\0') // match
                        {
                            StompMessage message = null;

                            try
                            {
                                message = new StompMessage(_buffer.Buffer, localCursor, i - localCursor);
                                StompStatistics.CountIncomingMessage();

                                if (OnMessageReceived != null)
                                {
                                    try
                                    {
                                        OnMessageReceived(this, message);
                                    }
                                    catch (Exception) { }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (StompLogger.CanLogException)
                                    StompLogger.LogException("Failed to parse stomp packet", ex);
                            }

                            localCursor = i+1;
                        }
                    }

                    if (localCursor > 0)
                    {
                        _buffer.Remove(localCursor);
                    }

                    BeginReceive();
                }
                else
                {
                    throw new Exception("Connection closed.");
                }
            }
            catch (Exception)
            {
                OnInternalDisconnect();
            }
        }
        public void Send(StompMessage message)
        {
            byte[] buffer = message.ToBuffer();

            Send(buffer, 0, buffer.Length);
        }
示例#10
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);
            }
        }
示例#11
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);
        }
示例#12
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);
        }
示例#13
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);
                    }
                }
            }
        }
示例#14
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);
        }