static void CompleteCallback(uint error, uint numBytes, NativeOverlapped *nativeOverlapped)
        {
            // Empty out the AsyncResult ASAP to close the leak window.
            Overlapped        overlapped = Overlapped.Unpack(nativeOverlapped);
            OverlappedContext pThis      = ((RootedHolder)overlapped.AsyncResult).ThisHolder;

            Fx.Assert(pThis != null, "Overlapped.AsyncResult not set. I/O completed multiple times, or cancelled I/O completed.");
            Fx.Assert(object.ReferenceEquals(pThis.overlapped, overlapped), "CompleteCallback completed with corrupt OverlappedContext.overlapped.");
            Fx.Assert(object.ReferenceEquals(pThis.rootedHolder, overlapped.AsyncResult), "CompleteCallback completed with corrupt OverlappedContext.rootedHolder.");
            pThis.rootedHolder.ThisHolder = null;

            Fx.Assert(pThis.bufferPtr == null || pThis.bufferPtr == (byte *)Marshal.UnsafeAddrOfPinnedArrayElement((byte[])pThis.bufferHolder[0], 0),
                      "Buffer moved during bound async operation!");

            // Release the pin.
            pThis.bufferPtr       = null;
            pThis.bufferHolder[0] = OverlappedContext.dummyBuffer;

            OverlappedIOCompleteCallback callback = pThis.pendingCallback;

            pThis.pendingCallback = null;
            Fx.Assert(callback != null, "PendingCallback not set. I/O completed multiple times, or cancelled I/O completed.");

            callback(true, (int)error, checked ((int)numBytes));
        }
示例#2
0
        private static unsafe void CleanupCallback(object state, bool timedOut)
        {
            OverlappedContext context = state as OverlappedContext;

            if (!timedOut)
            {
                context.pinnedTarget = null;
                context.rootedHolder.EventHolder.Close();
                Overlapped.Free(context.nativeOverlapped);
            }
        }
示例#3
0
        private static unsafe void CompleteCallback(uint error, uint numBytes, System.Threading.NativeOverlapped *nativeOverlapped)
        {
            OverlappedContext thisHolder = ((RootedHolder)Overlapped.Unpack(nativeOverlapped).AsyncResult).ThisHolder;

            thisHolder.rootedHolder.ThisHolder = null;
            thisHolder.bufferPtr       = null;
            thisHolder.bufferHolder[0] = dummyBuffer;
            OverlappedIOCompleteCallback pendingCallback = thisHolder.pendingCallback;

            thisHolder.pendingCallback = null;
            pendingCallback(true, (int)error, (int)numBytes);
        }
 public PendingAccept(PipeConnectionListener listener, PipeHandle pipeHandle, bool isBoundToCompletionPort, AsyncCallback callback, object state) : base(callback, state)
 {
     this.pipeHandle              = pipeHandle;
     this.result                  = pipeHandle;
     this.listener                = listener;
     this.onAcceptComplete        = new OverlappedIOCompleteCallback(this.OnAcceptComplete);
     this.overlapped              = new OverlappedContext();
     this.isBoundToCompletionPort = isBoundToCompletionPort;
     if (!Thread.CurrentThread.IsThreadPoolThread)
     {
         if (onStartAccept == null)
         {
             onStartAccept = new Action <object>(PipeConnectionListener.PendingAccept.OnStartAccept);
         }
         ActionItem.Schedule(onStartAccept, this);
     }
     else
     {
         this.StartAccept(true);
     }
 }
        static void CleanupCallback(object state, bool timedOut)
        {
            OverlappedContext pThis = state as OverlappedContext;

            Fx.Assert(pThis != null, "OverlappedContext.CleanupCallback registered wait doesn't have an OverlappedContext as state.");

            if (timedOut)
            {
                Fx.Assert("OverlappedContext.CleanupCallback registered wait timed out.");

                // Turn this into a leak.
                return;
            }

            Fx.Assert(pThis.bufferPtr == null || pThis.bufferPtr == (byte *)Marshal.UnsafeAddrOfPinnedArrayElement((byte[])pThis.bufferHolder[0], 0),
                      "Buffer moved during synchronous deferred cleanup!");

            Fx.Assert(pThis.syncOperationPending, "OverlappedContext.CleanupCallback called with no [....] operation pending.");
            pThis.pinnedTarget = null;
            pThis.rootedHolder.EventHolder.Close();
            Overlapped.Free(pThis.nativeOverlapped);
        }
