├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
└── runConfigurations.xml
├── app
├── .gitignore
├── build.gradle
├── libs
│ └── netty-all-4.0.44.Final.jar
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── zmm
│ │ └── netty4msgpacktest
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── zmm
│ │ │ └── netty4msgpacktest
│ │ │ ├── MainActivity.java
│ │ │ ├── client
│ │ │ ├── Client.java
│ │ │ └── ClientHandler.java
│ │ │ ├── code
│ │ │ ├── MsgPackDecode.java
│ │ │ └── MsgPackEncode.java
│ │ │ ├── common
│ │ │ └── CustomHeartbeatHandler.java
│ │ │ ├── domain
│ │ │ ├── DeviceValue.java
│ │ │ └── TypeData.java
│ │ │ └── server
│ │ │ ├── Server.java
│ │ │ └── ServerHandler.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── zmm
│ └── netty4msgpacktest
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | $USER_HOME$/.subversion
48 |
49 |
50 |
51 |
52 |
53 | 1.8
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.zmm.netty4msgpacktest"
8 | minSdkVersion 18
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.3.0'
28 | testCompile 'junit:junit:4.12'
29 | compile 'org.msgpack:msgpack:0.6.12'
30 | }
31 |
--------------------------------------------------------------------------------
/app/libs/netty-all-4.0.44.Final.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Giousa/Netty4MsgPackTest/257164063309f4bb40bf3db0e6932bbc95bf2b02/app/libs/netty-all-4.0.44.Final.jar
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/zhangmengmeng/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/zmm/netty4msgpacktest/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.zmm.netty4msgpacktest;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.zmm.netty4msgpacktest", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zmm/netty4msgpacktest/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.zmm.netty4msgpacktest;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 |
6 | public class MainActivity extends AppCompatActivity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_main);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zmm/netty4msgpacktest/client/Client.java:
--------------------------------------------------------------------------------
1 | package com.zmm.netty4msgpacktest.client;
2 |
3 | import com.zmm.netty4msgpacktest.code.MsgPackDecode;
4 | import com.zmm.netty4msgpacktest.code.MsgPackEncode;
5 | import com.zmm.netty4msgpacktest.domain.DeviceValue;
6 | import com.zmm.netty4msgpacktest.domain.TypeData;
7 |
8 | import java.util.Random;
9 | import java.util.concurrent.TimeUnit;
10 |
11 | import io.netty.bootstrap.Bootstrap;
12 | import io.netty.channel.Channel;
13 | import io.netty.channel.ChannelFuture;
14 | import io.netty.channel.ChannelFutureListener;
15 | import io.netty.channel.ChannelInitializer;
16 | import io.netty.channel.ChannelPipeline;
17 | import io.netty.channel.nio.NioEventLoopGroup;
18 | import io.netty.channel.socket.SocketChannel;
19 | import io.netty.channel.socket.nio.NioSocketChannel;
20 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
21 | import io.netty.handler.timeout.IdleStateHandler;
22 |
23 | /**
24 | * Description:
25 | * Author:Giousa
26 | * Date:2017/2/9
27 | * Email:65489469@qq.com
28 | */
29 | public class Client {
30 | private NioEventLoopGroup workGroup = new NioEventLoopGroup(4);
31 | private Channel channel;
32 | private Bootstrap bootstrap;
33 |
34 | public static void main(String[] args) throws Exception {
35 | Client client = new Client();
36 | client.start();
37 | client.sendData();
38 | }
39 |
40 | public void start() {
41 | try {
42 | bootstrap = new Bootstrap();
43 | bootstrap
44 | .group(workGroup)
45 | .channel(NioSocketChannel.class)
46 | .handler(new ChannelInitializer() {
47 | protected void initChannel(SocketChannel socketChannel) throws Exception {
48 | ChannelPipeline p = socketChannel.pipeline();
49 | p.addLast(new IdleStateHandler(0, 0, 5));
50 | // p.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, -4, 0));
51 | p.addLast(new MsgPackDecode());
52 | p.addLast(new MsgPackEncode());
53 | p.addLast(new ClientHandler(Client.this));
54 | }
55 | });
56 | doConnect();
57 |
58 | } catch (Exception e) {
59 | throw new RuntimeException(e);
60 | }
61 | }
62 |
63 | /**
64 | * 重连机制,每隔2s重新连接一次服务器
65 | */
66 | protected void doConnect() {
67 | if (channel != null && channel.isActive()) {
68 | return;
69 | }
70 |
71 | ChannelFuture future = bootstrap.connect("127.0.0.1", 12345);
72 |
73 | future.addListener(new ChannelFutureListener() {
74 | public void operationComplete(ChannelFuture futureListener) throws Exception {
75 | if (futureListener.isSuccess()) {
76 | channel = futureListener.channel();
77 | System.out.println("Connect to server successfully!");
78 | } else {
79 | System.out.println("Failed to connect to server, try connect after 2s");
80 |
81 | futureListener.channel().eventLoop().schedule(new Runnable() {
82 | @Override
83 | public void run() {
84 | doConnect();
85 | }
86 | }, 2, TimeUnit.SECONDS);
87 | }
88 | }
89 | });
90 | }
91 |
92 | /**
93 | * 发送数据 每隔2秒发送一次
94 | * @throws Exception
95 | */
96 | public void sendData() throws Exception {
97 | Random random = new Random(System.currentTimeMillis());
98 | for (int i = 0; i < 10000; i++) {
99 | if (channel != null && channel.isActive()) {
100 |
101 | DeviceValue deviceValue = new DeviceValue();
102 | deviceValue.setType(TypeData.CUSTOME);
103 | deviceValue.setAngle(i%15);
104 | deviceValue.setSeatId(i%30);
105 | deviceValue.setSpeed(i%120);
106 |
107 | System.out.println("client 发送数据:"+deviceValue.toString());
108 |
109 | channel.writeAndFlush(deviceValue);
110 | }
111 |
112 | Thread.sleep(random.nextInt(20000));
113 | }
114 | }
115 |
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zmm/netty4msgpacktest/client/ClientHandler.java:
--------------------------------------------------------------------------------
1 | package com.zmm.netty4msgpacktest.client;
2 |
3 | import com.zmm.netty4msgpacktest.common.CustomHeartbeatHandler;
4 | import com.zmm.netty4msgpacktest.domain.DeviceValue;
5 | import com.zmm.netty4msgpacktest.domain.TypeData;
6 |
7 | import io.netty.channel.ChannelHandlerContext;
8 |
9 | /**
10 | * Description:
11 | * Author:Giousa
12 | * Date:2017/2/9
13 | * Email:65489469@qq.com
14 | */
15 | public class ClientHandler extends CustomHeartbeatHandler {
16 | private Client client;
17 | public ClientHandler(Client client) {
18 | super("client");
19 | this.client = client;
20 | }
21 |
22 |
23 | @Override
24 | protected void handleData(ChannelHandlerContext channelHandlerContext, Object msg) {
25 | DeviceValue deviceValue = (DeviceValue) msg;
26 | System.out.println("client 接收数据:"+deviceValue.toString());
27 |
28 | // DeviceValue s = new DeviceValue();
29 | // s.setType(TypeData.CUSTOME);
30 | // s.setSpeed(0);
31 | // s.setAngle(15);
32 | // s.setSeatId(TypeData.SERVER_RESPONSE);
33 | // channelHandlerContext.writeAndFlush(s);
34 | }
35 |
36 | @Override
37 | protected void handleAllIdle(ChannelHandlerContext ctx) {
38 | super.handleAllIdle(ctx);
39 | sendPingMsg(ctx);
40 | }
41 |
42 | @Override
43 | public void channelInactive(ChannelHandlerContext ctx) throws Exception {
44 | super.channelInactive(ctx);
45 | client.doConnect();
46 | }
47 |
48 | @Override
49 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
50 | System.out.println(name + " exception"+cause.toString());
51 |
52 | }
53 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/zmm/netty4msgpacktest/code/MsgPackDecode.java:
--------------------------------------------------------------------------------
1 | package com.zmm.netty4msgpacktest.code;
2 |
3 | import com.zmm.netty4msgpacktest.domain.DeviceValue;
4 |
5 | import org.msgpack.MessagePack;
6 |
7 | import java.util.List;
8 |
9 | import io.netty.buffer.ByteBuf;
10 | import io.netty.channel.ChannelHandlerContext;
11 | import io.netty.handler.codec.MessageToMessageDecoder;
12 |
13 | /**
14 | * Description:解码
15 | * Author:Giousa
16 | * Date:2017/2/10
17 | * Email:65489469@qq.com
18 | */
19 | public class MsgPackDecode extends MessageToMessageDecoder {
20 |
21 |
22 | @Override
23 | protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List