private void InvokePendingOperations() { while (_operations.Count > 0) { AsyncOperation operation = _operations.Poll(); operation.Invoke(); } }
private void ExecuteHeartbeatCallbacks() { Runnable callback = null; while ((callback = heartbeatCallbacks.Poll()) != null) { callback.Run(); } }
public static ByteBuffer acquire() { var buffer = (ByteBuffer)pool.Poll(); if (buffer == null) { buffer = ByteBuffer.AllocateDirect(BUFFER_SIZE); // Using DirectBuffer for zero-copy } return(buffer); }
public virtual void SetLastHeartbeatTime(long timestamp) { lastHeartbeatTime = timestamp; Runnable callback = null; while ((callback = callbacks.Poll()) != null) { callback.Run(); } }
public TCompletion PollCompletion() { var continuation = Interlocked.Exchange(ref single, null); if (continuation == null && completed != null) { continuation = completed.Poll(); } return(continuation); }
public void Release(int count) { VerboseLog("{0:000}|{1}|about to release {2}; available permits {3}", Thread.CurrentThread.Id, _id, count, _sem.AvailablePermits()); if (count == 0) { return; } // Note: we are not being fair here. we always prefer async waiters. if (_asyncWaiters != null) { AsyncWaiter waiter; while (count > 0 && (waiter = _asyncWaiters.Poll()) != null) { CleanUpWaiter(waiter); if (waiter.Task.Task.IsCompleted) { VerboseLog("{0:000}|{1}|found completed async waiter", Thread.CurrentThread.Id, _id); continue; } VerboseLog("{0:000}|{1}|releasing async waiter: {2:X}; {3}", Thread.CurrentThread.Id, _id, waiter.GetHashCode(), waiter.Task.Task.Id); waiter.Task.SetResult(true); count -= 1; VerboseLog("{0:000}|{1}|async waiter released. remaining count: {2}; available permits: {3}", Thread.CurrentThread.Id, _id, count, _sem.AvailablePermits()); } } if (count == 0) { return; } _sem.Release(count); VerboseLog("{0:000}|{1}|released semaphore(s). Available: {2}", Thread.CurrentThread.Id, _id, _sem.AvailablePermits()); }
public bool TryGetNextCompletion(out TCompletion continuation) { continuation = null; if (single != null) { continuation = single; single = null; } else { if (completed != null) { continuation = completed.Poll(); } } return(continuation != null); }
public void Run() { Log.Info(TAG, "Started"); try { Thread currentThread = Thread.CurrentThread(); while (true) { Packet currentPacket; // TODO: Block when not connected do { currentPacket = (Packet)inputQueue.Poll(); if (currentPacket != null) { break; } Thread.Sleep(10); } while (!currentThread.IsInterrupted); if (currentThread.IsInterrupted) { break; } ByteBuffer payloadBuffer = currentPacket.backingBuffer; currentPacket.backingBuffer = null; ByteBuffer responseBuffer = ByteBufferPool.acquire(); InetAddress destinationAddress = currentPacket.ip4Header.destinationAddress; TCPHeader tcpHeader = currentPacket.tcpHeader; int destinationPort = tcpHeader.destinationPort; int sourcePort = tcpHeader.sourcePort; Java.Lang.String ipAndPort = new Java.Lang.String(destinationAddress.HostAddress + ":" + destinationPort + ":" + sourcePort); System.Console.WriteLine("TCP Out: " + ipAndPort); TCB tcb = TCB.GetTCB(ipAndPort); if (tcb == null) { InitializeConnection(ipAndPort, destinationAddress, destinationPort, currentPacket, tcpHeader, responseBuffer); } else if (tcpHeader.isSYN()) { processDuplicateSYN(tcb, tcpHeader, responseBuffer); } else if (tcpHeader.isRST()) { CloseCleanly(tcb, responseBuffer); } else if (tcpHeader.isFIN()) { processFIN(tcb, tcpHeader, responseBuffer); } else if (tcpHeader.isACK()) { processACK(tcb, tcpHeader, payloadBuffer, responseBuffer); } // XXX: cleanup later if (responseBuffer.Position() == 0) { ByteBufferPool.Release(responseBuffer); } ByteBufferPool.Release(payloadBuffer); } } catch (InterruptedException e) { Log.Info(TAG, "Stopping"); } catch (IOException e) { Log.Error(TAG, e.ToString(), e); } finally { TCB.CloseAll(); } }
public void Run() { Log.Info(TAG, "Started"); FileChannel vpnInput = new FileInputStream(vpnFileDescriptor).Channel; FileChannel vpnOutput = new FileOutputStream(vpnFileDescriptor).Channel; try { ByteBuffer bufferToNetwork = null; bool dataSent = true; bool dataReceived; while (!Thread.Interrupted()) { if (dataSent) { bufferToNetwork = ByteBufferPool.acquire(); } else { bufferToNetwork.Clear(); } // TODO: Block when not connected int readBytes = vpnInput.Read(bufferToNetwork); if (readBytes > 0) { dataSent = true; bufferToNetwork.Flip(); Packet packet = new Packet(bufferToNetwork); if (packet.IsUDP) { deviceToNetworkUDPQueue.Offer(packet); System.Console.WriteLine("-> Device (UDP) to network write"); } else if (packet.IsTCP) { deviceToNetworkTCPQueue.Offer(packet); System.Console.WriteLine("-> Device (TCP) to network write"); } else { Log.Warn(TAG, "Unknown packet type"); Log.Warn(TAG, packet.ip4Header.ToString()); dataSent = false; } } else { dataSent = false; } ByteBuffer bufferFromNetwork = (ByteBuffer)networkToDeviceQueue.Poll(); if (bufferFromNetwork != null) { bufferFromNetwork.Flip(); while (bufferFromNetwork.HasRemaining) { try { vpnOutput.Write(bufferFromNetwork); } catch (Exception ex) { bufferFromNetwork.Clear(); System.Console.WriteLine(ex.Message); } } dataReceived = true; System.Console.WriteLine("<- Network to device write"); ByteBufferPool.Release(bufferFromNetwork); } else { dataReceived = false; } // TODO: Sleep-looping is not very battery-friendly, consider blocking instead // Confirm if throughput with ConcurrentQueue is really higher compared to BlockingQueue if (!dataSent && !dataReceived) { Thread.Sleep(10); } } } catch (InterruptedException e) { Log.Info(TAG, "Stopping"); } catch (IOException e) { Log.Warn(TAG, e.ToString(), e); } finally { CloseResources(vpnInput, vpnOutput); } }