Example #1
0
 public override void ConnectionOpened(RtmpConnection connection)
 {
     base.ConnectionOpened(connection);
     if (connection.Context.Mode == RtmpMode.Server)
     {
         connection.StartWaitForHandshake();
     }
 }
Example #2
0
 public RtmptRequest(RtmpConnection connection, string url, string protocol, string httpMethod, Hashtable headers)
 {
     _connection = connection;
     _url = url;
     _protocol = protocol;
     _httpMethod = httpMethod;
     _headers = headers;
 }
Example #3
0
 public override void MessageSent(RtmpConnection connection, object message)
 {
     base.MessageSent(connection, message);
     RtmpPacket sent = message as RtmpPacket;
     int channelId = sent.Header.ChannelId;
     IClientStream stream = null;
     if( connection is IStreamCapableConnection )
         stream = (connection as IStreamCapableConnection).GetStreamByChannelId(channelId);
     // XXX we'd better use new event model for notification
     if (stream != null && (stream is PlaylistSubscriberStream))
     {
         (stream as PlaylistSubscriberStream).Written(sent.Message);
         
     }
 }
Example #4
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="serverBW"></param>
 protected abstract void OnServerBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ServerBW serverBW);
Example #5
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="header"></param>
 /// <param name="message"></param>
 protected abstract void OnSharedObject(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, SharedObjectMessage message);
Example #6
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="ping"></param>
 protected abstract void OnPing(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, Ping ping);
Example #7
0
 private static void SendSOPersistenceMismatch(RtmpConnection connection, string name, bool persistent)
 {
     SharedObjectMessage msg;
     if (connection.ObjectEncoding == ObjectEncoding.AMF0)
         msg = new SharedObjectMessage(name, 0, persistent);
     else
         msg = new FlexSharedObjectMessage(name, 0, persistent);
     msg.AddEvent(new SharedObjectEvent(SharedObjectEventType.CLIENT_STATUS, StatusASO.SO_PERSISTENCE_MISMATCH, "error"));
     connection.GetChannel((byte)3).Write(msg);
 }
Example #8
0
 protected override void OnClientBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ClientBW clientBW)
 {
     
 }
Example #9
0
 public CreateStreamCallBack(NetStream stream, RtmpConnection connection, IPendingServiceCallback callback)
 {
     ValidationUtils.ArgumentNotNull(connection, "connection");
     _stream = stream;
     _connection = connection;
     _callback = callback;
 }
        internal RtmpChannel(RtmpConnection connection, int channelId)
		{
			_connection = connection;
			_channelId = channelId;
		}
Example #11
0
        protected override void OnInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, Notify invoke)
        {
            IServiceCall serviceCall = invoke.ServiceCall;

            // If it's a callback for server remote call then pass it over to callbacks handler
            // and return
            if (serviceCall.ServiceMethodName.Equals("_result") || serviceCall.ServiceMethodName.Equals("_error"))
            {
                HandlePendingCallResult(connection, invoke);
                return;
            }

            bool   disconnectOnReturn = false;
            string action             = null;

            if (serviceCall.ServiceName == null)
            {
                action = serviceCall.ServiceMethodName;
                switch (action)
                {
                case ACTION_CONNECT:
                {
                    if (!connection.IsConnected)
                    {
                        IDictionary parameters = invoke.ConnectionParameters;
                        string      host       = null;
                        if (parameters.Contains("tcUrl"))
                        {
                            host = GetHostname(parameters["tcUrl"] as string);
                        }
                        if (host != null && host.IndexOf(":") != -1)
                        {
                            // Remove default port from connection string
                            host = host.Substring(0, host.IndexOf(":"));
                        }
                        string app  = parameters["app"] as string;
                        string path = parameters["app"] as string;
                        // App name as path, but without query string if there is one
                        if (path != null && path.IndexOf("?") != -1)
                        {
                            int idx = path.IndexOf("?");
                            parameters["queryString"] = path.Substring(idx);
                            path = path.Substring(0, idx);
                        }
                        parameters["path"] = path;

                        connection.Setup(host, path, parameters);
                        try
                        {
                            //IGlobalScope global = this.Endpoint.LookupGlobal(host, path);
                            IGlobalScope global = this.Endpoint.GetMessageBroker().GlobalScope;
                            if (global == null)
                            {
                                serviceCall.Status = Call.STATUS_SERVICE_NOT_FOUND;
                                if (serviceCall is IPendingServiceCall)
                                {
                                    StatusASO status = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_INVALID_APPLICATION, connection.ObjectEncoding);
                                    status.description = "No global scope on this server.";
                                    (serviceCall as IPendingServiceCall).Result = status;
                                }
                                log.Info(string.Format("No application scope found for {0} on host {1}. Misspelled or missing application folder?", path, host));
                                disconnectOnReturn = true;
                            }
                            else
                            {
                                IScopeContext context = global.Context;
                                IScope        scope   = null;
                                try
                                {
                                    scope = context.ResolveScope(global, path);
                                }
                                catch (ScopeNotFoundException /*exception*/)
                                {
                                    if (log.IsErrorEnabled)
                                    {
                                        log.Error(__Res.GetString(__Res.Scope_NotFound, path));
                                    }

                                    serviceCall.Status = Call.STATUS_SERVICE_NOT_FOUND;
                                    if (serviceCall is IPendingServiceCall)
                                    {
                                        StatusASO status = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_REJECTED, connection.ObjectEncoding);
                                        status.description = "No scope \"" + path + "\" on this server.";
                                        (serviceCall as IPendingServiceCall).Result = status;
                                    }
                                    disconnectOnReturn = true;
                                }
                                catch (ScopeShuttingDownException)
                                {
                                    serviceCall.Status = Call.STATUS_APP_SHUTTING_DOWN;
                                    if (serviceCall is IPendingServiceCall)
                                    {
                                        StatusASO status = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_APPSHUTDOWN, connection.ObjectEncoding);
                                        status.description = "Application at \"" + path + "\" is currently shutting down.";
                                        (serviceCall as IPendingServiceCall).Result = status;
                                    }
                                    log.Info(string.Format("Application at {0} currently shutting down on {1}", path, host));
                                    disconnectOnReturn = true;
                                }
                                if (scope != null)
                                {
                                    if (log.IsInfoEnabled)
                                    {
                                        log.Info(__Res.GetString(__Res.Scope_Connect, scope.Name));
                                    }
                                    bool okayToConnect;
                                    try
                                    {
                                        //The only way to differentiate NetConnection.connect() and Consumer.subscribe() seems to be the app name
                                        if (app == string.Empty)
                                        {
                                            connection.SetIsFlexClient(true);
                                            okayToConnect = connection.Connect(scope, serviceCall.Arguments);
                                            if (okayToConnect)
                                            {
                                                if (serviceCall.Arguments != null && serviceCall.Arguments.Length >= 3)
                                                {
                                                    string credentials = serviceCall.Arguments[2] as string;
                                                    if (credentials != null && credentials != string.Empty)
                                                    {
                                                        MessageBroker         messageBroker         = this.Endpoint.GetMessageBroker();
                                                        AuthenticationService authenticationService = messageBroker.GetService(AuthenticationService.ServiceId) as AuthenticationService;
                                                        authenticationService.Authenticate(credentials);
                                                    }
                                                }
                                                //FDS 2.0.1 fds.swc
                                                if (serviceCall.Arguments != null && serviceCall.Arguments.Length == 1)
                                                {
                                                    string credentials = serviceCall.Arguments[0] as string;
                                                    if (credentials != null && credentials != string.Empty)
                                                    {
                                                        MessageBroker         messageBroker         = this.Endpoint.GetMessageBroker();
                                                        AuthenticationService authenticationService = messageBroker.GetService(AuthenticationService.ServiceId) as AuthenticationService;
                                                        authenticationService.Authenticate(credentials);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            connection.SetIsFlexClient(false);
                                            okayToConnect = connection.Connect(scope, serviceCall.Arguments);
                                        }
                                        if (okayToConnect)
                                        {
                                            if (log.IsDebugEnabled)
                                            {
                                                log.Debug("Connected RtmpClient: " + connection.Client.Id);
                                            }
                                            serviceCall.Status = Call.STATUS_SUCCESS_RESULT;
                                            if (serviceCall is IPendingServiceCall)
                                            {
                                                StatusASO statusASO = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_SUCCESS, connection.ObjectEncoding);
                                                statusASO.Add("id", connection.Client.Id);
                                                (serviceCall as IPendingServiceCall).Result = statusASO;
                                            }
                                            // Measure initial roundtrip time after connecting
                                            connection.GetChannel((byte)2).Write(new Ping(Ping.StreamBegin, 0, -1));
                                            connection.StartRoundTripMeasurement();
                                        }
                                        else
                                        {
                                            if (log.IsDebugEnabled)
                                            {
                                                log.Debug("Connect failed");
                                            }
                                            serviceCall.Status = Call.STATUS_ACCESS_DENIED;
                                            if (serviceCall is IPendingServiceCall)
                                            {
                                                (serviceCall as IPendingServiceCall).Result = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_REJECTED, connection.ObjectEncoding);
                                            }
                                            disconnectOnReturn = true;
                                        }
                                    }
                                    catch (ClientRejectedException rejected)
                                    {
                                        if (log.IsDebugEnabled)
                                        {
                                            log.Debug("Connect rejected");
                                        }
                                        serviceCall.Status = Call.STATUS_ACCESS_DENIED;
                                        if (serviceCall is IPendingServiceCall)
                                        {
                                            StatusASO statusASO = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_REJECTED, connection.ObjectEncoding);
                                            statusASO.Application = rejected.Reason;
                                            (serviceCall as IPendingServiceCall).Result = statusASO;
                                        }
                                        disconnectOnReturn = true;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error("Error connecting", ex);
                            }

                            serviceCall.Status = Call.STATUS_GENERAL_EXCEPTION;
                            if (serviceCall is IPendingServiceCall)
                            {
                                (serviceCall as IPendingServiceCall).Result = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_FAILED, connection.ObjectEncoding);
                            }
                            disconnectOnReturn = true;
                        }
                    }
                    else
                    {
                        // Service calls, must be connected.
                        InvokeCall(connection, serviceCall);
                    }
                }
                break;

                case ACTION_DISCONNECT:
                    connection.Close();
                    break;

                case ACTION_CREATE_STREAM:
                case ACTION_DELETE_STREAM:
                case ACTION_RELEASE_STREAM:
                case ACTION_PUBLISH:
                case ACTION_PLAY:
                case ACTION_SEEK:
                case ACTION_PAUSE:
                case ACTION_CLOSE_STREAM:
                case ACTION_RECEIVE_VIDEO:
                case ACTION_RECEIVE_AUDIO:
                {
                    IStreamService streamService = ScopeUtils.GetScopeService(connection.Scope, typeof(IStreamService)) as IStreamService;
                    StatusASO      status        = null;
                    try
                    {
                        if (!InvokeCall(connection, serviceCall, streamService))
                        {
                            status             = StatusASO.GetStatusObject(StatusASO.NS_INVALID_ARGUMENT, connection.ObjectEncoding);
                            status.description = "Failed to " + action + " (stream ID: " + header.StreamId + ")";
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error while invoking " + action + " on stream service.", ex);
                        status             = StatusASO.GetStatusObject(StatusASO.NS_FAILED, connection.ObjectEncoding);
                        status.description = "Error while invoking " + action + " (stream ID: " + header.StreamId + ")";
                        status.details     = ex.Message;
                    }
                    if (status != null)
                    {
                        channel.SendStatus(status);
                    }
                }
                break;

                default:
                    if (connection.IsConnected)
                    {
                        InvokeCall(connection, serviceCall);
                    }
                    else
                    {
                        // Warn user attemps to call service without being connected
                        if (log.IsWarnEnabled)
                        {
                            log.Warn("Not connected, closing connection");
                        }
                        connection.Close();
                    }
                    break;
                }
            }

            /*
             *          if(invoke is FlexInvoke)
             *          {
             *                  FlexInvoke reply = new FlexInvoke();
             *                  reply.InvokeId = invoke.InvokeId;
             *                  reply.SetResponseSuccess();
             *                  //TODO
             *                  if( serviceCall is IPendingServiceCall )
             *                  {
             *                          IPendingServiceCall pendingCall = (IPendingServiceCall)serviceCall;
             *                          reply.Response = pendingCall.Result;
             *                  }
             *                  channel.Write(reply);
             *          }
             *          else if(invoke is Invoke)
             */
            if (invoke is Invoke)
            {
                if ((header.StreamId != 0) &&
                    (serviceCall.Status == Call.STATUS_SUCCESS_VOID || serviceCall.Status == Call.STATUS_SUCCESS_NULL))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Method does not have return value, do not reply");
                    }
                    return;
                }

                // The client expects a result for the method call.
                Invoke reply = new Invoke();
                reply.ServiceCall = serviceCall;
                reply.InvokeId    = invoke.InvokeId;
                //sending reply
                channel.Write(reply);
            }
            if (disconnectOnReturn)
            {
                connection.Close();
            }
            if (action == ACTION_CONNECT)
            {
                connection.Context.ObjectEncoding = connection.ObjectEncoding;
            }
        }
