Ejemplo n.º 1
0
        /// <summary>
        /// Replaces the invokeid
        /// </summary>
        /// <param name="notify"></param>
        protected void InternalSend(Notify notify, bool overwriteid)
        {
            if (overwriteid)
            {
                int current = CurrentInvoke.Increment();
                lock (InvokeIds)
                    InvokeIds.Add(current, notify.InvokeId);
                notify.InvokeId = current;
            }

            var buf = RtmpProtocolEncoder.Encode(sourcecontext, CreatePacket(notify));

            if (buf == null)
            {
                StaticLogger.Fatal("Unable to encode " + notify);
            }
            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);
                }
            }
        }
Ejemplo n.º 2
0
 public override void Write(RtmpPacket packet)
 {
     if (base.State != RtmpState.Disconnected)
     {
         lock (base.SyncRoot)
         {
             ByteBuffer buffer;
             try
             {
                 buffer = RtmpProtocolEncoder.Encode(base.Context, packet);
             }
             catch (Exception exception)
             {
                 log.Error("Could not encode message " + packet, exception);
                 return;
             }
             this.WritingMessage(packet);
             this.RawWrite(buffer);
             lock (this._notifyMessages.SyncRoot)
             {
                 this._notifyMessages.Add(packet);
             }
         }
     }
 }
Ejemplo n.º 3
0
 public override void Write(RtmpPacket packet)
 {
     _lock.AcquireReaderLock();
     try {
         if (IsClosed || IsClosing)
         {
             return;
         }
     } finally {
         _lock.ReleaseReaderLock();
     }
     try {
         _lock.AcquireWriterLock();
         ByteBuffer data;
         try {
             data = RtmpProtocolEncoder.Encode(this.Context, packet);
         } catch (Exception ex) {
             log.Error("Could not encode message " + packet, ex);
             return;
         }
         // Mark packet as being written
         WritingMessage(packet);
         if (_pendingMessages == null)
         {
             _pendingMessages = new LinkedList();
         }
         _pendingMessages.Add(new PendingData(data, packet));
     } finally {
         _lock.ReleaseWriterLock();
     }
 }
Ejemplo n.º 4
0
 public override void Write(RtmpPacket packet)
 {
     if (!base.IsDisposed && this.IsActive)
     {
         if (log.get_IsDebugEnabled())
         {
             log.Debug("Write " + packet.Header);
         }
         this.WritingMessage(packet);
         ByteBuffer buf = RtmpProtocolEncoder.Encode(base.Context, packet);
         this.Send(buf);
         this._handler.MessageSent(this, packet);
     }
 }
Ejemplo n.º 5
0
 public override void Write(RtmpPacket packet)
 {
     if (!IsClosed) {
     #if !SILVERLIGHT
         if (log.IsDebugEnabled)
             log.Debug("Write " + packet.Header);
     #endif
         //encode
         WritingMessage(packet);
         ByteBuffer outputStream = RtmpProtocolEncoder.Encode(Context, packet);
         Write(outputStream);
         _handler.MessageSent(this, packet);
     }
 }
Ejemplo n.º 6
0
        protected void SendPacket(Notify notify)
        {
            var buf = RtmpProtocolEncoder.Encode(sourcecontext, CreatePacket(notify));

            if (buf == null)
            {
                //StaticLogger.Fatal("Unable to encode " + notify);
            }
            else
            {
                var buff = buf.ToArray();
                base.Send(buff, 0, buff.Length);
            }
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Replaces the invokeid
        /// </summary>
        /// <param name="notify"></param>
        protected void InternalReceive(Notify notify)
        {
            if (RtmpUtil.IsResult(notify))
            {
                int ourid = notify.InvokeId;

                CallResultWait callresult = null;
                lock (WaitLock)
                {
                    if (WaitInvokeList != null)
                    {
                        int idx = WaitInvokeList.FindIndex(crw => crw.Call.InvokeId == ourid);
                        if (idx != -1)
                        {
                            callresult = WaitInvokeList[idx];
                            WaitInvokeList.RemoveAt(idx);
                        }
                    }
                }

                //InvokeId was found in the waitlist, that means its one of our calls.
                if (callresult != null)
                {
                    callresult.Result = notify;
                    callresult.Wait.Set();

                    //Not blocking, lets send it to the handler instead.
                    if (!callresult.Blocking)
                    {
                        OnCall(callresult.Call, callresult.Result);
                    }

                    return;                     //Return, we don't want LoL receiving the message.
                }

                int theirid;
                lock (InvokeIds)
                {
                    if (!InvokeIds.TryGetValue(ourid, out theirid))
                    {
                        StaticLogger.Error(string.Format("Call id not found for {0}", notify));
                        return;
                    }
                    InvokeIds.Remove(ourid);
                }
                notify.InvokeId = theirid;
            }

            var buf = RtmpProtocolEncoder.Encode(remotecontext, CreatePacket(notify));

            if (buf == null)
            {
                StaticLogger.Fatal("Unable to encode " + notify);
            }
            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);
                }
            }
        }