Пример #1
0
        public void AcceptInternal(AsyncSocket socket)
        {
            if (m_acceptSocketBufferAddress == IntPtr.Zero)
            {
                m_acceptSocketBufferSize = m_boundAddress.Size + 16;

                m_acceptSocketBufferAddress = Marshal.AllocHGlobal(m_acceptSocketBufferSize << 1);
            }

            int bytesReceived;

            m_acceptSocket = socket as Windows.Socket;

            m_inOverlapped.StartOperation(OperationType.Accept);

            if (!m_acceptEx(Handle, m_acceptSocket.Handle, m_acceptSocketBufferAddress, 0,
                            m_acceptSocketBufferSize,
                            m_acceptSocketBufferSize, out bytesReceived, m_inOverlapped.Address))
            {
                var socketError = (SocketError)Marshal.GetLastWin32Error();

                if (socketError != SocketError.IOPending)
                {
                    throw new SocketException((int)socketError);
                }
            }
            else
            {
                CompletionPort.PostCompletionStatus(m_inOverlapped.Address);
            }
        }
Пример #2
0
        public override void Connect(IPEndPoint endPoint)
        {
            if (m_remoteAddress != null)
            {
                m_remoteAddress.Dispose();
                m_remoteAddress = null;
            }

            m_remoteAddress = new SocketAddress(endPoint.Address, endPoint.Port);

            if (m_boundAddress == null)
            {
                if (endPoint.AddressFamily == AddressFamily.InterNetwork)
                {
                    Bind(new IPEndPoint(IPAddress.Any, 0));
                }
                else
                {
                    Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
                }
            }

            int bytesSend;

            m_outOverlapped.StartOperation(OperationType.Connect);

            if (m_connectEx(Handle, m_remoteAddress.Buffer, m_remoteAddress.Size, IntPtr.Zero, 0,
                            out bytesSend, m_outOverlapped.Address))
            {
                CompletionPort.PostCompletionStatus(m_outOverlapped.Address);
            }
            else
            {
                SocketError socketError = (SocketError)Marshal.GetLastWin32Error();

                if (socketError != SocketError.IOPending)
                {
                    throw new SocketException((int)socketError);
                }
            }
        }