UDP socket uses an optional address pointer for recv_from() instead of reference.

This commit is contained in:
fpagliughi
2019-08-09 10:28:17 -04:00
parent 1f614ad235
commit ee6342fd20
3 changed files with 18 additions and 14 deletions

View File

@@ -70,7 +70,7 @@ void run_echo(UDPSOCK sock)
// Read some data, also getting the address of the sender,
// then just send it back.
while ((n = sock.recv_from(buf, sizeof(buf), srcAddr)) > 0)
while ((n = sock.recv_from(buf, sizeof(buf), &srcAddr)) > 0)
sock.send_to(buf, n, srcAddr);
}

View File

@@ -205,7 +205,7 @@ public:
* @param addr Receives the address of the peer that sent the message
* @return The number of bytes read or @em -1 on error.
*/
ssize_t recv_from(void* buf, size_t n, int flags, sock_address& srcAddr);
ssize_t recv_from(void* buf, size_t n, int flags, sock_address* srcAddr=nullptr);
/**
* Receives a message on the socket.
* @param buf Buffer to get the incoming data.
@@ -213,8 +213,8 @@ public:
* @param addr Receives the address of the peer that sent the message
* @return The number of bytes read or @em -1 on error.
*/
ssize_t recv_from(void* buf, size_t n, sock_address& addr) {
return recv_from(buf, n, 0, addr);
ssize_t recv_from(void* buf, size_t n, sock_address* srcAddr=nullptr) {
return recv_from(buf, n, 0, srcAddr);
}
/**
* Receives a message on the socket.
@@ -342,21 +342,23 @@ public:
* Receives a message on the socket.
* @param buf Buffer to get the incoming data.
* @param n The number of bytes to read.
* @param addr Receives the address of the peer that sent the message
* @param srcAddr Receives the address of the peer that sent
* the message
* @return The number of bytes read or @em -1 on error.
*/
ssize_t recv_from(void* buf, size_t n, int flags, ADDR& srcAddr) {
ssize_t recv_from(void* buf, size_t n, int flags, ADDR* srcAddr) {
return base::recv_from(buf, n, flags, srcAddr);
}
/**
* Receives a message on the socket.
* @param buf Buffer to get the incoming data.
* @param n The number of bytes to read.
* @param addr Receives the address of the peer that sent the message
* @param srcAddr Receives the address of the peer that sent
* the message
* @return The number of bytes read or @em -1 on error.
*/
ssize_t recv_from(void* buf, size_t n, ADDR& addr) {
return base::recv_from(buf, n, addr);
ssize_t recv_from(void* buf, size_t n, ADDR* srcAddr=nullptr) {
return base::recv_from(buf, n, srcAddr);
}
};

View File

@@ -83,16 +83,18 @@ int datagram_socket::open()
}
#endif
ssize_t datagram_socket::recv_from(void* buf, size_t n, int flags, sock_address& srcAddr)
ssize_t datagram_socket::recv_from(void* buf, size_t n, int flags,
sock_address* srcAddr /*=nullptr*/)
{
socklen_t len = srcAddr.size();
sockaddr* p = srcAddr ? srcAddr->sockaddr_ptr() : nullptr;
socklen_t len = srcAddr ? srcAddr->size() : 0;
// TODO: Check returned length
#if defined(_WIN32)
return check_ret(::recvfrom(handle(), reinterpret_cast<char*>(buf),
int(n), flags, srcAddr.sockaddr_ptr(), &len));
int(n), flags, p, &len));
#else
return check_ret(::recvfrom(handle(), buf, n, flags,
srcAddr.sockaddr_ptr(), &len));
return check_ret(::recvfrom(handle(), buf, n, flags, p, &len));
#endif
}