代码拉取完成,页面将自动刷新
过去的几年一直在研究NIO技术,虽说没做出什么成绩,但对于Socket技术也是有了一定的了解。从2017年7月份正式开始接触AIO,尽管起步比较晚,但进步还是非常快的。断断续续的用了几周时间写了这个AIO版的通信框架smart-socket,目前的它非常轻量级,核心代码量才600多行,期待它成长后的模样。
项目 | 结果 |
---|---|
CPU | i7-4790 3.60Ghz |
内存 | 8G |
测试代码 | 服务端:P2PServer,客户端:P2PMultiClient |
测试时长 | 大于两分钟(服务端与客户端启动后的第一分钟数据是无效的,因为实际未跑满一分钟) |
时间单位 | 1分钟 |
数据总流量 | 7064MB |
消息大小 | 33B |
消息数 | 224484842 |
基于smart-socket进行通信服务的开发,主要有三个步骤:
接下来我们会通过一个简单例子来演示如何通过smart-socket开发服务端与客户端程序。为简化操作,服务端与客户端交互的数据为一个整型数据。
正常情况下服务端与客户端通信共用同一套协议规则,因此我们只需编写一份协议编解码实现即可。如下所示,协议编解码的需要实现接口Protocol。
public class IntegerProtocol implements Protocol<Integer> {
private static final int INT_LENGTH = 4;
@Override
public Integer decode(ByteBuffer data, AioSession<Integer> session) {
if (data.remaining() < INT_LENGTH)
return null;
return data.getInt();
}
@Override
public ByteBuffer encode(Integer s, AioSession<Integer> session) {
ByteBuffer b = ByteBuffer.allocate(INT_LENGTH);
b.putInt(s);
return b;
}
}
上述代码很简单,一个整数的长度为4bit,所以只要长度大于等于4,我们就能解析到一个整数。
业务消息的处理需要实现接口MessageProcessor
,该接口只有两个方法:process
,initSession
。其中 initSession仅在建立连接时调用一次,可在该方法中进行会话的初始化操作。process则会处理每一个接收到的业务消息。
public class IntegerServerProcessor implements MessageProcessor<Integer> {
@Override
public void process(AioSession<Integer> session, Integer msg) throws Exception {
Integer respMsg=msg+1;
System.out.println("接受到客户端数据:" + msg + " ,响应数据:" + (respMsg));
session.write(respMsg);
}
@Override
public void initSession(AioSession<Integer> session) {
}
}
public class IntegerClientProcessor implements MessageProcessor<Integer> {
private AioSession session;
@Override
public void process(AioSession<Integer> session, Integer msg) throws Exception {
System.out.println("接受到服务端响应数据:" + msg);
}
@Override
public void initSession(AioSession<Integer> session) {
this.session = session;
}
public AioSession getSession() {
return session;
}
}
public class IntegerServer {
public static void main(String[] args) {
AioQuickServer server = new AioQuickServer()
.bind(8888)
.setProtocol(new IntegerProtocol())
.setProcessor(new IntegerServerProcessor());
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class IntegerClient {
public static void main(String[] args) throws Exception {
IntegerClientProcessor processor=new IntegerClientProcessor();
AioQuickClient aioQuickClient=new AioQuickClient()
.connect("localhost",8888)
.setProtocol(new IntegerProtocol())
.setProcessor(processor);
aioQuickClient.start();
processor.getSession().write(1);
Thread.sleep(1000);
aioQuickClient.shutdown();
}
}
Edit By Seer
E-mail:zhengjunweimail@163.com
QQ:504166636
Update Date: 2017-08-24
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。