示例#1
0
        protected override void OnReceive(byte[] buffer, int idx, int len)
        {
            byte[] ReceiveBuffer = new byte[len];
            Array.Copy(buffer, idx, ReceiveBuffer, 0, len);

            var objs = RtmpProtocolDecoder.DecodeBuffer(remotecontext, new ByteBuffer(new MemoryStream(ReceiveBuffer)));

            if (objs != null)
            {
                foreach (var obj in objs)
                {
                    RtmpPacket packet = obj as RtmpPacket;
                    if (packet != null)
                    {
                        var result = packet.Message as Notify;
                        if (result != null)
                        {
                            CallResultWait callresult = null;
                            if (RtmpUtil.IsResult(result))
                            {
                                lock (WaitLock)
                                {
                                    int fidx = WaitInvokeList.FindIndex(crw => crw.Call.InvokeId == result.InvokeId);
                                    if (fidx != -1)
                                    {
                                        callresult = WaitInvokeList[fidx];
                                        WaitInvokeList.RemoveAt(fidx);
                                    }
                                }

                                if (callresult != null)
                                {
                                    callresult.Result = result;
                                    callresult.Wait.Set();

                                    if (!callresult.Blocking)
                                    {
                                        OnCall(callresult.Call, callresult.Result);
                                    }
                                }
                                else
                                {
                                    OnNotify(result);
                                }
                            }
                            else
                            {
                                OnNotify(result);
                            }
                        }
                    }
                }
            }
        }
示例#2
0
 public IList Decode(ByteBuffer data)
 {
     if (base.State == RtmpState.Disconnected)
     {
         return(EmptyList);
     }
     this._readBytes += data.Limit;
     this._buffer.Put(data);
     this._buffer.Flip();
     return(RtmpProtocolDecoder.DecodeBuffer(base.Context, this._buffer));
 }
示例#3
0
        public IList Decode(ByteBuffer data)
        {
            _lock.AcquireReaderLock();
            try
            {
                if (IsClosed || IsClosing)
                {
                    return(Internal.EmptyIList);// Already shutting down.
                }
            }
            finally
            {
                _lock.ReleaseReaderLock();
            }
            _readBytes.Increment(data.Limit);
            if (_buffer == null)
            {
                _buffer = ByteBuffer.Allocate(2048);
            }
            _buffer.Put(data);
            _buffer.Flip();
            try
            {
                IList result = RtmpProtocolDecoder.DecodeBuffer(this.Context, _buffer);
                return(result);
            }
            catch (HandshakeFailedException hfe)
            {
#if !SILVERLIGHT
                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("Handshake failed: {0}", hfe.Message));
                }
#endif

                // Clear buffer if something is wrong in protocol decoding.
                _buffer.Clear();
                this.Close();
            }
            catch (Exception ex)
            {
                // Catch any exception in the decoding then clear the buffer to eliminate memory leaks when we can't parse protocol
                // Also close Connection because we can't parse data from it
#if !SILVERLIGHT
                log.Error("Error decoding buffer", ex);
#endif
                // Clear buffer if something is wrong in protocol decoding.
                _buffer.Clear();
                this.Close();
            }
            return(null);
        }
 private void OnReceivedCallback(object state)
 {
     if (log.get_IsDebugEnabled())
     {
         log.Debug(__Res.GetString("Rtmp_SocketReadProcessing", new object[] { base._connectionId }));
     }
     if (log.get_IsDebugEnabled())
     {
         log.Debug("Begin handling packet " + this.ToString());
     }
     try
     {
         List <object> list = RtmpProtocolDecoder.DecodeBuffer(base.Context, this._readBuffer);
         if (base.Context.State == RtmpState.Handshake)
         {
             ByteBuffer src = list[0] as ByteBuffer;
             src.Skip(1);
             src.Compact();
             src.Limit = 0x600;
             ByteBuffer buf = ByteBuffer.Allocate(0x600);
             buf.Put(src);
             this.Send(buf);
             base.Context.State = RtmpState.Connected;
             this._handler.ConnectionOpened(this);
         }
         else if ((list != null) && (list.Count > 0))
         {
             foreach (object obj2 in list)
             {
                 if (obj2 is ByteBuffer)
                 {
                     ByteBuffer buffer3 = obj2 as ByteBuffer;
                     this.Send(buffer3);
                 }
                 else
                 {
                     FluorineRtmpContext.Initialize(this);
                     this._handler.MessageReceived(this, obj2);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         this.HandleError(exception);
     }
     if (log.get_IsDebugEnabled())
     {
         log.Debug("End handling packet " + this.ToString());
     }
     this.BeginReceive(false);
 }
        private void OnReceivedCallback(object state)
        {
#if !SILVERLIGHT
            if (log.IsDebugEnabled)
            {
                log.Debug(__Res.GetString(__Res.Rtmp_SocketReadProcessing, _connectionId));
            }

            if (log.IsDebugEnabled)
            {
                log.Debug("Begin handling packet " + ToString());
            }
#endif

            try
            {
                List <object> result;
                try
                {
                    result = RtmpProtocolDecoder.DecodeBuffer(Context, _readBuffer);
                }
                catch (HandshakeFailedException hfe)
                {
#if !SILVERLIGHT
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(string.Format("Handshake failed: {0}", hfe.Message));
                    }
#endif
                    // Clear buffer if something is wrong in protocol decoding.
                    _readBuffer.Clear();
                    Close();
                    return;
                }
                catch (Exception ex)
                {
                    // Catch any exception in the decoding then clear the buffer to eliminate memory leaks when we can't parse protocol
                    // Also close Connection because we can't parse data from it
#if !SILVERLIGHT
                    log.Error("Error decoding buffer", ex);
#endif
                    // Clear buffer if something is wrong in protocol decoding.
                    _readBuffer.Clear();
                    Close();
                    return;
                }

                if (Context.State == RtmpState.Handshake)
                {
                    ByteBuffer resultBuffer = result[0] as ByteBuffer;
                    //Handshake 3d phase
                    if (resultBuffer != null)
                    {
                        resultBuffer.Skip(1);
                        resultBuffer.Compact();
                        resultBuffer.Limit = RtmpProtocolDecoder.HandshakeSize;
                    }
                    ByteBuffer buffer = ByteBuffer.Allocate(RtmpProtocolDecoder.HandshakeSize);
                    buffer.Put(resultBuffer);
                    Write(buffer);
                    Context.State = RtmpState.Connected;
                    _handler.ConnectionOpened(this);
                }
                else
                {
                    if (result != null && result.Count > 0)
                    {
                        foreach (object obj in result)
                        {
                            if (obj is ByteBuffer)
                            {
                                ByteBuffer buf = obj as ByteBuffer;
                                Write(buf);
                            }
                            else
                            {
#if !SILVERLIGHT
                                FluorineRtmpContext.Initialize(this);
#endif
                                _handler.MessageReceived(this, obj);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
#if !SILVERLIGHT
            if (log.IsDebugEnabled)
            {
                log.Debug("End handling packet " + ToString());
            }
#endif
            //Ready to receive again
            BeginReceive(false);
        }
示例#6
0
        protected override void OnReceive(byte[] buffer, int idx, int len)
        {
            StaticLogger.Trace(string.Format("Recv {0} bytes", len));

            if (logtofiles)
            {
                using (var fs = File.Open("realrecv.dmp", FileMode.Append, FileAccess.Write))
                {
                    fs.Write(buffer, idx, len);
                }
            }

            receivebuffer.Append(buffer, idx, len);

            var objs = RtmpProtocolDecoder.DecodeBuffer(remotecontext, receivebuffer);

            if (objs != null)
            {
                foreach (var obj in objs)
                {
                    var pck = obj as RtmpPacket;
                    if (pck != null)
                    {
                        var result = pck.Message as Notify;
                        if (result != null)
                        {
                            Notify inv = null;
                            if (RtmpUtil.IsResult(result))
                            {
                                lock (InvokeList)
                                {
                                    int fidx = InvokeList.FindIndex(i => i.InvokeId == result.InvokeId);
                                    if (fidx != -1)
                                    {
                                        inv = InvokeList[fidx];
                                        InvokeList.RemoveAt(fidx);
                                    }
                                }

                                if (inv != null)
                                {
                                    OnCall(inv, result);

                                    StaticLogger.Trace(
                                        string.Format(
                                            "Ret  ({0}) (Id:{1})",
                                            string.Join(", ", inv.ServiceCall.Arguments.Select(o => o.ToString())),
                                            pck.Header.ChannelId
                                            )
                                        );
                                }
                            }
                            else
                            {
                                OnNotify(result);
                            }
                        }
                        else
                        {
                            StaticLogger.Trace(string.Format("Recv {0} (Id:{1})", pck.Message, pck.Header.ChannelId));
                        }
                    }
                    else if (obj is ByteBuffer)
                    {
                        //Just handshakes, ignore
                    }
                    else
                    {
                        StaticLogger.Warning(string.Format("Unknown object {0}", obj.GetType()));
                    }

                    if (obj != null && encode)
                    {
                        if (pck != null && pck.Message is Notify)
                        {
                            InternalReceive((Notify)pck.Message);
                            remotecontext.ObjectEncoding = ObjectEncoding.AMF3;
                        }
                        else
                        {
                            var buf = RtmpProtocolEncoder.Encode(remotecontext, obj);
                            if (buf == null)
                            {
                                StaticLogger.Fatal("Unable to encode " + obj);
                            }
                            else
                            {
                                var buff = buf.ToArray();
                                if (logtofiles)
                                {
                                    using (var fs = File.Open("recv.dmp", FileMode.Append, FileAccess.Write))
                                    {
                                        fs.Write(buff, 0, buff.Length);
                                    }
                                }
                                if (encode)
                                {
                                    base.OnReceive(buff, 0, buff.Length);
                                }
                            }
                        }
                    }
                }
            }

            if (!encode)
            {
                base.OnReceive(buffer, idx, len);
            }
        }
示例#7
0
        protected override void OnSend(byte[] buffer, int idx, int len)
        {
            if (postbuffer != null)
            {
                postbuffer.Append(buffer, idx, len);
                if (postbuffer.Length > 4)
                {
                    int num = postbuffer.GetInt();
                    postbuffer.Dispose();
                    postbuffer = null;
                    if (num == 0x504f5354)
                    {
                        StaticLogger.Trace(string.Format("Rejecting POST request", len));
                        Stop();
                        return;
                    }
                }
            }

            StaticLogger.Trace(string.Format("Send {0} bytes", len));

            if (logtofiles)
            {
                using (var fs = File.Open("realsend.dmp", FileMode.Append, FileAccess.Write))
                {
                    fs.Write(buffer, idx, len);
                }
            }

            sendbuffer.Append(buffer, idx, len);

            var objs = RtmpProtocolDecoder.DecodeBuffer(sourcecontext, sendbuffer);

            if (objs != null)
            {
                foreach (var obj in objs)
                {
                    var pck = obj as RtmpPacket;
                    if (pck != null)
                    {
                        var inv = pck.Message as Notify;
                        if (inv != null)
                        {
                            lock (InvokeList)
                            {
                                InvokeList.Add(inv);
                            }
                            StaticLogger.Trace(
                                string.Format("Call {0}({1}) (Id:{2})",
                                              inv.ServiceCall.ServiceMethodName,
                                              string.Join(", ", inv.ServiceCall.Arguments.Select(o => o.ToString())),
                                              pck.Header.ChannelId
                                              )
                                );
                        }
                        else
                        {
                            StaticLogger.Trace(string.Format("Sent {0} (Id:{1})", pck.Message.GetType(), pck.Header.ChannelId));
                        }
                    }
                    else if (obj is ByteBuffer)
                    {
                        //Just handshakes, ignore
                    }
                    else
                    {
                        StaticLogger.Warning(string.Format("Unknown object {0}", obj.GetType()));
                    }

                    if (obj != null && encode)
                    {
                        if (pck != null && pck.Message is Notify)
                        {
                            InternalSend((Notify)pck.Message, true);
                            sourcecontext.ObjectEncoding = ObjectEncoding.AMF3;
                        }
                        else
                        {
                            var buf = RtmpProtocolEncoder.Encode(sourcecontext, obj);
                            if (buf == null)
                            {
                                StaticLogger.Fatal("Unable to encode " + obj);
                            }
                            else
                            {
                                var buff = buf.ToArray();
                                if (logtofiles)
                                {
                                    using (var fs = File.Open("send.dmp", FileMode.Append, FileAccess.Write))
                                    {
                                        fs.Write(buff, 0, buff.Length);
                                    }
                                }
                                if (encode)
                                {
                                    base.OnSend(buff, 0, buff.Length);
                                }
                            }
                        }
                    }
                }
            }

            if (!encode)
            {
                base.OnSend(buffer, idx, len);
            }
        }