org.tio.core.intf.Packet Java Examples

The following examples show how to use org.tio.core.intf.Packet. 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: WsServerAioHandler.java    From t-io with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
	WsResponse wsResponse = (WsResponse) packet;

	// 握手包
	if (wsResponse.isHandShake()) {
		WsSessionContext imSessionContext = (WsSessionContext) channelContext.get();
		HttpResponse handshakeResponse = imSessionContext.getHandshakeResponse();
		try {
			return HttpResponseEncoder.encode(handshakeResponse, tioConfig, channelContext);
		} catch (UnsupportedEncodingException e) {
			log.error(e.toString(), e);
			return null;
		}
	}

	ByteBuffer byteBuffer = WsServerEncoder.encode(wsResponse, tioConfig, channelContext);
	return byteBuffer;
}
 
Example #2
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 #3
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 #4
Source File: SendRunnable.java    From t-io with Apache License 2.0 6 votes vote down vote up
private ByteBuffer getByteBuffer(Packet packet) {
	try {
		ByteBuffer byteBuffer = packet.getPreEncodedByteBuffer();
		if (byteBuffer == null) {
			byteBuffer = aioHandler.encode(packet, tioConfig, channelContext);
		}

		if (!byteBuffer.hasRemaining()) {
			byteBuffer.flip();
		}
		return byteBuffer;
	} catch (Exception e) {
		log.error(packet.logstr(), e);
		throw new RuntimeException(e);
	}
}
 
Example #5
Source File: SendRunnable.java    From t-io with Apache License 2.0 6 votes vote down vote up
public boolean sendPacket(Packet packet) {
	ByteBuffer byteBuffer = getByteBuffer(packet);
	if (isSsl) {
		if (!packet.isSslEncrypted()) {
			SslVo sslVo = new SslVo(byteBuffer, packet);
			try {
				channelContext.sslFacadeContext.getSslFacade().encrypt(sslVo);
				byteBuffer = sslVo.getByteBuffer();
			} catch (SSLException e) {
				log.error(channelContext.toString() + ", 进行SSL加密时发生了异常", e);
				Tio.close(channelContext, "进行SSL加密时发生了异常", CloseCode.SSL_ENCRYPTION_ERROR);
				return false;
			}
		}
	}

	sendByteBuffer(byteBuffer, packet);
	return true;
}
 
Example #6
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 #7
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 #8
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 #9
Source File: Tio.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 在集群环境下,把channelContextId消息通知到集群中的其它机器
 * @param tioConfig
 * @param channelContextId
 * @param packet
 */
public static void notifyClusterForId(TioConfig tioConfig, String channelContextId, Packet packet) {
	TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
	TioClusterVo tioClusterVo = new TioClusterVo(packet);
	tioClusterVo.setChannelId(channelContextId);
	tioClusterConfig.publish(tioClusterVo);
}
 
Example #10
Source File: Tio.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 在集群环境下,把IP消息通知到集群中的其它机器
 * @param tioConfig
 * @param ip
 * @param packet
 */
public static void notifyClusterForIp(TioConfig tioConfig, String ip, Packet packet) {
	TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
	TioClusterVo tioClusterVo = new TioClusterVo(packet);
	tioClusterVo.setIp(ip);
	tioClusterConfig.publish(tioClusterVo);
}
 
Example #11
Source File: SendRunnable.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public boolean addMsg(Packet packet) {
	if (this.isCanceled()) {
		log.info("{}, 任务已经取消,{}添加到发送队列失败", channelContext, packet.logstr());
		return false;
	}

	if (channelContext.sslFacadeContext != null && !channelContext.sslFacadeContext.isHandshakeCompleted() && SslUtils.needSslEncrypt(packet, tioConfig)) {
		return this.getForSendAfterSslHandshakeCompleted(true).add(packet);
	} else {
		return msgQueue.add(packet);
	}
}
 
Example #12
Source File: TioRpcServerEndpoint.java    From nutzcloud with Apache License 2.0 5 votes vote down vote up
@Override
public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException {
    if (readableLength < HEADER_LENGHT)
        return null;
    // 读取消息体的长度
    int bodyLength = buffer.getInt();

    // 数据不正确,则抛出AioDecodeException异常
    if (bodyLength < 2) {
        throw new AioDecodeException("bodyLength [" + bodyLength + "] is not right, remote:" + channelContext.getClientNode());
    }

    // 计算本次需要的数据长度
    int neededLength = HEADER_LENGHT + bodyLength;
    // 收到的数据是否足够组包
    int isDataEnough = readableLength - neededLength;
    // 不够消息体长度(剩下的buffe组不了消息体)
    if (isDataEnough < 0) {
        return null;
    } else // 组包成功
    {
        LiteRpcPacket rpcPacket = new LiteRpcPacket();
        // 版本信息占一个字节
        byte version = buffer.get();
        if (version != 1) {}
        // 操作类型占一个字节
        rpcPacket.opType = buffer.get();
        if (rpcPacket.opType == OP_PING) {
            // 心跳包没有剩余信息,清除多余的body,返回null
            if (bodyLength > 2)
                buffer.clear();
            return null;
        }
        byte[] dst = new byte[bodyLength - 2];
        buffer.get(dst);
        // log.debug(Lang.fixedHexString(dst));
        rpcPacket.setBody(dst);
        return rpcPacket;
    }
}
 
Example #13
Source File: BlockClientAioHandler.java    From md_blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * server端返回的响应会先进到该方法,将消息全丢到Disruptor中
 */
@Override
public void handler(Packet packet, ChannelContext channelContext)  {
    BlockPacket blockPacket = (BlockPacket) packet;

    //使用Disruptor来publish消息。所有收到的消息都进入Disruptor,同BlockServerAioHandler
    ApplicationContextProvider.getBean(MessageProducer.class).publish(new BaseEvent(blockPacket, channelContext));
}
 
Example #14
Source File: SslUtils.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 是否需要对这个packet进行SSL加密 
 * @param packet
 * @param tioConfig
 * @return
 */
public static boolean needSslEncrypt(Packet packet, TioConfig tioConfig) {
	if (!packet.isSslEncrypted() && tioConfig.sslConfig != null) {
		return true;
	}
	return false;
}
 
Example #15
Source File: BlockServerAioHandler.java    From md_blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * 自己是server,此处接收到客户端来的消息。这里是入口
 */
@Override
public void handler(Packet packet, ChannelContext channelContext) {
    BlockPacket blockPacket = (BlockPacket) packet;

    //使用Disruptor来publish消息。所有收到的消息都进入Disruptor,同BlockClientAioHandler
    ApplicationContextProvider.getBean(MessageProducer.class).publish(new BaseEvent(blockPacket, channelContext));
}
 
Example #16
Source File: Tio.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 在集群环境下,把token消息通知到集群中的其它机器
 * @param tioConfig
 * @param token
 * @param packet
 */
public static void notifyClusterForToken(TioConfig tioConfig, String token, Packet packet) {
	TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
	TioClusterVo tioClusterVo = new TioClusterVo(packet);
	tioClusterVo.setToken(token);
	tioClusterConfig.publish(tioClusterVo);
}
 
Example #17
Source File: WriteCompletionHandler.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param result
 * @param throwable
 * @param packet
 * @param isSentSuccess
 * @author tanyaowu
 */
public void handleOne(Integer result, Throwable throwable, Packet packet, Boolean isSentSuccess) {
	Meta meta = packet.getMeta();
	if (meta != null) {
		meta.setIsSentSuccess(isSentSuccess);
	}

	try {
		channelContext.processAfterSent(packet, isSentSuccess);
	} catch (Throwable e) {
		log.error(e.toString(), e);
	}

}
 
Example #18
Source File: SendRunnable.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public FullWaitQueue<Packet> getMsgQueue() {
	if (msgQueue == null) {
		synchronized (this) {
			if (msgQueue == null) {
				msgQueue = new TioFullWaitQueue<Packet>(Integer.getInteger("tio.fullqueue.capacity", null), false);
			}
		}
	}
	return msgQueue;
}
 
Example #19
Source File: SslHandshakeCompletedListener.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete() {
	log.info("{}, 完成SSL握手", channelContext);
	channelContext.sslFacadeContext.setHandshakeCompleted(true);

	if (channelContext.tioConfig.getAioListener() != null) {
		try {
			channelContext.tioConfig.getAioListener().onAfterConnected(channelContext, true, channelContext.isReconnect);
		} catch (Exception e) {
			log.error(e.toString(), e);
		}
	}

	ConcurrentLinkedQueue<Packet> forSendAfterSslHandshakeCompleted = channelContext.sendRunnable.getForSendAfterSslHandshakeCompleted(false);
	if (forSendAfterSslHandshakeCompleted == null || forSendAfterSslHandshakeCompleted.size() == 0) {
		return;
	}

	log.info("{} 业务层在SSL握手前就有{}条数据待发送", channelContext, forSendAfterSslHandshakeCompleted.size());
	while (true) {
		Packet packet = forSendAfterSslHandshakeCompleted.poll();
		if (packet != null) {
			if (channelContext.tioConfig.useQueueSend) {
				channelContext.sendRunnable.addMsg(packet);
			} else {
				channelContext.sendRunnable.sendPacket(packet);
			}

		} else {
			break;
		}
	}
	if (channelContext.tioConfig.useQueueSend) {
		channelContext.sendRunnable.execute();
	}
}
 
Example #20
Source File: Tio.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 阻塞发送消息到指定ChannelContext
 * @param channelContext
 * @param packet
 * @return
 * @author tanyaowu
 */
public static Boolean bSend(ChannelContext channelContext, Packet packet) {
	if (channelContext == null) {
		return false;
	}
	CountDownLatch countDownLatch = new CountDownLatch(1);
	return send(channelContext, packet, countDownLatch, PacketSendMode.SINGLE_BLOCK);
}
 
