• Stars
    star
    187
  • Rank 205,995 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

XAndroidSocket

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.

More Repositories

1

MDWechat

一个能让微信 Material Design 化的 Xposed 模块
Java
927
star
2

WechatBotXposed

微信回复机器人,Xposed模块
Kotlin
373
star
3

SoleBooks

类似豆瓣读书,提供一个书籍查看、搜索、交流的平台
Java
119
star
4

AirCalendarView

Airbnb APP CalendarView
Java
96
star
5

DownloadProgressButton

下载显示进度按钮控件
Java
80
star
6

AndroidBestModelDemo

Model层最佳实践demo。SqlDelight、AutoValue、Gson、SqlBrite、Retrofit
Java
12
star
7

MVPMosbyDemo

MVPMosbyDemo,简单分析源码,simple source analysis
Java
12
star
8

SimpleGanK

简单的gank.io客户端,MVP(mosby)+Retrofit+Dagger2+RxJava
Java
8
star
9

BasePageIndicator

BasePageIndicator-ViewPager指示器,参考JakeWharton/ViewPagerIndicator和astuetz/PagerSlidingTabStrip的源码,写的不完善,仅供参考。
Java
8
star
10

ScrollHideView

一个随滑动显示隐藏的自定义View,类似知乎回答详情页。适用于任何页面,可自定义in/out动画。
Java
6
star
11

HoldScaleImageView

有没有这样的需求,ImageView的宽度固定,高度和图片等比例缩放后的大小一致,反之高度固定亦然。来看看这个自定义控件可能会有帮助。
Java
6
star
12

BottomNavigationView_ViewPager_Demo

官方BottomNavigationView和ViewPager使用的正确姿势
Java
3
star
13

AnkiReader

AnkiReader
Java
3
star
14

DouBanYueDu

豆瓣阅读反编译代码,导入as报错,仅供研究之用
Java
3
star
15

ProgressButton

loading MaterialProgressDrawable when a user clicks the button
Java
3
star
16

XSwipeRefreshLayout

Google SwipeRefreshLayout增强版,支持上拉下拉加载,代码做了细节修改,源地址 https://github.com/neuyu/refresh
Java
2
star
17

diaomao163_mac

Lua
2
star
18

XZhihuDay

知乎日报,完善ing,Dagger2+MVP+Retrofit,尝试在Model层引入Repository(数据库缓存和网络)
Java
2
star
19

RecyclerImageLoadPooler

RecyclerView + ImageLoaderPool
Java
1
star
20

HttpCacheTest

自定义http缓存策略,使用的okhttp和retrofit.
Java
1
star
21

source-analysis-retrofit

retrofit simple source code analysis.
Java
1
star