// 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); } } }
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); }
public void onConnectable() { try { if (mInnerChannel.FinishConnect()) { onConnected(); Debug.WriteLine($"Connected to {mServerEP}"); } else { Debug.Fail($"Connect to {mServerEP} failed."); Dispose(); } } catch (Exception ex) { Debug.Fail($"Connect to {mServerEP.ToString()} failed: {ex}"); Dispose(); } }