Rust: set the SOF_KEEPALIVE flag on session sockets.

This commit is contained in:
whitequark 2016-10-04 06:40:38 +00:00
parent 2b3bc30396
commit 0e2cd38135
5 changed files with 30 additions and 0 deletions

View File

@ -103,6 +103,10 @@ pub struct udp_pcb {
pub const TCP_WRITE_FLAG_COPY: u8 = 0x01;
pub const TCP_WRITE_FLAG_MORE: u8 = 0x02;
pub const SOF_REUSEADDR: u8 = 0x04;
pub const SOF_KEEPALIVE: u8 = 0x08;
pub const SOF_BROADCAST: u8 = 0x20;
extern {
pub fn pbuf_alloc(l: pbuf_layer, length: u16, type_: pbuf_type) -> *mut pbuf;
pub fn pbuf_realloc(p: *mut pbuf, length: u16);
@ -144,6 +148,7 @@ extern {
// nonstandard
pub fn tcp_sndbuf_(pcb: *mut tcp_pcb) -> u16;
pub fn tcp_so_options_(pcb: *mut tcp_pcb) -> *mut u8;
pub fn udp_new() -> *mut udp_pcb;
pub fn udp_new_ip_type(type_: ip_addr_type) -> *mut udp_pcb;

View File

@ -397,6 +397,18 @@ impl TcpListener {
pub fn try_accept(&self) -> Option<TcpStream> {
self.state.borrow_mut().backlog.pop_front()
}
pub fn keepalive(&self) -> bool {
unsafe { *lwip_sys::tcp_so_options_(self.raw) & lwip_sys::SOF_KEEPALIVE != 0 }
}
pub fn set_keepalive(&self, keepalive: bool) {
if keepalive {
unsafe { *lwip_sys::tcp_so_options_(self.raw) |= lwip_sys::SOF_KEEPALIVE }
} else {
unsafe { *lwip_sys::tcp_so_options_(self.raw) &= !lwip_sys::SOF_KEEPALIVE }
}
}
}
impl Drop for TcpListener {

View File

@ -391,6 +391,14 @@ impl<'a> TcpListener<'a> {
pub fn acceptable(&self) -> bool {
self.lower.state().borrow().acceptable()
}
pub fn keepalive(&self) -> bool {
self.lower.keepalive()
}
pub fn set_keepalive(&self, keepalive: bool) {
self.lower.set_keepalive(keepalive)
}
}
pub use lwip::Shutdown;

View File

@ -396,6 +396,7 @@ pub fn thread(waiter: Waiter, spawner: Spawner) {
let addr = SocketAddr::new(IP_ANY, 1381);
let listener = TcpListener::bind(waiter, addr).expect("cannot bind socket");
listener.set_keepalive(true);
info!("accepting network sessions in Rust");
let mut kernel_thread = None;

View File

@ -237,6 +237,10 @@ u16_t tcp_sndbuf_(struct tcp_pcb *pcb) {
return tcp_sndbuf(pcb);
}
u8_t* tcp_so_options_(struct tcp_pcb *pcb) {
return &pcb->so_options;
}
int main(void)
{
irq_setmask(0);