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

com.zlyx.easynetty.client.AbstractNettyClient Maven / Gradle / Ivy

package com.zlyx.easynetty.client;

import java.util.Arrays;

import com.zlyx.easycore.tool.Console;
import com.zlyx.easycore.tool.EasyBuffer;
import com.zlyx.easycore.utils.ByteUtils;
import com.zlyx.easynetty.client.core.AbstractBaseNettyClient;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;

/**
 * 
 * @Auth 赵光
 * @Desc 描述  Netty非塞式->长连接->客户端
 * @Date 2019年4月8日
 */
public abstract class AbstractNettyClient extends AbstractBaseNettyClient implements Runnable{

	protected String key;

	protected ChannelFactory factory;

	public AbstractNettyClient(String key, String host, int port) throws Exception {
		this.key = key;
		this.factory = new ChannelFactory(this, host, port);
	}
	
	@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
		Console.info(this.getClass(), key+ "-> 客户端与服务端建立连接...");
		doOnConnection(ctx);
    }

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		Console.info(this.getClass(), key+ "-> 客户端与服务端连接关闭...");
		doCloseConnection(ctx);
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		try {
			Object res = doService(ctx.channel(), (byte[]) msg);
			if(res != null) {
				if(res.getClass() == String.class) {
					this.write(EasyBuffer.valueOf(res));
				}else {
					this.writeBytes((byte[])res);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ReferenceCountUtil.release(msg);
		}
	}
	
	
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}
	
	/**
	 * 向服务端输出hex字符串
	 * 
	 * @param hex 16进制字符串
	 * @throws Exception
	 */
	public void write(String hex) throws Exception {
		if (EasyBuffer.notEmpty(hex)) {
			writeBytes(ByteUtils.hexStringToByte(hex));
		}
	}
	
	/**
	 * 向服务端输出Byte数组
	 * 
	 * @param bytes byte数组
	 * @throws Exception
	 */
	public void writeBytes(byte[] bytes) throws Exception {
		if (bytes != null) {
			Console.info(this.getClass(), key+ " -> "+Arrays.toString(bytes)); 
			factory.getChannel(key).writeAndFlush(bytes);
		}
	}

	
	/**
	 * 向服务端输出非16进制字符串
	 * 
	 * @param msg 非16进制字符串
	 * @throws Exception
	 */
	public void writeMsg(String msg) throws Exception {
		if (EasyBuffer.notEmpty(msg)) {
			writeBytes(msg.getBytes());
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
		e.printStackTrace();
		ctx.close();
	}

	public String getKey() {
		return key;
	}
	
	@Override
	public void run() {
		try {
			this.factory.getChannel(key);
			Console.log(this.getClass(), key+ "-> 建立一个连接"); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 关闭通道
	 */
	public void close() {
		this.factory.getChannel(key).close();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy