Skip to content

Android TCP框架(基于MINA 2.0.0-M3)、增加Bytes工厂、无需依赖slf4j(新增Mina4Log打印输出日志), 处理Bytes粘包、半包、断包(ByteArrayDecoder)

License

Notifications You must be signed in to change notification settings

Mr-Jiang/mina4-android

Repository files navigation

mina4-android description

Android TCP框架(基于MINA 2.0.0-M3)、增加Bytes工厂、无需依赖slf4j(新增Mina4Log打印输出日志),
处理Bytes粘包、半包、断包(ByteArrayDecoder),需配置自己的首尾标识符,
如果与首尾标识符相同的数据出现在首尾标识符以内的范围,建议将该数据进行转义,
如这样配置转义规则(假设首尾标识符是0x7e):

0x7e = 0x7d,0x02 如果首尾以内出现标识符,将其转换为0x7d后面紧跟一个0x02
0x7d = 0x7d,0x01 如果首尾以内出现标识符转义字符,不进行转换,在其后面紧跟一个0x01

mina4-android use

private void initMain4Android() {

	final ConnectionConfiguration config = new ConnectionConfiguration.Builder(this).setIp("192.168.0.102")
			.setPort(10086).setReadBufferSize(10240).setConnectionTimeout(5000)
			.setFactory(new ByteArrayCodecFactory()).setBridge(new KeepAliveMessageBridge() {

				@Override
				public void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception {
					// 心跳超时
				}

				@Override
				public boolean isResponse(IoSession session, Object message) {
					// 字节数组编码
					if (message instanceof IoBuffer)
						return false;
					else if (message instanceof byte[])
					    // 判断服务端响应消息是否是心跳反馈包
						return isKeepAliveResponse((byte[]) message);
					return false;
				}

				@Override
				public boolean isRequest(IoSession session, Object message) {
					return false;
				}

				@Override
				public Object getResponse(IoSession session, Object request) {
					return null;
				}

				@Override
				public int getRequestTimeout() {
				    // 心跳超时时间,10s
					return 10;
				}

				@Override
				public int getRequestInterval() {
				    // 心跳请求频率,5s
					return 5;
				}

				@Override
				public Object getRequest(IoSession session) {
				    // 发送心跳数据包,byte[]
					return IoBuffer.wrap(Bytes);
				}

			}).bulid();

	// ConnectionManager manager;
	manager = ConnectionManager.getManagerInstance();
	manager.loadConfig(config);
	manager.setOnConnectionListener(new ConnectionListener() {

		@Override
		public void receiveData(Object data) {
			// 字节数组编码
			if (config.getFactory() instanceof ByteArrayCodecFactory) {
				MessageCheck((byte[]) data);
			}
		}

		@Override
		public void disConnect() {

		}

		@Override
		public void connectOk() {

		}

		@Override
		public void connectError(Exception e) {
		}

		@Override
		public void exceptionCaught(Throwable cause) {

		}
	});
}

mina4-android Suggest

如果接收方与发送方的数据类型不一致时,会出现类型转换异常,需要服务端与客户端的收发数据类型保持一致,
如果服务端是在不可控范围,就是说服务端不是自己公司的,是第三方的,你无法决定它的收发数据类型,
请实现ProtocolCodecFactory,进行业务的拓展

mina4-android feedback

Email:engineer.jsp.hn@gmail.com

mina4-android jar

License

Copyright (C) 2018 Engineer-Jsp

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.

About

Android TCP框架(基于MINA 2.0.0-M3)、增加Bytes工厂、无需依赖slf4j(新增Mina4Log打印输出日志), 处理Bytes粘包、半包、断包(ByteArrayDecoder)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages