Java Code Examples for org.tio.core.ChannelContext#getClientNode()
The following examples show how to use
org.tio.core.ChannelContext#getClientNode() .
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: AbstractAioHandler.java From md_blockchain with Apache License 2.0 | 5 votes |
/** * 解码:把接收到的ByteBuffer,解码成应用可以识别的业务消息包 * 消息头:type + bodyLength * 消息体:byte[] */ @Override public BlockPacket decode(ByteBuffer buffer, ChannelContext channelContext) throws AioDecodeException { int readableLength = buffer.limit() - buffer.position(); if (readableLength < BlockPacket.HEADER_LENGTH) { return null; } //消息类型 byte type = buffer.get(); int bodyLength = buffer.getInt(); if (bodyLength < 0) { throw new AioDecodeException("bodyLength [" + bodyLength + "] is not right, remote:" + channelContext .getClientNode()); } int neededLength = BlockPacket.HEADER_LENGTH + bodyLength; int test = readableLength - neededLength; // 不够消息体长度(剩下的buffer组不了消息体) if (test < 0) { return null; } BlockPacket imPacket = new BlockPacket(); imPacket.setType(type); if (bodyLength > 0) { byte[] dst = new byte[bodyLength]; buffer.get(dst); imPacket.setBody(dst); } return imPacket; }
Example 2
Source File: BaseAioHandler.java From blockchain with Apache License 2.0 | 5 votes |
/** * 解码:把接收到的ByteBuffer,解码成应用可以识别的业务消息包 * 总的消息结构:消息头 + 消息类别 + 消息体 * 消息头结构: 4个字节,存储消息体的长度 * 消息类别: 1 个字节, 存储类别,S => 字符串, B => 区块, T => 交易 * 消息体结构: 对象的json串的byte[] */ public MessagePacket decode(ByteBuffer buffer, ChannelContext channelContext) throws AioDecodeException { int readableLength = buffer.limit() - buffer.position(); //收到的数据组不了业务包,则返回null以告诉框架数据不够 if (readableLength < MessagePacket.HEADER_LENGTH) { return null; } //读取消息类别 byte messageType = buffer.get(); //读取消息体的长度 int bodyLength = buffer.getInt(); //数据不正确,则抛出AioDecodeException异常 if (bodyLength < 0) { throw new AioDecodeException("bodyLength [" + bodyLength + "] is not right, remote:" + channelContext.getClientNode()); } //计算本次需要的数据长度 int neededLength = MessagePacket.HEADER_LENGTH + bodyLength; //收到的数据是否足够组包 int isDataEnough = readableLength - neededLength; // 不够消息体长度(剩下的buffe组不了消息体) if (isDataEnough < 0) { return null; } else //组包成功 { MessagePacket imPacket = new MessagePacket(); imPacket.setType(messageType); if (bodyLength > 0) { byte[] dst = new byte[bodyLength]; buffer.get(dst); imPacket.setBody(dst); } return imPacket; } }
Example 3
Source File: ClientNodes.java From t-io with Apache License 2.0 | 5 votes |
/** * * @param channelContext * @return * @author tanyaowu */ public static String getKey(ChannelContext channelContext) { Node clientNode = channelContext.getClientNode(); if (clientNode == null) { throw new RuntimeException("client node is null"); } String key = getKey(clientNode.getIp(), clientNode.getPort()); return key; }
Example 4
Source File: TioRpcServerEndpoint.java From nutzcloud with Apache License 2.0 | 5 votes |
@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; } }