Skip to content

Commit a67ee07

Browse files
MabezDevwhitequark
authored andcommitted
Add capacity methods to all sockets.
1 parent 2b92dfc commit a67ee07

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

src/socket/icmp.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,30 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
189189
!self.rx_buffer.is_empty()
190190
}
191191

192+
/// Return the maximum number packets the socket can receive.
193+
#[inline]
194+
pub fn packet_recv_capacity(&self) -> usize {
195+
self.rx_buffer.packet_capacity()
196+
}
197+
198+
/// Return the maximum number packets the socket can transmit.
199+
#[inline]
200+
pub fn packet_send_capacity(&self) -> usize {
201+
self.tx_buffer.packet_capacity()
202+
}
203+
204+
/// Return the maximum number of bytes inside the recv buffer.
205+
#[inline]
206+
pub fn payload_recv_capacity(&self) -> usize {
207+
self.rx_buffer.payload_capacity()
208+
}
209+
210+
/// Return the maximum number of bytes inside the transmit buffer.
211+
#[inline]
212+
pub fn payload_send_capacity(&self) -> usize {
213+
self.tx_buffer.payload_capacity()
214+
}
215+
192216
/// Check whether the socket is open.
193217
#[inline]
194218
pub fn is_open(&self) -> bool {

src/socket/raw.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ impl<'a, 'b> RawSocket<'a, 'b> {
7474
!self.rx_buffer.is_empty()
7575
}
7676

77+
/// Return the maximum number packets the socket can receive.
78+
#[inline]
79+
pub fn packet_recv_capacity(&self) -> usize {
80+
self.rx_buffer.packet_capacity()
81+
}
82+
83+
/// Return the maximum number packets the socket can transmit.
84+
#[inline]
85+
pub fn packet_send_capacity(&self) -> usize {
86+
self.tx_buffer.packet_capacity()
87+
}
88+
89+
/// Return the maximum number of bytes inside the recv buffer.
90+
#[inline]
91+
pub fn payload_recv_capacity(&self) -> usize {
92+
self.rx_buffer.payload_capacity()
93+
}
94+
95+
/// Return the maximum number of bytes inside the transmit buffer.
96+
#[inline]
97+
pub fn payload_send_capacity(&self) -> usize {
98+
self.tx_buffer.payload_capacity()
99+
}
100+
77101
/// Enqueue a packet to send, and return a pointer to its payload.
78102
///
79103
/// This function returns `Err(Error::Exhausted)` if the transmit buffer is full,

src/socket/tcp.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,18 @@ impl<'a> TcpSocket<'a> {
640640
!self.tx_buffer.is_full()
641641
}
642642

643+
/// Return the maximum number of bytes inside the recv buffer.
644+
#[inline]
645+
pub fn recv_capacity(&self) -> usize {
646+
self.rx_buffer.capacity()
647+
}
648+
649+
/// Return the maximum number of bytes inside the transmit buffer.
650+
#[inline]
651+
pub fn send_capacity(&self) -> usize {
652+
self.tx_buffer.capacity()
653+
}
654+
643655
/// Check whether the receive half of the full-duplex connection buffer is open
644656
/// (see [may_recv](#method.may_recv), and the receive buffer is not empty.
645657
#[inline]

src/socket/udp.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
110110
!self.rx_buffer.is_empty()
111111
}
112112

113+
/// Return the maximum number packets the socket can receive.
114+
#[inline]
115+
pub fn packet_recv_capacity(&self) -> usize {
116+
self.rx_buffer.packet_capacity()
117+
}
118+
119+
/// Return the maximum number packets the socket can transmit.
120+
#[inline]
121+
pub fn packet_send_capacity(&self) -> usize {
122+
self.tx_buffer.packet_capacity()
123+
}
124+
125+
/// Return the maximum number of bytes inside the recv buffer.
126+
#[inline]
127+
pub fn payload_recv_capacity(&self) -> usize {
128+
self.rx_buffer.payload_capacity()
129+
}
130+
131+
/// Return the maximum number of bytes inside the transmit buffer.
132+
#[inline]
133+
pub fn payload_send_capacity(&self) -> usize {
134+
self.tx_buffer.payload_capacity()
135+
}
136+
113137
/// Enqueue a packet to be sent to a given remote endpoint, and return a pointer
114138
/// to its payload.
115139
///

src/storage/packet_buffer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ impl<'a, 'b, H> PacketBuffer<'a, 'b, H> {
168168
Err(Error::Exhausted)
169169
}
170170
}
171+
172+
/// Return the maximum number packets that can be stored.
173+
pub fn packet_capacity(&self) -> usize {
174+
self.metadata_ring.capacity()
175+
}
176+
177+
/// Return the maximum number of bytes in the payload ring buffer.
178+
pub fn payload_capacity(&self) -> usize {
179+
self.payload_ring.capacity()
180+
}
171181
}
172182

173183
#[cfg(test)]

0 commit comments

Comments
 (0)