本文整理汇总了C++中SockAddr::getPort方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::getPort方法的具体用法?C++ SockAddr::getPort怎么用?C++ SockAddr::getPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SockAddr
的用法示例。
在下文中一共展示了SockAddr::getPort方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
bool SockAddr::operator<(const SockAddr& r) const {
if (getType() < r.getType())
return true;
else if (getType() > r.getType())
return false;
if (getPort() < r.getPort())
return true;
else if (getPort() > r.getPort())
return false;
switch (getType()) {
case AF_INET:
return as<sockaddr_in>().sin_addr.s_addr < r.as<sockaddr_in>().sin_addr.s_addr;
case AF_INET6:
return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr,
r.as<sockaddr_in6>().sin6_addr.s6_addr,
sizeof(in6_addr)) < 0;
case AF_UNIX:
return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) < 0;
case AF_UNSPEC:
return false;
default:
massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
}
return false;
}
示例2: open
bool UdpEndPoint::open(const SockAddr& sockaddr,
int backlog,
auto_ptr<base::net::HandlerCreatorStrategyBase> handler_stg,
auto_ptr<base::net::FilterCreatorStrategyBase> sp_filter_stg,
ErrorCode* ec)
{
int fd = ::socket(sockaddr.getType(), SOCK_DGRAM, 0);
if (-1 == fd)
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
ScopedFD spfd(fd);
if (sockaddr.getType() == AF_UNIX)
{
const sockaddr_un& un = sockaddr.as<sockaddr_un>();
unlink(un.sun_path); // in case it already exists
}
SocketHelper::setNonBlocking(fd, ec);
SocketHelper::setReusable(fd, ec);
if (-1 == ::bind(fd, sockaddr.raw(), sockaddr.size()))
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
boost::shared_ptr<Handler> newhandler;
if (0 == sockaddr.getPort())
{
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
if (-1 == getsockname(fd, (struct sockaddr *)&addr, &len))
RETURN_NET_ERROR_WITH(ec, errno, "", false);
SockAddr setaddr(ntohs(addr.sin_port));
handler_stg->create(fd, setaddr, sp_reactor_impl_, newhandler);
}
else
handler_stg->create(fd, sockaddr, sp_reactor_impl_, newhandler);
if (!newhandler)
RETURN_NET_ERROR_WITH(ec, errno, "create handler failed" << sockaddr, false);
newhandler->setConnected();
if (sp_filter_stg.get())
newhandler->setFilter(auto_ptr<Filter>(sp_filter_stg->create(false, newhandler.get())));
HandlerScoper<Handler> handler_scoper(newhandler);
spfd.release();
if (!sp_reactor_impl_->registerHandler(newhandler, MASK_READ, ec))
return false;
handler_scoper.release();
setUdpHandler(newhandler);
return true;
}
示例3: switch
bool SockAddr::operator==(const SockAddr& r) const {
if (getType() != r.getType())
return false;
if (getPort() != r.getPort())
return false;
switch (getType()) {
case AF_INET: return as<sockaddr_in>().sin_addr.s_addr == r.as<sockaddr_in>().sin_addr.s_addr;
case AF_INET6: return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr, r.as<sockaddr_in6>().sin6_addr.s6_addr, sizeof(in6_addr)) == 0;
case AF_UNIX: return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) == 0;
case AF_UNSPEC: return true; // assume all unspecified addresses are the same
default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
}
}
示例4: open
bool UdpConnector::open(const SockAddr& sockaddr, boost::shared_ptr<Handler>* spout, ErrorCode* ec)
{
if (!sp_creator_stg_.get())
RETURN_ERROR_WITH(ec, E_NET_CREATE_NEW_HANDLER_FAILED, "addr=" << sockaddr, false);
struct sockaddr_in address;
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr(sockaddr.getAddr().data());
unsigned short port = sockaddr.getPort();
address.sin_port=htons(port);
int fd = ::socket(sockaddr.getType(), SOCK_DGRAM, 0);
if (-1 == fd)
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
ScopedFD spfd(fd);
SocketHelper::setNonBlocking(fd, ec);
SocketHelper::setReusable(fd, ec);
boost::shared_ptr<Handler> spnewhandler;
sp_creator_stg_->create(fd, sockaddr, sp_reactor_impl_, spnewhandler);
if (!spnewhandler)
RETURN_ERROR_WITH(ec, E_NET_CREATE_NEW_HANDLER_FAILED, "addr=" << sockaddr, false);
HandlerScoper<Handler> handler_scoper(spnewhandler);
spfd.release();
if (sp_filter_stg_.get())
spnewhandler->setFilter(auto_ptr<Filter>(sp_filter_stg_->create(true, spnewhandler.get())));
if (!sp_reactor_impl_->registerHandler(spnewhandler, MASK_CONNECT|MASK_READ|MASK_WRITE, ec))
RETURN_IF_ERROR_WITH(ec, false);
handler_scoper.release();
if (spout)
*spout = spnewhandler;
return true;
}
示例5: getPort
bool SockAddr::operator<(const SockAddr& r) const {
// Address family first
if (getType() < r.getType()) {
return true;
}
if (getType() > r.getType()) {
return false;
}
// Address second
int cmp;
switch (getType()) {
case AF_INET: {
const auto laddr = ntohl(as<sockaddr_in>().sin_addr.s_addr);
const auto raddr = ntohl(r.as<sockaddr_in>().sin_addr.s_addr);
cmp = (laddr < raddr) ? -1 : (laddr > raddr) ? 1 : 0;
break;
}
case AF_INET6:
cmp = memcmp(as<sockaddr_in6>().sin6_addr.s6_addr,
r.as<sockaddr_in6>().sin6_addr.s6_addr,
sizeof(in6_addr));
break;
case AF_UNIX:
cmp = strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path);
break;
default:
massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false);
}
if (cmp < 0) {
return true;
}
if (cmp > 0) {
return false;
}
// All else equal, compare port
return getPort() < r.getPort();
}
示例6: HostAndPort
MessagingPort::MessagingPort(std::shared_ptr<Socket> sock)
: _x509SubjectName(), _connectionId(), _tag(), _psock(std::move(sock)) {
SockAddr sa = _psock->remoteAddr();
_remoteParsed = HostAndPort(sa.getAddr(), sa.getPort());
}