Skip to content

Instantly share code, notes, and snippets.

@xjdrew
Last active June 22, 2017 03:14
Show Gist options
  • Save xjdrew/2c7fcf0e9b5ff368e6e8370b976b8041 to your computer and use it in GitHub Desktop.
Save xjdrew/2c7fcf0e9b5ff368e6e8370b976b8041 to your computer and use it in GitHub Desktop.
断线重连协议 2.0

断线重连协议 2.0

约定

  1. 定长协议: 每条协议的请求和响应应该是固定长度的。
  2. 小头编码:所有数字的字节都使用little-endian

协议

新建连接

请求:

NewConnReq {
    uint32 id;          // 填0
    uint32 magic;       // 用来指定选择哪个后端,为0表示随机后端
    uint64 dhPublicKey; // 客户端的 dhPublicKey
}

响应:

NewConnResp {
    uint32 id;          // 服务端为连接分配的id
    uint64 dhPublicKey; // 服务端的 dhPublicKey
}

恢复连接

请求:

ReuseConnReq {
        uint32 id          // 新建连接时服务端分配的id
	uint32 index       // 一个每次请求都要递增的值,用来防止重放攻击
	uint32 received    // 客户端收到的字符数模2^32
	uint64 token       // 对id、index、received编码后的8个字节,使用dhSecretKey签名后的值
}

响应:

ReuseConnResp {
    uint32 received; // 服务端收到的字符数
    uint16 code;     // 状态码
}

状态码含义:

200 OK
 	表示连接成功
 401 Unauthorized
 	表示 HMAC 计算错误
 403 Index Expired
 	表示 Index 已经使用过
 404 User Not Found
 	表示连接 id 已经无效
 406 Not Acceptable
 	表示 cache 的数据流不够

Wire

直接传输,不需要包头。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment