示例#1
0
        public void Listen(int backlog)
        {
            int r = listen(Handle, backlog);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }
        }
示例#2
0
        //assigns a name to the socket
        public void Bind(byte[] localEnd)
        {
            int r = bind(Handle, localEnd, (uint)localEnd.Length);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }
        }
示例#3
0
        public UnixSocket()
        {
            //TODO: don't hard-code PF_UNIX and SOCK_STREAM or SocketType.Stream
            //AddressFamily family, SocketType type, ProtocolType proto

            int r = socket(AF_UNIX, SOCK_STREAM, 0);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }

            Handle      = r;
            _ownsHandle = true;
        }
示例#4
0
        public int Write(byte *bufP, int count)
        {
            int r = 0;

            do
            {
                r = (int)write(Handle, bufP, (SizeT)count);
            }while (r < 0 && UnixError.ShouldRetry);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }

            return(r);
        }
示例#5
0
        //TODO: consider memory management
        public void Connect(byte[] remote_end)
        {
            int r = 0;

            do
            {
                r = connect(Handle, remote_end, (uint)remote_end.Length);
            } while (r < 0 && UnixError.ShouldRetry);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }

            Connected = true;
        }
示例#6
0
        public void Connect()
        {
            int r = 0;

            do
            {
                r = connect(Handle, null, 0);
            } while (r < 0 && UnixError.ShouldRetry);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }

            Connected = true;
        }
示例#7
0
        //TODO: consider memory management
        public void Close()
        {
            int r = 0;

            do
            {
                r = close(Handle);
            } while (r < 0 && UnixError.ShouldRetry);

            if (r < 0)
            {
                throw UnixError.GetLastUnixException();
            }

            Handle    = -1;
            Connected = false;
        }
示例#8
0
        public UnixSocket Accept()
        {
            byte[] addr    = new byte[110];
            uint   addrlen = (uint)addr.Length;

            fixed(byte *addrP = addr)
            {
                int r = 0;

                do
                {
                    r = accept(Handle, addrP, ref addrlen);
                } while (r < 0 && UnixError.ShouldRetry);

                if (r < 0)
                {
                    throw UnixError.GetLastUnixException();
                }

                return(new UnixSocket(r, true));
            }
        }
示例#9
0
        public int Read(byte *bufP, int count)
        {
            int r = 0;

            do
            {
                r = (int)read(Handle, bufP, (SizeT)count);
            }while (r < 0 && UnixError.ShouldRetry);

            /* Read was successful */
            if (r >= 0)
            {
                return(r);
            }

            var error = Marshal.GetLastWin32Error();

            if (error == 11)              /* EAGAIN (ignore for blocking sockets) */
            {
                return(-error);
            }
            else if (error >= 103 && error <= 113)             /* Bundle common socket errno identifiers */
            {
                /* ECONNABORTED, ECONNRESET, ENOBUFS, ETIMEDOUT, ... */
                // Console.WriteLine ("IO-Error " + error);
                throw new UnixSocketException(error);
            }
            else if (error == 9)             /* EBADF */
            {
                // Console.WriteLine ("Socket closed: bad file descriptor");
                throw new UnixSocketException(error);
            }
            else
            {
                // Console.WriteLine ("Error " + error);
                throw UnixError.GetLastUnixException();
            }
        }
示例#10
0
 public UnixException(int errno)
     : base($"Error {errno}: {UnixError.GetErrorString(errno)}")
 {
     Errno = errno;
 }