// does not reach here.
        /// <summary>
        /// The contract is similar to
        /// <see cref="SocketChannel.Connect(System.Net.EndPoint)"/>
        ///
        /// with a timeout.
        /// </summary>
        /// <seealso cref="SocketChannel.Connect(System.Net.EndPoint)"/>
        /// <param name="channel">
        /// - this should be a
        /// <see cref="SelectableChannel"/>
        /// </param>
        /// <param name="endpoint"/>
        /// <exception cref="System.IO.IOException"/>
        internal static void Connect(SocketChannel channel, EndPoint endpoint, int timeout
                                     )
        {
            bool blockingOn = channel.IsBlocking();

            if (blockingOn)
            {
                channel.ConfigureBlocking(false);
            }
            try
            {
                if (channel.Connect(endpoint))
                {
                    return;
                }
                long timeoutLeft = timeout;
                long endTime     = (timeout > 0) ? (Time.Now() + timeout) : 0;
                while (true)
                {
                    // we might have to call finishConnect() more than once
                    // for some channels (with user level protocols)
                    int ret = selector.Select((SelectableChannel)channel, SelectionKey.OpConnect, timeoutLeft
                                              );
                    if (ret > 0 && channel.FinishConnect())
                    {
                        return;
                    }
                    if (ret == 0 || (timeout > 0 && (timeoutLeft = (endTime - Time.Now())) <= 0))
                    {
                        throw new SocketTimeoutException(TimeoutExceptionString(channel, timeout, SelectionKey
                                                                                .OpConnect));
                    }
                }
            }
            catch (IOException e)
            {
                // javadoc for SocketChannel.connect() says channel should be closed.
                try
                {
                    channel.Close();
                }
                catch (IOException)
                {
                }
                throw;
            }
            finally
            {
                if (blockingOn && channel.IsOpen())
                {
                    channel.ConfigureBlocking(true);
                }
            }
        }
示例#2
0
        protected void beginReceived()
        {
            if (mInnerChannel.IsBlocking)
            {
                mInnerChannel.ConfigureBlocking(false);
            }

            mSelector.Wakeup();
            mInnerChannel.Register(mSelector, Operations.Read, this);
        }
 /// <summary>
 /// Consumes the test server's connection backlog by spamming non-blocking
 /// SocketChannel client connections.
 /// </summary>
 /// <remarks>
 /// Consumes the test server's connection backlog by spamming non-blocking
 /// SocketChannel client connections.  We never do anything with these sockets
 /// beyond just initiaing the connections.  The method saves a reference to each
 /// new SocketChannel so that it can be closed during tearDown.  We define a
 /// very small connection backlog, but the OS may silently enforce a larger
 /// minimum backlog than requested.  To work around this, we create far more
 /// client connections than our defined backlog.
 /// </remarks>
 /// <exception cref="System.IO.IOException">thrown for any I/O error</exception>
 private void ConsumeConnectionBacklog()
 {
     for (int i = 0; i < ClientsToConsumeBacklog; ++i)
     {
         SocketChannel client = SocketChannel.Open();
         client.ConfigureBlocking(false);
         client.Connect(nnHttpAddress);
         clients.AddItem(client);
     }
 }
示例#4
0
        public TcpTunnel(InetSocketAddress serverAddress, Selector selector, short portKey)
        {
            SocketChannel innerChannel = SocketChannel.Open();

            innerChannel.ConfigureBlocking(false);
            mInnerChannel = innerChannel;
            mSelector     = selector;
            mServerEP     = serverAddress;
            this.portKey  = portKey;

            sessionCount++;
        }
示例#5
0
        private void InitializeConnection(Java.Lang.String ipAndPort, InetAddress destinationAddress, int destinationPort,
                                          Packet currentPacket, TCPHeader tcpHeader, ByteBuffer responseBuffer)

        {
            currentPacket.SwapSourceAndDestination();
            if (tcpHeader.isSYN())
            {
                SocketChannel outputChannel = SocketChannel.Open();
                outputChannel.ConfigureBlocking(false);
                vpnService.Protect(outputChannel.Socket());

                TCB tcb = new TCB(ipAndPort, random.NextInt(Short.MaxValue + 1), tcpHeader.sequenceNumber, tcpHeader.sequenceNumber + 1,
                                  tcpHeader.acknowledgementNumber, outputChannel, currentPacket);
                TCB.PutTCB(ipAndPort, tcb);

                try
                {
                    outputChannel.Connect(new InetSocketAddress(destinationAddress, destinationPort));
                    if (outputChannel.FinishConnect())
                    {
                        tcb.status = TCB.TCBStatus.SYN_RECEIVED;
                        // TODO: Set MSS for receiving larger packets from the device
                        currentPacket.updateTCPBuffer(responseBuffer, (byte)(TCPHeader.SYN | TCPHeader.ACK),
                                                      tcb.mySequenceNum, tcb.myAcknowledgementNum, 0);
                        tcb.mySequenceNum++; // SYN counts as a byte
                    }
                    else
                    {
                        tcb.status = TCB.TCBStatus.SYN_SENT;
                        selector.Wakeup();
                        tcb.selectionKey = outputChannel.Register(selector, SelectionKey.OpConnect, tcb);
                        return;
                    }
                }
                catch (IOException e)
                {
                    Log.Error(TAG, "Connection error: " + ipAndPort, e);
                    currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0);
                    TCB.CloseTCB(tcb);
                }
            }
            else
            {
                currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST,
                                              0, tcpHeader.sequenceNumber + 1, 0);
            }

            outputQueue.Offer(responseBuffer);
        }