王虎 4 سال پیش
والد
کامیت
b7072d532c
9فایلهای تغییر یافته به همراه201 افزوده شده و 59 حذف شده
  1. 60 31
      README.md
  2. 5 0
      close.proto
  3. 30 0
      golang.md
  4. 0 13
      handshark.proto
  5. 13 0
      lobby.proto
  6. 0 15
      login.proto
  7. 21 0
      room.proto
  8. 31 0
      sync.proto
  9. 41 0
      user.proto

+ 60 - 31
README.md

@@ -1,33 +1,62 @@
 # 快递项目socket协议protobuf定义
 
-## go
-
-### gogoprotobuf
-* 安装插件 
-* gogoprotobuf有两个插件可以使用
-* protoc-gen-gogo:和protoc-gen-go生成的文件差不多,性能也几乎一样(稍微快一点点)
-* protoc-gen-gofast:生成的文件更复杂,性能也更高(快5-7倍)
-
-```
-//gogo
-go get github.com/gogo/protobuf/protoc-gen-gogo
-
-//gofast
-go get github.com/gogo/protobuf/protoc-gen-gofast
-```
-* 安装gogoprotobuf库文件
-
-```
-go get github.com/gogo/protobuf/proto
-go get github.com/gogo/protobuf/gogoproto  //这个不装也没关系
-```
-* 生成go文件
-```
-//gogo
-protoc --gogo_out=. *.proto
-
-//gofast
-protoc --gofast_out=. *.proto
-```
-
-## unity
+## 1. 概述
+* 本项目前后端使用 kcp 协议通讯  [KCP - A Fast and Reliable ARQ Protocol](https://github.com/skywind3000/kcp)
+
+## 2. 消息
+* 消息是前后端通讯的内容的封装
+### 2.1 消息格式:
+* 消息是拼接的二进制流
+* 消息由 4 段数据拼接而成, `act` + `type` + `body length` + `body`
+
+    * act: 单字节(byte), 协议号, 见下文的act定义, 根据`act`将`body`映射到对应的数据对象
+    * type: 单字节(byte), 定义了`body`的类型, 目前支持两种`(0=json;1=protobuf)`
+    * body length: 双字节(short), **大端序**, 对应的是`body`的数据长度, 可以避免保证数据读取的完整性
+    * body: 消息正文
+
+### 2.1 消息读取逻辑
+* 读取消息有两种选择
+    * 一种是使用kcp提供的read方法, 这种比较方便, 理论上应该也是没有问题的
+    * 另一种是自已实现读消息逻辑, 读取方法如下:
+        * 读取一个字节, 拿到act
+        * 读取一个字节, 拿到type
+        * 读取两个字节, 按大端序写入short, 拿到body length
+        * 读取 body length 个字节, 写入消息正文
+
+## 3. 消息正文
+### 3.1 act类别
+* `c -> s` 表示客户端给服务器的消息
+* `s -> c` 表示服务器器给客户端的消息
+* `c -> s` 消息中, 带有 `request` 的消息, 服务器都有响应一个对应的 `response` 消息
+* `notify`消息, 表示客户端通知服务器某些行为, 服务器知道就行了, 不需要返回任何数据
+* `push` 消息, 表示服务器通知客户端某些行为, 客户端需要
+
+### 3.2 act定义
+| 协议号 | message | 方向 | 备注 |
+| --- | --- | --- | --- |
+| 1 | HandsharkNotify | c -> s | 客户端发送握手请求 |
+| 2 | HandsharkPush | s -> c | 服务端确认握手请求 |
+| 3 | HandsharkAckNotify | c -> s | 客户端确认握手请求 |
+| 4 | CloseNotify | c -> s | 客户端关闭连接 |
+| 5 | ClosePush | s -> c | 客户端关闭用户连接 |
+| 10 | LoginRequest | c -> s | 客户端请求登陆 |
+| 11 | LoginResponse | s -> c | 服务器响应登陆请求 |
+| 12 | HeartbeatRequest | c -> s | 客户端心跳 |
+| 13 | HeartbeatResponse | s -> c | 服务器心跳 |
+| 100 | LobbyJoinPush | s -> c | 用户加入大厅 |
+| 101 | LobbyLeavePush | s -> c | 用户离开大厅 |
+| 120 | RoomCreateRequest | c -> s | 客户端请求创建房间 |
+| 121 | RoomCreateResponse | s -> c | 服务器响应创建房间请求 |
+| 122 | RoomJoinRequest | c -> s | 客户端请求加入房间 |
+| 123 | RoomJoinResponse | s -> c | 服务器响应加入房间请求 |
+| 124 | RoomJoinPush | s -> c | 服务端推送用户加入房间消息 |
+| 125 | RoomLeaveRequest | c -> s | 客户端请求退出房间 |
+| 126 | RoomLeaveResponse | s -> c | 服务器响应退出房间 |
+| 127 | RoomLeavePush | s -> c | 服务器推送用户离开房间消息 |
+| 150 | RestoreRequest | c -> s | 客户端请求恢复数据 |
+| 151 | RestoreResponse | s -> c | 服务器响应当前状态 |
+| 152 | SyncRequest | c -> s | 客户端同步数据 |
+| 152 | SyncResponse | s -> c | 服务器响应同步结果 |
+| 153 | SyncResponse | s -> c | 服务器响应同步结果 |
+
+

+ 5 - 0
close.proto

@@ -0,0 +1,5 @@
+
+syntax = "proto3";
+
+package protos;
+

+ 30 - 0
golang.md

@@ -0,0 +1,30 @@
+# go protobuf 使用
+
+## 安装插件 
+* gogoprotobuf有两个插件可以使用
+* protoc-gen-gogo:和protoc-gen-go生成的文件差不多,性能也几乎一样(稍微快一点点)
+* protoc-gen-gofast:生成的文件更复杂,性能也更高(快5-7倍)
+
+```
+//gogo
+go get github.com/gogo/protobuf/protoc-gen-gogo
+
+//gofast
+go get github.com/gogo/protobuf/protoc-gen-gofast
+```
+* 安装gogoprotobuf库文件
+
+```
+go get github.com/gogo/protobuf/proto
+go get github.com/gogo/protobuf/gogoproto  //这个不装也没关系
+```
+## 生成go文件
+```
+//gogo
+protoc --gogo_out=. *.proto
+
+//gofast
+protoc --gofast_out=. *.proto
+```
+
+## 语法说明

+ 0 - 13
handshark.proto

@@ -1,13 +0,0 @@
-
-syntax = "proto3";
-
-package protos;
-
-message HandsharkNotify {
-}
-
-message HandsharkPush {
-}
-
-message HandsharkAckPush {
-}

+ 13 - 0
lobby.proto

@@ -0,0 +1,13 @@
+syntax = "proto3";
+package protos;
+import "userinfo.proto";
+
+// 用户加入大厅
+message LobbyJoinPush {
+    UserInfo UserInfo = 1;
+}
+
+// 用户离开大厅
+message LobbyLeavePush {
+    int32 UserID = 1;
+}

+ 0 - 15
login.proto

@@ -1,15 +0,0 @@
-syntax = "proto3";
-package protos;
-
-import "result.proto";
-import "userinfo.proto";
-
-message LoginRequest {
-    UserInfo UserInfo = 1;
-}
-
-message LoginResponse {
-    Result Result = 1;
-    int32 LobbyID = 2;
-    int32 RoomID = 3;
-}

+ 21 - 0
room.proto

@@ -0,0 +1,21 @@
+syntax = "proto3";
+package protos;
+import "userinfo.proto";
+
+message RoomCreateRequest {
+}
+
+message RoomCreateResponse {
+}
+
+message RoomJoinPush {
+    UserInfo UserInfo = 1;
+}
+
+message RoomLeavePush {
+    int32 UserID = 1;
+}
+
+message RoomDismissPush {
+
+}

+ 31 - 0
sync.proto

@@ -0,0 +1,31 @@
+syntax = "proto3";
+
+package protos;
+
+// 载入游戏请求
+message RestoreRequest {
+}
+
+// 载入游戏响应
+message RestoreResponse {
+    map<int32, UserInfo> Users = 1;
+    int64 Step = 2;
+    map<string, bytes> Values = 3;
+}
+
+// 同步消息
+message SyncRequest {
+    int64 Step = 1;
+    map<string, bytes> Values = 2;
+}
+
+// 同步消息响应
+message SyncResponse {
+    bool Status = 1;
+}
+
+// 同步消息推送
+message SyncPush {
+    int64 Step = 1;
+    map<string, bytes> Values = 2;
+}

+ 41 - 0
user.proto

@@ -0,0 +1,41 @@
+syntax = "proto3";
+package protos;
+
+import "userinfo.proto";
+import "result.proto";
+
+message HandsharkNotify {
+}
+
+message HandsharkPush {
+}
+
+message HandsharkAckNotify {
+}
+
+
+message CloseNotify {
+}
+
+message closePush {
+    int32 reason = 1; // 关闭原因
+}
+
+message HeartbeatRequest {
+
+}
+
+message HeartbeatResponse {
+    
+}
+
+message LoginRequest {
+    UserInfo UserInfo = 1;
+}
+
+message LoginResponse {
+    Result Result = 1;
+    int64 LobbyID = 2;
+    int64 RoomID = 3;
+}
+