XAndroidSocket

Introduction: Android Socket 封装,支持 TCP/UDP 客户端和服务端,支持自定义粘包处理、验证处理、解析处理。
More: Author   ReportBugs   
Tags:
Socket-TCP-UDP-

Socket 封装,支持 TCP/UDP 客户端和服务端,支持自定义粘包处理、验证处理、解析处理。

使用

详见目录TCP/UDP 使用 demo 下的例子,使用简单。 其中只有 TCP 才支持支持粘包处理、验证处理、解析处理。

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.Blankeer:XAndroidSocket:1.0.0'
}

粘包处理

提供的粘包处理有

支持自定义粘包处理,只需要实现AbsStickPackageHelper接口,

/**
 * 接受消息,粘包处理的 helper,通过 inputstream,返回最终的数据,需手动处理粘包,返回的 byte[]是我们预期的完整数据
 * note:这个方法会反复调用,直到解析到一条完整的数据。该方法是同步的,尽量不要做耗时操作,否则会阻塞读取数据
 */
public interface AbsStickPackageHelper {
    byte[] execute(InputStream is);
}

把接收消息的 InputStream 给你,你返回一个完整包的 byte[]给我。

验证处理

提供的验证处理是 不处理,也是默认的。 自定义验证处理需要实现AbsValidationHelper:

public interface AbsValidationHelper {
    boolean execute(byte[] msg);
}

把完整的数据包给你,你需要返回是否验证通过,一般的自定义协议里都会有 MD5 验证,可以在这里验证。

解析处理

提供的解析处理是 不处理,也是默认的。 自定义解析处理需要实现AbsDecodeHelper

/**
 * 解析消息的处理
 */
public interface AbsDecodeHelper {
    /**
     *
     * @param data  完整的数据包
     * @param targetInfo    对方的信息(ip/port)
     * @param tcpConnConfig    tcp 连接配置,可自定义
     * @return
     */
    byte[][] execute(byte[] data, TargetInfo targetInfo, TcpConnConfig tcpConnConfig);
}

设计思路:一般自定义协议会设计好多个字段组成,比如:dataLen+data+type+md5,数据长度+数据+类型+MD5,解析处理就是把这 4 个字段解析出来,返回 byte[4][],便于后续处理。

Log

日志开关:XSocketLog.debug(true)

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools