Esempio n. 1
0
 public void Write(uint time, Stream data, bool unbuffered, bool first, int length = 0)
 {
     //Logger.INFO(_type == 0x09 ? "Video timestamp : {0}" : "Audio timestamp : {0}", time);
     if (unbuffered)
     {
         if (data.Position >= 5)
         {
             data.Position -= 5;
             data.WriteByte(_type);
             data.Write(time);
             WriteUnbufferedMessage(data as MemoryStream, data as MemoryStream);
         }
     }
     if (first)
     {
         Flush();
         _firstTime = time;
         _out       = WriterRawMessage(true);
         _out.Write(_type);
         _out.Write(time);
     }
     if (time >= _firstTime)
     {
         data.CopyDataTo(_out.BaseStream, length);
     }
 }
Esempio n. 2
0
        public static void EncodeAndPack(AESEngine aesEncrypt, H2NBinaryWriter writer, uint farId, int ignore = 0)
        {
            var s = writer.BaseStream;

            if (aesEncrypt.Type != AESEngine.AESType.EMPTY)
            {
                var paddingBytesLength = (0xFFFFFFFF - (int)s.Length + ignore + 5) & 0x0F;
                s.Position = s.Length;
                for (var i = 0; i < paddingBytesLength; i++)
                {
                    writer.Write((byte)0xFF);
                }
                //writer.Write(Enumerable.Repeat((byte)0xFF, (int) paddingBytesLength).ToArray());
            }
            //writeCRC
            s.Position = 6 + ignore;
            var sum = CheckSum(s);

            s.Position = 4 + ignore;
            writer.Write(sum);
            //writeCRC end
            s.Position = 4 + ignore;

            aesEncrypt.Process(s);
            //pack
            s.Position = 4 + ignore;
            var result = s.ReadUInt() ^ s.ReadUInt() ^ farId;

            s.Position = ignore;
            writer.Write(result);
        }
Esempio n. 3
0
        public void CreateCookie(H2NBinaryWriter writer, HelloAttempt attempt, byte[] tag, string queryUrl)
        {
            var cookie = attempt.Cookie;

            if (cookie == null)
            {
                cookie = new Cookie(this, tag, queryUrl);
                _cookies[cookie.Value.BytesToString()] = cookie;
                attempt.Cookie = cookie;
            }
            writer.Write(CookieComputing.COOKIE_SIZE);
            writer.Write(cookie.Value);
        }
        public void Send(ClusterMessageType type, Action <H2NBinaryWriter> writeAction)
        {
            var ms = Utils.Rms.GetStream();

            using (var writer = new H2NBinaryWriter(ms))
            {
                writer.Write((byte)type);
                writer.Write((ushort)0);
                writeAction(writer);
                var length = ms.Length - 3;
                ms.Position = 1;
                writer.Write((ushort)length);
                EnqueueForOutbound(ms);
            }
        }
    public bool Serialize(H2NBinaryWriter writer)
    {
        writer.Write(Utils.MakeTag("VER3"));
        writer.Write((byte)VideoCodecId);
        writer.Write((byte)AudioCodecId);
        writer.Write(BandwidthHint);

        if (VideoCodecId == VideoCodec.H264 && !Avc.Serialize(writer))
        {
            Logger.FATAL("Unable to serialize avc");
            return(false);
        }
        if (AudioCodecId == AudioCodec.Aac && !Aac.Serialize(writer))
        {
            Logger.FATAL("Unable to serialize avc");
            return(false);
        }
        return(true);
    }
Esempio n. 6
0
        public void Write(uint time, Stream data, bool unbuffered)
        {
            //Logger.INFO(_type == 0x09 ? "Video timestamp : {0}" : "Audio timestamp : {0}", time);
            if (unbuffered)
            {
                if (data.Position >= 5)
                {
                    data.Position -= 5;
                    var writer = new H2NBinaryWriter(data);
                    writer.Write(_type);
                    writer.Write(time);
                    WriteUnbufferedMessage(data as MemoryStream, data as MemoryStream);
                }
            }
            var _out = WriterRawMessage(true);

            _out.Write(_type);
            _out.Write(time);
            data.CopyDataTo(_out.BaseStream);
        }
Esempio n. 7
0
        public void Write()
        {
            if (_writer.BaseStream.Length == 0)
            {
                _writer.Write(Id);
                if (Target != null)
                {
                    _writer.Write7BitLongValue((ulong)Value.Length);

                    _writer.Write(Value);

                    _writer.Write7BitValue((uint)CookieComputing.Nonce.Length);

                    _writer.Write(CookieComputing.Nonce);

                    _writer.Write7BitValue((uint)CookieComputing.InitiatorNonce.Length);

                    _writer.Write(CookieComputing.InitiatorNonce);
                }
                else
                {
                    _writer.Write7BitLongValue((ulong)CookieComputing.Nonce.Length);

                    _writer.Write(CookieComputing.Nonce);
                }

                _writer.Write((byte)0x58);
            }
        }
