示例#1
0
文件: Channel.cs 项目: cezary12/kokos
        internal virtual SelectionKeys registerForSelection(Selector selector,
                                                            bool allowInput, bool allowOutput, bool useBlockingOnly)
        {
            Selector.Direction operations = Selector.Direction.NONE;
            if (allowInput)
            {
                operations |= Selector.Direction.READ;
            }

            // do not register for output if there is nothing to write
            bool hasSomeOutgoingFrames =
                channelWriter.hasSomeOutgoingFrames();

            if (allowOutput && hasSomeOutgoingFrames)
            {
                operations |= Selector.Direction.WRITE;
            }

            Socket key = null;

            if (operations != 0)
            {
                try
                {
                    key = connection.register(selector, operations, useBlockingOnly);
                }
                //TODO replace with proper exception
                catch
                //catch (ClosedChannelException ex)
                {
                    // ignore, will never happen
                }
            }

            SelectionKeys keys = new SelectionKeys();

            keys.key = key;
            keys.blockingChannelReadyForReading =
                ((operations & Selector.Direction.READ) != 0) &&
                connection.readingQueue != null &&
                connection.readingQueue.HasDataOrEOF();
            keys.blockingChannelReadyForWriting = (operations & Selector.Direction.WRITE) != 0;

            return(keys);
        }
示例#2
0
            internal virtual Socket register(Selector selector,
                                             Selector.Direction operations, bool useBlockingOnly)
            {
                if (useBlockingOnly)
                {
                    return(null);
                }

                if (connectedChannel != null)
                {
                    selector.Add(connectedChannel, operations);
                    return(connectedChannel);
                }
                else if (datagramChannel != null)
                {
                    selector.Add(datagramChannel, operations);
                    return(datagramChannel);
                }
                else
                {
                    return(null);
                }
            }