本文整理匯總了C#中System.Threading.Overlapped類的典型用法代碼示例。如果您正苦於以下問題:C# Overlapped類的具體用法?C# Overlapped怎麽用?C# Overlapped使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Overlapped類屬於System.Threading命名空間,在下文中一共展示了Overlapped類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: CreateToken
/// <summary>
/// Creates a <see cref="CancellationTokenSource"/> for the given <paramref name="connectionId"/> and registers it for disconnect.
/// </summary>
/// <param name="connectionId">The connection id.</param>
/// <returns>A <see cref="CancellationTokenSource"/> that is registered for disconnect for the connection associated with the <paramref name="connectionId"/>.</returns>
public CancellationToken CreateToken(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 overlapped = new Overlapped();
var cts = new CancellationTokenSource();
var nativeOverlapped = overlapped.UnsafePack((errorCode, numBytes, pOVERLAP) =>
{
Debug.WriteLine("Server: http.sys disconnect callback fired for connection ID: " + connectionId);
// Free the overlapped
Overlapped.Free(pOVERLAP);
// Pull the token out of the list and Cancel it.
Lazy<CancellationToken> token;
_connectionCancellationTokens.TryRemove(connectionId, out token);
cts.Cancel();
},
null);
uint hr = NativeMethods.HttpWaitForDisconnect(_requestQueueHandle, connectionId, nativeOverlapped);
if (hr != NativeMethods.HttpErrors.ERROR_IO_PENDING &&
hr != NativeMethods.HttpErrors.NO_ERROR)
{
// We got an unknown result so throw
Debug.WriteLine("Unable to register disconnect callback");
return CancellationToken.None;
}
return cts.Token;
}
示例2: BeginRead
internal unsafe IAsyncResult BeginRead(byte[] data, int offset, int size, AsyncCallback callback, object state)
{
bool flag;
PipeAsyncResult ar = new PipeAsyncResult(callback);
NativeOverlapped* lpOverlapped = new Overlapped(0, 0, IntPtr.Zero, ar).UnsafePack(IOCallback, data);
ar._overlapped = lpOverlapped;
fixed (byte* numRef = data)
{
flag = NativePipe.ReadFile(this._handle, numRef + offset, size, IntPtr.Zero, lpOverlapped);
}
if (!flag)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode == 0x6dL)
{
ar.CallUserCallback();
return ar;
}
if (errorCode != 0x3e5L)
{
throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ReadFailure"), new object[] { GetMessage(errorCode) }));
}
}
return ar;
}
示例3: OverlappedContext
public OverlappedContext()
{
if (OverlappedContext.completeCallback == null)
{
OverlappedContext.completeCallback = Fx.ThunkCallback(new IOCompletionCallback(CompleteCallback));
}
if (OverlappedContext.eventCallback == null)
{
OverlappedContext.eventCallback = Fx.ThunkCallback(new WaitOrTimerCallback(EventCallback));
}
if (OverlappedContext.cleanupCallback == null)
{
OverlappedContext.cleanupCallback = Fx.ThunkCallback(new WaitOrTimerCallback(CleanupCallback));
}
this.bufferHolder = new object[] { OverlappedContext.dummyBuffer };
this.overlapped = new Overlapped();
this.nativeOverlapped = this.overlapped.UnsafePack(OverlappedContext.completeCallback, this.bufferHolder);
// When replacing the buffer, we need to provoke the CLR to fix up the handle of the pin.
this.pinnedHandle = GCHandle.FromIntPtr(*((IntPtr*)nativeOverlapped +
(IntPtr.Size == 4 ? HandleOffsetFromOverlapped32 : HandleOffsetFromOverlapped64)));
this.pinnedTarget = this.pinnedHandle.Target;
// Create the permanently rooted holder and put it in the Overlapped.
this.rootedHolder = new RootedHolder();
this.overlapped.AsyncResult = rootedHolder;
}
示例4: OverlappedCache
internal unsafe OverlappedCache(Overlapped overlapped, object[] pinnedObjectsArray, IOCompletionCallback callback)
{
this.m_Overlapped = overlapped;
this.m_PinnedObjects = pinnedObjectsArray;
this.m_PinnedObjectsArray = pinnedObjectsArray;
this.m_NativeOverlapped = new SafeNativeOverlapped((IntPtr)((void*)overlapped.UnsafePack(callback, pinnedObjectsArray)));
}
示例5: OverlappedCache
internal OverlappedCache(System.Threading.Overlapped overlapped, object[] pinnedObjectsArray, IOCompletionCallback callback)
{
this.m_Overlapped = overlapped;
this.m_PinnedObjects = pinnedObjectsArray;
this.m_PinnedObjectsArray = pinnedObjectsArray;
this.m_NativeOverlapped = new SafeNativeOverlapped(overlapped.UnsafePack(callback, pinnedObjectsArray));
}
示例6: Unpack
unsafe public static Overlapped Unpack (NativeOverlapped *nativeOverlappedPtr)
{
if ((IntPtr) nativeOverlappedPtr == IntPtr.Zero)
throw new ArgumentNullException ("nativeOverlappedPtr");
Overlapped result = new Overlapped ();
result.offsetL = nativeOverlappedPtr->OffsetLow;
result.offsetH = nativeOverlappedPtr->OffsetHigh;
result.evt = (int)nativeOverlappedPtr->EventHandle;
return result;
}
示例7: HttpResponseStreamAsyncResult
internal unsafe HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback, byte[] buffer, int offset, int size, bool chunked, bool sentHeaders) : base(asyncObject, userState, callback)
{
this.m_SentHeaders = sentHeaders;
Overlapped overlapped = new Overlapped {
AsyncResult = this
};
if (size == 0)
{
this.m_DataChunks = null;
this.m_pOverlapped = overlapped.Pack(s_IOCallback, null);
}
else
{
this.m_DataChunks = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK[chunked ? 3 : 1];
object[] userData = new object[1 + this.m_DataChunks.Length];
userData[this.m_DataChunks.Length] = this.m_DataChunks;
int num = 0;
byte[] arr = null;
if (chunked)
{
arr = ConnectStream.GetChunkHeader(size, out num);
this.m_DataChunks[0] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[0].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[0].BufferLength = (uint) (arr.Length - num);
userData[0] = arr;
this.m_DataChunks[1] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[1].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[1].BufferLength = (uint) size;
userData[1] = buffer;
this.m_DataChunks[2] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[2].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[2].BufferLength = (uint) NclConstants.CRLF.Length;
userData[2] = NclConstants.CRLF;
}
else
{
this.m_DataChunks[0] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[0].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[0].BufferLength = (uint) size;
userData[0] = buffer;
}
this.m_pOverlapped = overlapped.Pack(s_IOCallback, userData);
if (chunked)
{
this.m_DataChunks[0].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(arr, num);
this.m_DataChunks[1].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
this.m_DataChunks[2].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(NclConstants.CRLF, 0);
}
else
{
this.m_DataChunks[0].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
}
}
}
開發者ID:pritesh-mandowara-sp,項目名稱:DecompliedDotNetLibraries,代碼行數:54,代碼來源:HttpResponseStreamAsyncResult.cs
示例8: AsyncFileStream_AsyncResult
internal AsyncFileStream_AsyncResult( AsyncCallback userCallback, Object stateObject, bool isWrite )
{
m_userCallback = userCallback;
m_userStateObject = stateObject;
m_waitHandle = new ManualResetEvent( false );
m_isWrite = isWrite;
Overlapped overlapped = new Overlapped( 0, 0, IntPtr.Zero, this );
m_overlapped = overlapped.Pack( s_callback, null );
}
示例9: InternalFree
private void InternalFree()
{
this.m_Overlapped = null;
this.m_PinnedObjects = null;
if (this.m_NativeOverlapped != null)
{
if (!this.m_NativeOverlapped.IsInvalid)
{
this.m_NativeOverlapped.Dispose();
}
this.m_NativeOverlapped = null;
}
}
示例10: ReInitialize
internal void ReInitialize()
{
this.m_asyncResult = null;
this.m_iocb = null;
this.m_iocbHelper = null;
this.m_overlapped = null;
this.m_userObject = null;
this.m_pinSelf = IntPtr.Zero;
this.m_userObjectInternal = IntPtr.Zero;
this.m_AppDomainId = 0;
this.m_nativeOverlapped.EventHandle = IntPtr.Zero;
this.m_isArray = 0;
this.m_nativeOverlapped.InternalHigh = IntPtr.Zero;
}
示例11: CreateToken
private unsafe CancellationToken CreateToken(ulong connectionId)
{
// Create a nativeOverlapped callback so we can register for disconnect callback
var overlapped = new Overlapped();
var cts = new CancellationTokenSource();
CancellationToken returnToken = cts.Token;
NativeOverlapped* nativeOverlapped = overlapped.UnsafePack(
(errorCode, numBytes, overlappedPtr) =>
{
// Free the overlapped
Overlapped.Free(overlappedPtr);
if (errorCode != NativeMethods.HttpErrors.NO_ERROR)
{
LogHelper.LogException(_logger, "IOCompletionCallback", new Win32Exception((int)errorCode));
}
// Pull the token out of the list and Cancel it.
ConnectionCancellation cancellation;
_connectionCancellationTokens.TryRemove(connectionId, out cancellation);
bool success = ThreadPool.UnsafeQueueUserWorkItem(CancelToken, cts);
Debug.Assert(success, "Unable to queue disconnect notification.");
},
null);
uint hr = NativeMethods.HttpWaitForDisconnect(_requestQueueHandle, connectionId, nativeOverlapped);
if (hr != NativeMethods.HttpErrors.ERROR_IO_PENDING &&
hr != NativeMethods.HttpErrors.NO_ERROR)
{
// We got an unknown result, assume the connection has been closed.
Overlapped.Free(nativeOverlapped);
ConnectionCancellation cancellation;
_connectionCancellationTokens.TryRemove(connectionId, out cancellation);
LogHelper.LogException(_logger, "HttpWaitForDisconnect", new Win32Exception((int)hr));
cts.Cancel();
cts.Dispose();
}
return returnToken;
}
示例12: OverlappedContext
public unsafe OverlappedContext()
{
if (completeCallback == null)
{
completeCallback = Fx.ThunkCallback(new IOCompletionCallback(OverlappedContext.CompleteCallback));
}
if (eventCallback == null)
{
eventCallback = Fx.ThunkCallback(new WaitOrTimerCallback(OverlappedContext.EventCallback));
}
if (cleanupCallback == null)
{
cleanupCallback = Fx.ThunkCallback(new WaitOrTimerCallback(OverlappedContext.CleanupCallback));
}
this.bufferHolder = new object[] { dummyBuffer };
this.overlapped = new Overlapped();
this.nativeOverlapped = this.overlapped.UnsafePack(completeCallback, this.bufferHolder);
this.pinnedHandle = GCHandle.FromIntPtr(*((IntPtr*) (this.nativeOverlapped + (((IntPtr.Size == 4) ? -4 : -3) * sizeof(IntPtr)))));
this.pinnedTarget = this.pinnedHandle.Target;
this.rootedHolder = new RootedHolder();
this.overlapped.AsyncResult = this.rootedHolder;
}
示例13: Reset
internal void Reset(uint size) {
if (size == m_Size)
{
return;
}
if (m_Size != 0)
{
Overlapped.Free(m_pOverlapped);
}
m_Size = size;
if (size == 0)
{
m_pOverlapped = null;
m_MemoryBlob = null;
m_BackingBuffer = null;
return;
}
m_BackingBuffer = new byte[checked((int) size)];
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = this;
m_pOverlapped = overlapped.Pack(s_IOCallback, m_BackingBuffer);
m_MemoryBlob = (UnsafeNclNativeMethods.HttpApi.HTTP_SSL_CLIENT_CERT_INFO*) Marshal.UnsafeAddrOfPinnedArrayElement(m_BackingBuffer, 0);
}
示例14: SetupOverlappedMultiple
// Method to setup an Overlapped object with with multiple buffers pinned.
unsafe private void SetupOverlappedMultiple() {
ArraySegment<byte>[] tempList = new ArraySegment<byte>[m_BufferList.Count];
m_BufferList.CopyTo(tempList, 0);
// Alloc new Overlapped.
m_Overlapped = new Overlapped();
// Number of things to pin is number of buffers.
// Ensure we have properly sized object array.
if(m_ObjectsToPin == null || (m_ObjectsToPin.Length != tempList.Length)) {
m_ObjectsToPin = new object[tempList.Length];
}
// Fill in object array.
for(int i = 0; i < (tempList.Length); i++) {
m_ObjectsToPin[i] = tempList[i].Array;
}
if(m_WSABufferArray == null || m_WSABufferArray.Length != tempList.Length) {
m_WSABufferArray = new WSABuffer[tempList.Length];
}
// Pin buffers and fill in WSABuffer descriptor pointers and lengths
#if SOCKETTHREADPOOL
m_Overlapped.AsyncResult = new DummyAsyncResult(CompletionPortCallback);
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(null, m_ObjectsToPin));
#else
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(CompletionPortCallback, m_ObjectsToPin));
#endif
for(int i = 0; i < tempList.Length; i++) {
ArraySegment<byte> localCopy = tempList[i];
ValidationHelper.ValidateSegment(localCopy);
m_WSABufferArray[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(localCopy.Array, localCopy.Offset);
m_WSABufferArray[i].Length = localCopy.Count;
}
m_PinState = PinState.MultipleBuffer;
}
示例15: SetupOverlappedSingle
// Method to setup an Overlapped object with either m_Buffer or m_AcceptBuffer pinned.
unsafe private void SetupOverlappedSingle(bool pinSingleBuffer) {
// Alloc new Overlapped.
m_Overlapped = new Overlapped();
// Pin buffer, get native pointers, and fill in WSABuffer descriptor.
if(pinSingleBuffer) {
if(m_Buffer != null) {
#if SOCKETTHREADPOOL
m_Overlapped.AsyncResult = new DummyAsyncResult(CompletionPortCallback);
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(null, m_Buffer));
#else
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(CompletionPortCallback, m_Buffer));
#endif
m_PinnedSingleBuffer = m_Buffer;
m_PinnedSingleBufferOffset = m_Offset;
m_PinnedSingleBufferCount = m_Count;
m_PtrSingleBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(m_Buffer, m_Offset);
m_PtrAcceptBuffer = IntPtr.Zero;
m_WSABuffer.Pointer = m_PtrSingleBuffer;
m_WSABuffer.Length = m_Count;
m_PinState = PinState.SingleBuffer;
} else {
#if SOCKETTHREADPOOL
m_Overlapped.AsyncResult = new DummyAsyncResult(CompletionPortCallback);
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(null, null));
#else
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(CompletionPortCallback, null));
#endif
m_PinnedSingleBuffer = null;
m_PinnedSingleBufferOffset = 0;
m_PinnedSingleBufferCount = 0;
m_PtrSingleBuffer = IntPtr.Zero;
m_PtrAcceptBuffer = IntPtr.Zero;
m_WSABuffer.Pointer = m_PtrSingleBuffer;
m_WSABuffer.Length = m_Count;
m_PinState = PinState.NoBuffer;
}
} else {
#if SOCKETTHREADPOOL
m_Overlapped.AsyncResult = new DummyAsyncResult(CompletionPortCallback);
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(null, m_AcceptBuffer));
#else
m_PtrNativeOverlapped = new SafeNativeOverlapped(m_Overlapped.UnsafePack(CompletionPortCallback, m_AcceptBuffer));
#endif
m_PinnedAcceptBuffer = m_AcceptBuffer;
m_PtrAcceptBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(m_AcceptBuffer, 0);
m_PtrSingleBuffer = IntPtr.Zero;
m_PinState = PinState.SingleAcceptBuffer;
}
}