Пример #1
0
        private void ProcessConnect(SelectionKey key, List <SelectionKey> keyIterator)
        {
            TCB    tcb             = (TCB)key.Attachment();
            Packet referencePacket = tcb.referencePacket;

            try
            {
                if (tcb.channel.FinishConnect())
                {
                    if (keyIterator[0] != null)
                    {
                        keyIterator.RemoveAt(0);
                    }
                    tcb.status = TCBStatus.SYN_RECEIVED;

                    // TODO: Set MSS for receiving larger packets from the device
                    ByteBuffer responseBuffer = ByteBufferPool.acquire();
                    referencePacket.updateTCPBuffer(responseBuffer, (byte)(Packet.TCPHeader.SYN | Packet.TCPHeader.ACK),
                                                    tcb.mySequenceNum, tcb.myAcknowledgementNum, 0);
                    outputQueue.Offer(responseBuffer);

                    tcb.mySequenceNum++; // SYN counts as a byte
                    //key.InterestOps(Operations.Read);
                    key.InterestOps();
                }
            }
            catch (IOException e)
            {
                Log.Error(TAG, "Connection error: " + tcb.ipAndPort, e);
                ByteBuffer responseBuffer = ByteBufferPool.acquire();
                referencePacket.updateTCPBuffer(responseBuffer, (byte)Packet.TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0);
                outputQueue.Offer(responseBuffer);
                TCB.CloseTCB(tcb);
            }
        }
Пример #2
0
        /// <summary>
        /// Registers this channel with the given selector, returning a selection key.
        ///
        /// <para>  This method first verifies that this channel is open and that the
        /// given initial interest set is valid.
        ///
        /// </para>
        /// <para> If this channel is already registered with the given selector then
        /// the selection key representing that registration is returned after
        /// setting its interest set to the given value.
        ///
        /// </para>
        /// <para> Otherwise this channel has not yet been registered with the given
        /// selector, so the <seealso cref="AbstractSelector#register register"/> method of
        /// the selector is invoked while holding the appropriate locks.  The
        /// resulting key is added to this channel's key set before being returned.
        /// </para>
        /// </summary>
        /// <exception cref="ClosedSelectorException"> {@inheritDoc}
        /// </exception>
        /// <exception cref="IllegalBlockingModeException"> {@inheritDoc}
        /// </exception>
        /// <exception cref="IllegalSelectorException"> {@inheritDoc}
        /// </exception>
        /// <exception cref="CancelledKeyException"> {@inheritDoc}
        /// </exception>
        /// <exception cref="IllegalArgumentException"> {@inheritDoc} </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public final SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException
        public sealed override SelectionKey Register(Selector sel, int ops, Object att)
        {
            lock (RegLock)
            {
                if (!Open)
                {
                    throw new ClosedChannelException();
                }
                if ((ops & ~ValidOps()) != 0)
                {
                    throw new IllegalArgumentException();
                }
                if (Blocking_Renamed)
                {
                    throw new IllegalBlockingModeException();
                }
                SelectionKey k = FindKey(sel);
                if (k != null)
                {
                    k.InterestOps(ops);
                    k.Attach(att);
                }
                if (k == null)
                {
                    // New registration
                    lock (KeyLock)
                    {
                        if (!Open)
                        {
                            throw new ClosedChannelException();
                        }
                        k = ((AbstractSelector)sel).Register(this, ops, att);
                        AddKey(k);
                    }
                }
                return(k);
            }
        }
Пример #3
0
        private void processInput(SelectionKey key, List <SelectionKey> keyIterator)
        {
            System.Console.WriteLine("TCP In");

            try
            {
                if (keyIterator[0] != null)
                {
                    keyIterator.RemoveAt(0);
                }

                ByteBuffer receiveBuffer = ByteBufferPool.acquire();
                // Leave space for the header
                receiveBuffer.Position(HEADER_SIZE);

                TCB tcb = (TCB)key.Attachment();
                lock (tcb)
                {
                    Packet        referencePacket = tcb.referencePacket;
                    SocketChannel inputChannel    = (SocketChannel)key.Channel();
                    int           readBytes;
                    try
                    {
                        readBytes = inputChannel.Read(receiveBuffer);
                    }
                    catch (IOException e)
                    {
                        Log.Error(TAG, "Network read error: " + tcb.ipAndPort, e);
                        referencePacket.updateTCPBuffer(receiveBuffer, (byte)Packet.TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0);
                        outputQueue.Offer(receiveBuffer);
                        TCB.CloseTCB(tcb);
                        return;
                    }

                    if (readBytes == -1)
                    {
                        // End of stream, stop waiting until we push more data
                        //key.InterestOps(0);
                        key.InterestOps();
                        tcb.waitingForNetworkData = false;

                        if (tcb.status != TCBStatus.CLOSE_WAIT)
                        {
                            ByteBufferPool.Release(receiveBuffer);
                            return;
                        }

                        tcb.status = TCBStatus.LAST_ACK;
                        referencePacket.updateTCPBuffer(receiveBuffer, (byte)Packet.TCPHeader.FIN, tcb.mySequenceNum, tcb.myAcknowledgementNum, 0);
                        tcb.mySequenceNum++; // FIN counts as a byte
                    }
                    else
                    {
                        // XXX: We should ideally be splitting segments by MTU/MSS, but this seems to work without
                        referencePacket.updateTCPBuffer(receiveBuffer, (byte)(Packet.TCPHeader.PSH | Packet.TCPHeader.ACK),
                                                        tcb.mySequenceNum, tcb.myAcknowledgementNum, readBytes);
                        tcb.mySequenceNum += readBytes; // Next sequence number
                        receiveBuffer.Position(HEADER_SIZE + readBytes);
                    }
                }

                outputQueue.Offer(receiveBuffer);
            }
            catch (Java.Lang.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }