示例#1
0
        public async Task WriteTrailersAsync(IEnumerable <HeaderField> headers)
        {
            HeaderValidationResult hvr = HeaderValidator.ValidateTrailingHeaders(headers);

            if (hvr != HeaderValidationResult.Ok)
            {
                throw new Exception(hvr.ToString());
            }

            var removeStream = false;

            await writeMutex.WaitAsync();

            try
            {
                lock (stateMutex)
                {
                    if (!dataSent)
                    {
                        throw new Exception("试图在没有数据的情况下写入");
                    }

                    switch (state)
                    {
                    case StreamState.Open:
                        state = StreamState.HalfClosedLocal;
                        break;

                    case StreamState.HalfClosedRemote:
                        state        = StreamState.HalfClosedRemote;
                        state        = StreamState.Closed;
                        removeStream = true;
                        break;

                    case StreamState.Idle:
                    case StreamState.ReservedRemote:
                    case StreamState.HalfClosedLocal:
                    case StreamState.Closed:
                        throw new Exception("发送的状态无效");

                    case StreamState.Reset:
                        throw new StreamResetException();

                    case StreamState.ReservedLocal:

                        throw new Exception("意外状态:发送数据后保留本地");
                    }
                }

                await SendHeaders(headers, true); // TODO: Use result
            }
            finally
            {
                writeMutex.Release();
                if (removeStream)
                {
                    connection.UnregisterStream(this);
                }
            }
        }
示例#2
0
        public async Task WriteTrailersAsync(IEnumerable <HeaderField> headers)
        {
            HeaderValidationResult hvr = HeaderValidator.ValidateTrailingHeaders(headers);

            // TODO: For push promises other validates might need to be used
            if (hvr != HeaderValidationResult.Ok)
            {
                throw new Exception(hvr.ToString());
            }

            var removeStream = false;

            await writeMutex.WaitAsync();

            try
            {
                // Check what already has been sent
                lock (stateMutex)
                {
                    if (!dataSent)
                    {
                        throw new Exception("Attempted to write trailers without data");
                    }

                    switch (state)
                    {
                    case StreamState.Open:
                        state = StreamState.HalfClosedLocal;
                        break;

                    case StreamState.HalfClosedRemote:
                        state        = StreamState.HalfClosedRemote;
                        state        = StreamState.Closed;
                        removeStream = true;
                        break;

                    case StreamState.Idle:
                    case StreamState.ReservedRemote:
                    case StreamState.HalfClosedLocal:
                    case StreamState.Closed:
                        throw new Exception("Invalid state for sending trailers");

                    case StreamState.Reset:
                        throw new StreamResetException();

                    case StreamState.ReservedLocal:
                        // We can't be in here if we already have data sent
                        throw new Exception("Unexpected state: ReservedLocal after data sent");
                    }
                }

                await SendHeaders(headers, true); // TODO: Use result
            }
            finally
            {
                writeMutex.Release();
                if (removeStream)
                {
                    connection.UnregisterStream(this);
                }
            }
        }