Skip to content

Commit 38f8502

Browse files
authored
add Socket::send method for non-blocking writes (#26)
1 parent abb864f commit 38f8502

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/raw.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ impl Socket {
180180
}
181181
}
182182

183+
/// Perform a non-blocking write on the socket.
184+
pub fn send(&self, buf: &[u8]) -> Result<Option<usize>, Error> {
185+
let length = buf.len();
186+
let ptr = buf.as_ptr();
187+
let result = unsafe {
188+
sys::nrf_send(
189+
self.fd,
190+
ptr as *const _,
191+
length as u32,
192+
sys::NRF_MSG_DONTWAIT as i32,
193+
)
194+
};
195+
if result == -1 && get_last_error() == sys::NRF_EAGAIN as i32 {
196+
// This is EAGAIN
197+
Ok(None)
198+
} else if result < 0 {
199+
Err(Error::Nordic("send", result as i32, get_last_error()))
200+
} else {
201+
Ok(Some(result as usize))
202+
}
203+
}
204+
183205
/// Perform a blocking write on the socket.
184206
pub fn write(&self, buf: &[u8]) -> Result<usize, Error> {
185207
let length = buf.len();

0 commit comments

Comments
 (0)