Пример #1
0
        private void OnClientMessage(ITcpChannel channel, object message)
        {
            // Let us get the actual channel to send the message to
            EslDecodedMessage decoded = message as EslDecodedMessage;

            _log.Debug("Received a new message from Client [#{0}--Address={1}] ", channel.ChannelId, channel.RemoteEndpoint);
            _log.Debug("Message :[{0}]", decoded.OriginalMessage);
            try {
                FreeSwitch freeSwitch;
                var        fetched = _clients.TryGetValue(channel.ChannelId, out freeSwitch);
                if (fetched)
                {
                    freeSwitch.MessageReceived(channel, message);
                }
            }
            catch (Exception) {
                // ignored
            }
        }
Пример #2
0
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer"></param>
        public void ProcessReadBytes(ISocketBuffer buffer)
        {
            // buffer offset received
            var offsetInSocketBuffer = buffer.Offset;

            // Number of Bytes received
            _bytesReceived = buffer.BytesTransferred;
            while (true)
            {
                if (_bytesReceived <= 0)
                {
                    break;
                }
                Buffer.BlockCopy(buffer.Buffer, offsetInSocketBuffer, _buffer, _offsetInOurBuffer, buffer.BytesTransferred);

                string textReceived = _encoding.GetString(_buffer);
                textReceived = textReceived.TrimStart(LINE_FEED_CHAR).Replace("\0", string.Empty);
                if (!textReceived.Contains(MESSAGE_END_STRING))
                {
                    _offsetInOurBuffer += _bytesReceived;
                    return;
                }

                int    contentLength = 0;
                string headerLine    = textReceived.Substring(0, textReceived.IndexOf(MESSAGE_END_STRING, StringComparison.Ordinal));
                var    headers       = ReadHeaderLines(headerLine);
                if (headers != null &&
                    headers.Count > 0)
                {
                    if (headers.ContainsKey("Content-Length"))
                    {
                        contentLength = int.Parse(headers["Content-Length"]);
                    }
                }

                int len;
                if (contentLength <= 0)
                {
                    // Message does not have content length only headers.
                    var decodedMessage = new EslDecodedMessage(headerLine, string.Empty, textReceived);
                    MessageReceived(decodedMessage);
                    Clear();

                    // Let us check whether there is another message in the buffer sent
                    textReceived = textReceived.Substring(textReceived.IndexOf(MESSAGE_END_STRING, StringComparison.Ordinal) + MESSAGE_END_STRING.Length);
                    if (string.IsNullOrEmpty(textReceived) ||
                        textReceived.Equals(MESSAGE_END_STRING) ||
                        textReceived.Equals(EOL))
                    {
                        continue;
                    }
                    // Length of extra bytes
                    len = _encoding.GetByteCount(textReceived);
                    _encoding.GetBytes(textReceived, 0, textReceived.Length, _buffer, 0);
                    _offsetInOurBuffer += len;
                }
                else
                {
                    // Message does have body as well as header.
                    string bodyLine = textReceived.Substring(textReceived.IndexOf(MESSAGE_END_STRING, StringComparison.Ordinal) + MESSAGE_END_STRING.Length);
                    if (string.IsNullOrEmpty(bodyLine))
                    {
                        len = _encoding.GetByteCount(textReceived);
                        // We need to read more bytes for the body
                        _offsetInOurBuffer += len;
                        return;
                    }

                    // body has been read. However there are more to read to make it complete
                    if (bodyLine.Length < contentLength)
                    {
                        // get the count of the received bytes
                        len = _encoding.GetByteCount(textReceived);
                        // The body is not yet complete we need to read more
                        _offsetInOurBuffer += len;
                        return;
                    }

                    // body has been fully read
                    if (contentLength == bodyLine.Length)
                    {
                        MessageReceived(new EslDecodedMessage(headerLine, bodyLine, textReceived));
                        Clear();
                        return;
                    }

                    // There is another message in the buffer
                    if (bodyLine.Length > contentLength)
                    {
                        string bodyLine2 = bodyLine.Substring(0, contentLength);
                        MessageReceived(new EslDecodedMessage(headerLine, bodyLine2, headerLine + bodyLine2));
                        Clear();

                        textReceived = bodyLine.Remove(bodyLine.IndexOf(bodyLine2, StringComparison.Ordinal), bodyLine2.Length);
                        if (string.IsNullOrEmpty(textReceived) ||
                            textReceived.Equals(MESSAGE_END_STRING) ||
                            textReceived.Equals(EOL))
                        {
                            return;
                        }
                        // Length of extra bytes
                        len = _encoding.GetByteCount(textReceived);
                        _encoding.GetBytes(textReceived, 0, textReceived.Length, _buffer, 0);
                        _offsetInOurBuffer += len;
                    }
                }
            }
        }
        /// <summary>
        ///     We've received bytes from the socket. Build a message out of them.
        /// </summary>
        /// <param name="buffer"></param>
        public void ProcessReadBytes(ISocketBuffer buffer) {
            // buffer offset received
            var offsetInSocketBuffer = buffer.Offset;           

            // Number of Bytes received
            _bytesReceived = buffer.BytesTransferred;
            while (true) {
                if (_bytesReceived <= 0) break;
                Buffer.BlockCopy(buffer.Buffer, offsetInSocketBuffer, _buffer, _offsetInOurBuffer, buffer.BytesTransferred);

                string textReceived = _encoding.GetString(_buffer);
                textReceived = textReceived.TrimStart(LINE_FEED_CHAR).Replace("\0", string.Empty);
                if (!textReceived.Contains(MESSAGE_END_STRING)) {
                    _offsetInOurBuffer += _bytesReceived;
                    return;
                }

                int contentLength = 0;
                string headerLine = textReceived.Substring(0, textReceived.IndexOf(MESSAGE_END_STRING, StringComparison.Ordinal));
                var headers = ReadHeaderLines(headerLine);
                if (headers != null
                    && headers.Count > 0)
                    if (headers.ContainsKey("Content-Length")) contentLength = int.Parse(headers["Content-Length"]);

                int len;
                if (contentLength <= 0) {
                    // Message does not have content length only headers.
                    var decodedMessage = new EslDecodedMessage(headerLine, string.Empty, textReceived);
                    MessageReceived(decodedMessage);
                    Clear();

                    // Let us check whether there is another message in the buffer sent
                    textReceived = textReceived.Substring(textReceived.IndexOf(MESSAGE_END_STRING, StringComparison.Ordinal) + MESSAGE_END_STRING.Length);
                    if (string.IsNullOrEmpty(textReceived)
                        || textReceived.Equals(MESSAGE_END_STRING)
                        || textReceived.Equals(EOL)) continue;
                    // Length of extra bytes
                    len = _encoding.GetByteCount(textReceived);
                    _encoding.GetBytes(textReceived, 0, textReceived.Length, _buffer, 0);
                    _offsetInOurBuffer += len;
                }
                else {
                    // Message does have body as well as header.
                    string bodyLine = textReceived.Substring(textReceived.IndexOf(MESSAGE_END_STRING, StringComparison.Ordinal) + MESSAGE_END_STRING.Length);
                    if (string.IsNullOrEmpty(bodyLine)) {
                        len = _encoding.GetByteCount(textReceived);
                        // We need to read more bytes for the body    
                        _offsetInOurBuffer += len;
                        return;
                    }

                    // body has been read. However there are more to read to make it complete
                    if (bodyLine.Length < contentLength) {
                        // get the count of the received bytes
                        len = _encoding.GetByteCount(textReceived);
                        // The body is not yet complete we need to read more  
                        _offsetInOurBuffer += len;
                        return;
                    }

                    // body has been fully read
                    if (contentLength == bodyLine.Length) {
                        MessageReceived(new EslDecodedMessage(headerLine, bodyLine, textReceived));
                        Clear();
                        return;
                    }

                    // There is another message in the buffer
                    if (bodyLine.Length > contentLength) {
                        string bodyLine2 = bodyLine.Substring(0, contentLength);
                        MessageReceived(new EslDecodedMessage(headerLine, bodyLine2, headerLine+ bodyLine2));
                        Clear();

                        textReceived = bodyLine.Remove(bodyLine.IndexOf(bodyLine2, StringComparison.Ordinal), bodyLine2.Length);
                        if (string.IsNullOrEmpty(textReceived)
                            || textReceived.Equals(MESSAGE_END_STRING)
                            || textReceived.Equals(EOL)) return;
                        // Length of extra bytes
                        len = _encoding.GetByteCount(textReceived);
                        _encoding.GetBytes(textReceived, 0, textReceived.Length, _buffer, 0);
                        _offsetInOurBuffer += len;
                    }
                }
            }
        }