System.Net.Sockets.Socket.InternalSetBlocking C# (CSharp) Method

InternalSetBlocking() private method

private InternalSetBlocking ( bool desired, bool &current ) : SocketError
desired bool
current bool
return SocketError
        private SocketError InternalSetBlocking(bool desired, out bool current)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, $"desired:{desired} willBlock:{_willBlock} willBlockInternal:{_willBlockInternal}");

            if (CleanedUp)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Exit(this, "ObjectDisposed");
                current = _willBlock;
                return SocketError.Success;
            }

            // Can we avoid this call if willBlockInternal is already correct?
            bool willBlock = false;
            SocketError errorCode;
            try
            {
                errorCode = SocketPal.SetBlocking(_handle, desired, out willBlock);
            }
            catch (ObjectDisposedException)
            {
                errorCode = SocketError.NotSocket;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.ioctlsocket returns errorCode:{errorCode}");

            // We will update only internal state but only on successfull win32 call
            // so if the native call fails, the state will remain the same.
            if (errorCode == SocketError.Success)
            {
                _willBlockInternal = willBlock;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"errorCode:{errorCode} willBlock:{_willBlock} willBlockInternal:{_willBlockInternal}");

            current = _willBlockInternal;
            return errorCode;
        }

Same methods

Socket::InternalSetBlocking ( bool desired ) : void

Usage Example

示例#1
0
        //
        // This method is called after an asynchronous call is made for the user,
        // it checks and acts accordingly if the IO:
        // 1) completed synchronously.
        // 2) was pended.
        // 3) failed.
        //
        internal void CheckAsyncCallResult(int status)
        {
            Socket socket = (Socket)AsyncObject;

            switch (status)
            {
            case SocketErrors.Success:
                //
                // the Async IO call completed synchronously:
                //
                break;

            case SocketErrors.WSAEWOULDBLOCK:
                //
                // the Async IO call was pended:
                // Queue our event to the thread pool.
                //
                GlobalLog.Assert(
                    socket.m_AsyncEvent != null,
                    "ConnectAsyncResult: m_AsyncConnectEvent == null", string.Empty);

                ThreadPool.RegisterWaitForSingleObject(
                    socket.m_AsyncEvent,
                    m_ConnectCallback,
                    this,
                    -1,
                    true);

                //
                // we're done, return
                //
                return;

            default:
                //
                // the Async IO call failed:
                // set the Result to the Win32 error
                //
                ErrorCode = status;
                break;
            }
            //
            // cancel async event
            //
            socket.SetAsyncEventSelect(AsyncEventBits.FdNone);
            //
            // go back to blocking mode
            //
            socket.InternalSetBlocking(true);
            if (status == SocketErrors.Success)
            {
                //
                // synchronously complete the IO and call the user's callback.
                //
                InvokeCallback(true);
            }
        }
All Usage Examples Of System.Net.Sockets.Socket::InternalSetBlocking