// To be used by HttpSys internal NativeRequestContext(SafeNativeOverlapped nativeOverlapped, MemoryPool <Byte> memoryPool, uint?bufferSize, ulong requestId) { _nativeOverlapped = nativeOverlapped; // TODO: // Apparently the HttpReceiveHttpRequest memory alignment requirements for non - ARM processors // are different than for ARM processors. We have seen 4 - byte - aligned buffers allocated on // virtual x64/x86 machines which were accepted by HttpReceiveHttpRequest without errors. In // these cases the buffer alignment may cause reading values at invalid offset. Setting buffer // alignment to 0 for now. // // _bufferAlignment = (int)(requestAddress.ToInt64() & 0x07); _bufferAlignment = 0; var newSize = (int)(bufferSize ?? DefaultBufferSize) + AlignmentPadding; if (newSize <= memoryPool.MaxBufferSize) { _backingBuffer = memoryPool.Rent(newSize); } else { // No size limit _backingBuffer = MemoryPool <byte> .Shared.Rent(newSize); } _backingBuffer.Memory.Span.Fill(0);// Zero the buffer _memoryHandle = _backingBuffer.Memory.Pin(); _nativeRequest = (HttpApiTypes.HTTP_REQUEST *)((long)_memoryHandle.Pointer + _bufferAlignment); RequestId = requestId; }
// ReleasePins() should be called exactly once. It must be called before Dispose() is called, which means it must be called // before an object (Request) which closes the RequestContext on demand is returned to the application. internal void ReleasePins() { Debug.Assert(_nativeRequest != null || _backingBuffer == null, "RequestContextBase::ReleasePins()|ReleasePins() called twice."); _originalBufferAddress = (IntPtr)_nativeRequest; _nativeRequest = null; _nativeOverlapped?.Dispose(); _nativeOverlapped = null; }
// To be used by HttpSys internal NativeRequestContext(SafeNativeOverlapped nativeOverlapped, int bufferAlignment, HttpApiTypes.HTTP_REQUEST *nativeRequest, byte[] backingBuffer, ulong requestId) { _nativeOverlapped = nativeOverlapped; _bufferAlignment = bufferAlignment; _nativeRequest = nativeRequest; _backingBuffer = backingBuffer; RequestId = requestId; }
internal static unsafe extern uint CancelIoEx(SafeHandle handle, SafeNativeOverlapped overlapped);