Skip to content

Commit

Permalink
Rust: set the SOF_KEEPALIVE flag on session sockets.
Browse files Browse the repository at this point in the history
whitequark committed Oct 4, 2016
1 parent 2b3bc30 commit 0e2cd38
Showing 5 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions artiq/runtime.rs/liblwip-sys/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions artiq/runtime.rs/liblwip/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {
8 changes: 8 additions & 0 deletions artiq/runtime.rs/src/sched.rs
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions artiq/runtime.rs/src/session.rs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions artiq/runtime/main.c
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 0e2cd38

Please sign in to comment.