11package wkproto
22
3+ type EndReason uint8
4+
5+ // EndReason constants define why a stream was completed
6+ const (
7+ // EndReasonSuccess indicates the stream completed successfully (default)
8+ EndReasonSuccess EndReason = 0
9+ // EndReasonTimeout indicates the stream ended due to inactivity timeout
10+ EndReasonTimeout EndReason = 1
11+ // EndReasonError indicates the stream ended due to an error
12+ EndReasonError EndReason = 2
13+ // EndReasonCancelled indicates the stream was manually cancelled
14+ EndReasonCancelled EndReason = 3
15+ // EndReasonForce indicates the stream was forcefully ended (e.g., channel closure)
16+ EndReasonForce EndReason = 4
17+ )
18+
19+ func (e EndReason ) String () string {
20+ switch e {
21+ case EndReasonSuccess :
22+ return "success"
23+ case EndReasonTimeout :
24+ return "timeout"
25+ case EndReasonError :
26+ return "error"
27+ case EndReasonCancelled :
28+ return "cancelled"
29+ case EndReasonForce :
30+ return "force"
31+ default :
32+ return "unknown"
33+ }
34+ }
35+
36+ func (e EndReason ) Value () uint8 {
37+ return uint8 (e )
38+ }
39+
340// ChunkPacket 消息块
441type ChunkPacket struct {
542 Framer
6- MessageID int64 // 消息ID(同个消息多个块的消息ID相同)
7- ChunkID uint64 // 块ID(顺序递增)
8- Payload []byte // 消息内容
43+ MessageID int64 // 消息ID(同个消息多个块的消息ID相同)
44+ ChunkID uint64 // 块ID(顺序递增)
45+ EndReason EndReason // 结束原因
46+ Payload []byte // 消息内容
947}
1048
1149// GetPacketType 获得包类型
@@ -24,6 +62,7 @@ func (c *ChunkPacket) SizeWithProtoVersion(protVersion uint8) int {
2462func encodeChunk (chunkPacket * ChunkPacket , enc * Encoder , _ uint8 ) error {
2563 enc .WriteInt64 (chunkPacket .MessageID )
2664 enc .WriteUint64 (chunkPacket .ChunkID )
65+ enc .WriteUint8 (chunkPacket .EndReason .Value ())
2766 enc .WriteBytes (chunkPacket .Payload )
2867 return nil
2968}
@@ -41,6 +80,12 @@ func decodeChunk(frame Frame, data []byte, _ uint8) (Frame, error) {
4180 return nil , err
4281 }
4382
83+ var endReason uint8
84+ if endReason , err = dec .Uint8 (); err != nil {
85+ return nil , err
86+ }
87+ chunkPacket .EndReason = EndReason (endReason )
88+
4489 if chunkPacket .Payload , err = dec .BinaryAll (); err != nil {
4590 return nil , err
4691 }
@@ -53,6 +98,7 @@ func encodeChunkSize(packet *ChunkPacket, _ uint8) int {
5398 size := 0
5499 size += MessageIDByteSize // 消息ID
55100 size += ChunkIDByteSize // 块ID
101+ size += EndReasonByteSize // 结束原因
56102 size += len (packet .Payload ) // 消息内容
57103 return size
58104}
0 commit comments