| 33 | } |
| 34 | |
| 35 | size_t UdpSocket::send(HostAddressWithPort const& address, char const* data, size_t size) { |
| 36 | ReadLocker locker(m_mutex); |
| 37 | checkOpen("UdpSocket::send"); |
| 38 | |
| 39 | struct sockaddr_storage sockAddr; |
| 40 | socklen_t sockAddrLen; |
| 41 | setNativeFromAddress(address, &sockAddr, &sockAddrLen); |
| 42 | |
| 43 | int len = ::sendto(m_impl->socketDesc, data, size, 0, (struct sockaddr*)&sockAddr, sockAddrLen); |
| 44 | if (len < 0) { |
| 45 | if (!isActive()) |
| 46 | throw SocketClosedException("Connection closed"); |
| 47 | else if (netErrorInterrupt()) |
| 48 | len = 0; |
| 49 | else |
| 50 | throw NetworkException(strf("udp send error: {}", netErrorString())); |
| 51 | } |
| 52 | |
| 53 | return len; |
| 54 | } |
| 55 | |
| 56 | UdpServer::UdpServer(HostAddressWithPort const& address) |
| 57 | : m_hostAddress(address), m_listenSocket(make_shared<UdpSocket>(m_hostAddress.address().mode())) { |