当前位置: 首页>>代码示例>>C++>>正文


C++ NetAddress::IsLocal方法代码示例

本文整理汇总了C++中NetAddress::IsLocal方法的典型用法代码示例。如果您正苦于以下问题:C++ NetAddress::IsLocal方法的具体用法?C++ NetAddress::IsLocal怎么用?C++ NetAddress::IsLocal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetAddress的用法示例。


在下文中一共展示了NetAddress::IsLocal方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: if

// FinishInitialization
status_t
InsecureConnection::FinishInitialization()
{
PRINT(("InsecureConnection::FinishInitialization()\n"));
	// get the down stream channel
	InsecureChannel* channel
		= dynamic_cast<InsecureChannel*>(DownStreamChannelAt(0));
	if (!channel)
		return B_BAD_VALUE;
	// receive the connect request
	ConnectRequest request;
	status_t error = channel->Receive(&request, sizeof(ConnectRequest));
	if (error != B_OK)
		return error;
	// check the protocol version
	int32 protocolVersion = B_BENDIAN_TO_HOST_INT32(request.protocolVersion);
	if (protocolVersion != kProtocolVersion) {
		_SendErrorReply(channel, B_ERROR);
		return B_ERROR;
	}
	// get our address (we need it for binding)
	in_addr serverAddr;
	serverAddr.s_addr = request.serverAddress;
	// check number of up and down stream channels
	int32 upStreamChannels = B_BENDIAN_TO_HOST_INT32(request.upStreamChannels);
	int32 downStreamChannels = B_BENDIAN_TO_HOST_INT32(
		request.downStreamChannels);
	if (upStreamChannels < kMinUpStreamChannels)
		upStreamChannels = kMinUpStreamChannels;
	else if (upStreamChannels > kMaxUpStreamChannels)
		upStreamChannels = kMaxUpStreamChannels;
	if (downStreamChannels < kMinDownStreamChannels)
		downStreamChannels = kMinDownStreamChannels;
	else if (downStreamChannels > kMaxDownStreamChannels)
		downStreamChannels = kMaxDownStreamChannels;
	// due to a bug on BONE we have a maximum of 2 working connections
	// accepted on one listener socket.
	NetAddress peerAddress;
	if (channel->GetPeerAddress(&peerAddress) == B_OK
		&& peerAddress.IsLocal()) {
		upStreamChannels = 1;
		if (downStreamChannels > 2)
			downStreamChannels = 2;
	}
	int32 allChannels = upStreamChannels + downStreamChannels;
	// create a listener socket
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		error = errno;
		_SendErrorReply(channel, error);
		return error;
	}
	SocketCloser _(fd);
	// bind it to some port
	sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = 0;
	addr.sin_addr = serverAddr;
	if (bind(fd, (sockaddr*)&addr, sizeof(addr)) < 0) {
		error = errno;
		_SendErrorReply(channel, error);
		return error;
	}
	// get the port
	int addrSize = sizeof(addr);
	if (getsockname(fd, (sockaddr*)&addr, &addrSize) < 0) {
		error = errno;
		_SendErrorReply(channel, error);
		return error;
	}
	// set socket to non-blocking
	int dontBlock = 1;
	if (setsockopt(fd, SOL_SOCKET, SO_NONBLOCK, &dontBlock, sizeof(int)) < 0) {
		error = errno;
		_SendErrorReply(channel, error);
		return error;
	}
	// start listening
	if (listen(fd, allChannels - 1) < 0) {
		error = errno;
		_SendErrorReply(channel, error);
		return error;
	}
	// send the reply
	ConnectReply reply;
	reply.error = B_HOST_TO_BENDIAN_INT32(B_OK);
	reply.upStreamChannels = B_HOST_TO_BENDIAN_INT32(upStreamChannels);
	reply.downStreamChannels = B_HOST_TO_BENDIAN_INT32(downStreamChannels);
	reply.port = addr.sin_port;
	error = channel->Send(&reply, sizeof(ConnectReply));
	if (error != B_OK)
		return error;
	// start accepting
	bigtime_t startAccepting = system_time();
	for (int32 i = 1; i < allChannels; ) {
		// accept a connection
		int channelFD = accept(fd, NULL, 0);
		if (channelFD < 0) {
			error = errno;
//.........这里部分代码省略.........
开发者ID:SummerSnail2014,项目名称:haiku,代码行数:101,代码来源:InsecureConnection.cpp


注:本文中的NetAddress::IsLocal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。