示例#1
0
        /// <summary>
        /// Helper method to set up the socket when calling bind
        /// </summary>
        /// <param name="family">The address family to use</param>
        private void SetupSocket(UnixAddressFamily family)
        {
            if (m_socket != -1)
            {
                throw new InvalidOperationException("The socket is already initialized");
            }

            // Create new socket
            m_socket = Syscall.socket(family, UnixSocketType.SOCK_STREAM, 0);

            // Allow address reuse
            Syscall.setsockopt(m_socket, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_REUSEADDR, 1);

            var opts = Syscall.fcntl(m_socket, FcntlCommand.F_GETFL);

            if (opts < 0)
            {
                throw new IOException($"Failed to get openflags from handle: {Stdlib.GetLastError()}");
            }

            opts |= (int)OpenFlags.O_NONBLOCK;

            if (Syscall.fcntl(m_socket, FcntlCommand.F_SETFL, opts) < 0)
            {
                throw new IOException($"Failed to set socket O_NOBLOCK: {Stdlib.GetLastError()}");
            }

            m_handle = m_handler.MonitoredHandle(m_socket);
        }