当前位置: 首页>>代码示例>>C#>>正文


C# SocketAsyncEventArgs.StartOperationAccept方法代码示例

本文整理汇总了C#中System.Net.Sockets.SocketAsyncEventArgs.StartOperationAccept方法的典型用法代码示例。如果您正苦于以下问题:C# SocketAsyncEventArgs.StartOperationAccept方法的具体用法?C# SocketAsyncEventArgs.StartOperationAccept怎么用?C# SocketAsyncEventArgs.StartOperationAccept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.SocketAsyncEventArgs的用法示例。


在下文中一共展示了SocketAsyncEventArgs.StartOperationAccept方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AcceptAsync

        public bool AcceptAsync(SocketAsyncEventArgs e)
        {
            bool retval;

            if (s_loggingEnabled)
            {
                Logging.Enter(Logging.Sockets, this, "AcceptAsync", "");
            }

            if (CleanedUp)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (e._bufferList != null)
            {
                throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList");
            }
            if (_rightEndPoint == null)
            {
                throw new InvalidOperationException(SR.net_sockets_mustbind);
            }
            if (!_isListening)
            {
                throw new InvalidOperationException(SR.net_sockets_mustlisten);
            }

            // Handle AcceptSocket property.
            SafeCloseSocket acceptHandle;
            e.AcceptSocket = GetOrCreateAcceptSocket(e.AcceptSocket, true, "AcceptSocket", out acceptHandle);

            // Prepare for the native call.
            e.StartOperationCommon(this);
            e.StartOperationAccept();

            // Local variables for sync completion.
            int bytesTransferred;
            SocketError socketError = SocketError.Success;

            // Make the native call.
            try
            {
                socketError = e.DoOperationAccept(this, _handle, acceptHandle, out bytesTransferred);
            }
            catch (Exception ex)
            {
                // Clear in-use flag on event args object.
                e.Complete();
                throw ex;
            }

            // Handle completion when completion port is not posted.
            if (socketError != SocketError.Success && socketError != SocketError.IOPending)
            {
                e.FinishOperationSyncFailure(socketError, bytesTransferred, SocketFlags.None);
                retval = false;
            }
            else
            {
                retval = true;
            }

            if (s_loggingEnabled)
            {
                Logging.Exit(Logging.Sockets, this, "AcceptAsync", retval);
            }
            return retval;
        }
开发者ID:ReedKimble,项目名称:corefx,代码行数:72,代码来源:Socket.cs

示例2: AcceptAsync

 /// <summary>
 /// Begins an asynchronous operation to accept an incoming connection attempt.
 /// </summary>
 /// 
 /// <returns>
 /// Returns true if the I/O operation is pending. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will be raised upon completion of the operation.Returns false if the I/O operation completed synchronously. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will not be raised and the <paramref name="e"/> object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
 /// </returns>
 /// <param name="e">The <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object to use for this asynchronous socket operation.</param><exception cref="T:System.ArgumentException">An argument is not valid. This exception occurs if the buffer provided is not large enough. The buffer must be at least 2 * (sizeof(SOCKADDR_STORAGE + 16) bytes. This exception also occurs if multiple buffers are specified, the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.BufferList"/> property is not null.</exception><exception cref="T:System.ArgumentOutOfRangeException">An argument is out of range. The exception occurs if the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.Count"/> is less than 0.</exception><exception cref="T:System.InvalidOperationException">An invalid operation was requested. This exception occurs if the accepting <see cref="T:System.Net.Sockets.Socket"/> is not listening for connections or the accepted socket is bound. You must call the <see cref="M:System.Net.Sockets.Socket.Bind(System.Net.EndPoint)"/> and <see cref="M:System.Net.Sockets.Socket.Listen(System.Int32)"/> method before calling the <see cref="M:System.Net.Sockets.Socket.AcceptAsync(System.Net.Sockets.SocketAsyncEventArgs)"/> method.This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified <paramref name="e"/> parameter. </exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information. </exception><exception cref="T:System.NotSupportedException">Windows XP or later is required for this method.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception>
 public bool AcceptAsync(SocketAsyncEventArgs e)
 {
     if (Socket.s_LoggingEnabled)
     Logging.Enter(Logging.Sockets, (object) this, "AcceptAsync", "");
       if (this.CleanedUp)
     throw new ObjectDisposedException(this.GetType().FullName);
       if (e.m_BufferList != null)
     throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
       if (this.m_RightEndPoint == null)
     throw new InvalidOperationException(SR.GetString("net_sockets_mustbind"));
       if (!this.isListening)
     throw new InvalidOperationException(SR.GetString("net_sockets_mustlisten"));
       if (e.AcceptSocket == null)
     e.AcceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType);
       else if (e.AcceptSocket.m_RightEndPoint != null && !e.AcceptSocket.m_IsDisconnected)
     throw new InvalidOperationException(SR.GetString("net_sockets_namedmustnotbebound", new object[1]
     {
       (object) "AcceptSocket"
     }));
       e.StartOperationCommon(this);
       e.StartOperationAccept();
       this.BindToCompletionPort();
       SocketError socketError = SocketError.Success;
       int bytesReceived;
       try
       {
     if (!this.AcceptEx(this.m_Handle, e.AcceptSocket.m_Handle, e.m_PtrSingleBuffer != IntPtr.Zero ? e.m_PtrSingleBuffer : e.m_PtrAcceptBuffer, e.m_PtrSingleBuffer != IntPtr.Zero ? e.Count - e.m_AcceptAddressBufferCount : 0, e.m_AcceptAddressBufferCount / 2, e.m_AcceptAddressBufferCount / 2, out bytesReceived, (SafeHandle) e.m_PtrNativeOverlapped))
       socketError = (SocketError) Marshal.GetLastWin32Error();
       }
       catch (Exception ex)
       {
     e.Complete();
     throw ex;
       }
       bool flag;
       if (socketError != SocketError.Success && socketError != SocketError.IOPending)
       {
     e.FinishOperationSyncFailure(socketError, bytesReceived, SocketFlags.None);
     flag = false;
       }
       else
     flag = true;
       if (Socket.s_LoggingEnabled)
     Logging.Exit(Logging.Sockets, (object) this, "AcceptAsync", (object) (bool) (flag ? 1 : 0));
       return flag;
 }