Esempio n. 8
0
 public void Flush(H2NBinaryWriter writer, ulong stage, byte flags, bool header, N2HBinaryReader reader, ushort size)
 {
     Debug.WriteLine("sent:{0} stage {1}", Id, stage);
     if (_stageAck == 0 && header)
     {
         flags |= MESSAGE_HEADER;
     }
     if (size == 0)
     {
         flags |= MESSAGE_ABANDONMENT;
     }
     if (Closed && _messages.Count == 1)
     {
         flags |= MESSAGE_END;
     }
     writer.Write(flags);
     if (header)
     {
         writer.Write7BitLongValue(Id);
         writer.Write7BitLongValue(stage);
         writer.Write7BitLongValue(stage - _stageAck);
         if (_stageAck == 0)
         {
             writer.WriteString8(Signature);
             if (FlowId > 0)
             {
                 writer.Write((byte)(1 + H2NBinaryWriter.Get7BitValueSize(FlowId)));
                 writer.Write((byte)0x0a);
                 writer.Write7BitLongValue(FlowId);
             }
             writer.Write((byte)0);
         }
     }
     if (size > 0)
     {
         reader.BaseStream.CopyPartTo(writer.BaseStream, size);
     }
 }
 public bool Serialize(H2NBinaryWriter writer)
 {
     writer.Write(SpsLength);
     writer.Write(SPS);
     writer.Write(PpsLength);
     writer.Write(PPS);
     writer.Write(_widthOverride);
     writer.Write(_heightOverride);
     return(true);
 }
Esempio n. 10
0
        public override byte PerformHandshake(byte[] tag, H2NBinaryWriter response, IPEndPoint address, byte[] peerIdWanted)
        {
            var peerIdWantedStr = peerIdWanted.BytesToString();
            var sessionWanted   = Sessions.Values.SingleOrDefault(x => x.Peer.IdStr == peerIdWantedStr);

            if (_pCirrus != null)
            {
                var session = Sessions.Values.SingleOrDefault(x => x.Peer.Address.Equals(address));
                if (session == null)
                {
                    FATAL("UDP Hole punching error : middle equivalence not found for session wanted");
                    return(0);
                }
                var request = (session as Middle).Handshaker;
                request.Write((byte)0x22);
                request.Write((byte)0x21);
                request.Write((byte)0x0F);
                request.Write(sessionWanted == null ? peerIdWanted : (session as Middle).Peer.Id, 0, 0x20);
                request.Write(tag);
                return(0);
            }
            if (sessionWanted == null)
            {
                Debug("UDP Hole punching : session {0} wanted not found", peerIdWantedStr);
                var addresses = new List <IPEndPoint>();
                if (addresses.Count == 0)
                {
                    return(0);
                }
                var first = true;
                foreach (var _address in addresses)
                {
                    response.WriteAddress(_address, first);
                    first = false;
                }
                return(0x71);
            }
            if (sessionWanted.Failed())
            {
                Debug("UDP Hole punching : session wanted is deleting");
                return(0);
            }
            byte result = 0x00;

            if (_middle)
            {
                if (sessionWanted.Target != null)
                {
                    var attempt = HandShakeSession.GetHelloAttempt <HelloAttempt>(tag.BytesToString());
                    attempt.Target = sessionWanted.Target;
                    HandShakeSession.CreateCookie(response, attempt, tag, "");
                    response.Write(sessionWanted.Target.PublicKey);
                    result = 0x70;
                }
                else
                {
                    FATAL("Peer/peer dumped exchange impossible : no corresponding 'Target' with the session wanted");
                }
            }
            if (result == 0x00)
            {
                /// Udp hole punching normal process
                var times = sessionWanted.GetHelloAttempt <Attempt>(tag.BytesToString()).Count;
                sessionWanted.P2PHandshake(address, tag, times, (times > 0 || address.Address.Equals(sessionWanted.Peer.Address.Address)) ? Sessions.Values.SingleOrDefault(x => x.Peer.Address.Equals(address)) : null);
                var first = true;
                foreach (var ipEndPoint in sessionWanted.Peer.Addresses)
                {
                    if (ipEndPoint.Equals(address))
                    {
                        WARN("A client tries to connect to himself (same {0} address)", address);
                    }
                    response.WriteAddress(ipEndPoint, first);
                    Debug("P2P address initiator exchange, {0}:{1}", ipEndPoint.Address, ipEndPoint.Port);
                    first = false;
                }
                result = 0x71;
            }
            return(result);
        }
 public bool Serialize(H2NBinaryWriter writer)
 {
     writer.Write(_aacLength);
     writer.Write(_pAAC);
     return(true);
 }