//
        // This method will be called by us when the IO completes synchronously and
        // by the ThreadPool when the IO completes asynchronously. (only called on WinNT)
        //

        private unsafe static void CompletionPortCallback(uint errorCode, uint numBytes, NativeOverlapped *nativeOverlapped)
        {
            //
            // Create an Overlapped object out of the native pointer we're provided with.
            // (this will NOT free the unmanaged memory in the native overlapped structure)
            //
            Overlapped callbackOverlapped = Overlapped.Unpack(nativeOverlapped);
            //
            // The Overlapped object contains the SocketAsyncResult object
            // that was used for the IO that just completed.
            //
            OverlappedAsyncResult asyncResult = (OverlappedAsyncResult)callbackOverlapped.AsyncResult;

            GlobalLog.Assert(!asyncResult.IsCompleted, "OverlappedAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::CompletionPortCallback() asyncResult.IsCompleted", "");

#if COMNET_PERFLOGGING
            long timer = 0;
            Microsoft.Win32.SafeNativeMethods.QueryPerformanceCounter(out timer);
            Console.WriteLine(timer + ", CompletionPortCallback(" + asyncResult.GetHashCode() + " 0x" + ((long)nativeOverlapped).ToString("X8") + ") numBytes:" + numBytes);
#endif // #if COMNET_PERFLOGGING

            GlobalLog.Print("OverlappedAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::CompletionPortCallback" +
                            " errorCode:" + errorCode.ToString() +
                            " numBytes:" + numBytes.ToString() +
                            " pOverlapped:" + ((int)nativeOverlapped).ToString());

            //
            // complete the IO and invoke the user's callback
            //
            if (errorCode != 0)
            {
                //
                // The Async IO completed with a failure.
                // here we need to call WSAGetOverlappedResult() just so WSAGetLastError() will return the correct error.
                //
                bool success =
                    UnsafeNclNativeMethods.OSSOCK.WSAGetOverlappedResult(
                        ((Socket)asyncResult.AsyncObject).Handle,
                        (IntPtr)nativeOverlapped,
                        out numBytes,
                        false,
                        IntPtr.Zero);

                GlobalLog.Assert(!success, "OverlappedAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::CompletionPortCallback() success: errorCode:" + errorCode + " numBytes:" + numBytes, "");

                errorCode = UnsafeNclNativeMethods.OSSOCK.WSAGetLastError();

                GlobalLog.Assert(errorCode != 0, "OverlappedAsyncResult#" + ValidationHelper.HashString(asyncResult) + "::CompletionPortCallback() errorCode is 0 numBytes:" + numBytes, "");
            }

            //
            // this will release the unmanaged pin handles and unmanaged overlapped ptr
            //
            asyncResult.ReleaseUnmanagedStructures();
            asyncResult.ErrorCode = (int)errorCode;
            asyncResult.InvokeCallback(false, (int)numBytes);
        }
예제 #2
0
파일: socket.cs 프로젝트: ArildF/masters
        /*++

        Routine Description:

           BeginReceive - Async implimentation of Recv call,

           Called when we want to start an async receive.
           We kick off the receive, and if it completes synchronously we'll
           call the callback. Otherwise we'll return an IASyncResult, which
           the caller can use to wait on or retrieve the final status, as needed.

           Uses Winsock 2 overlapped I/O.

        Arguments:

           ReadBuffer - status line that we wish to parse
           Index - Offset into ReadBuffer to begin reading from
           Size - Size of Buffer to recv
           Callback - Delegate function that holds callback, called on completeion of I/O
           State - State used to track callback, set by caller, not required

        Return Value:

           IAsyncResult - Async result used to retreive result

        --*/


        /// <include file='doc\Socket.uex' path='docs/doc[@for="Socket.BeginReceive"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, Object state) {
            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            //
            // parameter validation
            //
            if (buffer==null) {
                throw new ArgumentNullException("buffer");
            }
            if (offset<0 || offset>buffer.Length) {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size<0 || size>buffer.Length-offset) {
                throw new ArgumentOutOfRangeException("size");
            }

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginReceive() size:" + size.ToString());

            //
            // Allocate the async result and the event we'll pass to the
            // thread pool.
            //
            OverlappedAsyncResult asyncResult =
                new OverlappedAsyncResult(
                    this,
                    state,
                    callback );

            //
            // Set up asyncResult for overlapped WSARecv.
            // This call will use
            // completion ports on WinNT and Overlapped IO on Win9x.
            //

            asyncResult.SetUnmanagedStructures(
                                          buffer,
                                          offset,
                                          size,
                                          socketFlags,
                                          null,
                                          false // don't pin null RemoteEP
                                          );

            //
            // Get the Receive going.
            //

            int errorCode =
                UnsafeNclNativeMethods.OSSOCK.WSARecv(
                    m_Handle,
                    ref asyncResult.m_WSABuffer,
                    1,
                    OverlappedAsyncResult.m_BytesTransferred,
                    ref asyncResult.m_Flags,
                    asyncResult.IntOverlapped,
                    IntPtr.Zero );

            if (errorCode!=SocketErrors.Success) {
                errorCode = Marshal.GetLastWin32Error();
            }

#if COMNET_PERFLOGGING
long timer = 0;
Microsoft.Win32.SafeNativeMethods.QueryPerformanceCounter(out timer);
Console.WriteLine(timer + ", Socket#" + this.m_Handle + "::WSARecv(" + asyncResult.GetHashCode() + " 0x" + ((long)asyncResult.IntOverlapped).ToString("X8") + ") returns:" + errorCode);
#endif // #if COMNET_PERFLOGGING

            asyncResult.CheckAsyncCallOverlappedResult(errorCode);

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginReceive() UnsafeNclNativeMethods.OSSOCK.WSARecv returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult));

            //
            // if the asynchronous native call fails synchronously
            // we'll throw a SocketException
            //
            if (asyncResult.ErrorCode!=SocketErrors.Success) {
                //
                // update our internal state after this socket error and throw
                //
                UpdateStatusAfterSocketError();
                throw new SocketException(asyncResult.ErrorCode);
            }

            return asyncResult;
        }