Example #12
0
 protected override void OnClientBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ClientBW clientBW)
 {
 }
Example #13
0
 protected override void OnServerBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ServerBW serverBW)
 {
 }
Example #14
0
        protected override void OnInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, Notify invoke)
        {
            IServiceCall serviceCall = invoke.ServiceCall;

            if (serviceCall.ServiceMethodName.Equals("_result") || serviceCall.ServiceMethodName.Equals("_error"))
            {
                base.HandlePendingCallResult(connection, invoke);
            }
            else
            {
                bool   flag = false;
                string serviceMethodName = null;
                if (serviceCall.ServiceName == null)
                {
                    StatusASO statusObject;
                    Exception exception2;
                    serviceMethodName = serviceCall.ServiceMethodName;
                    switch (serviceMethodName)
                    {
                    case "connect":
                    {
                        if (connection.IsConnected)
                        {
                            InvokeCall(connection, serviceCall);
                            break;
                        }
                        IDictionary connectionParameters = invoke.ConnectionParameters;
                        string      host = null;
                        if (connectionParameters.Contains("tcUrl"))
                        {
                            host = BaseRtmpHandler.GetHostname(connectionParameters["tcUrl"] as string);
                        }
                        if ((host != null) && (host.IndexOf(":") != -1))
                        {
                            host = host.Substring(0, host.IndexOf(":"));
                        }
                        string str3 = connectionParameters["app"] as string;
                        string path = connectionParameters["app"] as string;
                        if ((path != null) && (path.IndexOf("?") != -1))
                        {
                            int index = path.IndexOf("?");
                            connectionParameters["queryString"] = path.Substring(index);
                            path = path.Substring(0, index);
                        }
                        connectionParameters["path"] = path;
                        connection.Setup(host, path, connectionParameters);
                        try
                        {
                            IGlobalScope globalScope = this.Endpoint.GetMessageBroker().GlobalScope;
                            if (globalScope == null)
                            {
                                serviceCall.Status = 0x10;
                                if (serviceCall is IPendingServiceCall)
                                {
                                    statusObject             = StatusASO.GetStatusObject("NetConnection.Connect.InvalidApp", connection.ObjectEncoding);
                                    statusObject.description = "No global scope on this server.";
                                    (serviceCall as IPendingServiceCall).Result = statusObject;
                                }
                                log.Info(string.Format("No application scope found for {0} on host {1}. Misspelled or missing application folder?", path, host));
                                flag = true;
                            }
                            else
                            {
                                IScopeContext context = globalScope.Context;
                                IScope        scope   = null;
                                try
                                {
                                    scope = context.ResolveScope(globalScope, path);
                                }
                                catch (ScopeNotFoundException)
                                {
                                    if (log.get_IsErrorEnabled())
                                    {
                                        log.Error(__Res.GetString("Scope_NotFound", new object[] { path }));
                                    }
                                    serviceCall.Status = 0x10;
                                    if (serviceCall is IPendingServiceCall)
                                    {
                                        statusObject             = StatusASO.GetStatusObject("NetConnection.Connect.Rejected", connection.ObjectEncoding);
                                        statusObject.description = "No scope \"" + path + "\" on this server.";
                                        (serviceCall as IPendingServiceCall).Result = statusObject;
                                    }
                                    flag = true;
                                }
                                catch (ScopeShuttingDownException)
                                {
                                    serviceCall.Status = 0x15;
                                    if (serviceCall is IPendingServiceCall)
                                    {
                                        statusObject             = StatusASO.GetStatusObject("NetConnection.Connect.AppShutdown", connection.ObjectEncoding);
                                        statusObject.description = "Application at \"" + path + "\" is currently shutting down.";
                                        (serviceCall as IPendingServiceCall).Result = statusObject;
                                    }
                                    log.Info(string.Format("Application at {0} currently shutting down on {1}", path, host));
                                    flag = true;
                                }
                                if (scope != null)
                                {
                                    StatusASO saso2;
                                    if (log.get_IsInfoEnabled())
                                    {
                                        log.Info(__Res.GetString("Scope_Connect", new object[] { scope.Name }));
                                    }
                                    try
                                    {
                                        bool flag2;
                                        if (str3 == string.Empty)
                                        {
                                            connection.SetIsFlexClient(true);
                                            flag2 = connection.Connect(scope, serviceCall.Arguments);
                                            if (flag2)
                                            {
                                                string str5;
                                                if ((serviceCall.Arguments != null) && (serviceCall.Arguments.Length == 3))
                                                {
                                                    str5 = serviceCall.Arguments[2] as string;
                                                    AuthenticationService service = this.Endpoint.GetMessageBroker().GetService("authentication-service") as AuthenticationService;
                                                    service.Authenticate(str5);
                                                }
                                                if ((serviceCall.Arguments != null) && (serviceCall.Arguments.Length == 1))
                                                {
                                                    str5 = serviceCall.Arguments[0] as string;
                                                    (this.Endpoint.GetMessageBroker().GetService("authentication-service") as AuthenticationService).Authenticate(str5);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            connection.SetIsFlexClient(false);
                                            flag2 = connection.Connect(scope, serviceCall.Arguments);
                                        }
                                        if (flag2)
                                        {
                                            if (log.get_IsDebugEnabled())
                                            {
                                                log.Debug("Connected RtmpClient: " + connection.Client.Id);
                                            }
                                            serviceCall.Status = 2;
                                            if (serviceCall is IPendingServiceCall)
                                            {
                                                saso2 = StatusASO.GetStatusObject("NetConnection.Connect.Success", connection.ObjectEncoding);
                                                saso2.Add("id", connection.Client.Id);
                                                (serviceCall as IPendingServiceCall).Result = saso2;
                                            }
                                            connection.GetChannel(2).Write(new Ping(0, 0, -1));
                                            connection.StartRoundTripMeasurement();
                                        }
                                        else
                                        {
                                            if (log.get_IsDebugEnabled())
                                            {
                                                log.Debug("Connect failed");
                                            }
                                            serviceCall.Status = 0x12;
                                            if (serviceCall is IPendingServiceCall)
                                            {
                                                (serviceCall as IPendingServiceCall).Result = StatusASO.GetStatusObject("NetConnection.Connect.Rejected", connection.ObjectEncoding);
                                            }
                                            flag = true;
                                        }
                                    }
                                    catch (ClientRejectedException exception)
                                    {
                                        if (log.get_IsDebugEnabled())
                                        {
                                            log.Debug("Connect rejected");
                                        }
                                        serviceCall.Status = 0x12;
                                        if (serviceCall is IPendingServiceCall)
                                        {
                                            saso2             = StatusASO.GetStatusObject("NetConnection.Connect.Rejected", connection.ObjectEncoding);
                                            saso2.Application = exception.Reason;
                                            (serviceCall as IPendingServiceCall).Result = saso2;
                                        }
                                        flag = true;
                                    }
                                }
                            }
                        }
                        catch (Exception exception5)
                        {
                            exception2 = exception5;
                            if (log.get_IsErrorEnabled())
                            {
                                log.Error("Error connecting", exception2);
                            }
                            serviceCall.Status = 20;
                            if (serviceCall is IPendingServiceCall)
                            {
                                (serviceCall as IPendingServiceCall).Result = StatusASO.GetStatusObject("NetConnection.Connect.Failed", connection.ObjectEncoding);
                            }
                            flag = true;
                        }
                        break;
                    }

                    case "disconnect":
                        connection.Close();
                        break;

                    case "createStream":
                    case "deleteStream":
                    case "releaseStream":
                    case "publish":
                    case "play":
                    case "seek":
                    case "pause":
                    case "closeStream":
                    case "receiveVideo":
                    case "receiveAudio":
                    {
                        IStreamService scopeService = ScopeUtils.GetScopeService(connection.Scope, typeof(IStreamService)) as IStreamService;
                        statusObject = null;
                        try
                        {
                            if (!InvokeCall(connection, serviceCall, scopeService))
                            {
                                statusObject             = StatusASO.GetStatusObject("NetStream.InvalidArg", connection.ObjectEncoding);
                                statusObject.description = string.Concat(new object[] { "Failed to ", serviceMethodName, " (stream ID: ", header.StreamId, ")" });
                            }
                        }
                        catch (Exception exception6)
                        {
                            exception2 = exception6;
                            log.Error("Error while invoking " + serviceMethodName + " on stream service.", exception2);
                            statusObject             = StatusASO.GetStatusObject("NetStream.Failed", connection.ObjectEncoding);
                            statusObject.description = string.Concat(new object[] { "Error while invoking ", serviceMethodName, " (stream ID: ", header.StreamId, ")" });
                            statusObject.details     = exception2.Message;
                        }
                        if (statusObject != null)
                        {
                            channel.SendStatus(statusObject);
                        }
                        break;
                    }

                    default:
                        if (connection.IsConnected)
                        {
                            InvokeCall(connection, serviceCall);
                        }
                        else
                        {
                            if (log.get_IsWarnEnabled())
                            {
                                log.Warn("Not connected, closing connection");
                            }
                            connection.Close();
                        }
                        break;
                    }
                }
                if (invoke is FlexInvoke)
                {
                    FlexInvoke message = new FlexInvoke {
                        InvokeId = invoke.InvokeId
                    };
                    message.SetResponseSuccess();
                    if (serviceCall is IPendingServiceCall)
                    {
                        IPendingServiceCall call2 = (IPendingServiceCall)serviceCall;
                        message.Response = call2.Result;
                    }
                    channel.Write(message);
                }
                else if (invoke is Invoke)
                {
                    if ((header.StreamId != 0) && ((serviceCall.Status == 4) || (serviceCall.Status == 3)))
                    {
                        if (log.get_IsDebugEnabled())
                        {
                            log.Debug("Method does not have return value, do not reply");
                        }
                        return;
                    }
                    Invoke invoke3 = new Invoke {
                        ServiceCall = serviceCall,
                        InvokeId    = invoke.InvokeId
                    };
                    channel.Write(invoke3);
                }
                if (flag)
                {
                    connection.Close();
                }
                if (serviceMethodName == "connect")
                {
                    connection.Context.ObjectEncoding = connection.ObjectEncoding;
                }
            }
        }
Example #15
0
        /// <summary>
        /// Handler for pending call result. Dispatches results to all pending call handlers.
        /// </summary>
        /// <param name="connection">Connection.</param>
        /// <param name="invoke">Pending call result event context.</param>
        protected void HandlePendingCallResult(RtmpConnection connection, Notify invoke)
        {
            IServiceCall call = invoke.ServiceCall;
            IPendingServiceCall pendingCall = connection.RetrievePendingCall(invoke.InvokeId);
            if (pendingCall != null)
            {
                pendingCall.Status = call.Status;
                // The client sent a response to a previously made call.
                object[] args = call.Arguments;
                if ((args != null) && (args.Length > 0))
                {
                    pendingCall.Result = args[0];
                }

                IPendingServiceCallback[] callbacks = pendingCall.GetCallbacks();
                if (callbacks != null && callbacks.Length > 0)
                {
                    foreach (IPendingServiceCallback callback in callbacks)
                    {
                        try
                        {
                            callback.ResultReceived(pendingCall);
                        }
                        catch (Exception ex)
                        {
#if !SILVERLIGHT
                            log.Error("Error while executing callback " + callback, ex);
#endif
                        }
                    }
                }
            }
        }
        public static RtmptRequest DecodeBuffer(RtmpConnection connection, ByteBuffer stream)
        {
            RtmpContext context  = connection.Context;
            int         position = (int)stream.Position;

            try
            {
                BufferStreamReader sr      = new BufferStreamReader(stream);
                string             request = sr.ReadLine();
                string[]           tokens  = request.Split(new char[] { ' ' });
                string             method  = tokens[0];
                string             url     = tokens[1];
                // Decode all encoded parts of the URL using the built in URI processing class
                int i = 0;
                while ((i = url.IndexOf("%", i)) != -1)
                {
                    url = url.Substring(0, i) + Uri.HexUnescape(url, ref i) + url.Substring(i);
                }
                // Lets just make sure we are using HTTP, thats about all I care about
                string protocol = tokens[2];// "HTTP/"
                //Read headers
                Hashtable headers = new Hashtable();
                string    line;
                string    name = null;
                while ((line = sr.ReadLine()) != null && line != string.Empty)
                {
                    // If the value begins with a space or a hard tab then this
                    // is an extension of the value of the previous header and
                    // should be appended
                    if (name != null && Char.IsWhiteSpace(line[0]))
                    {
                        headers[name] += line;
                        continue;
                    }
                    // Headers consist of [NAME]: [VALUE] + possible extension lines
                    int firstColon = line.IndexOf(":");
                    if (firstColon != -1)
                    {
                        name = line.Substring(0, firstColon);
                        string value = line.Substring(firstColon + 1).Trim();
                        headers[name] = value;
                    }
                    else
                    {
                        //400, "Bad header: " + line
                        break;
                    }
                }
                RtmptRequest rtmptRequest = new RtmptRequest(connection, url, protocol, method, headers);
                if (stream.Remaining == rtmptRequest.ContentLength)
                {
                    stream.Compact();
                    rtmptRequest.Data = ByteBuffer.Wrap(stream.ToArray());
                    stream.Flip();
                    return(rtmptRequest);
                }
                else
                {
                    // Move the position back to the start
                    stream.Position = position;
                }
            }
            catch
            {
                // Move the position back to the start
                stream.Position = position;
                throw;
            }
            return(null);
        }
 /// <summary>
 /// Connection open event.
 /// </summary>
 /// <param name="connection">Connection object.</param>
 public virtual void ConnectionOpened(RtmpConnection connection)
 {
 }
Example #18
0
 protected override void OnPing(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, Ping ping)
 {
     switch (ping.PingType)
     {
         case Ping.ClientBuffer:
             IClientStream stream = null;
             // Get the stream id
             int streamId = ping.Value2;
             // Get requested buffer size in milliseconds
             int buffer = ping.Value3;
             if (streamId != 0)
             {
                 // The client wants to set the buffer time
                 stream = connection.GetStreamById(streamId);
                 if (stream != null)
                 {
                     stream.SetClientBufferDuration(buffer);
                     if (log.IsDebugEnabled)
                         log.Debug(string.Format("Client sent a buffer size: {0} ms for stream id: {1}", buffer, streamId ));
                 }
             }
             // Catch-all to make sure buffer size is set
             if (stream == null)
             {
                 // Remember buffer time until stream is created
                 connection.RememberStreamBufferDuration(streamId, buffer);
                 if (log.IsDebugEnabled)
                     log.Debug(string.Format("Remembering client buffer size: {0} on stream id: {1} ", buffer, streamId));
             }
             break;
         case Ping.PongServer:
             // This is the response to an IConnection.Ping request
             connection.PingReceived(ping);
             break;
         default:
             log.Warn("Unhandled ping: " + ping);
             break;
     }
 }
        /// <summary>
        /// Message recieved.
        /// </summary>
        /// <param name="connection">Connection object.</param>
        /// <param name="obj">Message object.</param>
        public void MessageReceived(RtmpConnection connection, object obj)
        {
            //Console.WriteLine("NEW MESSAGE");
            IRtmpEvent    message = null;
            RtmpPacket    packet  = null;
            RtmpHeader    header  = null;
            RtmpChannel   channel = null;
            IClientStream stream  = null;

            try
            {
                packet  = obj as RtmpPacket;
                message = packet.Message;
                header  = packet.Header;
                //Console.WriteLine("DATA TYPE: " + header.DataType);
                channel = connection.GetChannel(header.ChannelId);
                if (connection is IStreamCapableConnection)
                {
                    stream = (connection as IStreamCapableConnection).GetStreamById(header.StreamId);
                }

                // Support stream ids
#if !SILVERLIGHT
                FluorineContext.ValidateContext();
                FluorineContext.Current.Connection.SetAttribute(FluorineContext.FluorineStreamIdKey, header.StreamId);
#endif
                // Increase number of received messages
                connection.MessageReceived();

#if !SILVERLIGHT
                if (log != null && log.IsDebugEnabled)
                {
                    log.Debug("RtmpConnection message received, type = " + header.DataType);
                }
#endif

                if (message != null)
                {
                    message.Source = connection;
                }

                switch (header.DataType)
                {
                case Constants.TypeInvoke:
                    OnInvoke(connection, channel, header, message as Invoke);
                    if (message.Header.StreamId != 0 &&
                        (message as Invoke).ServiceCall.ServiceName == null &&
                        (message as Invoke).ServiceCall.ServiceMethodName == BaseRtmpHandler.ACTION_PUBLISH)
                    {
                        if (stream != null)     //Dispatch if stream was created
                        {
                            (stream as IEventDispatcher).DispatchEvent(message);
                        }
                    }
                    break;

                case Constants.TypeFlexInvoke:
                    OnFlexInvoke(connection, channel, header, message as FlexInvoke);
                    if (message.Header.StreamId != 0 &&
                        (message as Invoke).ServiceCall.ServiceName == null &&
                        (message as Invoke).ServiceCall.ServiceMethodName == BaseRtmpHandler.ACTION_PUBLISH)
                    {
                        if (stream != null)     //Dispatch if stream was created
                        {
                            (stream as IEventDispatcher).DispatchEvent(message);
                        }
                    }
                    break;

                case Constants.TypeNotify:    // just like invoke, but does not return
                    //Console.WriteLine("NOTIFY MESSAGE");
                    if ((message as Notify).Data != null && stream != null)
                    {
                        // Stream metadata
                        (stream as IEventDispatcher).DispatchEvent(message);
                    }
                    else
                    {
                        OnInvoke(connection, channel, header, message as Notify);
                    }
                    break;

                case Constants.TypePing:
                    OnPing(connection, channel, header, message as Ping);
                    break;

                case Constants.TypeBytesRead:
                    OnStreamBytesRead(connection, channel, header, message as BytesRead);
                    break;

                case Constants.TypeSharedObject:
                case Constants.TypeFlexSharedObject:
                    OnSharedObject(connection, channel, header, message as SharedObjectMessage);
                    break;

                case Constants.TypeFlexStreamEnd:
                    if (stream != null)
                    {
                        (stream as IEventDispatcher).DispatchEvent(message);
                    }
                    break;

                case Constants.TypeChunkSize:
                    OnChunkSize(connection, channel, header, message as ChunkSize);
                    break;

                case Constants.TypeAudioData:
                case Constants.TypeVideoData:
                    // NOTE: If we respond to "publish" with "NetStream.Publish.BadName",
                    // the client sends a few stream packets before stopping. We need to
                    // ignore them.
                    if (stream != null)
                    {
                        ((IEventDispatcher)stream).DispatchEvent(message);
                    }
                    break;

                case Constants.TypeServerBandwidth:
                    OnServerBW(connection, channel, header, message as ServerBW);
                    break;

                case Constants.TypeClientBandwidth:
                    OnClientBW(connection, channel, header, message as ClientBW);
                    break;

                default:
                    //Console.WriteLine("Package not WORKING or SENT");
#if !SILVERLIGHT
                    if (log != null && log.IsDebugEnabled)
                    {
                        log.Debug("RtmpService event not handled: " + header.DataType);
                    }
#endif
                    break;
                }
            }
            catch (Exception ex)
            {
#if !SILVERLIGHT
                if (log.IsErrorEnabled)
                {
                    log.Error(__Res.GetString(__Res.Rtmp_HandlerError), ex);
                    log.Error(__Res.GetString(__Res.Error_ContextDump));
                    //log.Error(Environment.NewLine);
                    log.Error(packet);
                    //Console.WriteLine("PACKET ERROR");
                }
#endif
            }
        }
Example #20
0
        protected override void OnSharedObject(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, SharedObjectMessage message) 
		{
			ISharedObject so = null;
			string name = message.Name;
			IScope scope = connection.Scope;
            bool persistent = message.IsPersistent;
			if(scope == null) 
			{
                // The scope already has been deleted.
                SendSOCreationFailed(connection, name, persistent);
				return;
			}
            ISharedObjectService sharedObjectService = ScopeUtils.GetScopeService(scope, typeof(ISharedObjectService)) as ISharedObjectService;
			if (!sharedObjectService.HasSharedObject(scope, name))
			{
                ISharedObjectSecurityService securityService = ScopeUtils.GetScopeService(scope, typeof(ISharedObjectSecurityService)) as ISharedObjectSecurityService;
			    if (securityService != null) 
                {
				    // Check handlers to see if creation is allowed
                    IEnumerator enumerator = securityService.GetSharedObjectSecurity();
                    while(enumerator.MoveNext())
                    {
                        ISharedObjectSecurity handler = enumerator.Current as ISharedObjectSecurity;
                        if (!handler.IsCreationAllowed(scope, name, persistent))
                        {
                            SendSOCreationFailed(connection, name, persistent);
						    return;
					    }
				    }
			    }

                if (!sharedObjectService.CreateSharedObject(scope, name, persistent)) 
				{
                    SendSOCreationFailed(connection, name, persistent);
					return;
				}
			}
			so = sharedObjectService.GetSharedObject(scope, name);
            if (so.IsPersistentObject != persistent)
            {
                log.Debug(string.Format("Shared object '{0}' persistence mismatch", name));
                SendSOPersistenceMismatch(connection, name, persistent);
                return;
            }
			so.DispatchEvent(message);
		}
 /// <summary>
 /// Connection closed.
 /// </summary>
 /// <param name="connection">Connection object.</param>
 public virtual void ConnectionClosed(RtmpConnection connection)
 {
     connection.Close();
 }
Example #22
0
        public static void InvokeCall(RtmpConnection connection, IServiceCall serviceCall) 
		{
			IScope scope = connection.Scope;
			if(scope.HasHandler) 
			{
				IScopeHandler handler = scope.Handler;
				if(!handler.ServiceCall(connection, serviceCall)) 
				{
					// What do do here? Return an error?
					return;
				}
			}
			IScopeContext context = scope.Context;
			context.ServiceInvoker.Invoke(serviceCall, scope);
		}
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="streamBytesRead"></param>
 protected void OnStreamBytesRead(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, BytesRead streamBytesRead)
 {
     connection.ReceivedBytesRead(streamBytesRead.Bytes);
 }
Example #24
0
        public void MessageReceived(RtmpConnection connection, object obj)
        {
            IRtmpEvent evt = null;

            try
            {
                RtmpPacket packet = obj as RtmpPacket;
                evt = packet.Message;
                RtmpHeader    source     = packet.Header;
                RtmpChannel   channel    = connection.GetChannel(source.ChannelId);
                IClientStream streamById = null;
                if (connection is IStreamCapableConnection)
                {
                    streamById = (connection as IStreamCapableConnection).GetStreamById(source.StreamId);
                }
                FluorineContext.Current.Connection.SetAttribute("__@fluorinestreamid", source.StreamId);
                connection.MessageReceived();
                if ((log != null) && log.get_IsDebugEnabled())
                {
                    log.Debug("RtmpConnection message received, type = " + source.DataType);
                }
                if (evt != null)
                {
                    evt.Source = connection;
                }
                switch (source.DataType)
                {
                case 1:
                    this.OnChunkSize(connection, channel, source, evt as ChunkSize);
                    return;

                case 3:
                    this.OnStreamBytesRead(connection, channel, source, evt as BytesRead);
                    return;

                case 4:
                    this.OnPing(connection, channel, source, evt as Ping);
                    return;

                case 5:
                    this.OnServerBW(connection, channel, source, evt as ServerBW);
                    return;

                case 6:
                    this.OnClientBW(connection, channel, source, evt as ClientBW);
                    return;

                case 8:
                case 9:
                    if (streamById != null)
                    {
                        ((IEventDispatcher)streamById).DispatchEvent(evt);
                    }
                    return;

                case 15:
                    if (streamById != null)
                    {
                        (streamById as IEventDispatcher).DispatchEvent(evt);
                    }
                    return;

                case 0x10:
                case 0x13:
                    this.OnSharedObject(connection, channel, source, evt as SharedObjectMessage);
                    return;

                case 0x11:
                    this.OnFlexInvoke(connection, channel, source, evt as FlexInvoke);
                    if ((((evt.Header.StreamId != 0) && ((evt as Invoke).ServiceCall.ServiceName == null)) && ((evt as Invoke).ServiceCall.ServiceMethodName == "publish")) && (streamById != null))
                    {
                        (streamById as IEventDispatcher).DispatchEvent(evt);
                    }
                    return;

                case 0x12:
                    if (((evt as Notify).Data == null) || (streamById == null))
                    {
                        break;
                    }
                    (streamById as IEventDispatcher).DispatchEvent(evt);
                    return;

                case 20:
                    this.OnInvoke(connection, channel, source, evt as Invoke);
                    if ((((evt.Header.StreamId != 0) && ((evt as Invoke).ServiceCall.ServiceName == null)) && ((evt as Invoke).ServiceCall.ServiceMethodName == "publish")) && (streamById != null))
                    {
                        (streamById as IEventDispatcher).DispatchEvent(evt);
                    }
                    return;

                default:
                    goto Label_0304;
                }
                this.OnInvoke(connection, channel, source, evt as Notify);
                return;

Label_0304:
                if ((log != null) && log.get_IsDebugEnabled())
                {
                    log.Debug("RtmpService event not handled: " + source.DataType);
                }
            }
            catch (Exception exception)
            {
                log.Error("Runtime error", exception);
            }
        }
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="chunkSize"></param>
 protected abstract void OnChunkSize(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ChunkSize chunkSize);
Example #26
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="header"></param>
 /// <param name="invoke"></param>
 protected abstract void OnInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, Notify invoke);
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="ping"></param>
 protected abstract void OnPing(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, Ping ping);
Example #28
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="header"></param>
 /// <param name="invoke"></param>
 protected abstract void OnFlexInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, FlexInvoke invoke);
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="header"></param>
 /// <param name="invoke"></param>
 protected abstract void OnInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, Notify invoke);
