internal virtual void Prepare(Socket socket, object asyncState, AsyncCallback asyncCallback) { base.Prepare(socket, asyncState, asyncCallback); /*TO-DO: Recycle these fields: private GCHandle[] m_GCHandles; !!! private OverlappedCache m_Cache; !!! */ this.m_UnmanagedBlob = null; this.m_OverlappedEvent = null; this.m_DisableOverlapped = false; this.m_Cache = null; this.m_GCHandles = null; this.m_UseOverlappedIO = (Socket.UseOverlappedIO || socket.UseOnlyOverlappedIO); this.m_CleanupCount = (m_UseOverlappedIO ? 1 : 2); }
internal void SetUnmanagedStructures(object objectsToPin) { if (!this.m_DisableOverlapped) { object[] array = null; bool alreadyTriedCast = false; bool flag = false; if (this.m_Cache != null) { if (objectsToPin == null && this.m_Cache.PinnedObjects == null) { flag = true; } else { if (this.m_Cache.PinnedObjects != null) { if (this.m_Cache.PinnedObjectsArray == null) { if (objectsToPin == this.m_Cache.PinnedObjects) { flag = true; } } else { if (objectsToPin != null) { alreadyTriedCast = true; array = (objectsToPin as object[]); if (array != null && array.Length == 0) { array = null; } if (array != null && array.Length == this.m_Cache.PinnedObjectsArray.Length) { flag = true; for (int i = 0; i < array.Length; i++) { if (array[i] != this.m_Cache.PinnedObjectsArray[i]) { flag = false; break; } } } } } } } } if (!flag && this.m_Cache != null) { this.m_Cache.Free(); this.m_Cache = null; } Socket socket = (Socket)base.AsyncObject; if (this.m_UseOverlappedIO) { this.m_UnmanagedBlob = SafeOverlappedFree.Alloc(socket.SafeHandle); this.PinUnmanagedObjects(objectsToPin); this.m_OverlappedEvent = new AutoResetEvent(false); Marshal.WriteIntPtr(this.m_UnmanagedBlob.DangerousGetHandle(), OverlappedhEventOffset, this.m_OverlappedEvent.SafeWaitHandle.DangerousGetHandle()); return; } socket.BindToCompletionPort(); if (this.m_Cache == null) { if (array != null) { this.m_Cache = new OverlappedCache(new Overlapped(), array, BaseOverlappedAsyncResult.s_IOCallback); } else { this.m_Cache = new OverlappedCache(new Overlapped(), objectsToPin, BaseOverlappedAsyncResult.s_IOCallback, alreadyTriedCast); } } this.m_Cache.Overlapped.AsyncResult = this; } }
protected virtual void ForceReleaseUnmanagedStructures() { this.ReleaseGCHandles(); GC.SuppressFinalize(this); if (this.m_UnmanagedBlob != null && !this.m_UnmanagedBlob.IsInvalid) { this.m_UnmanagedBlob.Close(true); this.m_UnmanagedBlob = null; } OverlappedCache.InterlockedFree(ref this.m_Cache); if (this.m_OverlappedEvent != null) { this.m_OverlappedEvent.Close(); this.m_OverlappedEvent = null; } }