Exemplo n.º 1
0
        static void Main(string[] args)
        {
            byte[]          bytes    = new VariableInteger(12345);
            VariableInteger integer  = bytes;
            UInt64          uinteger = integer;
            int             size     = VariableInteger.Size(bytes[0]);

            InitialPacket packet = new InitialPacket()
            {
                Version                 = 16,
                SourceConnectionId      = 124,
                DestinationConnectionId = 0,
                PacketNumber            = 777521,
                TokenLength             = 0
            };

            packet = new PacketCreator().CreateInitialPacket(124, 0);

            ConnectionCloseFrame frame     = new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, "The server is too busy to process your request.");
            MaxStreamIdFrame     msidframe = new MaxStreamIdFrame(144123, StreamType.ClientUnidirectional);

            //packet.AttachFrame(frame);
            packet.AttachFrame(msidframe);

            byte[] data = packet.Encode();
            string b64  = ToBase64(data);

            byte[] shpdata1 = new byte[] { 1, 1, 2, 3, 5, 8 };
            byte[] shpdata2 = new byte[] { 13, 21, 34, 55, 89, 144 };

            ShortHeaderPacket shp = new ShortHeaderPacket();

            shp.DestinationConnectionId = 124;
            shp.PacketNumber            = 2;

            shp.AttachFrame(new StreamFrame()
            {
                StreamId = 1, Length = new VariableInteger((UInt64)shpdata2.Length), StreamData = shpdata2, Offset = 6, EndOfStream = true
            });
            shp.AttachFrame(new StreamFrame()
            {
                StreamId = 1, Length = new VariableInteger((UInt64)shpdata1.Length), StreamData = shpdata1, Offset = 0
            });

            string shpb64 = ToBase64(shp.Encode());

            packet.Decode(data);

            byte[] ccfData = frame.Encode();
            frame.Decode(new ByteArray(ccfData));

            byte[]   streamIdData = new StreamId(123, StreamType.ClientUnidirectional);
            StreamId streamId     = streamIdData;

            QuicListener listener = new QuicListener(11000);

            listener.OnClientConnected += Listener_OnClientConnected;
            listener.Start();
        }
Exemplo n.º 2
0
        public ShortHeaderPacket CreateDataPacket(UInt64 streamId, byte[] data)
        {
            ShortHeaderPacket packet = new ShortHeaderPacket();

            packet.PacketNumber            = _ns.Get();
            packet.DestinationConnectionId = (byte)_peerConnectionId;
            packet.AttachFrame(new StreamFrame(streamId, data, 0, true));

            return(packet);
        }
Exemplo n.º 3
0
        public ShortHeaderPacket CreateConnectionClosePacket(ErrorCode code, string reason)
        {
            ShortHeaderPacket packet = new ShortHeaderPacket();

            packet.PacketNumber            = _ns.Get();
            packet.DestinationConnectionId = (byte)_peerConnectionId;
            packet.AttachFrame(new ConnectionCloseFrame(code, reason));

            return(packet);
        }
Exemplo n.º 4
0
        public ShortHeaderPacket CreateDataPacket(UInt64 streamId, byte[] data, UInt64 offset, bool eos)
        {
            ShortHeaderPacket packet = new ShortHeaderPacket(_peerConnectionId.Size);

            packet.PacketNumber            = _ns.Get();
            packet.DestinationConnectionId = (byte)_peerConnectionId;
            packet.AttachFrame(new StreamFrame(streamId, data, offset, eos));

            return(packet);
        }
Exemplo n.º 5
0
        public bool Send(byte[] data)
        {
            if (Type == StreamType.ServerUnidirectional)
            {
                throw new StreamException("Cannot send data on unidirectional stream.");
            }

            _connection.IncrementRate(data.Length);

            int numberOfPackets = (data.Length / QuicSettings.PMTU) + 1;
            int leftoverCarry   = data.Length % QuicSettings.PMTU;

            for (int i = 0; i < numberOfPackets; i++)
            {
                bool eos      = false;
                int  dataSize = QuicSettings.PMTU;
                if (i == numberOfPackets - 1)
                {
                    eos      = true;
                    dataSize = leftoverCarry;
                }

                byte[] buffer = new byte[dataSize];
                Buffer.BlockCopy(data, (Int32)_sendOffset, buffer, 0, dataSize);

                ShortHeaderPacket packet = _connection.PacketCreator.CreateDataPacket(this.StreamId.IntegerValue, buffer, _sendOffset, eos);
                if (i == 0 && data.Length >= QuicSettings.MaxStreamData)
                {
                    packet.AttachFrame(new MaxStreamDataFrame(this.StreamId.IntegerValue, (UInt64)(data.Length + 1)));
                }

                if (_connection.MaximumReached())
                {
                    packet.AttachFrame(new StreamDataBlockedFrame(StreamId.IntegerValue, (UInt64)data.Length));
                }

                _sendOffset += (UInt64)buffer.Length;

                _connection.SendData(packet);
            }

            return(true);
        }
Exemplo n.º 6
0
        public ShortHeaderPacket CreateDataPacket(ulong streamId, byte[] data)
        {
            var packet = new ShortHeaderPacket
            {
                PacketNumber = _ns.Get(), DestinationConnectionId = (byte)_peerConnectionId
            };

            packet.AttachFrame(new StreamFrame(streamId, data, 0, true));
            return(packet);
        }
Exemplo n.º 7
0
        public bool Send(byte[] data)
        {
            if (Type == StreamType.ServerUnidirectional)
            {
                throw new StreamException("Cannot send data on unidirectional stream.");
            }

            _connection.IncrementRate(data.Length);

            ShortHeaderPacket packet = _connection.PacketCreator.CreateDataPacket(this.StreamId.IntegerValue, data);

            if (_connection.MaximumReached())
            {
                packet.AttachFrame(new StreamDataBlockedFrame(StreamId.IntegerValue, (UInt64)data.Length));
            }

            return(_connection.SendData(packet));
        }