Kotlin ❤️ Netty = 房间系统小练习

2020-05-05 19:32:56 +08:00
 dcalsky

Github 仓库链接: https://github.com/dcalsky/wws-socket

N 年前用 socket.io 做了一个简单的多人游戏对战服务器,当时用的 websocket 。最近发现对 socket 了解与使用并不深刻,于是从头做一个小项目,一步步加深对 TCP 数据传输的理解。

收获

  1. 能够熟练地拆装 tcp frame (所谓“粘包半包问题”,这样说比较好理解
  2. Bytes 读写小 /大端序(little/big endian)了解
  3. 尝试了用 Kotlin 试试不同的 Java 生态,并且阅读了许多 netty 经典 codec 的代码,受益匪浅
  4. 尝试定义了简单的 packet 结构,不再依赖 json 或其他通用数据格式
  5. Kotlin 玩位操作真的麻烦

部分介绍(详见 README

The Simplest room IM system made by Netty TCP. Inspired by @WWS.

PackageT

PackageT is a TCP communication packet structure of WWS-SOCKET that includes two parts: Header and Body.

Client use socket to create connection with server and then send packet based PackageT to it. Once server validates packet successfully, it will reply a packet back.

                              PackageT                                                                          
+---------------------------------------------------------------------+                                          
|       20 Bytes                          Unlimited                   |                                          
|+---------------------++--------------------------------------------+|                                          
||       Header        ||                    Body                    ||                                          
|+---------------------++--------------------------------------------+|                                          
+---------------------------------------------------------------------+

Header

The header of packageT has fixed 20 bytes including 4 parts. Message Type has 2 bytes with uint16 (little-endian), body Length has 4 bytes with uint32 (little-endian), Send Time has 8 bytes with uint64 (little-endian) and Package Hash has 6 bytes.

                                    PackageT Header                                               
+--------------------------------------------------------------------------------------+          
|    2 Bytes         4 Bytes                  8 Bytes                   6 Bytes        |          
| +------------++-----------------+ +-------------------------++---------------------+ |          
| |Message Type||   Body Length   | |        Send Time        ||     Package Hash    | |          
| +------------++-----------------+ +-------------------------++---------------------+ |          
+--------------------------------------------------------------------------------------+                                                                                   

Message Type

Body Length

The byte length of packet body, client/server should measure body before sending packet.

Send Time

Unix millisecond timestamp when the packet sent.

Hash

Client generates a unique hash value before sending a packet and attach it to the header of packet. And then server should reply a packet with this hash in the header (except ROOM_MSG response type).

Body

The body structure of PackageT depends on Message Type, which means different message type has corresponding body.

2345 次点击
所在节点    分享创造
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/668727

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX