GithubHelp home page GithubHelp logo

rambowong2015 / mina4-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mr-jiang/mina4-android

0.0 1.0 0.0 1.59 MB

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

License: Apache License 2.0

Java 100.00%

mina4-android's Introduction

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:[email protected]

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.

mina4-android's People

Contributors

mr-jiang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.