Example #30
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="clientBW"></param>
 protected abstract void OnClientBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ClientBW clientBW);
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="header"></param>
 /// <param name="message"></param>
 protected abstract void OnSharedObject(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, SharedObjectMessage message);
Example #32
0
        internal static void Push(RtmpConnection connection, IMessage message, IMessageClient messageClient)
        {
            if (connection != null)
            {
                object response = message;
                if (message is BinaryMessage)
                {
                    BinaryMessage binaryMessage = message as BinaryMessage;
                    binaryMessage.Update(messageClient);
                    byte[] binaryContent = binaryMessage.body as byte[];
                    //byte[] destClientBinaryId = messageClient.GetBinaryId();
                    //Array.Copy(destClientBinaryId, 0, binaryContent, binaryMessage.PatternPosition, destClientBinaryId.Length);

                    RawBinary result = new RawBinary(binaryContent);
                    response = result;
                }
                else
                {
                    //This should be a clone of the original message
                    message.SetHeader(MessageBase.DestinationClientIdHeader, messageClient.ClientId);
                    message.clientId = messageClient.ClientId;
                }

                RtmpChannel channel = connection.GetChannel(3);
                FlexInvoke reply = new FlexInvoke();
                Call call = new Call("receive", new object[] { response });
                reply.ServiceCall = call;
                //reply.Cmd = "receive";
                //reply.Response = response;
                reply.InvokeId = connection.InvokeId;
                channel.Write(reply);
            }
        }
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="header"></param>
 /// <param name="invoke"></param>
 protected abstract void OnFlexInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, FlexInvoke invoke);
 internal RtmpChannel(RtmpConnection connection, int channelId)
 {
     _connection = connection;
     _channelId  = channelId;
 }
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="serverBW"></param>
 protected abstract void OnServerBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ServerBW serverBW);
Example #36
0
/*
FlexInvoke flexInvoke = new FlexInvoke();
flexInvoke.Cmd = "onstatus";
flexInvoke.DataType = DataType.TypeUnknown;
StatusASO statusASO = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_CLOSED, connection.ObjectEncoding);
flexInvoke.Parameters = new object[]{ statusASO };
RtmpChannel channel = connection.GetChannel(3);
channel.Write(flexInvoke);
*/


        protected override void OnChunkSize(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ChunkSize chunkSize) 
        {
            if (connection is IStreamCapableConnection)
            {
                IStreamCapableConnection streamCapableConnection = connection as IStreamCapableConnection;
                {
                    foreach (IClientStream stream in streamCapableConnection.GetStreams())
                    {
                        if (stream is IClientBroadcastStream)
                        {
                            IClientBroadcastStream bs = stream as IClientBroadcastStream;
                            IBroadcastScope scope = bs.Scope.GetBasicScope(Constants.BroadcastScopeType, bs.PublishedName) as IBroadcastScope;
                            if (scope == null)
                                continue;

                            OOBControlMessage setChunkSize = new OOBControlMessage();
                            setChunkSize.Target = "ClientBroadcastStream";
                            setChunkSize.ServiceName = "chunkSize";
                            setChunkSize.ServiceParameterMap.Add("chunkSize", chunkSize.Size);
                            scope.SendOOBControlMessage((IConsumer)null, setChunkSize);
                            if (log.IsDebugEnabled)
                            {
                                log.Debug("Sending chunksize " + chunkSize + " to " + bs.Provider);
                            }
                        }
                    }
                }
            }
        }
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="clientBW"></param>
 protected abstract void OnClientBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ClientBW clientBW);
Example #38
0
 protected override void OnServerBW(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ServerBW serverBW)
 {
 }
Example #39
0
 /// <summary>
 /// Connection open event.
 /// </summary>
 /// <param name="connection">Connection object.</param>
 public virtual void ConnectionOpened(RtmpConnection connection)
 {
 }
Example #40
0
        protected override void OnInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, Notify invoke)
		{
			IServiceCall serviceCall = invoke.ServiceCall;

			// If it's a callback for server remote call then pass it over to callbacks handler
			// and return
			if(serviceCall.ServiceMethodName.Equals("_result") || serviceCall.ServiceMethodName.Equals("_error"))
			{
                HandlePendingCallResult(connection, invoke);
				return;
			}

			bool disconnectOnReturn = false;
			string action = null;
            if (serviceCall.ServiceName == null)
            {
                action = serviceCall.ServiceMethodName;
                switch (action)
                {
                    case ACTION_CONNECT:
                        {
                            if (!connection.IsConnected)
                            {
                                IDictionary parameters = invoke.ConnectionParameters;
                                string host = null;
                                if( parameters.Contains("tcUrl") )
                                    host = GetHostname(parameters["tcUrl"] as string);
                                if (host != null && host.IndexOf(":") != -1)
                                {
                                    // Remove default port from connection string
                                    host = host.Substring(0, host.IndexOf(":"));
                                }
                                string app = parameters["app"] as string;
                                string path = parameters["app"] as string;
                                // App name as path, but without query string if there is one
                                if (path != null && path.IndexOf("?") != -1)
                                {
                                    int idx = path.IndexOf("?");
                                    parameters["queryString"] = path.Substring(idx);
                                    path = path.Substring(0, idx);
                                }
                                parameters["path"] = path;

                                connection.Setup(host, path, parameters);
                                try
                                {
                                    //IGlobalScope global = this.Endpoint.LookupGlobal(host, path);
                                    IGlobalScope global = this.Endpoint.GetMessageBroker().GlobalScope;
                                    if (global == null)
                                    {
                                        serviceCall.Status = Call.STATUS_SERVICE_NOT_FOUND;
                                        if (serviceCall is IPendingServiceCall)
                                        {
                                            StatusASO status = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_INVALID_APPLICATION, connection.ObjectEncoding);
                                            status.description = "No global scope on this server.";
                                            (serviceCall as IPendingServiceCall).Result = status;
                                        }
                                        log.Info(string.Format("No application scope found for {0} on host {1}. Misspelled or missing application folder?", path, host));
                                        disconnectOnReturn = true;
                                    }
                                    else
                                    {
                                        IScopeContext context = global.Context;
                                        IScope scope = null;
                                        try
                                        {
                                            scope = context.ResolveScope(global, path);
                                        }
                                        catch (ScopeNotFoundException /*exception*/)
                                        {
                                            if (log.IsErrorEnabled)
                                                log.Error(__Res.GetString(__Res.Scope_NotFound, path));

                                            serviceCall.Status = Call.STATUS_SERVICE_NOT_FOUND;
                                            if (serviceCall is IPendingServiceCall)
                                            {
                                                StatusASO status = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_REJECTED, connection.ObjectEncoding);
                                                status.description = "No scope \"" + path + "\" on this server.";
                                                (serviceCall as IPendingServiceCall).Result = status;
                                            }
                                            disconnectOnReturn = true;
                                        }
                                        catch (ScopeShuttingDownException)
                                        {
                                            serviceCall.Status = Call.STATUS_APP_SHUTTING_DOWN;
                                            if (serviceCall is IPendingServiceCall)
                                            {
                                                StatusASO status = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_APPSHUTDOWN, connection.ObjectEncoding);
                                                status.description = "Application at \"" + path + "\" is currently shutting down.";
                                                (serviceCall as IPendingServiceCall).Result = status;
                                            }
                                            log.Info(string.Format("Application at {0} currently shutting down on {1}", path, host));
                                            disconnectOnReturn = true;
                                        }
                                        if (scope != null)
                                        {
                                            if (log.IsInfoEnabled)
                                                log.Info(__Res.GetString(__Res.Scope_Connect, scope.Name));
                                            bool okayToConnect;
                                            try
                                            {
                                                //The only way to differentiate NetConnection.connect() and Consumer.subscribe() seems to be the app name
                                                if (app == string.Empty)
                                                {
                                                    connection.SetIsFlexClient(true);
                                                    okayToConnect = connection.Connect(scope, serviceCall.Arguments);
                                                    if (okayToConnect)
                                                    {
                                                        if (serviceCall.Arguments != null && serviceCall.Arguments.Length >= 3)
                                                        {
                                                            string credentials = serviceCall.Arguments[2] as string;
                                                            if (credentials != null && credentials != string.Empty)
                                                            {
                                                                MessageBroker messageBroker = this.Endpoint.GetMessageBroker();
                                                                AuthenticationService authenticationService = messageBroker.GetService(AuthenticationService.ServiceId) as AuthenticationService;
                                                                authenticationService.Authenticate(credentials);
                                                            }
                                                        }
                                                        //FDS 2.0.1 fds.swc
                                                        if (serviceCall.Arguments != null && serviceCall.Arguments.Length == 1)
                                                        {
                                                            string credentials = serviceCall.Arguments[0] as string;
                                                            if (credentials != null && credentials != string.Empty)
                                                            {
                                                                MessageBroker messageBroker = this.Endpoint.GetMessageBroker();
                                                                AuthenticationService authenticationService = messageBroker.GetService(AuthenticationService.ServiceId) as AuthenticationService;
                                                                authenticationService.Authenticate(credentials);
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    connection.SetIsFlexClient(false);
                                                    okayToConnect = connection.Connect(scope, serviceCall.Arguments);
                                                }
                                                if (okayToConnect)
                                                {
                                                    if (log.IsDebugEnabled)
                                                        log.Debug("Connected RtmpClient: " + connection.Client.Id);
                                                    serviceCall.Status = Call.STATUS_SUCCESS_RESULT;
                                                    if (serviceCall is IPendingServiceCall)
                                                    {
                                                        StatusASO statusASO = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_SUCCESS, connection.ObjectEncoding);
                                                        statusASO.Add("id", connection.Client.Id);
                                                        (serviceCall as IPendingServiceCall).Result = statusASO;
                                                    }
                                                    // Measure initial roundtrip time after connecting
                                                    connection.GetChannel((byte)2).Write(new Ping(Ping.StreamBegin, 0, -1));
                                                    connection.StartRoundTripMeasurement();
                                                }
                                                else
                                                {
                                                    if (log.IsDebugEnabled)
                                                        log.Debug("Connect failed");
                                                    serviceCall.Status = Call.STATUS_ACCESS_DENIED;
                                                    if (serviceCall is IPendingServiceCall)
                                                        (serviceCall as IPendingServiceCall).Result = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_REJECTED, connection.ObjectEncoding);
                                                    disconnectOnReturn = true;
                                                }
                                            }
                                            catch (ClientRejectedException rejected)
                                            {
                                                if (log.IsDebugEnabled)
                                                    log.Debug("Connect rejected");
                                                serviceCall.Status = Call.STATUS_ACCESS_DENIED;
                                                if (serviceCall is IPendingServiceCall)
                                                {
                                                    StatusASO statusASO = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_REJECTED, connection.ObjectEncoding);
                                                    statusASO.Application = rejected.Reason;
                                                    (serviceCall as IPendingServiceCall).Result = statusASO;
                                                }
                                                disconnectOnReturn = true;
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    if (log.IsErrorEnabled)
                                        log.Error("Error connecting", ex);

                                    serviceCall.Status = Call.STATUS_GENERAL_EXCEPTION;
                                    if (serviceCall is IPendingServiceCall)
                                        (serviceCall as IPendingServiceCall).Result = StatusASO.GetStatusObject(StatusASO.NC_CONNECT_FAILED, connection.ObjectEncoding);
                                    disconnectOnReturn = true;
                                }
                            }
                            else
                            {
                                // Service calls, must be connected.
                                InvokeCall(connection, serviceCall);
                            }
                        }
                        break;
                    case ACTION_DISCONNECT:
                        connection.Close();
                        break;
                    case ACTION_CREATE_STREAM:
                    case ACTION_DELETE_STREAM:
                    case ACTION_RELEASE_STREAM:
                    case ACTION_PUBLISH:
                    case ACTION_PLAY:
                    case ACTION_SEEK:
                    case ACTION_PAUSE:
                    case ACTION_CLOSE_STREAM:
                    case ACTION_RECEIVE_VIDEO:
                    case ACTION_RECEIVE_AUDIO:
                        {
                            IStreamService streamService = ScopeUtils.GetScopeService(connection.Scope, typeof(IStreamService)) as IStreamService;
                            StatusASO status = null;
                            try
                            {
                                if (!InvokeCall(connection, serviceCall, streamService))
                                {
                                    status = StatusASO.GetStatusObject(StatusASO.NS_INVALID_ARGUMENT, connection.ObjectEncoding);
                                    status.description = "Failed to " + action + " (stream ID: " + header.StreamId + ")";
                                }
                            }
                            catch (Exception ex)
                            {
                                log.Error("Error while invoking " + action + " on stream service.", ex);
                                status = StatusASO.GetStatusObject(StatusASO.NS_FAILED, connection.ObjectEncoding);
                                status.description = "Error while invoking " + action + " (stream ID: " + header.StreamId + ")";
                                status.details = ex.Message;
                            }
                            if (status != null)
                                channel.SendStatus(status);
                        }
                        break;
                    default:
                        if (connection.IsConnected)
                            InvokeCall(connection, serviceCall);
                        else
                        {
                            // Warn user attemps to call service without being connected
                            if (log.IsWarnEnabled)
                                log.Warn("Not connected, closing connection");
                            connection.Close();
                        }
                        break;
                }
            }
            /*
			if(invoke is FlexInvoke) 
			{
				FlexInvoke reply = new FlexInvoke();
				reply.InvokeId = invoke.InvokeId;
				reply.SetResponseSuccess();
				//TODO
				if( serviceCall is IPendingServiceCall )
				{
					IPendingServiceCall pendingCall = (IPendingServiceCall)serviceCall;
					reply.Response = pendingCall.Result;
				}
				channel.Write(reply);
			}
			else if(invoke is Invoke) 
            */
            if (invoke is Invoke) 
			{
				if((header.StreamId != 0)
					&& (serviceCall.Status == Call.STATUS_SUCCESS_VOID || serviceCall.Status == Call.STATUS_SUCCESS_NULL)) 
				{
				    if (log.IsDebugEnabled)
					    log.Debug("Method does not have return value, do not reply");
					return;
				}

				// The client expects a result for the method call.
				Invoke reply = new Invoke();
				reply.ServiceCall = serviceCall;
				reply.InvokeId = invoke.InvokeId;
				//sending reply
				channel.Write(reply);
			}
			if (disconnectOnReturn)
			{
				connection.Close();
			}
            if (action == ACTION_CONNECT)
            {
                connection.Context.ObjectEncoding = connection.ObjectEncoding;
            }
		}
