smartsocket 整数读写实例
所属分类 smartboot
浏览量 75
客户端写入一个整数,服务接收到整数后 加1 返回
IntegerProtocol.java (服务端客户端共用)
IntDemoServer.java
ServerMessageProcessor.java
IntDemoClient.java
org.smartboot.socket.Protocol
org.smartboot.socket.MessageProcessor
org.smartboot.socket.transport.AioSession
org.smartboot.socket.transport.AioQuickServer
org.smartboot.socket.transport.AioQuickClient
public interface Protocol< T> {
// 业务消息对象解析 ,返回null则表示解码未完成
T decode(final ByteBuffer readBuffer, AioSession session);
}
// 对完成解码的消息进行业务处理
public interface MessageProcessor< T> {
// 处理接收到的消息
void process(AioSession session, T msg);
// 状态机事件,当枚举事件发生时由框架触发该方法
default void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
if (stateMachineEnum == StateMachineEnum.DECODE_EXCEPTION || stateMachineEnum == StateMachineEnum.PROCESS_EXCEPTION) {
throwable.printStackTrace();
}
}
}
// 实现 MessageProcessor 和 IntegerProtocol
MessageProcessor serverProcessor = new ServerMessageProcessor();
AioQuickServer server = new AioQuickServer(8888, new IntegerProtocol(), serverProcessor);
server.start();
AioSession smart-socket最核心的类,封装AsynchronousSocketChannel API接口,简化IO操作。
其中开放给用户使用的接口为:
close()
close(boolean)
getAttachment()
getLocalAddress()
getRemoteAddress()
getSessionID()
isInvalid()
setAttachment(Object)
final class TcpAioSession extends AioSession
// 底层通信channel对象
private final AsynchronousSocketChannel channel;
// 输出流
private final WriteBuffer byteBuf;
// 读缓冲 , 大小取决于AioQuickClient/AioQuickServer设置的setReadBufferSize
private VirtualBuffer readBuffer;
// 写缓冲
private VirtualBuffer writeBuffer;
// 同步输入流
private InputStream inputStream;
private final IoServerConfig config;
private final Supplier< VirtualBuffer > readBufferSupplier;
IntegerProtocol.java
import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;
import java.nio.ByteBuffer;
public class IntegerProtocol implements Protocol< Integer> {
@Override
public Integer decode(ByteBuffer data, AioSession session) {
if (data.remaining() < Integer.BYTES) {
return null;
}
return data.getInt();
}
}
IntDemoServer.java
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioQuickServer;
MessageProcessor< Integer> serverProcessor = new ServerMessageProcessor();
AioQuickServer server = new AioQuickServer(8888, new IntegerProtocol(), serverProcessor);
server.start();
Thread.sleep(1000000);
server.shutdown();
ServerMessageProcessor.java
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioSession;
import java.io.IOException;
public class ServerMessageProcessor implements MessageProcessor< Integer> {
@Override
public void process(AioSession session, Integer msg){
int respMsg = msg + 1;
System.out.println("receive data from client[]: " + msg + " ,rsp:" + (respMsg));
try {
session.writeBuffer().writeInt(respMsg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
IntDemoClient.java
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioQuickClient;
import org.smartboot.socket.transport.AioSession;
public class IntDemoClient {
public static void main(String[] args) throws Exception {
MessageProcessor< Integer> clientProcessor = (session, msg) -> System.out.println("receive data from server:" + msg);
AioQuickClient aioQuickClient = new AioQuickClient("localhost", 8888, new IntegerProtocol(), clientProcessor);
AioSession session = aioQuickClient.start();
for(int i=0;i<100;i++) {
session.writeBuffer().writeInt(i);
session.writeBuffer().flush();
}
Thread.sleep(5000);
aioQuickClient.shutdownNow();
}
}
https://gitee.com/dyyx/work2024/tree/master/demo/smartbootdemo/src/main/java/intdemo
参考
https://gitee.com/smartboot/smart-socket
https://gitee.com/dyyx/work2024/blob/master/demo/smart-socket/example/src/main/java/org/smartboot/socket/example/basic/IntegerDemo.java
上一篇
下一篇
smart-http 1.6.1 实例
java NIO ByteBuffer 读写整数
java NIO ByteBuffer 使用技巧
MQTT 特性简介
20240930 A股刷新多项历史记录
MQTT协议简史