All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fisco.bcos.channel.handler.ChannelHandler Maven / Gradle / Ivy

There is a newer version: 2.6.6
Show newest version
package org.fisco.bcos.channel.handler;

import java.util.concurrent.RejectedExecutionException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateEvent;

public class ChannelHandler extends SimpleChannelInboundHandler {
	private static Logger logger = LoggerFactory.getLogger(ChannelHandler.class);
	
	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress();
		Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort();
		
		if (evt instanceof IdleStateEvent) {
			IdleStateEvent e = (IdleStateEvent) evt;
			switch (e.state()) {
			case READER_IDLE:
			case WRITER_IDLE:
			case ALL_IDLE:
				logger.error("event:{} connect{}:{} long time Inactive,disconnect", e.state(), host, port);
				channelInactive(ctx);
				ctx.disconnect();
				ctx.close();
				break;
			default:
				break;
			}
		}
	}
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		try {
			//已连上,获取ip信息
			String host = ((SocketChannel)ctx.channel()).remoteAddress().getAddress().getHostAddress();
			Integer port = ((SocketChannel)ctx.channel()).remoteAddress().getPort();
			
			logger.debug("success,connected[" + host + "]:[" + String.valueOf(port) + "]," + String.valueOf(ctx.channel().isActive()));
			
			if(isServer) {
				logger.debug("server accept new connect: {}:{}", host, port);
				//将此新连接增加到connections
				ConnectionInfo info = new ConnectionInfo();
				info.setHost(host);
				info.setPort(port);
				
				connections.getConnections().add(info);
				connections.setNetworkConnectionByHost(info.getHost(), info.getPort(), ctx);
				connections.getCallback().onConnect(ctx);
			}
			else {
				//更新ctx信息
				ChannelHandlerContext connection = connections.getNetworkConnectionByHost(host, port);
				if(connection != null && connection.channel().isActive()) {
					logger.debug("connect available, close reconnect: {}:{}", host, port);
					
					ctx.channel().disconnect();
					ctx.channel().close();
				}
				else {
					logger.debug("client connect success {}:{}", host, port);
					connections.setNetworkConnectionByHost(host, port, ctx);
					connections.getCallback().onConnect(ctx);
				}
			}
		}
		catch(Exception e) {
			logger.error("error", e);
		}
	}
	
	@Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		try {
			logger.debug("disconnect");
			//已断连,获取ip信息
			String host = ((SocketChannel)ctx.channel()).remoteAddress().getAddress().getHostAddress();
			Integer port = ((SocketChannel)ctx.channel()).remoteAddress().getPort();
			
			logger.debug("disconnect " + host + ":" + String.valueOf(port) + " ," + String.valueOf(ctx.channel().isActive()));
			
			if(isServer) {
				//server模式下,移除该connectionInfo
				for(Integer i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy