1
- # tio-boot 内置 Tcp
1
+ # tio-boot 内置 tcp 处理器
2
2
3
3
tio-boot 内置了 tcp 的支持,可以使用一个端口支持 tcp,http,websocket 三种协议,当一个数据包发送到 tio-boot-server 时,TioBootServerHandler 会根据内置的判断方法,选择对应处理器进行协议的处理.
4
4
5
- - com.litongjava.tio.http.server.HttpServerAioHandler 内置的 http 协议处理器
6
- - com.litongjava.tio.websocket.server.WsServerAioHandler 内置的 WebSocket 协议处理器
7
- - com.litongjava.tio.boot.tcp.ServerTcpHandler tcp 协议处理器,这是一个接口,需要自己实现 tcp 的处理逻辑
8
-
9
5
下面介绍如何使用 tio-boot 内置 Tcp 功能
10
6
11
- ```
12
- package com.litongjava.tio.web.hello.tcp;
7
+ ``` hava
8
+ package com.litongjava.tio.boot.hello.tcp.packet;
9
+
13
10
import com.litongjava.tio.core.intf.Packet;
14
11
15
12
/**
@@ -29,11 +26,12 @@ public class DemoPacket extends Packet {
29
26
}
30
27
```
31
28
32
- ```
33
- package com.litongjava.tio.boot.hello.tcp;
29
+ ``` java
30
+ package com.litongjava.tio.boot.hello.tcp.handler ;
34
31
35
32
import java.nio.ByteBuffer ;
36
33
34
+ import com.litongjava.tio.boot.hello.tcp.packet.DemoPacket ;
37
35
import com.litongjava.tio.boot.tcp.ServerTcpHandler ;
38
36
import com.litongjava.tio.core.ChannelContext ;
39
37
import com.litongjava.tio.core.Tio ;
@@ -52,7 +50,7 @@ public class DemoHandler implements ServerTcpHandler {
52
50
// 获取由ByteBuffer支持的字节数组
53
51
byte [] bytes = new byte [readableLength];
54
52
buffer. get(bytes);
55
- // 封装为ShowcasePacket
53
+ // 封装为Packet
56
54
DemoPacket imPackage = new DemoPacket ();
57
55
imPackage. setBody(bytes);
58
56
return imPackage;
@@ -90,18 +88,19 @@ public class DemoHandler implements ServerTcpHandler {
90
88
DemoPacket responsePacket = new DemoPacket ();
91
89
responsePacket. setBody(bytes);
92
90
// 响应消息
93
- log.info("开始响应 ");
91
+ log. info(" 开始发送响应 " );
94
92
Tio . send(channelContext, responsePacket);
95
93
log. info(" 响应完成" );
96
94
}
97
95
}
98
96
```
99
97
100
- ```
101
- package com.litongjava.tio.boot.hello.tcp;
98
+ DemoListener
102
99
103
- import com.litongjava.jfinal.aop.annotation.Component;
104
- import com.litongjava.tio.boot.tcp.ServerListener;
100
+ ``` java
101
+ package com.litongjava.tio.boot.hello.tcp.listener ;
102
+
103
+ import com.litongjava.tio.boot.tcp.ServerHanlderListener ;
105
104
import com.litongjava.tio.core.ChannelContext ;
106
105
import com.litongjava.tio.core.intf.Packet ;
107
106
@@ -158,13 +157,13 @@ public class DemoListener implements ServerHanlderListener {
158
157
}
159
158
```
160
159
161
- ```
162
- package com.litongjava.tio.boot.hello.config;
160
+ ``` java
161
+ package com.litongjava.tio.boot.hello.tcp. config ;
163
162
164
163
import com.litongjava.jfinal.aop.annotation.AInitialization ;
165
164
import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration ;
166
- import com.litongjava.tio.boot.hello.tcp.DemoHandler;
167
- import com.litongjava.tio.boot.hello.tcp.DemoListener;
165
+ import com.litongjava.tio.boot.hello.tcp.handler. DemoHandler ;
166
+ import com.litongjava.tio.boot.hello.tcp.listener. DemoListener ;
168
167
import com.litongjava.tio.boot.server.TioBootServer ;
169
168
import com.litongjava.tio.boot.tcp.ServerTcpHandler ;
170
169
@@ -182,6 +181,24 @@ public class TioBootServerConfig {
182
181
}
183
182
```
184
183
184
+ 运行日志
185
+
186
+ ```
187
+ 2024-01-29 12:01:09.841 [tio-group-6] INFO DemoListener.onAfterConnected:13 - server:0.0.0.0:80, client:127.0.0.1:2820,true,false
188
+ 2024-01-29 12:01:09.842 [tio-group-7] INFO DemoListener.onAfterReceivedBytes:21 - server:0.0.0.0:80, client:127.0.0.1:2820,2
189
+ 2024-01-29 12:01:09.842 [tio-group-7] INFO DemoHandler.decode:20 - buffer:java.nio.HeapByteBuffer[pos=0 lim=2 cap=30720]
190
+ 2024-01-29 12:01:09.842 [tio-group-7] INFO DemoListener.onAfterDecoded:17 - server:0.0.0.0:80, client:127.0.0.1:2820,com.litongjava.tio.boot.hello.tcp.packet.DemoPacket@27114133,2
191
+ 2024-01-29 12:01:09.842 [tio-group-7] INFO DemoHandler.handler:53 - received:gu
192
+ 2024-01-29 12:01:09.842 [tio-group-7] INFO DemoHandler.handler:56 - sendMessage:echo:gu
193
+ 2024-01-29 12:01:09.842 [tio-group-7] INFO DemoHandler.handler:62 - 开始响应
194
+ 2024-01-29 12:01:09.843 [tio-group-7] INFO DemoHandler.handler:64 - 响应完成
195
+ 2024-01-29 12:01:09.843 [tio-worker-4] INFO DemoHandler.encode:35 - encode:7
196
+ 2024-01-29 12:01:09.843 [tio-group-7] INFO DemoListener.onAfterHandled:29 - server:0.0.0.0:80, client:127.0.0.1:2820,com.litongjava.tio.boot.hello.tcp.packet.DemoPacket@27114133,0
197
+ 2024-01-29 12:01:09.844 [tio-group-8] INFO DemoListener.onAfterSent:25 - server:0.0.0.0:80, client:127.0.0.1:2820,com.litongjava.tio.boot.hello.tcp.packet.DemoPacket@5da82e61
198
+ 2024-01-29 12:01:09.845 [tio-worker-5] INFO DemoListener.onBeforeClose:44 - server:0.0.0.0:80, client:127.0.0.1:2820,null,对方关闭了连接,true
199
+
200
+ ```
201
+
185
202
### 1. ` DemoPacket ` 类
186
203
187
204
这是一个继承自 ` Packet ` 的类,用于表示一个 socket 消息包。它主要包含一个 ` byte[] body ` 字段来存储消息体的数据。
@@ -269,4 +286,16 @@ public class TioBootServerConfig {
269
286
270
287
### 总结
271
288
272
- 整体上,这段代码展示了如何使用 ` tio-boot ` 框架来构建一个同时支持 TCP、HTTP 和 WebSocket 协议的服务器。它通过定义消息包的格式(` DemoPacket ` )、处理逻辑(` DemoHandler ` )、事件监听(` DemoListener ` )以及应用程序启动配置(` ServerConfig ` ),实现了一个基本的网络通信服务器。
289
+ 整体上,这段代码展示了如何使用 ` tio-boot ` 框架来构建一个同时支持 TCP、HTTP 和 WebSocket 协议的服务器。它通过定义消息包的格式(` DemoPacket ` )、处理逻辑(` DemoHandler ` )、事件监听(` DemoListener ` )以及应用程序启动配置(` ServerConfig ` ),实现了一个基本的网络通信服务器。
290
+
291
+ ### 测试源码地址
292
+
293
+ https://github.com/litongjava/java-ee-tio-boot-study/tree/main/tio-boot-latest-study/tio-boot-tcp-hello
294
+
295
+ ### ByteBufferPacket
296
+
297
+ 如果你不行将 ByteBuffer 进行编码和解码,可以使用 tio-boot 提供的 ByteBufferPacket 直接将 ByteBuffer 添加到 ByteBufferPacket 中
298
+
299
+ ```
300
+ import com.litongjava.tio.boot.tcp.ByteBufferPacket
301
+ ```
0 commit comments