Esempio n. 1
0
 void TinyCLRFdSetToIntPtr(TinyCLRSockets.fd_set fd_set, IntPtr fds)
 {
     if (fds != IntPtr.Zero)
     {
         Marshal.StructureToPtr(fd_set, fds, true);
     }
 }
Esempio n. 2
0
        bool TinyCLRFdSetToFdSet(TinyCLRSockets.fd_set fd_setsrc, out List <Socket> fd_setDst)
        {
            bool fRes = true;

            fd_setDst = null;

            if (fd_setsrc != null)
            {
                fd_setDst = new List <Socket>();

                for (int i = 0; i < fd_setsrc.count; i++)
                {
                    SocketData sd;

                    if (!GetSocketData(fd_setsrc.fd_array[i], out sd))
                    {
                        fRes = false;
                        break;
                    }

                    if (sd.IsSelectable)
                    {
                        fd_setDst.Add(sd.Socket);
                    }
                }
            }

            return(fRes);
        }
Esempio n. 3
0
        TinyCLRSockets.fd_set IntPtrToTinyCLRFdSet(IntPtr fds)
        {
            TinyCLRSockets.fd_set fd_set = null;

            if (fds != IntPtr.Zero)
            {
                fd_set = new TinyCLRSockets.fd_set();

                Marshal.PtrToStructure(fds, fd_set);
            }

            return(fd_set);
        }
Esempio n. 4
0
        bool FilterFd_set(List <Socket> fdNative, TinyCLRSockets.fd_set fdCLR)
        {
            if (fdNative != null)
            {
                Debug.Assert(fdCLR != null);

                for (int i = 0; i < fdCLR.count;)
                {
                    bool   fSet = false;
                    Socket sock = null;

                    if (!GetSocket(fdCLR.fd_array[i], out sock))
                    {
                        return(false);
                    }

                    for (int j = 0; j < fdNative.Count; j++)
                    {
                        if (fdNative[j] == sock)
                        {
                            fSet = true;
                            break;
                        }
                    }

                    if (!fSet)
                    {
                        Array.Copy(fdCLR.fd_array, i + 1, fdCLR.fd_array, i, fdCLR.count - i - 1);
                        fdCLR.count--;
                    }
                    else
                    {
                        i++;
                    }
                }
            }

            return(true);
        }
Esempio n. 5
0
        int ISocketsDriver.select(int nfds, IntPtr readfds, IntPtr writefds, IntPtr exceptfds, ref TimeVal timeout)
        {
            List <Socket> readList;
            List <Socket> writeList;
            List <Socket> exceptList;

            int ret = 0;

            TinyCLRSockets.fd_set read   = IntPtrToTinyCLRFdSet(readfds);
            TinyCLRSockets.fd_set write  = IntPtrToTinyCLRFdSet(writefds);
            TinyCLRSockets.fd_set except = IntPtrToTinyCLRFdSet(exceptfds);

            if (!TinyCLRFdSetToFdSet(read, out readList))
            {
                return(ReturnError(SocketError.NotSocket));
            }
            if (!TinyCLRFdSetToFdSet(write, out writeList))
            {
                return(ReturnError(SocketError.NotSocket));
            }
            if (!TinyCLRFdSetToFdSet(except, out exceptList))
            {
                return(ReturnError(SocketError.NotSocket));
            }

            if ((readList != null && readList.Count > 0) ||
                (writeList != null && writeList.Count > 0) ||
                (exceptList != null && exceptList.Count > 0))
            {
                try
                {
                    //Socket.Select(readList, writeList, exceptList, timeout.tv_usec);
                    InternalSelect(readList, writeList, exceptList);
                }
                catch (SocketException se)
                {
                    ret = ReturnError(se.SocketErrorCode);
                }
            }

            if (!FilterFd_set(readList, read))
            {
                return(ReturnError(SocketError.NotSocket));
            }
            if (!FilterFd_set(writeList, write))
            {
                return(ReturnError(SocketError.NotSocket));
            }
            if (!FilterFd_set(exceptList, except))
            {
                return(ReturnError(SocketError.NotSocket));
            }

            TinyCLRFdSetToIntPtr(read, readfds);
            TinyCLRFdSetToIntPtr(write, writefds);
            TinyCLRFdSetToIntPtr(except, exceptfds);

            if (ret != (int)SocketError.SocketError)
            {
                if (readList != null)
                {
                    ret += readList.Count;
                }
                if (writeList != null)
                {
                    ret += writeList.Count;
                }
                if (exceptList != null)
                {
                    ret += exceptList.Count;
                }
            }

            return(ret);
        }