void write()
        {
            foreach (var frame in queue.GetConsumingEnumerable())
            {
                if (frame == null)
                {
                    Log.Info("Null frame dequeued");
                    continue;
                }

                Log.Debug("-> {0}", frame);

                var data = AltLinq.ToArray(frame.ToBytes());

                lockWrite.WaitOne();

                try {
                    clientStream.Write(data, 0, data.Length);
                    clientStream.Flush();
                    var stream = streamManager.Get(frame.StreamIdentifier);
                    stream.ProcessSentFrame(frame);
                } catch (Exception ex) {
                    Log.Warn("Error writing frame: {0}, {1}", frame.StreamIdentifier, ex);
                } finally {
                    lockWrite.Release();
                }
            }
        }
Esempio n. 2
0
        public IEnumerable <byte> ToBytes()
        {
            var data = new List <byte> ();

            // Copy Frame Length
            var frameLength = To24BitInt(Length);

            data.AddRange(ByteArrayExtensions.EnsureBigEndian(frameLength));

            // Copy Type
            data.Add((byte)Type);

            // Copy Flags
            data.Add(Flags);

            // 1 Bit reserved as unset (0) so let's take the first bit of the next 32 bits and unset it
            var streamId = Util.ConvertToUInt31(StreamIdentifier);

            data.AddRange(ByteArrayExtensions.EnsureBigEndian(streamId));

            var payloadData = AltLinq.ToArray(Payload);

            // Now the payload
            data.AddRange(payloadData);

            return(data);
        }
Esempio n. 3
0
        public bool Ping(byte[] opaqueData)
        {
            if (opaqueData == null || opaqueData.Length <= 0)
            {
                throw new ArgumentNullException("opaqueData");
            }

            connection.Connect();
            var connectionStream = streamManager.Get(0);
            var semaphoreWait    = new Semaphore(0, 100);
            var opaqueDataMatch  = false;

            Http2Stream.FrameReceivedDelegate frameRxAction;
            frameRxAction = new Http2Stream.FrameReceivedDelegate(frame => {
                var pf = frame as PingFrame;
                if (pf != null)
                {
                    opaqueDataMatch = pf.Ack && pf.OpaqueData != null && AltLinq.SequenceEqual(pf.OpaqueData, opaqueData);
                    semaphoreWait.Release();
                }
            });

            // Wire up the event to listen for ping response
            connectionStream.OnFrameReceived += frameRxAction;

            // Construct ping request
            var pingFrame = new PingFrame();

            pingFrame.OpaqueData = new byte[opaqueData.Length];
            opaqueData.CopyTo(pingFrame.OpaqueData, 0);

            // Send ping
            connection.QueueFrame(pingFrame);

            // Wait for either a ping response or timeout
            semaphoreWait.WaitOne();

            // Cleanup the event
            connectionStream.OnFrameReceived -= frameRxAction;

            return(opaqueDataMatch);
        }