Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

协议改进 #1

Open
bg5sbk opened this issue Mar 16, 2017 · 0 comments
Open

协议改进 #1

bg5sbk opened this issue Mar 16, 2017 · 0 comments

Comments

@bg5sbk
Copy link
Contributor

bg5sbk commented Mar 16, 2017

改进点:

  • 让握手和重连区分更明显
  • 重连失败有明确响应

新建连接,上行:

  • 新建连接时,客户端先发送一个全0的字节告知服务端这是一个新连接

  • 接着客户端发送8个字节的握手请求

  • PublicKey为DH密钥交换用的公钥

     type HandshakeRequest struct {
     	PublicKey uint64
     }
    

新建连接,下行:

  • 当服务端收到新建连接请求后,下发16个字节的握手响应

  • PublicKey为DH密钥交换用的公钥

  • CryptedConnID为加密后的连接ID,加密所需密钥通过DH密钥交换算法计算得出

     type HandshakeResponse struct {
     	PublicKey     uint64
     	CryptedConnID uint64
     }
    

重连,上行:

  • 当客户端尝试重连时,新建一个TCP/IP连接,并发送一个全1的字节告知服务端这是一个重连

  • 接着客户端40个字节的重连请求

  • ConnID为连接ID

  • WriteCount为客户端已发送字节数

  • ReadCount为客户端已接收字节数

  • MD5Hash为消息前24个字节加通讯密钥计算得出的MD5哈希值

     type ReconnRequest struct {
     	ConnID     uint64
     	WriteCount uint64
     	ReadCount  uint64
     	MD5Hash    [16]byte
     }
    

重连,下行:

  • 当服务端接收到重连请求时,对连接的合法性进行验证

  • 服务端下发16个字节的重连响应

  • 验证失败:

    • WriteCount始终为-1
    • ReadCount始终为-1
  • 验证成功:

    • WriteCount为服务端已发送字节数
    • ReadCount为服务端已接收字节数
  • 紧接着服务端立即下发需要重传的数据

  • 客户端在收到重连响应后,比较收发字节数差值来读取服务端下发的重传数据

     type ReconnResponse struct {
     	WriteCount uint64
     	ReadCount  uint64
     }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant