Java Code Examples for io.netty.channel.socket.SocketChannel#attr()
The following examples show how to use
io.netty.channel.socket.SocketChannel#attr() .
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: ConnectionManager.java From nuls-v2 with MIT License | 5 votes |
private void cacheNode(Node node, SocketChannel channel) { String name = "node-" + node.getId(); boolean exists = AttributeKey.exists(name); AttributeKey attributeKey; if (exists) { attributeKey = AttributeKey.valueOf(name); } else { attributeKey = AttributeKey.newInstance(name); } Attribute<Node> attribute = channel.attr(attributeKey); attribute.set(node); }
Example 2
Source File: ServerChannelHandler.java From nuls-v2 with MIT License | 5 votes |
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) { SocketChannel channel = (SocketChannel) ctx.channel(); ByteBuf buf = (ByteBuf) msg; String remoteIP = channel.remoteAddress().getHostString(); NulsByteBuffer byteBuffer = null; Node node = null; try { String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); node = nodeAttribute.get(); if (node != null) { byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); byteBuffer = new NulsByteBuffer(bytes); } else { LoggerUtil.COMMON_LOG.error("-----------------Server channelRead node is null -----------------" + remoteIP + ":" + channel.remoteAddress().getPort()); ctx.channel().close(); } } catch (Exception e) { LoggerUtil.COMMON_LOG.error(e); // throw e; } finally { buf.clear(); } MessageManager.getInstance().receiveMessage(byteBuffer, node); }
Example 3
Source File: ServerChannelHandler.java From nuls-v2 with MIT License | 5 votes |
@Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); SocketChannel channel = (SocketChannel) ctx.channel(); String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); Node node = nodeAttribute.get(); if (node != null && node.getDisconnectListener() != null) { node.getDisconnectListener().action(); } LoggerUtil.COMMON_LOG.info("Server Node is channelUnregistered:{}:{}", channel.remoteAddress().getHostString(), channel.remoteAddress().getPort()); }
Example 4
Source File: NodeManager.java From nuls with MIT License | 5 votes |
private void cacheNode(Node node, SocketChannel channel) { String name = "node-" + node.getId(); boolean exists = AttributeKey.exists(name); AttributeKey attributeKey; if (exists) { attributeKey = AttributeKey.valueOf(name); } else { attributeKey = AttributeKey.newInstance(name); } Attribute<Node> attribute = channel.attr(attributeKey); attribute.set(node); }
Example 5
Source File: ServerChannelHandler.java From nuls with MIT License | 5 votes |
/** * 继承SimpleChannelInboundHandler后,只需要重新channelRead0方法,msg会自动释放 * @param ctx * @param msg * @throws Exception */ @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { SocketChannel channel = (SocketChannel) ctx.channel(); String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); Node node = nodeAttribute.get(); ByteBuf buf = (ByteBuf) msg; messageProcessor.processor(buf, node); }
Example 6
Source File: ServerChannelHandler.java From nuls with MIT License | 5 votes |
@Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); SocketChannel channel = (SocketChannel) ctx.channel(); String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); Node node = nodeAttribute.get(); if (node != null && node.getDisconnectListener() != null) { node.getDisconnectListener().action(); } //channelUnregistered之前,channel就已经close了,可以调用channel.isOpen()查看状态 //channel.close(); }