本文整理汇总了C++中IPV4SocketAddress::ToSockAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ IPV4SocketAddress::ToSockAddr方法的具体用法?C++ IPV4SocketAddress::ToSockAddr怎么用?C++ IPV4SocketAddress::ToSockAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPV4SocketAddress
的用法示例。
在下文中一共展示了IPV4SocketAddress::ToSockAddr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Connect
TCPConnector::TCPConnectionID TCPConnector::Connect(
const IPV4SocketAddress &endpoint,
const ola::TimeInterval &timeout,
TCPConnectCallback *callback) {
struct sockaddr server_address;
if (!endpoint.ToSockAddr(&server_address, sizeof(server_address))) {
callback->Run(-1, 0);
return 0;
}
int sd = socket(endpoint.Family(), SOCK_STREAM, 0);
if (sd < 0) {
int error = errno;
OLA_WARN << "socket() failed, " << strerror(error);
callback->Run(-1, error);
return 0;
}
#ifdef _WIN32
ola::io::DescriptorHandle descriptor;
descriptor.m_handle.m_fd = sd;
descriptor.m_type = ola::io::SOCKET_DESCRIPTOR;
#else
ola::io::DescriptorHandle descriptor = sd;
#endif
ola::io::ConnectedDescriptor::SetNonBlocking(descriptor);
int r = connect(sd, &server_address, sizeof(server_address));
if (r) {
#ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK) {
#else
if (errno != EINPROGRESS) {
#endif
int error = errno;
OLA_WARN << "connect() to " << endpoint << " returned, "
<< strerror(error);
close(sd);
callback->Run(-1, error);
return 0;
}
} else {
// connect returned immediately
callback->Run(sd, 0);
return 0;
}
PendingTCPConnection *connection = new PendingTCPConnection(
this,
endpoint.Host(),
sd,
callback);
m_connections.insert(connection);
connection->timeout_id = m_ss->RegisterSingleTimeout(
timeout,
ola::NewSingleCallback(this, &TCPConnector::TimeoutEvent, connection));
m_ss->AddWriteDescriptor(connection);
return connection;
}
bool TCPConnector::Cancel(TCPConnectionID id) {
PendingTCPConnection *connection =
const_cast<PendingTCPConnection*>(
reinterpret_cast<const PendingTCPConnection*>(id));
ConnectionSet::iterator iter = m_connections.find(connection);
if (iter == m_connections.end())
return false;
if (connection->timeout_id != ola::thread::INVALID_TIMEOUT) {
m_ss->RemoveTimeout(connection->timeout_id);
connection->timeout_id = ola::thread::INVALID_TIMEOUT;
}
Timeout(iter);
m_connections.erase(iter);
return true;
}