示例#1
0
文件: socket.cs 项目: ArildF/masters
        //
        // Transform the list socketList such that the only sockets left are those
        // with a file descriptor contained in the array "fileDescriptorArray"
        //
        private static int SelectFileDescriptor(IList socketList, FileDescriptorSet fileDescriptorSet) {
            //
            // Walk the list in order
            // Note that the counter is not necessarily incremented at each step;
            // when the socket is removed, advancing occurs automatically as the
            // other elements are shifted down.
            //
            Socket socket;
            int currentSocket, currentFileDescriptor;

            if (socketList==null) {
                return 0;
            }

            if (fileDescriptorSet.Count==0) {
                //
                // no socket present, will never find it, remove all sockets
                //
                socketList.Clear();
            }

            if (socketList.Count==0) {
                //
                // list is already empty
                //
                return 0;
            }

            lock (socketList) {

                for (currentSocket = 0; currentSocket < socketList.Count; currentSocket++) {
                    //
                    // parameter validation: only Socket should be here
                    //
                    if (!(socketList[currentSocket] is Socket)) {
                        throw new ArgumentException("socketList");
                    }

                    socket = (Socket)socketList[currentSocket];

                    //
                    // Look for the file descriptor in the array
                    //
                    for (currentFileDescriptor = 0; currentFileDescriptor < fileDescriptorSet.Count; currentFileDescriptor++) {
                        if (fileDescriptorSet.Array[currentFileDescriptor]==socket.m_Handle) {
                            break;
                        }
                    }

                    if (currentFileDescriptor==fileDescriptorSet.Count) {
                        //
                        // descriptor not found: remove the current socket and start again
                        //
                        socketList.RemoveAt(currentSocket--);
                    }
                }
            }

            return socketList.Count;
        }
示例#2
0
文件: socket.cs 项目: ArildF/masters
        private static FileDescriptorSet SocketListToFileDescriptorSet(IList socketList) {
            if (socketList==null || socketList.Count==0) {
                return FileDescriptorSet.Empty;
            }
            if (socketList.Count>FileDescriptorSet.Size) {
                throw new ArgumentOutOfRangeException("socketList.Count");
            }

            FileDescriptorSet fileDescriptorSet = new FileDescriptorSet(socketList.Count);

            for (int current = 0; current < fileDescriptorSet.Count; current++) {
                if (!(socketList[current] is Socket)) {
                    throw new ArgumentException(SR.GetString(SR.net_sockets_select, socketList[current].GetType().FullName, typeof(System.Net.Sockets.Socket).FullName));
                }

                fileDescriptorSet.Array[current] = ((Socket)socketList[current]).m_Handle;
            }

            return fileDescriptorSet;
        }
示例#3
0
文件: socket.cs 项目: ArildF/masters
        /*
         * This function servers to isolate the Select/Poll functionality
         * from the representation used by the FileDescriptorSet structure by converting
         * into a flat array of integers.
         * Assumption: the descriptors in the set are always placed as a contigious
         *             block at the beginning of the file descriptor array.
         */
        private static IntPtr[] FileDescriptorSetToFileDescriptorArray(FileDescriptorSet fileDescriptorSet) {
            if (fileDescriptorSet.Count==0) {
                return null;
            }

            IntPtr[] fileDescriptorArray = new IntPtr[fileDescriptorSet.Count];

            for (int current = 0; current < fileDescriptorSet.Count; current++) {
                fileDescriptorArray[current] = fileDescriptorSet.Array[current];
            }

            return fileDescriptorArray;
        }
示例#4
0
文件: socket.cs 项目: ArildF/masters
        /// <include file='doc\Socket.uex' path='docs/doc[@for="Socket.Poll"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Determines the status of the socket.
        ///    </para>
        /// </devdoc>
        public bool Poll(int microSeconds, SelectMode mode) {
            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            FileDescriptorSet fileDescriptorSet = new FileDescriptorSet(1);
            fileDescriptorSet.Array[0] = m_Handle;

            TimeValue IOwait = new TimeValue();

            //
            // negative timeout value implies indefinite wait
            //
            if (microSeconds>=0) {
                MicrosecondsToTimeValue(microSeconds, ref IOwait);
            }

            int errorCode;

            switch (mode) {
                case SelectMode.SelectRead:
                    errorCode = (microSeconds>=0)
                            ? UnsafeNclNativeMethods.OSSOCK.select(
                                0,
                                ref fileDescriptorSet,
                                IntPtr.Zero,
                                IntPtr.Zero,
                                ref IOwait)
                            : UnsafeNclNativeMethods.OSSOCK.select(
                                0,
                                ref fileDescriptorSet,
                                IntPtr.Zero,
                                IntPtr.Zero,
                                IntPtr.Zero);
                    break;
                case SelectMode.SelectWrite:
                    errorCode = (microSeconds>=0)
                            ? UnsafeNclNativeMethods.OSSOCK.select(
                                0,
                                IntPtr.Zero,
                                ref fileDescriptorSet,
                                IntPtr.Zero,
                                ref IOwait)
                            : UnsafeNclNativeMethods.OSSOCK.select(
                                0,
                                IntPtr.Zero,
                                ref fileDescriptorSet,
                                IntPtr.Zero,
                                IntPtr.Zero);
                    break;
                case SelectMode.SelectError:
                    errorCode = (microSeconds>=0)
                            ? UnsafeNclNativeMethods.OSSOCK.select(
                                0,
                                IntPtr.Zero,
                                IntPtr.Zero,
                                ref fileDescriptorSet,
                                ref IOwait)
                            : UnsafeNclNativeMethods.OSSOCK.select(
                                0,
                                IntPtr.Zero,
                                IntPtr.Zero,
                                ref fileDescriptorSet,
                                IntPtr.Zero);
                    break;
                default:
                    throw new NotSupportedException(SR.GetString(SR.net_SelectModeNotSupportedException, mode.ToString()));
            };

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode==SocketErrors.SocketError) {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException();
                UpdateStatusAfterSocketError();
                throw socketException;
            }

            if (fileDescriptorSet.Count==0) {
                return false;
            }

            //return (fileDescriptorSet.Count!=0);

            return fileDescriptorSet.Array[0]==m_Handle;
        }