networking - UDP API in Rust -


1) api send here returns result<usize>. why ? in head, udp send or none. return value seems suggest sending can succeed entire data may not written makes me code like:

let mut bytes_written = 0; while bytes_written < data.len() {     bytes_written += match udp_socket.send_to(&data[bytes_written..]) {          ok(bytes_tx) => bytes_tx,          err(_) => break,     } } 

recently told me unnecessary. don't understand. if true why return not result<()> instead, expecting ?

2) for reads though understand. give buffer of size 100 bytes datagram might 50 bytes long. should utilise read_buf[..size_read]. here question happens if buffer size 100 datagram size 150 bytes ? recv_from fill in 100 bytes , return ok(100, some_peer_addr) ? if re-read fill in remaining of datagram ? if datagram of 50 bytes arrived before second read ? remaining 50 bytes second time , 50 bytes of new datagram 3rd time or complete 100 bytes 2nd time contains new datagram ? or error , lose 1st datagram on initial read , never able recover ?

the answer both of these questions lies in documentation of respective bsd sockets functions, sendto() , recvfrom(). if use *nix system (os x or linux, example), can use man sendto , man recvfrom find it.

1) sendto() man page rather vague on this; windows api page explicitly says possible return value less len argument. see this question. looks particular moment under-documented. think safe assume return value equal either len or error code. problems may happen if length of data sent through sendto() exceeds internal buffer size inside os kernel, seems @ least windows return error in case.

2) recvfrom() man page unambiguously states part of datagram not fit buffer discarded:

the recvfrom() function shall return length of message written buffer pointed buffer argument. message-based sockets, such sock_raw, sock_dgram, , sock_seqpacket, entire message shall read in single operation. if message long fit in supplied buffer, , msg_peek not set in flags argument, excess bytes shall discarded.

so yes, recv_from() fill 100 bytes, rest discarded, , further calls recv_from() return new datagrams.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -