Esempio 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);
                }
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
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);
                }
            }
        }