开发者ID:korifey,项目名称:hackathon-Ideaphone,代码行数:54,代码来源:Socket.cs

示例3: AcceptAsync

        //
        // AcceptAsync
        //        
        public bool AcceptAsync(SocketAsyncEventArgs e) {

            bool retval;

            if(s_LoggingEnabled) Logging.Enter(Logging.Sockets, this, "AcceptAsync", "");
       
            // Throw if socket disposed
            if(CleanedUp) {
               throw new ObjectDisposedException(GetType().FullName);
            }
            
            // Throw if multiple buffers specified.
            if(e.m_BufferList != null) {
                throw new ArgumentException(SR.GetString(SR.net_multibuffernotsupported), "BufferList");
            }
            
            // Throw if not bound.
            if(m_RightEndPoint == null) {
                throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustbind));
            }

            // Throw if not listening.
            if(!isListening) {
                throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustlisten));
            }

            // Handle AcceptSocket property.
            if(e.AcceptSocket == null) {
                // Accept socket not specified - create it.
                e.AcceptSocket = new Socket(addressFamily, socketType, protocolType);
            } else {
                // Validate accept socket for use here.
                if(e.AcceptSocket.m_RightEndPoint != null && !e.AcceptSocket.m_IsDisconnected) {
                    throw new InvalidOperationException(SR.GetString(SR.net_sockets_namedmustnotbebound, "AcceptSocket"));
                }
            }

            // Prepare for the native call.
            e.StartOperationCommon(this);
            e.StartOperationAccept();
            BindToCompletionPort();

            // Local variables for [....] completion.
            int bytesTransferred;
            SocketError socketError = SocketError.Success;

            // Make the native call.
            try {
                if(!AcceptEx(
                        m_Handle,
                        e.AcceptSocket.m_Handle,
                        (e.m_PtrSingleBuffer != IntPtr.Zero) ? e.m_PtrSingleBuffer : e.m_PtrAcceptBuffer,
                        (e.m_PtrSingleBuffer != IntPtr.Zero) ? e.Count - e.m_AcceptAddressBufferCount : 0,
                        e.m_AcceptAddressBufferCount / 2,
                        e.m_AcceptAddressBufferCount / 2,
                        out bytesTransferred,
                        e.m_PtrNativeOverlapped)) {
                    socketError = (SocketError)Marshal.GetLastWin32Error();
                }
            }
            catch (Exception ex) {
                // clear in-use on event arg object 
                e.Complete();
                throw ex;
            }

            // Handle completion when completion port is not posted.
            if(socketError != SocketError.Success && socketError != SocketError.IOPending) {
                e.FinishOperationSyncFailure(socketError, bytesTransferred, SocketFlags.None);
                retval = false;
            } else {
                retval = true;
            }

            if(s_LoggingEnabled) Logging.Exit(Logging.Sockets, this, "AcceptAsync", retval);
            return retval;
        }
开发者ID:REALTOBIZ,项目名称:mono,代码行数:80,代码来源:Socket.cs

示例4: AcceptAsync

 public bool AcceptAsync(SocketAsyncEventArgs e)
 {
     bool flag;
     int num;
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, this, "AcceptAsync", "");
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (e.m_BufferList != null)
     {
         throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
     }
     if (this.m_RightEndPoint == null)
     {
         throw new InvalidOperationException(SR.GetString("net_sockets_mustbind"));
     }
     if (!this.isListening)
     {
         throw new InvalidOperationException(SR.GetString("net_sockets_mustlisten"));
     }
     if (e.AcceptSocket == null)
     {
         e.AcceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType);
     }
     else if ((e.AcceptSocket.m_RightEndPoint != null) && !e.AcceptSocket.m_IsDisconnected)
     {
         throw new InvalidOperationException(SR.GetString("net_sockets_namedmustnotbebound", new object[] { "AcceptSocket" }));
     }
     e.StartOperationCommon(this);
     e.StartOperationAccept();
     this.BindToCompletionPort();
     SocketError success = SocketError.Success;
     try
     {
         if (!this.AcceptEx(this.m_Handle, e.AcceptSocket.m_Handle, (e.m_PtrSingleBuffer != IntPtr.Zero) ? e.m_PtrSingleBuffer : e.m_PtrAcceptBuffer, (e.m_PtrSingleBuffer != IntPtr.Zero) ? (e.Count - e.m_AcceptAddressBufferCount) : 0, e.m_AcceptAddressBufferCount / 2, e.m_AcceptAddressBufferCount / 2, out num, e.m_PtrNativeOverlapped))
         {
             success = (SocketError) Marshal.GetLastWin32Error();
         }
     }
     catch (Exception exception)
     {
         e.Complete();
         throw exception;
     }
     if ((success != SocketError.Success) && (success != SocketError.IOPending))
     {
         e.FinishOperationSyncFailure(success, num, SocketFlags.None);
         flag = false;
     }
     else
     {
         flag = true;
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, this, "AcceptAsync", flag);
     }
     return flag;
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:63,代码来源:Socket.cs


注:本文中的System.Net.Sockets.SocketAsyncEventArgs.StartOperationAccept方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。