Exemple #1
0
        private void Reset(uint size)
        {
            if (size == _size)
            {
                return;
            }
            if (_size != 0)
            {
                _overlapped.Dispose();
            }
            _size = size;
            if (size == 0)
            {
                _overlapped    = null;
                _memoryBlob    = null;
                _backingBuffer = null;
                return;
            }
            _backingBuffer = new byte[checked ((int)size)];
            var boundHandle = RequestContext.Server.BoundHandle;

            _overlapped = new SafeNativeOverlapped(boundHandle,
                                                   boundHandle.AllocateNativeOverlapped(IOCallback, this, _backingBuffer));
            _memoryBlob = (UnsafeNclNativeMethods.HttpApi.HTTP_SSL_CLIENT_CERT_INFO *)Marshal.UnsafeAddrOfPinnedArrayElement(_backingBuffer, 0);
        }
 protected void Dispose(bool disposing)
 {
     if (_nativeOverlapped != null)
     {
         Debug.Assert(!disposing, "AsyncRequestContext::Dispose()|Must call ReleasePins() before calling Dispose().");
         _nativeOverlapped.Dispose();
     }
 }
 private void OnReleasePins()
 {
     if (_nativeOverlapped != null)
     {
         SafeNativeOverlapped nativeOverlapped = _nativeOverlapped;
         _nativeOverlapped = null;
         nativeOverlapped.Dispose();
     }
 }
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_overlapped != null)
         {
             _overlapped.Dispose();
         }
         _cancellationRegistration.Dispose();
     }
 }
 public void Dispose()
 {
     if (_overlapped != null)
     {
         _overlapped.Dispose();
     }
     if (_fileStream != null)
     {
         _fileStream.Dispose();
     }
     _cancellationRegistration.Dispose();
 }
        private UnsafeNclNativeMethods.HttpApi.HTTP_REQUEST *Allocate(uint size)
        {
            uint newSize = size != 0 ? size : RequestBuffer == null ? DefaultBufferSize : Size;

            // We can't reuse overlapped objects
            if (_nativeOverlapped != null)
            {
                SafeNativeOverlapped nativeOverlapped = _nativeOverlapped;
                _nativeOverlapped = null;
                nativeOverlapped.Dispose();
            }
            if (_nativeOverlapped == null)
            {
                SetBuffer(checked ((int)newSize));
                var boundHandle = _acceptResult.Server.BoundHandle;
                _nativeOverlapped = new SafeNativeOverlapped(boundHandle,
                                                             boundHandle.AllocateNativeOverlapped(AsyncAcceptContext.IOCallback, _acceptResult, RequestBuffer));
                return((UnsafeNclNativeMethods.HttpApi.HTTP_REQUEST *)Marshal.UnsafeAddrOfPinnedArrayElement(RequestBuffer, 0));
            }
            return(RequestBlob);
        }
Exemple #7
0
        private unsafe CancellationToken CreateDisconnectToken(ulong connectionId)
        {
            // Debug.WriteLine("Server: Registering connection for disconnect for connection ID: " + connectionId);

            // Create a nativeOverlapped callback so we can register for disconnect callback
            var cts = new CancellationTokenSource();

            SafeNativeOverlapped nativeOverlapped = null;

            nativeOverlapped = new SafeNativeOverlapped(_boundHandle, _boundHandle.AllocateNativeOverlapped(
                                                            (errorCode, numBytes, overlappedPtr) =>
            {
                // Debug.WriteLine("Server: http.sys disconnect callback fired for connection ID: " + connectionId);

                // Free the overlapped
                nativeOverlapped.Dispose();

                // Pull the token out of the list and Cancel it.
                ConnectionCancellation token;
                _connectionCancellationTokens.TryRemove(connectionId, out token);
                try
                {
                    cts.Cancel();
                }
                catch (AggregateException exception)
                {
                    LogHelper.LogException(_logger, "CreateDisconnectToken::Disconnected", exception);
                }

                cts.Dispose();
            },
                                                            null, null));

            uint statusCode;

            try
            {
                statusCode = UnsafeNclNativeMethods.HttpApi.HttpWaitForDisconnectEx(requestQueueHandle: _requestQueueHandle,
                                                                                    connectionId: connectionId, reserved: 0, overlapped: nativeOverlapped);
            }
            catch (Win32Exception exception)
            {
                statusCode = (uint)exception.NativeErrorCode;
                LogHelper.LogException(_logger, "CreateDisconnectToken", exception);
            }

            if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING &&
                statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS)
            {
                // We got an unknown result so return a None
                // TODO: return a canceled token?
                return(CancellationToken.None);
            }

            if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS && WebListener.SkipIOCPCallbackOnSuccess)
            {
                // IO operation completed synchronously - callback won't be called to signal completion.
                // TODO: return a canceled token?
                return(CancellationToken.None);
            }

            return(cts.Token);
        }