本文整理汇总了C++中HostAndPort::port方法的典型用法代码示例。如果您正苦于以下问题:C++ HostAndPort::port方法的具体用法?C++ HostAndPort::port怎么用?C++ HostAndPort::port使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HostAndPort
的用法示例。
在下文中一共展示了HostAndPort::port方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: isSelf
bool isSelf(const HostAndPort& hostAndPort, ServiceContext* const ctx) {
// Fastpath: check if the host&port in question is bound to one
// of the interfaces on this machine.
// No need for ip match if the ports do not match
if (hostAndPort.port() == serverGlobalParams.port) {
std::vector<std::string> myAddrs = serverGlobalParams.bind_ips;
// If any of the bound addresses is the default route (0.0.0.0 on IPv4) it means we are
// listening on all network interfaces and need to check against any of them.
if (myAddrs.empty() ||
std::any_of(myAddrs.cbegin(), myAddrs.cend(), [](std::string const& addrStr) {
return HostAndPort(addrStr, serverGlobalParams.port).isDefaultRoute();
})) {
myAddrs = getBoundAddrs(IPv6Enabled());
}
const std::vector<std::string> hostAddrs =
getAddrsForHost(hostAndPort.host(), hostAndPort.port(), IPv6Enabled());
for (std::vector<std::string>::const_iterator i = myAddrs.begin(); i != myAddrs.end();
++i) {
for (std::vector<std::string>::const_iterator j = hostAddrs.begin();
j != hostAddrs.end();
++j) {
if (*i == *j) {
return true;
}
}
}
}
ctx->waitForStartupComplete();
try {
DBClientConnection conn;
conn.setSoTimeout(30); // 30 second timeout
// We need to avoid the isMaster call triggered by a normal connect, which would
// cause a deadlock. 'isSelf' is called by the Replication Coordinator when validating
// a replica set configuration document, but the 'isMaster' command requires a lock on the
// replication coordinator to execute. As such we call we call 'connectSocketOnly', which
// does not call 'isMaster'.
if (!conn.connectSocketOnly(hostAndPort).isOK()) {
return false;
}
if (auth::isInternalAuthSet() && !conn.authenticateInternalUser().isOK()) {
return false;
}
BSONObj out;
bool ok = conn.simpleCommand("admin", &out, "_isSelf");
bool me = ok && out["id"].type() == jstOID && instanceId == out["id"].OID();
return me;
} catch (const std::exception& e) {
warning() << "couldn't check isSelf (" << hostAndPort << ") " << e.what() << std::endl;
}
return false;
}
示例2: initSharding
void PubSubSendSocket::initSharding(const std::string configServers) {
if (!pubsubEnabled)
return;
vector<string> configdbs;
splitStringDelim(configServers, &configdbs, ',');
// find config db we are using for pubsub
HostAndPort maxConfigHP;
maxConfigHP.setPort(0);
for (vector<string>::iterator it = configdbs.begin(); it != configdbs.end(); it++) {
HostAndPort configHP = HostAndPort(*it);
if (configHP.port() > maxConfigHP.port())
maxConfigHP = configHP;
}
HostAndPort configPullEndpoint = HostAndPort(maxConfigHP.host(), maxConfigHP.port() + 1234);
try {
dbEventSocket = new zmq::socket_t(zmqContext, ZMQ_PUSH);
dbEventSocket->connect(("tcp://" + configPullEndpoint.toString()).c_str());
}
catch (zmq::error_t& e) {
log() << "PubSub could not connect to config server. Turning off db events..."
<< causedBy(e);
publishDataEvents = false;
}
}
示例3: isSelf
bool isSelf(const HostAndPort& hostAndPort) {
// Fastpath: check if the host&port in question is bound to one
// of the interfaces on this machine.
// No need for ip match if the ports do not match
if (hostAndPort.port() == serverGlobalParams.port) {
std::vector<std::string> myAddrs = serverGlobalParams.bind_ip.empty() ?
getBoundAddrs(IPv6Enabled()) :
std::vector<std::string>();
if (!serverGlobalParams.bind_ip.empty()) {
boost::split(myAddrs, serverGlobalParams.bind_ip, boost::is_any_of(", "));
}
const std::vector<std::string> hostAddrs = getAddrsForHost(hostAndPort.host(),
hostAndPort.port(),
IPv6Enabled());
for (std::vector<std::string>::const_iterator i = myAddrs.begin();
i != myAddrs.end(); ++i) {
for (std::vector<std::string>::const_iterator j = hostAddrs.begin();
j != hostAddrs.end(); ++j) {
if (*i == *j) {
return true;
}
}
}
}
if (!Listener::getTimeTracker()) {
// this ensures we are actually running a server
// this may return true later, so may want to retry
return false;
}
try {
DBClientConnection conn;
std::string errmsg;
if (!conn.connect(hostAndPort, errmsg)) {
return false;
}
if (getGlobalAuthorizationManager()->isAuthEnabled() && isInternalAuthSet()) {
if (!authenticateInternalUser(&conn)) {
return false;
}
}
BSONObj out;
bool ok = conn.simpleCommand("admin" , &out, "_isSelf");
bool me = ok && out["id"].type() == jstOID && instanceId == out["id"].OID();
return me;
}
catch (const std::exception& e) {
warning() << "could't check isSelf (" << hostAndPort << ") " << e.what() << std::endl;
}
return false;
}
示例4: appendGeneric
static void appendGeneric(const HostAndPort& hp, const SinkFunc& write) {
// wrap ipv6 addresses in []s for roundtrip-ability
if (hp.host().find(':') != std::string::npos) {
write("[");
write(hp.host());
write("]");
} else {
write(hp.host());
}
if (hp.host().find('/') == std::string::npos) {
write(":");
write(hp.port());
}
}
示例5: getSlave
HostAndPort ReplicaSetMonitor::getSlave( const HostAndPort& prev ) {
// make sure its valid
if ( prev.port() > 0 ) {
scoped_lock lk( _lock );
for ( unsigned i=0; i<_nodes.size(); i++ ) {
if ( prev != _nodes[i].addr )
continue;
if ( _nodes[i].ok )
return prev;
break;
}
}
return getSlave();
}
示例6: updateReplSetMember
void PubSubSendSocket::updateReplSetMember(HostAndPort hp) {
if (!pubsubEnabled)
return;
std::map<HostAndPort, bool>::iterator member = rsMembers.find(hp);
if (!hp.isSelf() && member == rsMembers.end()) {
std::string endpoint = str::stream() << "tcp://" << hp.host()
<< ":" << hp.port() + 1234;
try {
SimpleMutex::scoped_lock lk(sendMutex);
extSendSocket->connect(endpoint.c_str());
}
catch (zmq::error_t& e) {
log() << "PubSub error connecting to replica set member." << causedBy(e);
}
// don't need to lock around the map because this is called from a locked context
rsMembers.insert(std::make_pair(hp, true));
}
else {
member->second = true;
}
}
示例7: host
bool HostAndPort::operator==(const HostAndPort& r) const {
return host() == r.host() && port() == r.port();
}
示例8: port
bool HostAndPort::operator<(const HostAndPort& r) const {
const int cmp = host().compare(r.host());
if (cmp)
return cmp < 0;
return port() < r.port();
}
示例9: isSelf
bool isSelf(const HostAndPort& hostAndPort) {
// Fastpath: check if the host&port in question is bound to one
// of the interfaces on this machine.
// No need for ip match if the ports do not match
if (hostAndPort.port() == serverGlobalParams.port) {
std::vector<std::string> myAddrs = serverGlobalParams.bind_ip.empty()
? getBoundAddrs(IPv6Enabled())
: std::vector<std::string>();
if (!serverGlobalParams.bind_ip.empty()) {
boost::split(myAddrs, serverGlobalParams.bind_ip, boost::is_any_of(", "));
}
const std::vector<std::string> hostAddrs =
getAddrsForHost(hostAndPort.host(), hostAndPort.port(), IPv6Enabled());
for (std::vector<std::string>::const_iterator i = myAddrs.begin(); i != myAddrs.end();
++i) {
for (std::vector<std::string>::const_iterator j = hostAddrs.begin();
j != hostAddrs.end();
++j) {
if (*i == *j) {
return true;
}
}
}
}
// Ensure that the server is up and ready to accept incoming network requests.
const Listener* listener = Listener::getTimeTracker();
if (!listener) {
return false;
}
listener->waitUntilListening();
try {
DBClientConnection conn;
conn.setSoTimeout(30); // 30 second timeout
// We need to avoid the isMaster call triggered by a normal connect, which would
// cause a deadlock. 'isSelf' is called by the Replication Coordinator when validating
// a replica set configuration document, but the 'isMaster' command requires a lock on the
// replication coordinator to execute. As such we call we call 'connectSocketOnly', which
// does not call 'isMaster'.
if (!conn.connectSocketOnly(hostAndPort).isOK()) {
return false;
}
if (getGlobalAuthorizationManager()->isAuthEnabled() && isInternalAuthSet()) {
if (!conn.authenticateInternalUser()) {
return false;
}
}
BSONObj out;
bool ok = conn.simpleCommand("admin", &out, "_isSelf");
bool me = ok && out["id"].type() == jstOID && instanceId == out["id"].OID();
return me;
} catch (const std::exception& e) {
warning() << "couldn't check isSelf (" << hostAndPort << ") " << e.what() << std::endl;
}
return false;
}