Esempio n. 1
0
        private void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                m_disposed = true;

                m_inOverlapped.Dispose();
                m_outOverlapped.Dispose();

                // for Windows XP
                if (Environment.OSVersion.Version.Major == 5)
                {
                    UnsafeMethods.CancelIo(Handle);
                }
                else
                {
                    UnsafeMethods.CancelIoEx(Handle, IntPtr.Zero);
                }

                int error = UnsafeMethods.closesocket(Handle);

                if (error != 0)
                {
                    error = Marshal.GetLastWin32Error();
                }


                if (m_remoteAddress != null)
                {
                    m_remoteAddress.Dispose();
                    m_remoteAddress = null;
                }

                if (m_boundAddress != null)
                {
                    m_boundAddress.Dispose();
                    m_boundAddress = null;
                }

                if (m_sendPinnedBuffer != null)
                {
                    m_sendPinnedBuffer.Dispose();
                    m_sendPinnedBuffer = null;
                }

                if (m_receivePinnedBuffer != null)
                {
                    m_receivePinnedBuffer.Dispose();
                    m_receivePinnedBuffer = null;
                }

                if (m_acceptSocketBufferAddress != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(m_acceptSocketBufferAddress);
                }
            }
        }
Esempio n. 2
0
        private void Free()
        {
            Marshal.FreeHGlobal(m_address);

            if (m_handle.IsAllocated)
            {
                m_handle.Free();
            }

            if (m_pinnedBuffer != null)
            {
                m_pinnedBuffer.Dispose();
                m_pinnedBuffer = null;
            }
        }
Esempio n. 3
0
        public void StartOperation(OperationType operationType, byte[] buffer)
        {
            InProgress    = true;
            Success       = false;
            OperationType = operationType;

            if (buffer != null)
            {
                if (m_pinnedBuffer == null)
                {
                    m_pinnedBuffer = new PinnedBuffer(buffer);
                }
                else if (m_pinnedBuffer.Buffer != buffer)
                {
                    m_pinnedBuffer.Switch(buffer);
                }
            }
        }
        public override void Receive(byte[] buffer, int offset, int count, SocketFlags flags)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (m_receivePinnedBuffer == null)
            {
                m_receivePinnedBuffer = new PinnedBuffer(buffer);
            }
            else if (m_receivePinnedBuffer.Buffer != buffer)
            {
                m_receivePinnedBuffer.Switch(buffer);
            }

            m_inOverlapped.StartOperation(OperationType.Receive);

            m_receiveWSABuffer.Pointer = new IntPtr(m_receivePinnedBuffer.Address + offset);
            m_receiveWSABuffer.Length  = count;

            int bytesTransferred;

            SocketError socketError = UnsafeMethods.WSARecv(Handle, ref m_receiveWSABuffer, 1,
                                                            out bytesTransferred, ref flags, m_inOverlapped.Address, IntPtr.Zero);

            if (socketError != SocketError.Success)
            {
                socketError = (SocketError)Marshal.GetLastWin32Error();

                if (socketError != SocketError.IOPending)
                {
                    throw new SocketException((int)socketError);
                }
            }
        }
Esempio n. 5
0
        private void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                m_disposed = true;

                m_inOverlapped.Dispose();
                m_outOverlapped.Dispose();

                UnsafeMethods.CancelIoEx(Handle, IntPtr.Zero);

                int error = UnsafeMethods.closesocket(Handle);

                if (error != 0)
                {
                    error = Marshal.GetLastWin32Error();
                }

                if (m_remoteAddress != null)
                {
                    m_remoteAddress.Dispose();
                    m_remoteAddress = null;
                }

                if (m_boundAddress != null)
                {
                    m_boundAddress.Dispose();
                    m_boundAddress = null;
                }

                if (m_sendPinnedBuffer != null)
                {
                    m_sendPinnedBuffer.Dispose();
                    m_sendPinnedBuffer = null;
                }

                if (m_receivePinnedBuffer != null)
                {
                    m_receivePinnedBuffer.Dispose();
                    m_receivePinnedBuffer = null;
                }

                if (m_acceptSocketBufferAddress != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(m_acceptSocketBufferAddress);
                }
            }
        }
Esempio n. 6
0
        public override void Send(byte[] buffer, int offset, int count, SocketFlags flags)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");

            int bytesTransferred;

            if (m_sendPinnedBuffer == null)
            {
                m_sendPinnedBuffer = new PinnedBuffer(buffer);
            }
            else if (m_sendPinnedBuffer.Buffer != buffer)
            {
                m_sendPinnedBuffer.Switch(buffer);
            }

            m_outOverlapped.StartOperation(OperationType.Send);

            m_sendWSABuffer.Pointer = new IntPtr(m_sendPinnedBuffer.Address + offset);
            m_sendWSABuffer.Length = count;

            SocketError socketError = UnsafeMethods.WSASend(Handle, ref m_sendWSABuffer, 1,
              out bytesTransferred, flags, m_outOverlapped.Address, IntPtr.Zero);

            if (socketError != SocketError.Success)
            {
                socketError = (SocketError)Marshal.GetLastWin32Error();

                if (socketError != SocketError.IOPending)
                {
                    throw new SocketException((int)socketError);
                }
            }
        }
Esempio n. 7
0
        private void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                m_disposed = true;

                m_inOverlapped.Dispose();
                m_outOverlapped.Dispose();

                // for Windows XP
#if NETSTANDARD1_3
                UnsafeMethods.CancelIoEx(Handle, IntPtr.Zero);
#else
                if (false)
                {
                    UnsafeMethods.CancelIo(Handle);
                }
                else
                {
                    UnsafeMethods.CancelIoEx(Handle, IntPtr.Zero);
                }
#endif

                int error = UnsafeMethods.closesocket(Handle);

                if (error != 0)
                {
                    error = Marshal.GetLastWin32Error();
                }


                if (m_remoteAddress != null)
                {
                    m_remoteAddress.Dispose();
                    m_remoteAddress = null;
                }

                if (m_boundAddress != null)
                {
                    m_boundAddress.Dispose();
                    m_boundAddress = null;
                }

                if (m_sendPinnedBuffer != null)
                {
                    m_sendPinnedBuffer.Dispose();
                    m_sendPinnedBuffer = null;
                }

                if (m_receivePinnedBuffer != null)
                {
                    m_receivePinnedBuffer.Dispose();
                    m_receivePinnedBuffer = null;
                }

                if (m_acceptSocketBufferAddress != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(m_acceptSocketBufferAddress);
                }

                if (m_acceptSocket != null)
                {
                    m_acceptSocket.Dispose();
                }
            }
        }