Android Netty开发示例

Android Netty开发示例

本文涉及到其它的基础知识有:

  • 对数据的字节处理
  • 对数据进行CRC32校验
  • 对数据的内容进行Blowfish加/解密

这部分内容不会在本文深入探究,在“代码片段讲解”部分会提到一二。


Netty是什么

Netty是一个高性能、异步事件驱动的NIO框架,它提供了对TCP、UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户可以方便的主动获取或者通过通知机制获得IO操作结果。扩展阅读

代码片段讲解

package com.osanwen.nettydemo;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import timber.log.Timber;

/**
 * Netty客户端
 * Created by LiuSaibao on 11/23/2016.
 */
public class NettyClient {

    private static NettyClient nettyClient = new NettyClient();

    private EventLoopGroup group;

    private NettyListener listener;

    private Channel channel;

    private boolean isConnect = false;

    private int reconnectNum = Integer.MAX_VALUE;

    private long reconnectIntervalTime = 5000;

    public static NettyClient getInstance(){
        return nettyClient;
    }

    public synchronized NettyClient connect() {
        if (!isConnect) {
            group = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap().group(group)
                    .option(ChannelOption.SO_KEEPALIVE,true)
                    .channel(NioSocketChannel.class)
                    .handler(new NettyClientInitializer(listener));

            try {
                bootstrap.connect(Const.HOST, Const.TCP_PORT).addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture channelFuture) throws Exception {
                        if (channelFuture.isSuccess()) {
                            isConnect = true;
                            channel = channelFuture.channel();
                        } else {
                            isConnect = false;
                        }
                    }
                }).sync();

            } catch (Exception e) {
                Timber.e(e, e.getMessage());
                listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_ERROR);
                reconnect();
            }
        }
        return this;
    }

    public void disconnect() {
        group.shutdownGracefully();
    }

    public void reconnect() {
        if(reconnectNum >0 && !isConnect){
            reconnectNum--;
            try {
                Thread.sleep(reconnectIntervalTime);
            } catch (InterruptedException e) {}
            Timber.e("重新连接");
            disconnect();
            connect();
        }else{
            disconnect();
        }
    }

    public boolean sendMsgToServer(byte[] data, ChannelFutureListener listener) {
        boolean flag = channel != null && isConnect;
        if (flag) {
            ByteBuf buf = Unpooled.copiedBuffer(data);
            channel.writeAndFlush(buf).addListener(listener);
        }
        return flag;
    }

    public void setReconnectNum(int reconnectNum) {
        this.reconnectNum = reconnectNum;
    }

    public void setReconnectIntervalTime(long reconnectIntervalTime) {
        this.reconnectIntervalTime = reconnectIntervalTime;
    }

    public boolean getConnectStatus(){
        return isConnect;
    }

    public void setConnectStatus(boolean status){
        this.isConnect = status;
    }

    public void setListener(NettyListener listener) {
        this.listener = listener;
    }
}

NettyClient类是对Netty进行封装,用于建立连接、断开连接、异常断开重连以及向服务器发送消息。

package com.osanwen.nettydemo;

import io.netty.buffer.ByteBuf;

/**
 *
 * Created by LiuSaibao on 11/23/2016.
 */
public interface NettyListener {

    public final static byte STATUS_CONNECT_SUCCESS = 1;

    public final static byte STATUS_CONNECT_CLOSED = 0;

    public final static byte STATUS_CONNECT_ERROR = 0;


    /**
     * 对消息的处理
     */
    void onMessageResponse(ByteBuf byteBuf);

    /**
     * 当服务状态发生变化时触发
     */
    public void onServiceStatusConnectChanged(int statusCode);
}

NettyListener类用于监听连接的状态和消息响应。

package com.osanwen.nettydemo;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

/**
 *
 * Created by LiuSaibao on 11/23/2016.
 */
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {

    private NettyListener listener;

    private int WRITE_WAIT_SECONDS = 10;

    private int READ_WAIT_SECONDS = 13;

    public NettyClientInitializer(NettyListener listener) {
        this.listener = listener;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        SslContext sslCtx = SslContextBuilder.forClient()
                .trustManager(InsecureTrustManagerFactory.INSTANCE).build();

        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));    // 开启SSL
        pipeline.addLast(new LoggingHandler(LogLevel.INFO));    // 开启日志,可以设置日志等级
//        pipeline.addLast(new IdleStateHandler(30, 60, 100));
        pipeline.addLast(new NettyClientHandler(listener));
    }
}

在Netty中,每个Channel被创建的时候都需要被关联一个对应的pipeline(通道),这种关联关系是永久的(整个程序运行的生命周期中)。ChannelPipeline可以理解成一个消息( 或消息事件,ChanelEvent)流转的通道,在这个通道中可以被附上许多用来处理消息的handler,当消息在这个通道中流转的时候,如果有与这个消息类型相对应的handler,就会触发这个handler去执行相应的动作。扩展阅读

