1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-22 11:30:31 +08:00

完善注释

This commit is contained in:
fumiama
2021-10-25 19:29:59 +08:00
parent e29d5b2f48
commit fe6abd3898
10 changed files with 132 additions and 61 deletions

View File

@@ -1,7 +1,7 @@
package head
// map[peerip]endpoint
// Notify 是 map[peerip]endpoint
type Notify map[string]string
// peerips array
// Query 是 peerips 组成的数组
type Query []string

View File

@@ -2,17 +2,31 @@ package head
import "encoding/json"
// Packet 是发送和接收的最小单位
type Packet struct {
DataSZ uint32
Proto uint8
TTL uint8
// DataSZ len(Data)
DataSZ uint32
// Proto 详见 head
Proto uint8
// TTL is time to live
TTL uint8
// SrcPort 源端口
SrcPort uint16
// DstPort 目的端口
DstPort uint16
Src string
Dst string
Data []byte
// Src 源 ip
Src string
// Dst 目的 ip
Dst string
// Hash 使用 BLAKE2 生成加密前 Packet 的摘要
// 生成时 Hash 全 0
// https://github.com/minio/blake2b-simd
Hash [32]byte
// Data 承载的数据
Data []byte
}
// NewPacket 生成一个新包
func NewPacket(proto uint8, srcPort uint16, dstPort uint16, data []byte) *Packet {
return &Packet{
Proto: proto,
@@ -23,10 +37,14 @@ func NewPacket(proto uint8, srcPort uint16, dstPort uint16, data []byte) *Packet
}
}
// UnMashal 将 data 的数据解码到自身
// 同时通过 Hash 验证数据完整性
func (p *Packet) UnMashal(data []byte) error {
return json.Unmarshal(data, p)
}
// Mashal 将自身数据编码为 []byte
// 同时生成 Hash
func (p *Packet) Mashal(src string, dst string) ([]byte, error) {
p.DataSZ = uint32(len(p.Data))
p.Src = src

View File

@@ -1,5 +1,6 @@
package head
// Proto 类型定义
const (
ProtoHello uint8 = iota
ProtoNotify