├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── goav │ │ └── app │ │ ├── MainActivity.java │ │ ├── NettyTest.kt │ │ └── SocketPong.kt │ └── 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 ├── build.gradle ├── config.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml ├── kotlin.gradle ├── netty-android ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── goav │ │ └── netty │ │ ├── Handler │ │ ├── ChannelCloseHandler.kt │ │ ├── ChannelResponseHandler.kt │ │ ├── Client.kt │ │ ├── ClientHelper.kt │ │ ├── DecodeHandler.kt │ │ └── EncodeHandler.kt │ │ ├── Impl │ │ ├── ChannelConnectImpl.kt │ │ ├── ChannelHandlerImpl.kt │ │ ├── ChannelMessageImpl.kt │ │ ├── ChannelReleaseImpl.kt │ │ ├── ClientImpl.kt │ │ └── ClientOptImpl.kt │ │ └── message │ │ ├── GsonHelper.kt │ │ └── MessageBasic.kt │ └── res │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | build 9 | /captures 10 | .externalNativeBuild 11 | .idea 12 | 13 | gradle.properties -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | #sudo: required 3 | sudo: false 4 | jdk: oraclejdk8 5 | android: 6 | components: 7 | - platform-tools 8 | - tools 9 | - android-25 10 | - build-tools-26.0.2 11 | - add-one 12 | - extra-android-m2repository 13 | - extra-google-m2repository 14 | - extra-android-support 15 | licenses: 16 | - 'android-sdk-license-.+' 17 | 18 | #before_cache: 19 | # - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 20 | # - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 21 | 22 | #cache: 23 | # directories: 24 | # - $HOME/.gradle/caches/ 25 | # - $HOME/.gradle/wrapper/ 26 | 27 | script: 28 | - ./gradlew clean build assembleRelease 29 | - ./gradlew jacocoTestReport 30 | 31 | after_success: 32 | - bash <(curl -s https://codecov.io/bash) 33 | 34 | 35 | branches: 36 | only: 37 | - kotlin-master 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 goAV 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Netty for Android Client 2 | 3 | [![GitHub tag](https://img.shields.io/github/tag/goAV/NettyAndroid.svg)](https://github.com/goAV/NettyAndroid/tags) 4 | [![GitHub release](https://img.shields.io/github/release/goAV/NettyAndroid.svg)](https://github.com/goAV/NettyAndroid/releases/latest) 5 | [![Github commits (since latest release)](https://img.shields.io/github/commits-since/goav/nettyandroid/latest.svg)](https://github.com/goAV/NettyAndroid/commits/kotlin-master) 6 | 7 | [![NettyAndroid](https://jitpack.io/v/kotow-hub/NettyAndroid.svg)](https://jitpack.io/#kotow-hub/NettyAndroid) 8 | [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/FIRHQ/fir-cli/master/LICENSE.txt) 9 | [![Build Status](https://travis-ci.org/kotow-hub/NettyAndroid.svg?branch=kotlin-master)](https://travis-ci.org/kotow-hub/NettyAndroid) 10 | ### Introduction 11 | 12 | * user library of [`io.netty:netty-all:4.1.16.Final`](https://github.com/netty/netty) 13 | * parse data of [`gson-2.7`](http://mvnrepository.com/artifact/com.google.code.gson/gson/2.7) 14 | 15 | ### Simple 16 | See [`app->NettyTest.kt`](./app/src/main/java/com/goav/app/NettyTest.kt) 17 | 18 | ### Gradle 19 | ```groovy 20 | dependencies { 21 | compile 'com.github.kotow-hub:NettyAndroid:latest.release' 22 | } 23 | ``` 24 | 25 | ### PS 26 | 27 | * look at the [`EncodeHandler`](netty-android/src/main/java/com/goav/netty/Handler/EncodeHandler.kt) 28 | 29 | The content’s length and `int`’s length are the length of request’s body, see the [`size + 4`](netty-android/src/main/java/com/goav/netty/Handler/EncodeHandler.kt#L27) 30 | 31 | 32 | * look at the [`DecodeHandler`](netty-android/src/main/java/com/goav/netty/Handler/DecodeHandler.kt) 33 | 34 | The content's length and `int`'s length are the length of response's body, see the [`'in'.readInt() - 4`](netty-android/src/main/java/com/goav/netty/Handler/DecodeHandler.kt#L40) 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /src/test 3 | /src/androidTest -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'me.tatarka.retrolambda' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "26.0.2" 7 | defaultConfig { 8 | applicationId "com.goav.app" 9 | minSdkVersion 14 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | 16 | compileOptions { 17 | targetCompatibility JavaVersion.VERSION_1_8 18 | sourceCompatibility JavaVersion.VERSION_1_8 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | 28 | lintOptions { 29 | abortOnError false 30 | } 31 | 32 | 33 | } 34 | 35 | dependencies { 36 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 37 | exclude group: 'com.android.support', module: 'support-annotations' 38 | }) 39 | compile 'com.android.support:appcompat-v7:25.3.1' 40 | testCompile 'junit:junit:4.12' 41 | compile 'com.google.code.gson:gson:2.7' 42 | compile project(":netty-android") 43 | } 44 | 45 | 46 | apply from: "../kotlin.gradle" -------------------------------------------------------------------------------- /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/moo/work/android-sdk-macosx/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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/goav/app/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.goav.app; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | // ClientBus.Initialization(this, "token", "host", 8080); 14 | } 15 | 16 | public void push(Object o) { 17 | // ClientBus.request(o); 18 | } 19 | 20 | @Override 21 | protected void onDestroy() { 22 | super.onDestroy(); 23 | // ClientBus.Recycle(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/goav/app/NettyTest.kt: -------------------------------------------------------------------------------- 1 | import com.goav.netty.Handler.ClientHelper 2 | import com.goav.netty.Handler.ReConnect 3 | import com.goav.netty.Impl.ChannelConnectImpl 4 | import io.netty.channel.ChannelHandlerContext 5 | import io.netty.channel.ChannelOutboundHandlerAdapter 6 | import io.netty.channel.ChannelPipeline 7 | import io.netty.channel.ChannelPromise 8 | import io.netty.channel.socket.SocketChannel 9 | import java.util.concurrent.TimeUnit 10 | 11 | fun main(args: Array) { 12 | val client = ClientHelper.init() 13 | .reConnect(ReConnect(true, 2)) 14 | .address("0", 7373) 15 | .addCallBack(object : ChannelConnectImpl { 16 | override fun onConnectCallBack(sc: SocketChannel?) { 17 | System.out.println((sc == null).toString()) 18 | } 19 | 20 | override fun addChannelHandler(pipeline: ChannelPipeline): ChannelPipeline = pipeline.addLast(TestHandler()).addLast(OutChannel()) 21 | }) 22 | .build() 23 | .connect(); 24 | } 25 | 26 | 27 | class TestHandler : io.netty.channel.ChannelDuplexHandler() { 28 | override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) { 29 | val value = String(msg as ByteArray) 30 | System.out.println(value) 31 | 32 | // super.channelRead(ctx, msg) 33 | } 34 | 35 | override fun write(ctx: ChannelHandlerContext?, msg: Any?, promise: ChannelPromise?) { 36 | System.out.println("write") 37 | super.write(ctx, msg, promise) 38 | } 39 | 40 | override fun channelActive(ctx: ChannelHandlerContext?) { 41 | System.out.println("channelActive") 42 | ctx?.writeAndFlush("connectByKotlin") 43 | ctx?.executor()?.scheduleAtFixedRate( 44 | { ctx.writeAndFlush(SocketPong()) }, 45 | 0, 46 | 10, 47 | TimeUnit.SECONDS) 48 | // super.channelActive(ctx) 49 | } 50 | 51 | override fun channelRegistered(ctx: ChannelHandlerContext?) { 52 | System.out.println("channelRegistered") 53 | super.channelRegistered(ctx) 54 | } 55 | 56 | override fun channelUnregistered(ctx: ChannelHandlerContext?) { 57 | System.out.println("channelUnregistered") 58 | ctx?.close() 59 | // super.channelUnregistered(ctx) 60 | } 61 | 62 | override fun channelInactive(ctx: ChannelHandlerContext?) { 63 | System.out.println("channelInactive") 64 | super.channelInactive(ctx) 65 | } 66 | 67 | override fun close(ctx: ChannelHandlerContext?, promise: ChannelPromise?) { 68 | System.out.println("close") 69 | super.close(ctx, promise) 70 | } 71 | 72 | override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) { 73 | System.out.println(cause?.message) 74 | super.exceptionCaught(ctx, cause) 75 | } 76 | } 77 | 78 | 79 | class OutChannel : ChannelOutboundHandlerAdapter() { 80 | 81 | override fun write(ctx: ChannelHandlerContext?, msg: Any?, promise: ChannelPromise?) { 82 | System.out.print("write out") 83 | super.write(ctx, msg, promise) 84 | } 85 | 86 | 87 | } -------------------------------------------------------------------------------- /app/src/main/java/com/goav/app/SocketPong.kt: -------------------------------------------------------------------------------- 1 | 2 | import com.goav.netty.message.MessageBasic 3 | import com.google.gson.annotations.SerializedName 4 | 5 | /** 6 | * Created by moo on 20/10/2017. 7 | */ 8 | class SocketPong : MessageBasic() { 9 | 10 | @SerializedName("_method_") 11 | private val method: String = "pong" 12 | 13 | private val device: String = "android_kotlin"; 14 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotow-hub/NettyAndroid/a5b63adf07fe5183b03caf0d022742bdc3ee58e0/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotow-hub/NettyAndroid/a5b63adf07fe5183b03caf0d022742bdc3ee58e0/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotow-hub/NettyAndroid/a5b63adf07fe5183b03caf0d022742bdc3ee58e0/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotow-hub/NettyAndroid/a5b63adf07fe5183b03caf0d022742bdc3ee58e0/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotow-hub/NettyAndroid/a5b63adf07fe5183b03caf0d022742bdc3ee58e0/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | netty-android 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | apply from: "config.gradle" 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.1.51' 6 | 7 | repositories { 8 | jcenter() 9 | mavenCentral() 10 | maven { 11 | url 'https://maven.google.com/' 12 | name 'Google' 13 | } 14 | } 15 | dependencies { 16 | classpath 'com.android.tools.build:gradle:3.0.1' 17 | // classpath 'com.novoda:bintray-release:0.5.0' 18 | // NOTE: Do not place your application dependencies here; they belong 19 | // in the individual module build.gradle files 20 | classpath 'me.tatarka:gradle-retrolambda:3.7.0' 21 | classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2' 22 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 23 | // classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' 24 | classpath 'com.dicedmelon.gradle:jacoco-android:0.1.2'//codecov 25 | } 26 | configurations.classpath.exclude group: 'com.android.tools.external.lombok' 27 | } 28 | 29 | allprojects { 30 | repositories { 31 | jcenter() 32 | maven { 33 | url 'https://maven.google.com/' 34 | name 'Google' 35 | } 36 | } 37 | 38 | tasks.withType(JavaCompile) { 39 | sourceCompatibility = 1.7 40 | targetCompatibility = 1.7 41 | } 42 | 43 | } 44 | 45 | task clean(type: Delete) { 46 | delete rootProject.buildDir 47 | } 48 | 49 | task javadoc(type: Javadoc) { 50 | options.encoding = "utf-8" 51 | } 52 | 53 | task wrapper(type: Wrapper) { 54 | gradleVersion = '3.5' 55 | } 56 | 57 | //tasks.getByPath(":netty-android:mavenAndroidJavadocs").enabled = false 58 | -------------------------------------------------------------------------------- /config.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | netty = [ 3 | compileSdkVersion: 25, 4 | buildToolsVersion: "26.0.2", 5 | supportVersion : "25.3.1", 6 | minSdkVersion : 14, 7 | targetSdkVersion : 25, 8 | versionCode : 10, 9 | versionName : "0.5" 10 | ]; 11 | 12 | push = [ 13 | userOrg : '1024icloud', 14 | groupId : 'com.goav', 15 | artifactId : 'netty-android', 16 | publishVersion: netty.versionName, 17 | desc : 'NettyAndroid', 18 | website : 'https://github.com/goAV/NettyAndroid' 19 | ]; 20 | } 21 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotow-hub/NettyAndroid/a5b63adf07fe5183b03caf0d022742bdc3ee58e0/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Oct 20 19:25:11 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | java: 2 | - oraclejdk8 -------------------------------------------------------------------------------- /kotlin.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'kotlin-android' 2 | apply plugin: 'kotlin-android-extensions' 3 | apply plugin: 'kotlin-kapt' 4 | 5 | 6 | dependencies { 7 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 8 | // compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" 9 | } 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /netty-android/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | src/androidTest 3 | src/test 4 | -------------------------------------------------------------------------------- /netty-android/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. By moo. All rights reserved. 3 | * 4 | * Email: chinaume@163.com 5 | */ 6 | 7 | apply plugin: 'com.android.library' 8 | //apply plugin: 'com.github.dcendents.android-maven' 9 | //apply plugin: 'com.novoda.bintray-release' 10 | apply plugin: 'jacoco-android' //codecov 11 | 12 | def netty = rootProject.ext.netty 13 | def push = rootProject.ext.push 14 | 15 | //group = 'com.github.goav' 16 | //version.name = '0.2.3' 17 | //project.archivesBaseName = push.artifactId 18 | 19 | android { 20 | compileSdkVersion netty.compileSdkVersion as int 21 | buildToolsVersion netty.buildToolsVersion as String 22 | 23 | defaultConfig { 24 | minSdkVersion netty.minSdkVersion as int 25 | targetSdkVersion netty.targetSdkVersion as int 26 | versionCode netty.versionCode as int 27 | versionName netty.versionName as String 28 | } 29 | 30 | compileOptions { 31 | targetCompatibility JavaVersion.VERSION_1_8 32 | sourceCompatibility JavaVersion.VERSION_1_8 33 | } 34 | 35 | buildTypes { 36 | release { 37 | minifyEnabled false 38 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 39 | } 40 | 41 | debug { 42 | testCoverageEnabled true 43 | } 44 | } 45 | 46 | 47 | lintOptions { 48 | abortOnError false 49 | } 50 | 51 | } 52 | // 53 | //publish { 54 | // userOrg = push.userOrg 55 | // groupId = push.groupId 56 | // artifactId = push.artifactId 57 | // publishVersion = push.publishVersion 58 | // desc = push.desc 59 | // website = push.website 60 | //} 61 | 62 | dependencies { 63 | compile fileTree(dir: 'libs', include: ['*.jar']) 64 | compile 'io.netty:netty-all:4.1.16.Final' 65 | provided 'com.google.code.gson:gson:2.7' 66 | } 67 | 68 | 69 | apply from: "../kotlin.gradle" 70 | -------------------------------------------------------------------------------- /netty-android/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/moo/work/android-sdk-macosx/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 | -------------------------------------------------------------------------------- /netty-android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Handler/ChannelCloseHandler.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Handler 2 | 3 | import io.netty.channel.ChannelHandlerAdapter 4 | 5 | /** 6 | * Copyright (c) 2017. 7 | * chinaume@163.com 8 | */ 9 | class ChannelCloseHandler : ChannelHandlerAdapter() { 10 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Handler/ChannelResponseHandler.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Handler 2 | 3 | import com.goav.netty.Impl.ChannelHandlerImpl 4 | import com.goav.netty.Impl.ChannelReleaseImpl 5 | import io.netty.channel.ChannelHandlerContext 6 | import io.netty.channel.ChannelInboundHandlerAdapter 7 | import java.util.* 8 | 9 | /** 10 | * Copyright (c) 2017. 11 | * chinaume@163.com 12 | */ 13 | public class ChannelResponseHandler : ChannelInboundHandlerAdapter(), ChannelReleaseImpl { 14 | 15 | private val mHandlerArray: ArrayList; 16 | 17 | init { 18 | mHandlerArray = ArrayList(); 19 | } 20 | 21 | 22 | fun addChannelResponseHandler(channelHandlerImpl: ChannelHandlerImpl?) { 23 | if (channelHandlerImpl != null) { 24 | mHandlerArray.add(channelHandlerImpl) 25 | } 26 | } 27 | 28 | /** 29 | * dispath the message 30 | */ 31 | override fun channelRead(ctx: ChannelHandlerContext, msg: Any?) { 32 | 33 | if (msg != null) { 34 | val response = msg as ByteArray 35 | if (mHandlerArray.any { it.channelRead(ctx, response) }) { 36 | return 37 | } 38 | } 39 | 40 | //nobody receiver 41 | super.channelRead(ctx, msg) 42 | } 43 | 44 | override fun destroy() { 45 | mHandlerArray.clear(); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Handler/Client.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. 3 | * chinaume@163.com 4 | */ 5 | 6 | package com.goav.netty.Handler 7 | 8 | import android.system.Os.shutdown 9 | import android.util.TimeUtils 10 | import com.goav.netty.Impl.ChannelConnectImpl 11 | import com.goav.netty.Impl.ClientImpl 12 | import com.goav.netty.Impl.ClientOptImpl 13 | import com.goav.netty.message.MessageBasic 14 | import io.netty.bootstrap.Bootstrap 15 | import io.netty.channel.ChannelInitializer 16 | import io.netty.channel.nio.NioEventLoopGroup 17 | import io.netty.channel.socket.SocketChannel 18 | import io.netty.channel.socket.nio.NioSocketChannel 19 | import io.netty.handler.timeout.IdleStateHandler 20 | import io.netty.util.internal.logging.InternalLoggerFactory 21 | import java.util.concurrent.* 22 | 23 | /** 24 | * 长链接 25 | * 26 | * @time: 16/10/8 12:41.