Example #21
Source File: Tio.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 发送到指定的ip和port
 * @param tioConfig
 * @param ip
 * @param port
 * @param packet
 * @param isBlock
 * @return
 * @author tanyaowu
 */
private static Boolean send(TioConfig tioConfig, String ip, int port, Packet packet, boolean isBlock) {
	ChannelContext channelContext = tioConfig.clientNodes.find(ip, port);
	if (channelContext != null) {
		if (isBlock) {
			return bSend(channelContext, packet);
		} else {
			return send(channelContext, packet);
		}
	} else {
		log.info("{}, can find channelContext by {}:{}", tioConfig.getName(), ip, port);
		return false;
	}
}
 
Example #22
Source File: HttpServerAioHandler.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
	HttpResponse httpResponse = (HttpResponse) packet;
	ByteBuffer byteBuffer;
	try {
		byteBuffer = HttpResponseEncoder.encode(httpResponse, tioConfig, channelContext);
		return byteBuffer;
	} catch (UnsupportedEncodingException e) {
		log.error(e.toString(), e);
		return null;
	}
}
 
Example #23
Source File: HttpClientAioHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
/** 
 * @return
 * @author tanyaowu
 */
@Override
public Packet heartbeatPacket(ChannelContext channelContext) {
	return null;
}
 
Example #24
Source File: TioConfig.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @return the syns
 */
public MapWithLock<Integer, Packet> getWaitingResps() {
	return waitingResps;
}
 
Example #25
Source File: TioClusterMessageListener.java    From t-io with Apache License 2.0 4 votes vote down vote up
public void onMessage(CharSequence channel, TioClusterVo tioClusterVo) {
	log.info("收到topic:{}, count:{}, tioClusterVo:{}", channel, RECEIVED_TOPIC_COUNT.incrementAndGet(), Json.toJson(tioClusterVo));
	String clientid = tioClusterVo.getClientId();
	if (StrUtil.isBlank(clientid)) {
		log.error("clientid is null");
		return;
	}
	if (Objects.equals(TioClusterVo.CLIENTID, clientid)) {
		log.info("自己发布的消息,忽略掉,{}", clientid);
		return;
	}

	Packet packet = tioClusterVo.getPacket();
	if (packet == null) {
		log.error("packet is null");
		return;
	}
	packet.setFromCluster(true);

	//发送给所有
	boolean isToAll = tioClusterVo.isToAll();
	if (isToAll) {
		Tio.sendToAll(tioConfig, packet);
	}

	//发送给指定组
	String group = tioClusterVo.getGroup();
	if (StrUtil.isNotBlank(group)) {
		Tio.sendToGroup(tioConfig, group, packet);
	}

	//发送给指定用户
	String userid = tioClusterVo.getUserid();
	if (StrUtil.isNotBlank(userid)) {
		Tio.sendToUser(tioConfig, userid, packet);
	}

	//发送给指定token
	String token = tioClusterVo.getToken();
	if (StrUtil.isNotBlank(token)) {
		Tio.sendToToken(tioConfig, token, packet);
	}

	//发送给指定ip
	String ip = tioClusterVo.getIp();
	if (StrUtil.isNotBlank(ip)) {
		Tio.sendToIp(tioConfig, ip, packet);
	}

	//发送给指定channelId
	String channelId = tioClusterVo.getChannelId();
	if (StrUtil.isNotBlank(channelId)) {
		Tio.sendToId(tioConfig, channelId, packet);
	}

	//发送给指定bsId
	String bsId = tioClusterVo.getBsId();
	if (StrUtil.isNotBlank(bsId)) {
		Tio.sendToBsId(tioConfig, bsId, packet);
	}
}
 
Example #26
Source File: TioClusterVo.java    From t-io with Apache License 2.0 4 votes vote down vote up
public void setPacket(Packet packet) {
	this.packet = packet;
}
 
Example #27
Source File: HttpClientAioListener.java    From t-io with Apache License 2.0 4 votes vote down vote up
@Override
public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) {
	@SuppressWarnings("unused")
	ClientHttpRequest request = (ClientHttpRequest) packet;
}
 
Example #28
Source File: FlashPolicyServerAioHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * 处理消息
 */
@Override
public void handler(Packet packet, ChannelContext channelContext) throws Exception {
	Tio.send(channelContext, FlashPolicyPacket.RESPONSE);
	//		Tio.close(channelContext, "消息发送完毕");
}
 
Example #29
Source File: DefaultIpStatListener.java    From t-io with Apache License 2.0 4 votes vote down vote up
@Override
public void onAfterHandled(ChannelContext channelContext, Packet packet, IpStat ipStat, long cost) throws Exception {
}
 
Example #30
Source File: TioClusterVo.java    From t-io with Apache License 2.0 4 votes vote down vote up
public Packet getPacket() {
	return packet;
}