Skip to content

Commit

Permalink
bug fix: no member sa_len in non-macos.
Browse files Browse the repository at this point in the history
  • Loading branch information
levalup committed Jul 8, 2024
1 parent 42eeda8 commit 1a8623a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions examples/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ int main() {
});
server.recv_start().call(
[=](ssize_t nread, const uv_buf_t *buf, const sockaddr *addr, uv_udp_flags flags) mutable {
// std::cout << "[INFO] received [" << server_sock << " <- " << uvcxx::any_address_t(addr) << "]" << std::endl;
std::cout << "server read: " << std::string(buf->base, nread) << " with "
<< uvcxx::to_string(uv_udp_flags(flags)) << std::endl;
<< uvcxx::to_string(uv_udp_flags(flags)) << " from " << uvcxx::any_address_t(addr)
<< std::endl;
}).except([=](const std::exception &e) {
std::cout << "[ERROR] recv " << addr << " failed. " << e.what() << std::endl;
throw uvcxx::close_handle();
Expand Down
10 changes: 8 additions & 2 deletions include/uvcxx/cxx/sockaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ namespace uvcxx {
}

explicit any_address_t(const sockaddr *addr) {
std::memcpy(&m_storage, addr, addr->sa_len);
m_size = addr->sa_len;
if (!addr) return;
if (addr->sa_family == AF_INET) { // ipv4
m_size = sizeof(sockaddr_in);
std::memcpy(&m_storage, addr, m_size);
} else if (addr->sa_family == AF_INET6) { // ipv6
m_size = sizeof(sockaddr_in6);
std::memcpy(&m_storage, addr, m_size);
}
}

UVCXX_NODISCARD
Expand Down

0 comments on commit 1a8623a

Please sign in to comment.