github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/internal/highway/frame.go (about)

     1  package highway
     2  
     3  // from https://github.com/Mrs4s/MiraiGo/tree/master/client/internal/highway/frame.go
     4  
     5  import (
     6  	"encoding/binary"
     7  	"net"
     8  )
     9  
    10  var etx = []byte{0x29}
    11  
    12  // Frame 包格式
    13  //
    14  //   - STX: 0x28(40)
    15  //   - head length
    16  //   - body length
    17  //   - head data
    18  //   - body data
    19  //   - ETX: 0x29(41)
    20  //
    21  // 节省内存, 可被go runtime优化为writev操作
    22  func Frame(head []byte, body []byte) net.Buffers {
    23  	buffers := make(net.Buffers, 4)
    24  	// buffer0 format:
    25  	// 	- STX
    26  	// 	- head length
    27  	// 	- body length
    28  	buffer0 := make([]byte, 9)
    29  	buffer0[0] = 0x28
    30  	binary.BigEndian.PutUint32(buffer0[1:], uint32(len(head)))
    31  	binary.BigEndian.PutUint32(buffer0[5:], uint32(len(body)))
    32  	buffers[0] = buffer0
    33  	buffers[1] = head
    34  	buffers[2] = body
    35  	buffers[3] = etx
    36  	return buffers
    37  }