|
@@ -1,185 +0,0 @@
|
|
|
-package com.wdkl.ncs.android.middleware.tcp;
|
|
|
-
|
|
|
-import android.util.Log;
|
|
|
-
|
|
|
-import com.wdkl.ncs.android.component.nursehome.common.Constants;
|
|
|
-
|
|
|
-import java.util.Scanner;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-import io.netty.bootstrap.Bootstrap;
|
|
|
-import io.netty.channel.*;
|
|
|
-import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
-import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
-import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
|
|
-import io.netty.handler.codec.LengthFieldPrepender;
|
|
|
-import io.netty.handler.codec.string.StringDecoder;
|
|
|
-import io.netty.handler.codec.string.StringEncoder;
|
|
|
-import io.netty.handler.timeout.IdleStateHandler;
|
|
|
-import io.netty.util.CharsetUtil;
|
|
|
-
|
|
|
-//单例
|
|
|
-public class TcpClient {
|
|
|
- private String TAG = TcpClient.class.getSimpleName();
|
|
|
-
|
|
|
- private NioEventLoopGroup workGroup = new NioEventLoopGroup();
|
|
|
- public Channel channel;
|
|
|
- public Bootstrap bootstrap;
|
|
|
-
|
|
|
- //数据处理
|
|
|
- TcpClientHandler tcpClientHandler = new TcpClientHandler();
|
|
|
- //重试间隔
|
|
|
- private Integer retrySeconds = 3;
|
|
|
- //重试计数
|
|
|
- public Integer retryTimes = 1;
|
|
|
-
|
|
|
-
|
|
|
- //单例
|
|
|
- private static class TcpClientHolder {
|
|
|
- private static TcpClient instance = new TcpClient();
|
|
|
- }
|
|
|
-
|
|
|
- public static TcpClient getInstance() {
|
|
|
- return TcpClientHolder.instance;
|
|
|
- }
|
|
|
-
|
|
|
- //初始化Netty Tcp Client 并连接
|
|
|
- public synchronized void init(String serverIP, Integer serverPort, Integer heartBeatSeconds, ITcpCallBack iTcpCallBack) {
|
|
|
- if (TcpClientHandler.getConnected()){
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (channel!=null){
|
|
|
- if (channel.isActive()||channel.isOpen()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (bootstrap!=null){
|
|
|
- if (channel!=null) {
|
|
|
- channel.disconnect();
|
|
|
- channel = null;
|
|
|
- }
|
|
|
- bootstrap = null;
|
|
|
- workGroup = null;
|
|
|
- }
|
|
|
-
|
|
|
- final Integer hbSeconds = heartBeatSeconds;
|
|
|
- bootstrap = new Bootstrap();
|
|
|
- if (workGroup==null){
|
|
|
- workGroup = new NioEventLoopGroup();
|
|
|
- }
|
|
|
- bootstrap.group(workGroup)
|
|
|
- .channel(NioSocketChannel.class)
|
|
|
- .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 15 * 1000)
|
|
|
- .option(ChannelOption.SO_KEEPALIVE, true)
|
|
|
- .handler(new ChannelInitializer<SocketChannel>() {
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
|
- // 这里将LengthFieldBasedFrameDecoder添加到pipeline的首位,因为其需要对接收到的数据
|
|
|
- // 进行长度字段解码,这里也会对数据进行粘包和拆包处理
|
|
|
- socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
|
|
|
- // LengthFieldPrepender是一个编码器,主要是在响应字节数据前面添加字节长度字段
|
|
|
- socketChannel.pipeline().addLast(new LengthFieldPrepender(2));
|
|
|
- //心跳包应当小于服务器间隔
|
|
|
- socketChannel.pipeline().addLast(new IdleStateHandler(hbSeconds*2, hbSeconds, 0, TimeUnit.SECONDS));
|
|
|
- socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
|
|
|
- socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
|
|
|
- socketChannel.pipeline().addLast(tcpClientHandler);
|
|
|
- }
|
|
|
- }).remoteAddress(serverIP, serverPort);
|
|
|
- doConnect(iTcpCallBack);
|
|
|
- }
|
|
|
-
|
|
|
- //独立连接方法,用于重新连接
|
|
|
- public synchronized void doConnect(final ITcpCallBack iTcpCallBack) {
|
|
|
- if (channel != null && channel.isActive()) {
|
|
|
- System.out.println("TcpClient connecting");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (TcpClientHandler.getConnected()){
|
|
|
- System.out.println("TcpClient is connected");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- System.out.println("TcpClient connect start");
|
|
|
- ChannelFuture future = bootstrap.connect().addListener(new ChannelFutureListener() {
|
|
|
- @Override
|
|
|
- public void operationComplete(final ChannelFuture channelFuture) throws Exception {
|
|
|
- if (channelFuture.isSuccess()) {
|
|
|
- channel = channelFuture.channel();
|
|
|
- retryTimes = 0;
|
|
|
- System.out.println("TcpClient connect success");
|
|
|
- if (iTcpCallBack!=null) {
|
|
|
- iTcpCallBack.connected();
|
|
|
- }
|
|
|
- } else {
|
|
|
- //连接失败时的处理
|
|
|
- System.out.println("TcpClient connect retry : " + retryTimes);
|
|
|
-
|
|
|
- channelFuture.channel().eventLoop().schedule(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- if (retryTimes > 30) {
|
|
|
- System.out.println("TcpClient 重试" + (retryTimes - 1) + "次,结束");
|
|
|
-
|
|
|
- channelFuture.channel().eventLoop().schedule(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- retryTimes = 1;
|
|
|
- System.out.println("TcpClient 10分钟后 重试连接");
|
|
|
- init(Constants.Companion.getTcpServer(),Constants.Companion.getTcpPort(),Constants.Companion.getHeartBeat(),iTcpCallBack);
|
|
|
-// doConnect(iTcpCallBack);
|
|
|
- }
|
|
|
- },60*10, TimeUnit.SECONDS);
|
|
|
- } else {
|
|
|
- retryTimes++;
|
|
|
-// doConnect(iTcpCallBack);
|
|
|
- init(Constants.Companion.getTcpServer(),Constants.Companion.getTcpPort(),Constants.Companion.getHeartBeat(),iTcpCallBack);
|
|
|
- }
|
|
|
- }
|
|
|
- }, retrySeconds, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-// try {
|
|
|
-// future.sync();
|
|
|
-// future.channel().closeFuture().sync();
|
|
|
-// } catch (InterruptedException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// } finally {
|
|
|
- //重试之前不关
|
|
|
-// try {
|
|
|
-// Thread.sleep(retrySeconds * retryTimes*1000 + 1000);
|
|
|
-// } catch (InterruptedException e) {
|
|
|
-// }
|
|
|
-// workGroup.shutdownGracefully();
|
|
|
-// }
|
|
|
- }
|
|
|
-
|
|
|
- //发送消息,线程安全
|
|
|
- public synchronized void sendMsg(final String content) {
|
|
|
- Log.d(TAG, "准备发送的数据 " + content);
|
|
|
- tcpClientHandler.sendMsg(content);
|
|
|
- }
|
|
|
-
|
|
|
- //测试
|
|
|
- public static void main(String[] args) {
|
|
|
-
|
|
|
- new Thread(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- TcpClient.getInstance().init("192.168.1.188", 5080, 9,null);
|
|
|
- }
|
|
|
- }).start();
|
|
|
-
|
|
|
- Scanner scanner = new Scanner(System.in);
|
|
|
- while (true) {
|
|
|
- System.out.println("please type : ");
|
|
|
- String line = scanner.nextLine();
|
|
|
- TcpClient.getInstance().sendMsg(line);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|