Inheritance: System.IO.BinaryWriter
Exemplo n.º 1
0
 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);
     }
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
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);
     
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
 }
Exemplo n.º 6
0
    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;
    }
Exemplo n.º 7
0
 public bool Serialize(H2NBinaryWriter writer)
 {
     writer.Write(_aacLength);
     writer.Write(_pAAC);
     return true;
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
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);
     }
 }
Exemplo n.º 10
0
 public virtual byte PerformHandshake(byte[] tag, H2NBinaryWriter response, IPEndPoint address, byte[] peerIdWanted)
 {
     //throw new NotImplementedException();
     return 0;
 }
Exemplo n.º 11
0
        private bool SaveSeekFile()
        {
            if (_frames.Count <= 2)
            {
                Logger.FATAL("No frames found");
                return false;
            }
            //1. Open the file
            var seekFile= MediaFile.Initialize(_seekFilePath+".tmp",FileMode.Create,FileAccess.Write);
            if (seekFile == null)
            {
                Logger.FATAL("Unable to open seeking file {0}",_seekFilePath);
                return false;
            }
            //2. Setup the bandwidth hint in bytes/second
            var totalSeconds = (_frames[_frames.Count - 1].AbsoluteTime) / 1000.0;
            _streamCapabilities.BandwidthHint =
                    (uint)(MediaFile.Length / totalSeconds / 1024.0 * 8.0);
            var raw = Utils.Rms.GetStream();
            using (var writer = new H2NBinaryWriter(raw))
            {
                if (!_streamCapabilities.Serialize(writer))
                {
                    Logger.FATAL("Unable to serialize stream capabilities");
                    return false;
                }
                seekFile.Bw.Write((uint) raw.Length);
                raw.WriteTo(seekFile.DataStream);
            }
            seekFile.Bw.Write(_frames.Count);
            //3. Write the frames
            var hasVideo = false;
            ulong maxFrameSize = 0;
            foreach (var mediaFrame in _frames)
            {
               
                if (maxFrameSize < mediaFrame.Length) maxFrameSize = mediaFrame.Length;
                hasVideo |= mediaFrame.Type == MediaFrameType.Video;
                seekFile.Bw.Write(mediaFrame.GetBytes());
                //if (seekFile.WriteBuffer(mediaFrame.GetBytes())) continue;
                //Logger.FATAL("Unable to write frame");
                //return false;
            }
            _keyframeSeek &= hasVideo;

            //4. Write the seek granularity
            seekFile.Bw.Write(_seekGranularity);
            //4. create the time to frame index table. First, see what is the total time
            if (_frames.Count >= 1)
            {
                var totalTime = _frames[_frames.Count - 1].AbsoluteTime;

                //5. build the table
                int frameIndex = 0;
                int seekPoint = 0;
                for (double i = 0; i <= totalTime; i += _seekGranularity)
                {
                    while (_frames[frameIndex].AbsoluteTime < i)
                    {
                        frameIndex++;
                        if (frameIndex >= _frames.Count)
                            break;

                        if (_keyframeSeek)
                        {
                            if ((_frames[frameIndex].Type == MediaFrameType.Video)
                                    && (_frames[frameIndex].IsKeyFrame))
                            {
                                seekPoint = frameIndex;
                            }
                        }
                        else
                        {
                            seekPoint = frameIndex;
                        }
                    }
                    if (frameIndex >= _frames.Count) break;
                    seekFile.Bw.Write(seekPoint);
                }
            }
            //6. Save the max frame size
            seekFile.Bw.Write(maxFrameSize);
            seekFile.Bw.Dispose();
            //7. Done
            return true;
        }
Exemplo n.º 12
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;
 }