Esempio n. 1
0
 private void OnConnectMessageReceived(object sender, ConnectMessageEventArgs e)
 {
     if (e.Message.Operation == CommandOperation.ClientPing)
     {
         _remote = new RtmpProxyRemote(_remoteUri, _serializationContext, ObjectEncoding.Amf3);
         _remote.MessageReceived += OnAsyncMessageReceived;
         _remote.Disconnected += OnServerDisconnected;
         e.Result = _remote.ConnectAckAsync(e.InvokeId, e.ConnectionParameters, false, e.ClientId,e.AuthToken, e.Message).Result;
         if (Connected != null)
             Connected(this, new EventArgs());
     }
     else
     {
         _remote = new RtmpProxyRemote(_remoteUri, _serializationContext, ObjectEncoding.Amf3);
         _remote.MessageReceived += OnAsyncMessageReceived;
         _remote.Disconnected += OnServerDisconnected;
         e.Result = _remote.ReconnectAckAsync(e.InvokeId, e.ConnectionParameters, false, e.ClientId, e.AuthToken, e.Message).Result;
         if (Connected != null)
             Connected(this, new EventArgs());
     }
 }
        async void EventReceivedCallback(object sender, EventReceivedEventArgs e)
        {
            try
            {
                switch (e.Event.MessageType)
                {
                    case MessageType.UserControlMessage:
                        var m = (UserControlMessage)e.Event;
                        if (m.EventType == UserControlMessageType.PingRequest)
                            WriteProtocolControlMessage(new UserControlMessage(UserControlMessageType.PingResponse, m.Values));
                        break;

                    case MessageType.DataAmf3:
#if DEBUG
                        // Have no idea what the contents of these packets are.
                        // Study these packets if we receive them.
                        System.Diagnostics.Debugger.Break();
#endif
                        break;
                    case MessageType.CommandAmf3:
                    case MessageType.DataAmf0:
                    case MessageType.CommandAmf0:
                        var command = (Command)e.Event;
                        var call = command.MethodCall;

                        var param = call.Parameters.Length == 1 ? call.Parameters[0] : call.Parameters;
                        if (call.Name == "_result"||call.Name == "_error"||call.Name == "receive")
                        {
                            //should not happen here
                            throw new InvalidDataException();
                        }
                        else if (call.Name == "onstatus")
                        {
                            System.Diagnostics.Debug.Print("Received status.");
                        }
                        else if (call.Name == "connect")
                        {
                            var message = (CommandMessage)call.Parameters[3];
                            object endpoint;
                            message.Headers.TryGetValue(AsyncMessageHeaders.Endpoint, out endpoint);
                            object id;
                            message.Headers.TryGetValue(AsyncMessageHeaders.ID, out id);
                            //ClientId = (string) id;

                            var args = new ConnectMessageEventArgs((string) call.Parameters[1], (string) call.Parameters[2], message, (string) endpoint, (string) id, command.InvokeId, (AsObject)command.ConnectionParameters);
                            if(ConnectMessageReceived!=null)
                                ConnectMessageReceived(this, args);
                            if(message.Operation==CommandOperation.ClientPing)
                                await InvokeConnectResultAsync(command.InvokeId, (AsObject)args.Result.Body);
                            else
                                await InvokeReconnectResultInvokeAsync(command.InvokeId, (AsObject)args.Result.Body);
                        }
                        else if (param is RemotingMessage)
                        {
                            var message = param as RemotingMessage;

                            object endpoint;
                            message.Headers.TryGetValue(AsyncMessageHeaders.Endpoint, out endpoint);
                            object id;
                            message.Headers.TryGetValue(AsyncMessageHeaders.ID, out id);

                            var args = new RemotingMessageReceivedEventArgs(message, (string) endpoint, (string) id, command.InvokeId);
                            if (RemotingMessageReceived != null)
                                RemotingMessageReceived(this, args);
                            if (args.Error == null)
                                InvokeResult(command.InvokeId, args.Result);
                            else
                                InvokeError(command.InvokeId, args.Error);

                        }
                        else if (param is CommandMessage)
                        {
                            var message = param as CommandMessage;

                            object endpoint;
                            message.Headers.TryGetValue(AsyncMessageHeaders.Endpoint, out endpoint);
                            object id;
                            message.Headers.TryGetValue(AsyncMessageHeaders.ID, out id);

                            var args = new CommandMessageReceivedEventArgs(message, endpoint as string, id as string, command.InvokeId);
                            if (CommandMessageReceived != null)
                                CommandMessageReceived(this, args);
                            InvokeResult(command.InvokeId, args.Result);
                        }
                        else
                        {
#if DEBUG
                            System.Diagnostics.Debug.Print("Unknown RTMP Command: " + call.Name);
                            System.Diagnostics.Debugger.Break();
#endif
                        }
                        break;
                }
            }
            catch(ClientDisconnectedException ex)
            {
                //Close();
            }
        }