Example #41
0
        /// <summary>
        /// Message recieved.
        /// </summary>
        /// <param name="connection">Connection object.</param>
        /// <param name="obj">Message object.</param>
        public void MessageReceived(RtmpConnection connection, object obj)
        {
            IRtmpEvent message = null;
            RtmpPacket packet = null;
            RtmpHeader header = null;
            RtmpChannel channel = null;
            IClientStream stream = null;
            try
            {
                packet = obj as RtmpPacket;
                message = packet.Message;
                header = packet.Header;
                channel = connection.GetChannel(header.ChannelId);
                if( connection is IStreamCapableConnection )
                    stream = (connection as IStreamCapableConnection).GetStreamById(header.StreamId);

                // Support stream ids
#if !SILVERLIGHT
                FluorineContext.ValidateContext();
                FluorineContext.Current.Connection.SetAttribute(FluorineContext.FluorineStreamIdKey, header.StreamId);
#endif
                // Increase number of received messages
                connection.MessageReceived();

#if !SILVERLIGHT
                if (log != null && log.IsDebugEnabled)
                    log.Debug("RtmpConnection message received, type = " + header.DataType);
#endif

                if (message != null)
                    message.Source = connection;

                switch (header.DataType)
                {
                    case Constants.TypeInvoke:
                        OnInvoke(connection, channel, header, message as Invoke);
                        if (message.Header.StreamId != 0
                            && (message as Invoke).ServiceCall.ServiceName == null
                            && (message as Invoke).ServiceCall.ServiceMethodName == BaseRtmpHandler.ACTION_PUBLISH)
                        {
                            if (stream != null) //Dispatch if stream was created
                                (stream as IEventDispatcher).DispatchEvent(message);
                        }
                        break;
                    case Constants.TypeFlexInvoke:
                        OnFlexInvoke(connection, channel, header, message as FlexInvoke);
                        if (message.Header.StreamId != 0
                            && (message as Invoke).ServiceCall.ServiceName == null
                            && (message as Invoke).ServiceCall.ServiceMethodName == BaseRtmpHandler.ACTION_PUBLISH)
                        {
                            if (stream != null) //Dispatch if stream was created
                                (stream as IEventDispatcher).DispatchEvent(message);
                        }
                        break;
                    case Constants.TypeNotify:// just like invoke, but does not return
                        if ((message as Notify).Data != null && stream != null)
                        {
                            // Stream metadata
                            (stream as IEventDispatcher).DispatchEvent(message);
                        }
                        else
                            OnInvoke(connection, channel, header, message as Notify);
                        break;
                    case Constants.TypePing:
                        OnPing(connection, channel, header, message as Ping);
                        break;
                    case Constants.TypeBytesRead:
                        OnStreamBytesRead(connection, channel, header, message as BytesRead);
                        break;
                    case Constants.TypeSharedObject:
                    case Constants.TypeFlexSharedObject:
                        OnSharedObject(connection, channel, header, message as SharedObjectMessage);
                        break;
                    case Constants.TypeFlexStreamEnd:
                        if (stream != null)
                            (stream as IEventDispatcher).DispatchEvent(message);
                        break;
                    case Constants.TypeChunkSize:
                        OnChunkSize(connection, channel, header, message as ChunkSize);
                        break;
                    case Constants.TypeAudioData:
                    case Constants.TypeVideoData:
                        // NOTE: If we respond to "publish" with "NetStream.Publish.BadName",
                        // the client sends a few stream packets before stopping. We need to
                        // ignore them.
                        if (stream != null)
                            ((IEventDispatcher)stream).DispatchEvent(message);
                        break;
                    case Constants.TypeServerBandwidth:
                        OnServerBW(connection, channel, header, message as ServerBW);
                        break;
                    case Constants.TypeClientBandwidth:
                        OnClientBW(connection, channel, header, message as ClientBW);
                        break;
                    default:
#if !SILVERLIGHT
                        if (log != null && log.IsDebugEnabled)
                            log.Debug("RtmpService event not handled: " + header.DataType);
#endif
                        break;
                }
            }
            catch (Exception ex)
            {
#if !SILVERLIGHT
                if (log.IsErrorEnabled)
                {
                    log.Error(__Res.GetString(__Res.Rtmp_HandlerError), ex);
                    log.Error(__Res.GetString(__Res.Error_ContextDump));
                    //log.Error(Environment.NewLine);
                    log.Error(packet);
                }
#endif
            }
        }
