java.lang.Object
org.jnetpcap.windows.PcapSendQueue
- All Implemented Interfaces:
AutoCloseable
Represents a queue of raw packets for efficient batch transmission on
Microsoft Windows platforms. This class provides functionality to queue
multiple packets and transmit them either as quickly as possible or with
precise timing synchronization.
Native Structure
Maps to the nativepcap_send_queue
structure:
struct pcap_send_queue {
u_int maxlen; // Maximum size of the queue buffer in bytes
u_int len; // Current size of the queue in bytes
char *buffer; // Buffer containing queued packets
};
Usage Example
try (PcapSendQueue queue = new PcapSendQueue(65536)) { // 64KB queue
// Queue multiple packets
queue.queue(header1, packet1);
queue.queue(header2, packet2);
// Transmit all queued packets with timing synchronization
int bytesSent = queue.transmit(pcapHandle, true);
}
Performance Considerations
- Using a send queue is more efficient than multiple
pcap_sendpacket()
calls due to reduced context switching - Synchronized transmission (sync=true) provides microsecond precision but requires more CPU resources
- The CRC is automatically calculated by the network interface
Platform Support
This functionality is only available on Microsoft Windows platforms through WinPcap/Npcap.- Since:
- WinPcap 1.0
- See Also:
-
Constructor Summary
ConstructorDescriptionPcapSendQueue
(int capacity) Creates a new send queue with specified capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes and deallocates the send queue.int
len()
Returns the current size of the queue in bytes.int
maxlen()
Returns the maximum capacity of the send queue in bytes.int
queue
(MemorySegment header, MemorySegment packet) Adds a packet to the end of the send queue using native memory segments.int
queue
(PcapHeader header, byte[] packet, int offset) Adds a packet to the end of the send queue using Java byte array.
-
Constructor Details
-
PcapSendQueue
public PcapSendQueue(int capacity) Creates a new send queue with specified capacity. The capacity determines the maximum amount of packet data that can be queued.- Parameters:
capacity
- Maximum size of the queue in bytes- Throws:
OutOfMemoryError
- if native memory allocation fails
-
-
Method Details
-
close
public void close()Closes and deallocates the send queue. This method must be called to prevent memory leaks.- Specified by:
close
in interfaceAutoCloseable
- Throws:
IllegalStateException
- if the queue has already been closed
-
queue
Adds a packet to the end of the send queue using native memory segments. Both packet header and data must be properly formatted in native memory.- Parameters:
header
- The pcap_pkthdr structure containing timestamp and lengthpacket
- The packet data buffer- Returns:
- 0 on success, -1 on failure (queue full)
-
queue
Adds a packet to the end of the send queue using Java byte array. Automatically handles conversion to native memory format.- Parameters:
header
- The pcap header containing timestamp and lengthpacket
- The packet data as byte arrayoffset
- Starting offset in the packet array- Returns:
- 0 on success, -1 on failure
- Throws:
IllegalArgumentException
- if offset is outside packet array bounds
-
maxlen
public int maxlen()Returns the maximum capacity of the send queue in bytes.- Returns:
- The maximum number of bytes that can be queued
-
len
public int len()Returns the current size of the queue in bytes.- Returns:
- The number of bytes currently queued
-