Java Code Examples for org.tio.core.intf.Packet#isFromCluster()

The following examples show how to use org.tio.core.intf.Packet#isFromCluster() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tio.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param tioConfig
 * @param packet
 * @param channelContextFilter
 * @param isBlock
 * @author tanyaowu
 */
private static Boolean sendToAll(TioConfig tioConfig, Packet packet, ChannelContextFilter channelContextFilter, boolean isBlock) {
	try {
		SetWithLock<ChannelContext> setWithLock = tioConfig.connections;
		if (setWithLock == null) {
			log.debug("{}, 没有任何连接", tioConfig.getName());
			return false;
		}
		Boolean ret = sendToSet(tioConfig, setWithLock, packet, channelContextFilter, isBlock);
		return ret;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4all()) {
				TioClusterVo tioClusterVo = new TioClusterVo(packet);
				tioClusterVo.setToAll(true);
				tioClusterConfig.publish(tioClusterVo);
			}
		}
	}
}
 
Example 2
Source File: Tio.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 发消息给指定业务ID
 * @param tioConfig
 * @param bsId
 * @param packet
 * @param isBlock
 * @return
 * @author tanyaowu
 */
private static Boolean sendToBsId(TioConfig tioConfig, String bsId, Packet packet, boolean isBlock) {
	ChannelContext channelContext = Tio.getChannelContextByBsId(tioConfig, bsId);
	if (channelContext == null) {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4bsId()) {
				notifyClusterForBsId(tioConfig, bsId, packet);
			}
		}
		return false;
	}
	if (isBlock) {
		return bSend(channelContext, packet);
	} else {
		return send(channelContext, packet);
	}
}
 
Example 3
Source File: Tio.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 发消息到组
 * @param tioConfig
 * @param group
 * @param packet
 * @param channelContextFilter
 * @param isBlock
 * @return
 */
private static Boolean sendToGroup(TioConfig tioConfig, String group, Packet packet, ChannelContextFilter channelContextFilter, boolean isBlock) {
	try {
		SetWithLock<ChannelContext> setWithLock = tioConfig.groups.clients(tioConfig, group);
		if (setWithLock == null) {
			log.debug("{}, 组[{}]不存在", tioConfig.getName(), group);
			return false;
		}
		Boolean ret = sendToSet(tioConfig, setWithLock, packet, channelContextFilter, isBlock);
		return ret;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4group()) {
				notifyClusterForGroup(tioConfig, group, packet);
			}
		}
	}
}
 
Example 4
Source File: Tio.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 发消息给指定ChannelContext id
 * @param channelContextId
 * @param packet
 * @param isBlock
 * @return
 * @author tanyaowu
 */
private static Boolean sendToId(TioConfig tioConfig, String channelContextId, Packet packet, boolean isBlock) {
	ChannelContext channelContext = Tio.getChannelContextById(tioConfig, channelContextId);
	if (channelContext == null) {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4channelId()) {
				notifyClusterForId(tioConfig, channelContextId, packet);
			}
		}
		return false;
	}
	if (isBlock) {
		return bSend(channelContext, packet);
	} else {
		return send(channelContext, packet);
	}
}
 
Example 5
Source File: Tio.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 发送到指定ip对应的集合
 * @param tioConfig
 * @param ip
 * @param packet
 * @param channelContextFilter
 * @param isBlock
 * @return
 * @author: tanyaowu
 */
private static Boolean sendToIp(TioConfig tioConfig, String ip, Packet packet, ChannelContextFilter channelContextFilter, boolean isBlock) {
	try {
		SetWithLock<ChannelContext> setWithLock = tioConfig.ips.clients(tioConfig, ip);
		if (setWithLock == null) {
			log.info("{}, 没有ip为[{}]的对端", tioConfig.getName(), ip);
			return false;
		}
		Boolean ret = sendToSet(tioConfig, setWithLock, packet, channelContextFilter, isBlock);
		return ret;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4ip()) {
				notifyClusterForIp(tioConfig, ip, packet);
			}
		}
	}
}
 
Example 6
Source File: Tio.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * 发消息给指定token
 * @param tioConfig
 * @param token
 * @param packet
 * @param isBlock
 * @author tanyaowu
 */
private static Boolean sendToToken(TioConfig tioConfig, String token, Packet packet, boolean isBlock) {
	SetWithLock<ChannelContext> setWithLock = tioConfig.tokens.find(tioConfig, token);
	try {
		if (setWithLock == null) {
			return false;
		}

		ReadLock readLock = setWithLock.readLock();
		readLock.lock();
		try {
			Set<ChannelContext> set = setWithLock.getObj();
			boolean ret = false;
			for (ChannelContext channelContext : set) {
				boolean singleRet = false;
				// 不要用 a = a || b(),容易漏执行后面的函数
				if (isBlock) {
					singleRet = bSend(channelContext, packet);
				} else {
					singleRet = send(channelContext, packet);
				}
				if (singleRet) {
					ret = true;
				}
			}
			return ret;
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		} finally {
			readLock.unlock();
		}
		return false;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4user()) {
				notifyClusterForToken(tioConfig, token, packet);
			}
		}
	}
}
 
Example 7
Source File: Tio.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * 发消息给指定用户
 * @param tioConfig
 * @param userid
 * @param packet
 * @param isBlock
 * @author tanyaowu
 */
private static Boolean sendToUser(TioConfig tioConfig, String userid, Packet packet, boolean isBlock) {
	SetWithLock<ChannelContext> setWithLock = tioConfig.users.find(tioConfig, userid);
	try {
		if (setWithLock == null) {
			return false;
		}

		ReadLock readLock = setWithLock.readLock();
		readLock.lock();
		try {
			Set<ChannelContext> set = setWithLock.getObj();
			boolean ret = false;
			for (ChannelContext channelContext : set) {
				boolean singleRet = false;
				// 不要用 a = a || b(),容易漏执行后面的函数
				if (isBlock) {
					singleRet = bSend(channelContext, packet);
				} else {
					singleRet = send(channelContext, packet);
				}
				if (singleRet) {
					ret = true;
				}
			}
			return ret;
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		} finally {
			readLock.unlock();
		}
		return false;
	} finally {
		if (tioConfig.isCluster() && !packet.isFromCluster()) {
			TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();

			if (tioClusterConfig.isCluster4user()) {
				notifyClusterForUser(tioConfig, userid, packet);
			}
		}
	}
}