Example #42
0
 private static void SendSOCreationFailed(RtmpConnection connection, string name, bool persistent)
 {
     SharedObjectMessage msg;
     if (connection.ObjectEncoding == ObjectEncoding.AMF0)
         msg = new SharedObjectMessage(name, 0, persistent);
     else
         msg = new FlexSharedObjectMessage(name, 0, persistent);
     msg.AddEvent(new SharedObjectEvent(SharedObjectEventType.CLIENT_STATUS, StatusASO.SO_CREATION_FAILED, "error"));
     connection.GetChannel((byte)3).Write(msg);
 }
Example #43
0
        /// <summary>
        /// Message sent.
        /// </summary>
        /// <param name="connection">Connection object.</param>
        /// <param name="message">Message object.</param>
        public virtual void MessageSent(RtmpConnection connection, object message)
        {
    		if (message is ByteBuffer)
			    return;

		    // Increase number of sent messages
		    connection.MessageSent(message as RtmpPacket);
        }
Example #44
0
        protected override void OnFlexInvoke(RtmpConnection connection, RtmpChannel channel, RtmpHeader header, FlexInvoke invoke)
		{
            IMessage message = null;
            if (invoke.ServiceCall.Arguments != null && invoke.ServiceCall.Arguments.Length > 0)
                message = invoke.ServiceCall.Arguments[0] as IMessage;
			if( message != null )
			{
                MessageBroker messageBroker = this.Endpoint.GetMessageBroker();
				if( message.clientId == null )
				{
					message.clientId = Guid.NewGuid().ToString("D");
					/*
					if( !(message is CommandMessage) )
					{
						//producer may send messages without subscribing
						CommandMessage commandMessageSubscribe = new CommandMessage(CommandMessage.SubscribeOperation);
						commandMessageSubscribe.messageId = Guid.NewGuid().ToString("D");
						commandMessageSubscribe.headers = message.headers.Clone() as Hashtable;
						commandMessageSubscribe.messageRefType = message.GetType().FullName;//"flex.messaging.messages.AsyncMessage"
						commandMessageSubscribe.destination = message.destination;

						IMessage subscribeResponse = messageBroker.RouteMessage(commandMessageSubscribe, _endpoint, connection);
						message.clientId = subscribeResponse.clientId;
					}
					}
					*/
				}
                IMessage response = messageBroker.RouteMessage(message, this.Endpoint);
                invoke.ServiceCall.Status = response is ErrorMessage ? Call.STATUS_INVOCATION_EXCEPTION : Call.STATUS_SUCCESS_RESULT;
                if (invoke.ServiceCall is IPendingServiceCall)
                    (invoke.ServiceCall as IPendingServiceCall).Result = response;

				FlexInvoke reply = new FlexInvoke();
				reply.InvokeId = invoke.InvokeId;
                reply.ServiceCall = invoke.ServiceCall;
                /*
                if( response is ErrorMessage )
                    reply.SetResponseFailure();
                else
				    reply.SetResponseSuccess();
				reply.Response = response;
                */
				channel.Write(reply);
			}
			else
			{
				// If it's a callback for server remote call then pass it over to callbacks handler and return
				OnInvoke(connection, channel, header, invoke);
			}
		}
Example #45
0
 /// <summary>
 /// Connection closed.
 /// </summary>
 /// <param name="connection">Connection object.</param>
 public virtual void ConnectionClosed(RtmpConnection connection)
 {
     connection.Close();
 }
Example #46
0
        /// <summary>
        /// Remoting call invocation handler.
        /// </summary>
        /// <param name="connection">RTMP connection.</param>
        /// <param name="serviceCall">Service call.</param>
        /// <param name="service">Server-side service object.</param>
        /// <returns><code>true</code> if the call was performed, otherwise <code>false</code>.</returns>
	    private static bool InvokeCall(RtmpConnection connection, IServiceCall serviceCall, object service) 
        {
		    IScope scope = connection.Scope;
		    IScopeContext context = scope.Context;
		    if (log.IsDebugEnabled) 
            {
			    log.Debug("Scope: " + scope);
			    log.Debug("Service: " + service);
			    log.Debug("Context: " + context);
		    }
            return context.ServiceInvoker.Invoke(serviceCall, service);
	    }
Example #47
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="streamBytesRead"></param>
 protected void OnStreamBytesRead(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, BytesRead streamBytesRead)
 {
     connection.ReceivedBytesRead(streamBytesRead.Bytes);
 }
Example #48
0
 /// <summary>
 /// This method supports the Fluorine infrastructure and is not intended to be used directly from your code.
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="channel"></param>
 /// <param name="source"></param>
 /// <param name="chunkSize"></param>
 protected abstract void OnChunkSize(RtmpConnection connection, RtmpChannel channel, RtmpHeader source, ChunkSize chunkSize);
Example #49
0
 public virtual void ConnectionClosed(RtmpConnection connection)
 {
     FluorineRtmpContext.Initialize(connection);
     connection.Close();
 }