package com.osanwen.nettydemo;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 *
 * Created by LiuSaibao on 11/23/2016.
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private NettyListener listener;
    public NettyClientHandler(NettyListener listener){
        this.listener = listener;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        NettyClient.getInstance().setConnectStatus(true);
        listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_SUCCESS);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        NettyClient.getInstance().setConnectStatus(false);
        listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_CLOSED);
        NettyClient.getInstance().reconnect();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        listener.onMessageResponse(byteBuf);
    }

    /*@Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE){
                ctx.close();
            }else if (event.state() == IdleState.WRITER_IDLE){
                try{
                    ctx.channel().writeAndFlush("Chilent-Ping\r\n");
                } catch (Exception e){
                    Timber.e(e.getMessage());
                }
            }
        }
        super.userEventTriggered(ctx, evt);
    }*/
}

NettyClientHandler类注释掉了Netty提供心跳机制,稍后会在NettyService类中看到实现自定义的心跳。该类用于获取状态发生改变和消息响应。

package com.osanwen.nettydemo;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import timber.log.Timber;

/**
 *
 * Created by LiuSaibao on 11/17/2016.
 */
public class NettyService extends Service implements NettyListener {

    private NetworkReceiver receiver;
    private static String sessionId = null;

    private ScheduledExecutorService mScheduledExecutorService;
    private void shutdown() {
        if (mScheduledExecutorService != null) {
            mScheduledExecutorService.shutdown();
            mScheduledExecutorService = null;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        receiver = new NetworkReceiver();
        IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);

        // 自定义心跳,每隔20秒向服务器发送心跳包
        mScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                byte[] requestBody = {(byte) 0xFE, (byte)0xED, (byte)0xFE, 5};
                NettyClient.getInstance().sendMsgToServer(requestBody, new ChannelFutureListener() {    //3
                    @Override
                    public void operationComplete(ChannelFuture future) {
                        if (future.isSuccess()) {                //4
                            Timber.d("Write heartbeat successful");

                        } else {
                            Timber.e("Write heartbeat error");
                            WriteLogUtil.writeLogByThread("heartbeat error");
                        }
                    }
                });
            }
        }, 20, 20, TimeUnit.SECONDS);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NettyClient.getInstance().setListener(this);
        connect();
        return START_NOT_STICKY;
    }

    @Override
    public void onServiceStatusConnectChanged(int statusCode) {     //连接状态监听
        Timber.d("connect status:%d", statusCode);
        if (statusCode == NettyListener.STATUS_CONNECT_SUCCESS) {
            authenticData();
        } else {
            WriteLogUtil.writeLogByThread("tcp connect error");
        }
    }

    /**
     * 认证数据请求
     */
    private void authenticData() {
        AuthModel auth = new AuthModel();
        auth.setI(102);
        auth.setU("YAMSUCFS8G");
        auth.setN("G4-一单元");
        auth.setF("51");
        auth.setT((int)(System.currentTimeMillis() / 1000));
        byte[] content = RequestUtil.getEncryptBytes(auth);
        byte[] requestHeader = RequestUtil.getRequestHeader(content, 1, 1001);
        byte[] requestBody = RequestUtil.getRequestBody(requestHeader, content);
        NettyClient.getInstance().sendMsgToServer(requestBody, new ChannelFutureListener() {    //3
            @Override
            public void operationComplete(ChannelFuture future) {
                if (future.isSuccess()) {                //4
                    Timber.d("Write auth successful");
                } else {
                    Timber.d("Write auth error");
                    WriteLogUtil.writeLogByThread("tcp auth error");
                }
            }
        });
    }

    @Override
    public void onMessageResponse(ByteBuf byteBuf) {
        byte[] bytes = byteBuf.array();
        Timber.d("tcp receive data:%s", ByteUtil.bytesToHex(bytes));
        // 接收
        if (0xED == ByteUtil.unsignedByteToInt(bytes[0])
                && 0xFE == ByteUtil.unsignedByteToInt(bytes[1])) {
            if (1 == bytes[2]) {
                int cardinal = (int)ByteUtil.unsigned4BytesToInt(bytes, 5);
                int realLen = cardinal + 9;
                int len = byteBuf.writerIndex();
                // 接收到的数据有可能会粘包,只需要判断数据的长度大于或者等于真实的长度即可
                if (len >= realLen) {

                    int word = ByteUtil.bytesToShort(ByteUtil.subBytes(bytes, 3, 2));
                    if (word == 1001) {
                        byte[] data = new byte[cardinal];
                        System.arraycopy(bytes, 9, data, 0, data.length);
                        Blowfish blowfish = new Blowfish();
                        String result = new String(blowfish.decryptByte(data));
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            sessionId = jsonObject.getString("s");
                        } catch (JSONException e) {
                            Timber.e(e, e.getMessage());
                        }
                    } else if (word == 2002) {
                        byte[] data = new byte[cardinal];
                        System.arraycopy(bytes, 9, data, 0, data.length);
                        Blowfish blowfish = new Blowfish();
                        String result = new String(blowfish.decryptByte(data));
                        Timber.d(result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            handle(word, jsonObject.getInt("i"), jsonObject.getInt("r"));
                        } catch (JSONException e) {
                            Timber.e(e, e.getMessage());
                        }
                    } else {
                        String log = "undefined request type";
                        Timber.e(log);
                        WriteLogUtil.writeLogByThread(log);
                    }
                } else {
                    String log = String.format("request byte array content length inequality, realLen=%d, len=%d", realLen, len);
                    Timber.e(log);
                    WriteLogUtil.writeLogByThread(log);
                }
            } else if (5 == bytes[2]) {
                Timber.e("heartbeat");
            }

            // 响应
        } else if (0xFE == ByteUtil.unsignedByteToInt(bytes[0])
                && 0xED == ByteUtil.unsignedByteToInt(bytes[1])
                && 0xFE == ByteUtil.unsignedByteToInt(bytes[2])) {
            if (1 == bytes[3]) {
                // 忽略bytes[4],bytes[5]。作用是接口升级
                int cardinal = (int)ByteUtil.unsigned4BytesToInt(bytes, 8);
                int len = byteBuf.writerIndex();
                // 前12个字节是请求头,后4个字节是校验值
                int realLen = cardinal + 12 + 4;
                // 返回的数据有可能会粘包,只需要判断数据的长度大于或者等于真实的长度即可
                if (len >= realLen) {
                    int word = ByteUtil.bytesToShort(ByteUtil.subBytes(bytes, 6, 2));
                    if (word == 2001) {
                        byte[] data = new byte[cardinal];
                        System.arraycopy(bytes, 12, data, 0, data.length);
                        byte[] crc32 = new byte[4];
                        System.arraycopy(bytes, realLen - 4, crc32, 0, crc32.length);

                        // 对内容进行CRC校验
                        if (CRC32Util.getCRC32Long(data) == ByteUtil.unsigned4BytesToInt(crc32, 0)) {
                            Blowfish blowfish = new Blowfish();
                            String result = new String(blowfish.decryptByte(data));
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                int i = jsonObject.getInt("i");
                                if (sessionId == null) {
                                    WriteLogUtil.writeLogByThread("sessionId is null");
                                    authenticData();
                                    handle(word, i, 0);
                                    return;
                                }
                                byte[] session = sessionId.getBytes();
                                byte[] sign = "WiseUC@2016".getBytes();
                                byte[] content = new byte[session.length + sign.length];
                                System.arraycopy(session, 0, content, 0, session.length);
                                System.arraycopy(sign, 0, content, session.length, sign.length);

                                // 对Session ID进行CRC校验
                                if (jsonObject.getLong("c") == CRC32Util.getCRC32(content)) {
                                    handle(word, i, 1);
                                } else {
                                    String log = "open the door session id crc32 verification failure";
                                    Timber.e(log);
                                    WriteLogUtil.writeLogByThread(log);
                                }
                            } catch (JSONException e) {
                                Timber.e(e, e.getMessage());
                            }
                        } else {
                            String log = "open the door crc32 data verification failure";
                            Timber.e(log);
                            WriteLogUtil.writeLogByThread(log);
                        }
                    } else {
                        String log = "undefined response type";
                        Timber.e(log);
                        WriteLogUtil.writeLogByThread(log);
                    }
                } else {
                    String log = String.format("response byte array content length inequality, realLen=%d, len=%d", realLen, len);
                    Timber.e(log);
                    WriteLogUtil.writeLogByThread(log);
                }
            } else if (5 == bytes[3]) {
                Timber.e("heartbeat");
            }
        } else {
            Timber.e("unknown");
            WriteLogUtil.writeLogByThread("unknown");
        }
    }

    private void handle(int t, int i, int f) {
        // TODO 实现自己的业务逻辑
    }

    private void connect(){
        if (!NettyClient.getInstance().getConnectStatus()) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    NettyClient.getInstance().connect();//连接服务器
                }
            }).start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        shutdown();
        NettyClient.getInstance().setReconnectNum(0);
        NettyClient.getInstance().disconnect();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public class NetworkReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if (activeNetwork != null) { // connected to the internet
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI
                        || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    connect();
                }
            }
        }
    }
}

NettyService类实现了业务的处理逻辑。

基本数据类型对应二进制的位数

基本数据类型二进制位数
boolean1
byte8
char16
short16
int32
long64
float32
double64

这张表大家都不会陌生,对于网络编程这块,都是对字节数组操作。例如:截取字节数组;将多个字节数组拼接到一个新的字节数组中传输。Java中的基本数据类型是没有无符号的,需要自己去封装,基本数据类型之间的转换就需要看这张表,将byte[]转换成short,int,long等其他类型是有区别,看对应二进制位数。

网络编程会出现粘包的情况,可以和服务端约定数据长度来解决粘包问题,示例就是使用这种方式解决粘包问题的。

示例源代码


  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值