/// <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); } }
// 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); } } }
public void connect(InetSocketAddress destAddress) { if (VpnServiceHelper.protect(mInnerChannel.Socket())) { mDestAddress = destAddress; mInnerChannel.Register(mSelector, Operations.Connect, this); mInnerChannel.Connect(mServerEP); Debug.WriteLine($"Connecting to {mServerEP}"); } else { throw new Exception("VPN protect socket failed."); } }
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 Options() { var socket = new SocketChannel(_settings.IpAddress, _settings.Port); socket.Connect(); var beaconId = new BeaconId { InternalId = Guid.NewGuid() }; _manager.AddChannel(beaconId, socket); // TODO: Implement more robust request/response configuration HttpContext.Response.Headers.Add("X-Id-Header", IdHeader); HttpContext.Response.Headers.Add("X-Identifier", beaconId.InternalId.ToString()); }
/// <summary> /// Called when [connected]. /// </summary> /// <param name="socket">The socket.</param> /// <returns></returns> public override async Task OnConnected(WebSocket socket) { await base.OnConnected(socket); var socketId = WebSocketManager.GetId(socket); Console.WriteLine($"Beacon connected: {socketId}"); var serverChannel = new SocketChannel(_settings.IpAddress, _settings.Port); serverChannel.Connect(); _socketManager.AddChannel(socketId, serverChannel); Console.WriteLine("Sending connect acknowledgement"); var frame = new WebSocketFrame(FrameType.Connect, socketId.InternalId, new byte[] { 0x00 }); await SendMessageAsync(socketId, frame.Encode()); }