27 | * @author: Created by moo

28 | */ 29 | 30 | internal class Client constructor() : ClientImpl, ClientOptImpl { 31 | 32 | private var socketChannel: SocketChannel? = null 33 | private val service: ScheduledExecutorService 34 | private val messageSupers: BlockingDeque 35 | private var onDestrOY: Boolean 36 | private val logger = InternalLoggerFactory.getInstance(this.javaClass) 37 | private var bootstrap: Bootstrap? = null 38 | 39 | private var host: String = "" 40 | private var port: Int = 0 41 | private var callBack: ChannelConnectImpl = ChannelConnectImpl.DEFAULT 42 | private var readerIdleTimeSeconds: Int = 60 43 | private var writerIdleTimeSeconds: Int = 60 44 | private var allIdleTimeSeconds: Int = 90 45 | private var reConnect: ReConnect? = null 46 | private var mThread: Thread? = null 47 | private var eventLoopGroup: NioEventLoopGroup? = null 48 | 49 | init { 50 | eventLoopGroup = NioEventLoopGroup(); 51 | service = Executors.newSingleThreadScheduledExecutor() 52 | messageSupers = LinkedBlockingDeque() 53 | onDestrOY = false 54 | 55 | mThread = object : Thread("Socket_Push_Message") { 56 | override fun run() { 57 | while (!onDestrOY) { 58 | var message: Any? 59 | try { 60 | message = messageSupers.take() 61 | if (socketChannel == null) { 62 | request(message) 63 | TimeUnit.SECONDS.sleep(1) 64 | } else { 65 | socketChannel?.writeAndFlush(message) 66 | } 67 | } catch (e: Exception) { 68 | e.printStackTrace() 69 | } 70 | } 71 | } 72 | } 73 | 74 | mThread!!.start() 75 | } 76 | 77 | 78 | override fun reConnect(reConnect: ReConnect): ClientOptImpl { 79 | this.reConnect = reConnect 80 | return this 81 | } 82 | 83 | override 84 | fun address(host: String, port: Int): ClientOptImpl { 85 | this.host = host 86 | this.port = port 87 | return this 88 | } 89 | 90 | override 91 | fun addTimeOut(readerIdleTimeSeconds: Int, writerIdleTimeSeconds: Int, allIdleTimeSeconds: Int): ClientOptImpl { 92 | this.readerIdleTimeSeconds = readerIdleTimeSeconds 93 | this.writerIdleTimeSeconds = writerIdleTimeSeconds 94 | this.allIdleTimeSeconds = allIdleTimeSeconds 95 | return this 96 | } 97 | 98 | override 99 | fun addCallBack(callBack: ChannelConnectImpl): ClientOptImpl { 100 | this.callBack = callBack 101 | return this 102 | } 103 | 104 | 105 | override 106 | fun build(): ClientImpl { 107 | 108 | bootstrap = Bootstrap() 109 | bootstrap!!.channel(NioSocketChannel::class.java) 110 | .group(eventLoopGroup) 111 | .remoteAddress(host, port) 112 | .handler(object : ChannelInitializer() { 113 | @kotlin.jvm.Throws(java.lang.Exception::class) 114 | override fun initChannel(ch: SocketChannel) { 115 | var pipeline = ch.pipeline() 116 | .addLast(IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds) 117 | , EncodeHandler() 118 | , DecodeHandler()) 119 | 120 | callBack.addChannelHandler(pipeline) 121 | } 122 | }) 123 | return this 124 | } 125 | 126 | 127 | private fun InitializationWithWorkThread() { 128 | 129 | if (onDestrOY) return 130 | 131 | try { 132 | val future = bootstrap!!.connect().sync() 133 | if (future.isSuccess) { 134 | socketChannel = future.channel() as SocketChannel 135 | } else { 136 | socketChannel?.disconnect() 137 | socketChannel?.close() 138 | 139 | if (reConnect != null && reConnect!!.able) { 140 | future.channel().eventLoop().schedule( 141 | { InitializationWithWorkThread() }, 142 | reConnect!!.time, 143 | TimeUnit.SECONDS 144 | ) 145 | } else { 146 | throw InterruptedException() 147 | } 148 | } 149 | 150 | 151 | } catch (e: Exception) { 152 | logger.info("socketChannel连接失败", e) 153 | // eventLoopGroup.shutdownGracefully();//it's can't restart when server close 154 | } finally { 155 | callBack.onConnectCallBack(socketChannel) 156 | } 157 | 158 | } 159 | 160 | override fun connect() { 161 | if (onDestrOY) return 162 | logger.info("socket 连接2s后建立") 163 | service.schedule({ InitializationWithWorkThread() }, 2, TimeUnit.SECONDS) 164 | } 165 | 166 | override fun request(message: MessageBasic) { 167 | try { 168 | messageSupers.put(message) 169 | } catch (e: InterruptedException) { 170 | e.printStackTrace() 171 | } 172 | } 173 | 174 | override fun destroy() { 175 | onDestrOY = true 176 | mThread?.interrupt() 177 | mThread?.join() 178 | messageSupers.clear() 179 | socketChannel?.close() 180 | socketChannel = null 181 | bootstrap = null 182 | shutdown(service) 183 | try { 184 | eventLoopGroup?.shutdownGracefully() 185 | } catch (e: Exception) { 186 | } finally { 187 | eventLoopGroup = null 188 | } 189 | } 190 | } 191 | 192 | @JvmSynthetic 193 | fun shutdown(service: ExecutorService): Unit { 194 | service.shutdown() 195 | try { 196 | if (!service.awaitTermination(1, TimeUnit.SECONDS)) { 197 | service.shutdownNow() 198 | } 199 | } catch (e: Exception) { 200 | 201 | } finally { 202 | service.shutdownNow() 203 | } 204 | } 205 | 206 | 207 | class ReConnect(val able: Boolean, val time: Long) { 208 | } 209 | -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Handler/ClientHelper.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Handler 2 | 3 | import com.goav.netty.Impl.ClientOptImpl 4 | 5 | /** 6 | * Created by moo on 20/10/2017. 7 | */ 8 | object ClientHelper { 9 | 10 | /** 11 | * 得到对象自己管理 12 | */ 13 | fun init(): ClientOptImpl = Client() 14 | 15 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Handler/DecodeHandler.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. 3 | * chinaume@163.com 4 | */ 5 | 6 | package com.goav.netty.Handler 7 | 8 | 9 | import io.netty.buffer.ByteBuf 10 | import io.netty.channel.ChannelHandlerContext 11 | import io.netty.handler.codec.ByteToMessageDecoder 12 | 13 | /** 14 | * 接收解析器 15 | * 16 | * @time: 16/10/8 12:14.

17 | * @author: Created by moo

18 | */ 19 | 20 | internal class DecodeHandler : ByteToMessageDecoder() { 21 | 22 | 23 | /** 24 | * @param ctx 25 | * @param in 26 | * @param out 27 | * @throws Exception 28 | * @see EncodeHandler.encode 29 | */ 30 | @Throws(Exception::class) 31 | override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: MutableList) { 32 | 33 | try { 34 | 35 | if (`in`.isReadable) { 36 | 37 | if (`in`.readableBytes() > 4) { 38 | 39 | `in`.markReaderIndex() 40 | val dataLength = `in`.readInt() - 4 41 | if (dataLength <= 0 || `in`.readableBytes() < dataLength) { 42 | `in`.resetReaderIndex() 43 | return 44 | } 45 | 46 | val bytes = ByteArray(dataLength) 47 | `in`.readBytes(bytes) 48 | out.add(bytes) 49 | } 50 | } 51 | } catch (e: Exception) { 52 | System.out.println(e.message) 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Handler/EncodeHandler.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016. 3 | * chinaume@163.com 4 | */ 5 | 6 | package com.goav.netty.Handler 7 | 8 | 9 | import com.goav.netty.message.MessageBasic 10 | import io.netty.buffer.ByteBuf 11 | import io.netty.channel.ChannelHandlerContext 12 | import io.netty.handler.codec.MessageToByteEncoder 13 | 14 | /** 15 | * 发送编码器 16 | * 17 | * @time: 16/10/8 12:14.

18 | * @author: Created by moo

19 | */ 20 | 21 | internal class EncodeHandler : MessageToByteEncoder() { 22 | @Throws(Exception::class) 23 | override fun encode(ctx: ChannelHandlerContext, msg: MessageBasic, out: ByteBuf) { 24 | try { 25 | val bytes = msg.read(); 26 | val size = bytes.size 27 | out.writeInt(size + 4)//body长度 28 | out.writeBytes(bytes) 29 | System.out.println("size=" + size) 30 | } catch (e: Exception) { 31 | System.out.println(e.message) 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Impl/ChannelConnectImpl.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Impl 2 | 3 | import io.netty.channel.ChannelPipeline 4 | import io.netty.channel.socket.SocketChannel 5 | 6 | /** 7 | * Copyright (c) 2017. 8 | * chinaume@163.com 9 | */ 10 | interface ChannelConnectImpl { 11 | 12 | fun addChannelHandler(pipeline: ChannelPipeline): ChannelPipeline 13 | 14 | fun onConnectCallBack(sc: SocketChannel?) 15 | 16 | 17 | object DEFAULT : ChannelConnectImpl { 18 | override fun onConnectCallBack(sc: SocketChannel?) { 19 | // throw UnsupportedOperationException() 20 | } 21 | 22 | override fun addChannelHandler(pipeline: ChannelPipeline): ChannelPipeline = pipeline 23 | 24 | } 25 | 26 | } 27 | 28 | -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Impl/ChannelHandlerImpl.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Impl 2 | 3 | import io.netty.channel.ChannelHandlerContext 4 | 5 | /** 6 | * Copyright (c) 2017. 7 | * chinaume@163.com 8 | */ 9 | interface ChannelHandlerImpl { 10 | 11 | fun channelRead(ctx: ChannelHandlerContext, msg: ByteArray): Boolean 12 | 13 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Impl/ChannelMessageImpl.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Impl 2 | 3 | import java.io.Serializable 4 | 5 | /** 6 | * Copyright (c) 2017. 7 | * chinaume@163.com 8 | */ 9 | 10 | /** 11 | * for data 12 | */ 13 | interface ChannelMessageImpl { 14 | 15 | 16 | fun read(): ByteArray 17 | 18 | 19 | fun write(message: ByteArray): Serializable 20 | 21 | 22 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Impl/ChannelReleaseImpl.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Impl 2 | 3 | /** 4 | * Copyright (c) 2017. 5 | * chinaume@163.com 6 | */ 7 | interface ChannelReleaseImpl { 8 | 9 | fun destroy(): Unit 10 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Impl/ClientImpl.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Impl 2 | 3 | import com.goav.netty.message.MessageBasic 4 | 5 | /** 6 | * Copyright (c) 2017. 7 | * chinaume@163.com 8 | */ 9 | interface ClientImpl { 10 | 11 | fun connect() 12 | 13 | fun request(message: MessageBasic) 14 | 15 | fun destroy() 16 | 17 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/Impl/ClientOptImpl.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.Impl 2 | 3 | import com.goav.netty.Handler.ReConnect 4 | 5 | /** 6 | * Created by moo on 20/10/2017. 7 | */ 8 | interface ClientOptImpl { 9 | 10 | fun reConnect(reConnect: ReConnect): ClientOptImpl 11 | 12 | fun address(host: String, port: Int): ClientOptImpl 13 | 14 | fun addTimeOut(readerIdleTimeSeconds: Int, writerIdleTimeSeconds: Int, allIdleTimeSeconds: Int): ClientOptImpl 15 | 16 | fun addCallBack(callBack: ChannelConnectImpl): ClientOptImpl 17 | 18 | fun build(): ClientImpl 19 | 20 | } -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/message/GsonHelper.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.message 2 | 3 | import com.google.gson.GsonBuilder 4 | 5 | import java.lang.reflect.Modifier 6 | 7 | /** 8 | * Created by moo on 16/10/8. 9 | */ 10 | object GsonHelper { 11 | 12 | private var gson: com.google.gson.Gson? = null 13 | 14 | init { 15 | gson = GsonBuilder() 16 | .excludeFieldsWithModifiers(Modifier.PROTECTED) //@protected 修饰的过滤,比如自动增长的id 17 | .create() 18 | } 19 | 20 | 21 | fun newInstances(): com.google.gson.Gson { 22 | return gson!! 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /netty-android/src/main/java/com/goav/netty/message/MessageBasic.kt: -------------------------------------------------------------------------------- 1 | package com.goav.netty.message 2 | 3 | import com.goav.netty.Impl.ChannelMessageImpl 4 | import java.io.Serializable 5 | 6 | /** 7 | * Copyright (c) 2017. 8 | * chinaume@163.com 9 | */ 10 | 11 | 12 | /** 13 | * super message for resule 14 | */ 15 | 16 | abstract class MessageBasic : Serializable, ChannelMessageImpl { 17 | 18 | 19 | override fun toString(): String = GsonHelper.newInstances().toJson(this); 20 | 21 | override fun read(): ByteArray = GsonHelper.newInstances().toJson(this).toByteArray() 22 | 23 | override fun write(message:ByteArray): MessageBasic = GsonHelper.newInstances().fromJson(String(message),this.javaClass); 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /netty-android/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | netty-android 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', 2 | ':netty-android' --------------------------------------------------------------------------------