本文整理汇总了C++中Address::af方法的典型用法代码示例。如果您正苦于以下问题:C++ Address::af方法的具体用法?C++ Address::af怎么用?C++ Address::af使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Address
的用法示例。
在下文中一共展示了Address::af方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: send
int UDPSocket::send(const char* data, std::size_t len,
const Address& peerAddress, int /* flags */)
{
TraceS(this) << "Send: " << len << ": " << peerAddress << endl;
assert(Thread::currentID() == tid());
// assert(len <= net::MAX_UDP_PACKET_SIZE);
if (_peer.valid() && _peer != peerAddress) {
ErrorS(this) << "Peer not authorized: " << peerAddress << endl;
return -1;
}
if (!peerAddress.valid()) {
ErrorS(this) << "Peer not valid: " << peerAddress << endl;
return -1;
}
int r;
auto sr = new internal::SendRequest;
sr->buf = uv_buf_init((char*)data, len); // TODO: memcpy data?
r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(),
UDPSocket::afterSend);
#if 0
switch (peerAddress.af()) {
case AF_INET:
r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(), UDPSocket::afterSend);
break;
case AF_INET6:
r = uv_udp_send6(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1,
*reinterpret_cast<const sockaddr_in6*>(peerAddress.addr()), UDPSocket::afterSend);
break;
default:
throw std::runtime_error("Unexpected address family");
}
#endif
if (r) {
ErrorS(this) << "Send failed: " << uv_err_name(r) << endl;
setUVError("Invalid UDP socket", r);
}
// R is -1 on error, otherwise return len
return r ? r : len;
}
示例2: bind
void UDPSocket::bind(const Address& address, unsigned flags)
{
TraceS(this) << "Binding on " << address << endl;
int r;
switch (address.af()) {
case AF_INET:
r = uv_udp_bind(ptr<uv_udp_t>(), address.addr(), flags);
break;
//case AF_INET6:
// r = uv_udp_bind6(ptr<uv_udp_t>(), address.addr(), flags);
// break;
default:
throw std::runtime_error("Unexpected address family");
}
// Throw and exception of error
if (r)
setAndThrowError("Cannot bind UDP socket", r);
// Open the receiver channel
recvStart();
}