本文整理汇总了C++中SockAddr::size方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::size方法的具体用法?C++ SockAddr::size怎么用?C++ SockAddr::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SockAddr
的用法示例。
在下文中一共展示了SockAddr::size方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: Connect
LNE_UINT SockConnector::Connect(SockPad &skpad, const SockAddr &addr, const TimeValue *tv)
{
LNE_UINT result = LNERR_UNKNOW;
SOCKET sock = socket(addr.family(), SOCK_STREAM, IPPROTO_TCP);
if(sock != INVALID_SOCKET) {
if(tv) {
#if defined(LNE_WIN32)
unsigned long value = 1;
if(ioctlsocket(sock, FIONBIO, &value) == 0) {
#else
int flags = fcntl(sock, F_GETFL);
if(flags >= 0 && fcntl(sock, F_SETFL, flags | O_NONBLOCK) == 0) {
#endif
connect(sock, addr.addr(), addr.size());
fd_set fds;
FD_ZERO(&fds);
FD_SET((SOCKET)sock, &fds);
TimeValue timeout(*tv);
if(select(static_cast<int>((SOCKET)sock + 1), NULL, &fds, NULL, static_cast<timeval *>(timeout)) < 1)
result = LNERR_TIMEOUT;
else {
#if defined(LNE_WIN32)
value = 0;
if(ioctlsocket(sock, FIONBIO, &value) == 0)
#else
if(fcntl(sock, F_SETFL, flags) == 0)
#endif
result = LNERR_OK;
}
}
} else {
if(connect(sock, addr.addr(), addr.size()) == 0)
result = LNERR_OK;
}
if(result == LNERR_OK)
skpad.Attach(addr.family(), sock);
}
return result;
}
示例3: open
bool Acceptor::open(const SockAddr& sockaddr,
int backlog,
auto_ptr<HandlerCreatorStrategyBase> handler_creator_stg,
auto_ptr<FilterCreatorStrategyBase> filter_stg,
ErrorCode* ec)
{
if (!handler_creator_stg.get())
RETURN_ERROR_WITH(ec, E_NET_CREATE_NEW_HANDLER_FAILED, "addr=" << sockaddr, false);
int fd = ::socket(sockaddr.getType(), SOCK_STREAM, 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 (flag_ & OPT_KEEPALIVE)
SocketHelper::setKeepAlive(fd, ec);
if (flag_ & OPT_NODELAY)
SocketHelper::disableNagle(fd, ec);
if (-1 == ::bind(fd, sockaddr.raw(), sockaddr.size()))
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
if (-1 == ::listen(fd, backlog))
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
boost::shared_ptr<Handler> newhandler;
newhandler.reset(new AcceptorHandler(fd, sockaddr, sp_reactor_impl_, handler_creator_stg, filter_stg));
HandlerScoper<Handler> handler_scoper(newhandler);
spfd.release();
if (!sp_reactor_impl_->registerHandler(newhandler, MASK_ACCEPT, ec))
return false;
LOG(info, "listen on "<< sockaddr);
handler_scoper.release();
setAcceptorHandler(newhandler);
return true;
}
示例4: open
bool Connector::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);
int fd = ::socket(sockaddr.getType(), SOCK_STREAM, 0);
if (-1 == fd)
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
ScopedFD spfd(fd);
SocketHelper::setNonBlocking(fd, ec);
SocketHelper::setReusable(fd, ec);
if (flag_ & OPT_KEEPALIVE)
SocketHelper::setKeepAlive(fd, ec);
if (flag_ & OPT_NODELAY)
SocketHelper::disableNagle(fd, ec);
LOG(info, "connect to " << sockaddr);
if (-1 == ::connect(fd, sockaddr.raw(), sockaddr.size()))
{
if (EINTR != errno && EINPROGRESS != errno)
RETURN_NET_ERROR_WITH(ec, errno, "addr=" << sockaddr, false);
}
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: if
int
SockConnector::connect
(
SockStream& stream,
const SockAddr& sockAddr,
int reuseAddr,
int protocolFamily,
int protocol
)
{
TRACE_CALL;
if (stream.handleInvalid ())
{
// open on a new socket descriptor
if (stream.open (SOCK_STREAM,
protocolFamily,
protocol,
reuseAddr) < 0)
return -1; // cannot open
}
sockaddr* remoteAddr = 0;
if (sockAddr.clone (remoteAddr) != -1)
{
size_t size = sockAddr.size ();
REACTOR_HANDLE handle = stream.handleGet ();
if (::connect (handle, remoteAddr, size) < 0)
stream.close ();
#if 0
if (::bind (handle, remoteAddr, size) < 0)
stream.close ();
else if (::connect (handle, remoteAddr, size) < 0)
stream.close ();
#endif
}
delete remoteAddr;
return stream.handleInvalid () ? -1 : 0;
}