示例#6
0
        private static unsafe void EventCallback(object state, bool timedOut)
        {
            OverlappedContext context = state as OverlappedContext;

            if (timedOut)
            {
                if ((context == null) || (context.rootedHolder == null))
                {
                    DiagnosticUtility.FailFast("Can't prevent heap corruption.");
                }
                context.rootedHolder.ThisHolder = context;
            }
            else
            {
                context.registration    = null;
                context.bufferPtr       = null;
                context.bufferHolder[0] = dummyBuffer;
                OverlappedIOCompleteCallback pendingCallback = context.pendingCallback;
                context.pendingCallback = null;
                pendingCallback(false, 0, 0);
            }
        }
示例#7
0
 public SocketConnection(Socket socket, ConnectionBufferPool connectionBufferPool, bool autoBindToCompletionPort)
 {
     if (socket == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("socket");
     }
     this.closeState            = CloseState.Open;
     this.exceptionEventType    = TraceEventType.Error;
     this.socket                = socket;
     this.connectionBufferPool  = connectionBufferPool;
     this.readBuffer            = this.connectionBufferPool.Take();
     this.asyncReadBufferSize   = this.readBuffer.Length;
     this.socket.SendBufferSize = this.socket.ReceiveBufferSize = this.asyncReadBufferSize;
     this.sendTimeout           = this.receiveTimeout = TimeSpan.MaxValue;
     this.onReceive             = Fx.ThunkCallback(new AsyncCallback(this.OnReceive));
     this.asyncReadOverlapped   = new OverlappedContext();
     if (autoBindToCompletionPort)
     {
         this.socket.UseOnlyOverlappedIO = false;
     }
     this.TraceSocketInfo(socket, 0x40019, "TraceCodeSocketConnectionCreate", null);
 }
 public SocketConnection(Socket socket, ConnectionBufferPool connectionBufferPool, bool autoBindToCompletionPort)
 {
     if (socket == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("socket");
     }
     this.closeState = CloseState.Open;
     this.exceptionEventType = TraceEventType.Error;
     this.socket = socket;
     this.connectionBufferPool = connectionBufferPool;
     this.readBuffer = this.connectionBufferPool.Take();
     this.asyncReadBufferSize = this.readBuffer.Length;
     this.socket.SendBufferSize = this.socket.ReceiveBufferSize = this.asyncReadBufferSize;
     this.sendTimeout = this.receiveTimeout = TimeSpan.MaxValue;
     this.onReceive = Fx.ThunkCallback(new AsyncCallback(this.OnReceive));
     this.asyncReadOverlapped = new OverlappedContext();
     if (autoBindToCompletionPort)
     {
         this.socket.UseOnlyOverlappedIO = false;
     }
     this.TraceSocketInfo(socket, 0x40019, "TraceCodeSocketConnectionCreate", null);
 }
 public PipeConnection(PipeHandle pipe, int connectionBufferSize, bool isBoundToCompletionPort, bool autoBindToCompletionPort)
 {
     if (pipe == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pipe");
     }
     if (pipe.IsInvalid)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pipe");
     }
     this.closeState = CloseState.Open;
     this.exceptionEventType = TraceEventType.Error;
     this.isBoundToCompletionPort = isBoundToCompletionPort;
     this.autoBindToCompletionPort = autoBindToCompletionPort;
     this.pipe = pipe;
     this.readBufferSize = connectionBufferSize;
     this.writeBufferSize = connectionBufferSize;
     this.readOverlapped = new OverlappedContext();
     this.writeOverlapped = new OverlappedContext();
     this.atEOFEvent = new ManualResetEvent(false);
     this.onAsyncReadComplete = new OverlappedIOCompleteCallback(this.OnAsyncReadComplete);
     this.onAsyncWriteComplete = new OverlappedIOCompleteCallback(this.OnAsyncWriteComplete);
 }
        static void EventCallback(object state, bool timedOut)
        {
            OverlappedContext pThis = state as OverlappedContext;

            Fx.Assert(pThis != null, "OverlappedContext.EventCallback registered wait doesn't have an OverlappedContext as state.");

            if (timedOut)
            {
                Fx.Assert("OverlappedContext.EventCallback registered wait timed out.");

                // Turn this into a leak.  Don't let ourselves get cleaned up - could scratch the heap.
                if (pThis == null || pThis.rootedHolder == null)
                {
                    // We're doomed to do a wild write and corrupt the process.
                    DiagnosticUtility.FailFast("Can't prevent heap corruption.");
                }
                pThis.rootedHolder.ThisHolder = pThis;
                return;
            }

            pThis.registration = null;

            Fx.Assert(pThis.bufferPtr == null || pThis.bufferPtr == (byte *)Marshal.UnsafeAddrOfPinnedArrayElement((byte[])pThis.bufferHolder[0], 0),
                      "Buffer moved during unbound async operation!");

            // Release the pin.
            pThis.bufferPtr       = null;
            pThis.bufferHolder[0] = OverlappedContext.dummyBuffer;

            OverlappedIOCompleteCallback callback = pThis.pendingCallback;

            pThis.pendingCallback = null;
            Fx.Assert(callback != null, "PendingCallback not set. I/O completed multiple times, or cancelled I/O completed.");

            callback(false, 0, 0);
        }
 public PendingAccept(PipeConnectionListener listener, PipeHandle pipeHandle, bool isBoundToCompletionPort, AsyncCallback callback, object state) : base(callback, state)
 {
     this.pipeHandle = pipeHandle;
     this.result = pipeHandle;
     this.listener = listener;
     this.onAcceptComplete = new OverlappedIOCompleteCallback(this.OnAcceptComplete);
     this.overlapped = new OverlappedContext();
     this.isBoundToCompletionPort = isBoundToCompletionPort;
     if (!Thread.CurrentThread.IsThreadPoolThread)
     {
         if (onStartAccept == null)
         {
             onStartAccept = new Action<object>(PipeConnectionListener.PendingAccept.OnStartAccept);
         }
         ActionItem.Schedule(onStartAccept, this);
     }
     else
     {
         this.StartAccept(true);
     }
 }
示例#12
0
            public unsafe PendingAccept(PipeConnectionListener listener, PipeHandle pipeHandle, bool isBoundToCompletionPort,
                AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.pipeHandle = pipeHandle;
                this.result = pipeHandle;
                this.listener = listener;
                onAcceptComplete = new OverlappedIOCompleteCallback(OnAcceptComplete);
                overlapped = new OverlappedContext();
                this.isBoundToCompletionPort = isBoundToCompletionPort;

                if (TD.PipeConnectionAcceptStartIsEnabled())
                {
                    this.eventTraceActivity = new EventTraceActivity();
                    TD.PipeConnectionAcceptStart(this.eventTraceActivity, this.listener.pipeUri != null ? this.listener.pipeUri.ToString() : string.Empty);
                }

                if (!Thread.CurrentThread.IsThreadPoolThread)
                {
                    if (onStartAccept == null)
                    {
                        onStartAccept = new Action<object>(OnStartAccept);
                    }
                    ActionItem.Schedule(onStartAccept, this);
                }
                else
                {
                    StartAccept(true);
                }
            }