예제 #1
0
		internal static void InterlockedFree(ref OverlappedCache overlappedCache)
		{
			OverlappedCache overlappedCache2 = (overlappedCache == null) ? null : Interlocked.Exchange<OverlappedCache>(ref overlappedCache, null);
			if (overlappedCache2 != null)
			{
				overlappedCache2.Free();
			}
		}
예제 #2
0
		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);
		}
예제 #3
0
		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;
			}
		}
예제 #4
0
		internal void ExtractCache(ref OverlappedCache overlappedCache)
		{
			if (!this.m_UseOverlappedIO && !this.m_DisableOverlapped)
			{
				OverlappedCache overlappedCache2 = (this.m_Cache == null) ? null : Interlocked.Exchange<OverlappedCache>(ref this.m_Cache, null);
				if (overlappedCache2 != null)
				{
					if (overlappedCache == null)
					{
						overlappedCache = overlappedCache2;
					}
					else
					{
						OverlappedCache overlappedCache3 = Interlocked.Exchange<OverlappedCache>(ref overlappedCache, overlappedCache2);
						if (overlappedCache3 != null)
						{
							overlappedCache3.Free();
						}
					}
				}
				this.ReleaseUnmanagedStructures();
			}
		}
예제 #5
0
		protected void PinUnmanagedObjects(object objectsToPin)
		{
			if (this.m_Cache != null)
			{
				this.m_Cache.Free();
				this.m_Cache = null;
			}
			if (objectsToPin != null)
			{
				if (objectsToPin.GetType() == typeof(object[]))
				{
					object[] array = (object[])objectsToPin;
					this.m_GCHandles = new GCHandle[array.Length];
					for (int i = 0; i < array.Length; i++)
					{
						if (array[i] != null)
						{
							this.m_GCHandles[i] = GCHandle.Alloc(array[i], GCHandleType.Pinned);
						}
					}
					return;
				}
				this.m_GCHandles = new GCHandle[1];
				this.m_GCHandles[0] = GCHandle.Alloc(objectsToPin, GCHandleType.Pinned);
			}
		}
예제 #6
0
		protected void SetupCache(ref OverlappedCache overlappedCache)
		{
			if (!this.m_UseOverlappedIO && !this.m_DisableOverlapped)
			{
				this.m_Cache = ((overlappedCache == null) ? null : Interlocked.Exchange<OverlappedCache>(ref overlappedCache, null));
				this.m_CleanupCount++;
			}
		}
예제 #7
0
		internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, ref OverlappedCache overlappedCache)
		{
			base.SetupCache(ref overlappedCache);
			this.SetUnmanagedStructures(buffer, offset, size, null, false);
		}