https://github.com/noear/fineio
Nio and aio
https://github.com/noear/fineio
Last synced: 4 months ago
JSON representation
Nio and aio
- Host: GitHub
- URL: https://github.com/noear/fineio
- Owner: noear
- License: apache-2.0
- Created: 2020-05-25T06:49:21.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T22:20:15.000Z (over 5 years ago)
- Last Synced: 2024-12-30T18:38:48.160Z (over 1 year ago)
- Language: Java
- Size: 188 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FineIO
基于NIO实现的轻量级通讯框架
## 特性
暂无...
## 示例
### 服务端
```java
public class ServerTest {
public static void main(String[] args) {
//定义代理
//
MessageHandler handler = (session, message)->{
System.out.println("我收到:" + message);
Thread.sleep(10);
session.write("我收到:" + message);
};
//启动服务
//
FineIO.server(new StringProtocol())
.bind("localhost", 8080)
.handle(handler.pools()) //handler.pools() //线程池模式
.start(false);
}
}
```
### 客户端
```java
public class ClientTest1 {
public static void main(String[] args) {
//定义代理
//
MessageHandler handler = (session, message)->{
System.out.println("客户端:收到:" + message);
};
//定义客户端(内置连接池功能)
//
var client = FineIO.client(new StringProtocol()).handle(handler).bind("localhost", 8080);
//测试(请先启动服务端)
//
client.send("测试1");
client.send("测试2");
}
}
```
### 编码协议
```java
public class StringProtocol implements Protocol {
@Override
public String decode(ByteBuffer buffer) {
if (buffer.remaining() > Integer.BYTES) {
int size = buffer.getInt();
if (size > 0 && size <= buffer.remaining()) {
byte[] bytes = new byte[size];
buffer.get(bytes);
return new String(bytes);
}
}
return null;
}
@Override
public ByteBuffer encode(String message) {
byte[] bytes = message.getBytes();
ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length + Integer.BYTES);
buf.putInt(bytes.length);
buf.put(bytes);
buf.flip();
return buf;
}
}
```
## 性能报告
暂无...