forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
frame.go
227 lines (191 loc) · 7.04 KB
/
frame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package qlog
import (
"fmt"
"github.com/quic-go/quic-go/internal/wire"
"github.com/quic-go/quic-go/logging"
"github.com/francoispqt/gojay"
)
type frame struct {
Frame logging.Frame
}
var _ gojay.MarshalerJSONObject = frame{}
var _ gojay.MarshalerJSONArray = frames{}
func (f frame) MarshalJSONObject(enc *gojay.Encoder) {
switch frame := f.Frame.(type) {
case *logging.PingFrame:
marshalPingFrame(enc, frame)
case *logging.AckFrame:
marshalAckFrame(enc, frame)
case *logging.ResetStreamFrame:
marshalResetStreamFrame(enc, frame)
case *logging.StopSendingFrame:
marshalStopSendingFrame(enc, frame)
case *logging.CryptoFrame:
marshalCryptoFrame(enc, frame)
case *logging.NewTokenFrame:
marshalNewTokenFrame(enc, frame)
case *logging.StreamFrame:
marshalStreamFrame(enc, frame)
case *logging.MaxDataFrame:
marshalMaxDataFrame(enc, frame)
case *logging.MaxStreamDataFrame:
marshalMaxStreamDataFrame(enc, frame)
case *logging.MaxStreamsFrame:
marshalMaxStreamsFrame(enc, frame)
case *logging.DataBlockedFrame:
marshalDataBlockedFrame(enc, frame)
case *logging.StreamDataBlockedFrame:
marshalStreamDataBlockedFrame(enc, frame)
case *logging.StreamsBlockedFrame:
marshalStreamsBlockedFrame(enc, frame)
case *logging.NewConnectionIDFrame:
marshalNewConnectionIDFrame(enc, frame)
case *logging.RetireConnectionIDFrame:
marshalRetireConnectionIDFrame(enc, frame)
case *logging.PathChallengeFrame:
marshalPathChallengeFrame(enc, frame)
case *logging.PathResponseFrame:
marshalPathResponseFrame(enc, frame)
case *logging.ConnectionCloseFrame:
marshalConnectionCloseFrame(enc, frame)
case *logging.HandshakeDoneFrame:
marshalHandshakeDoneFrame(enc, frame)
case *logging.DatagramFrame:
marshalDatagramFrame(enc, frame)
default:
panic("unknown frame type")
}
}
func (f frame) IsNil() bool { return false }
type frames []frame
func (fs frames) IsNil() bool { return fs == nil }
func (fs frames) MarshalJSONArray(enc *gojay.Encoder) {
for _, f := range fs {
enc.Object(f)
}
}
func marshalPingFrame(enc *gojay.Encoder, _ *wire.PingFrame) {
enc.StringKey("frame_type", "ping")
}
type ackRanges []wire.AckRange
func (ars ackRanges) MarshalJSONArray(enc *gojay.Encoder) {
for _, r := range ars {
enc.Array(ackRange(r))
}
}
func (ars ackRanges) IsNil() bool { return false }
type ackRange wire.AckRange
func (ar ackRange) MarshalJSONArray(enc *gojay.Encoder) {
enc.AddInt64(int64(ar.Smallest))
if ar.Smallest != ar.Largest {
enc.AddInt64(int64(ar.Largest))
}
}
func (ar ackRange) IsNil() bool { return false }
func marshalAckFrame(enc *gojay.Encoder, f *logging.AckFrame) {
enc.StringKey("frame_type", "ack")
enc.FloatKeyOmitEmpty("ack_delay", milliseconds(f.DelayTime))
enc.ArrayKey("acked_ranges", ackRanges(f.AckRanges))
if hasECN := f.ECT0 > 0 || f.ECT1 > 0 || f.ECNCE > 0; hasECN {
enc.Uint64Key("ect0", f.ECT0)
enc.Uint64Key("ect1", f.ECT1)
enc.Uint64Key("ce", f.ECNCE)
}
}
func marshalResetStreamFrame(enc *gojay.Encoder, f *logging.ResetStreamFrame) {
enc.StringKey("frame_type", "reset_stream")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("error_code", int64(f.ErrorCode))
enc.Int64Key("final_size", int64(f.FinalSize))
}
func marshalStopSendingFrame(enc *gojay.Encoder, f *logging.StopSendingFrame) {
enc.StringKey("frame_type", "stop_sending")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("error_code", int64(f.ErrorCode))
}
func marshalCryptoFrame(enc *gojay.Encoder, f *logging.CryptoFrame) {
enc.StringKey("frame_type", "crypto")
enc.Int64Key("offset", int64(f.Offset))
enc.Int64Key("length", int64(f.Length))
}
func marshalNewTokenFrame(enc *gojay.Encoder, f *logging.NewTokenFrame) {
enc.StringKey("frame_type", "new_token")
enc.ObjectKey("token", &token{Raw: f.Token})
}
func marshalStreamFrame(enc *gojay.Encoder, f *logging.StreamFrame) {
enc.StringKey("frame_type", "stream")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("offset", int64(f.Offset))
enc.IntKey("length", int(f.Length))
enc.BoolKeyOmitEmpty("fin", f.Fin)
}
func marshalMaxDataFrame(enc *gojay.Encoder, f *logging.MaxDataFrame) {
enc.StringKey("frame_type", "max_data")
enc.Int64Key("maximum", int64(f.MaximumData))
}
func marshalMaxStreamDataFrame(enc *gojay.Encoder, f *logging.MaxStreamDataFrame) {
enc.StringKey("frame_type", "max_stream_data")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("maximum", int64(f.MaximumStreamData))
}
func marshalMaxStreamsFrame(enc *gojay.Encoder, f *logging.MaxStreamsFrame) {
enc.StringKey("frame_type", "max_streams")
enc.StringKey("stream_type", streamType(f.Type).String())
enc.Int64Key("maximum", int64(f.MaxStreamNum))
}
func marshalDataBlockedFrame(enc *gojay.Encoder, f *logging.DataBlockedFrame) {
enc.StringKey("frame_type", "data_blocked")
enc.Int64Key("limit", int64(f.MaximumData))
}
func marshalStreamDataBlockedFrame(enc *gojay.Encoder, f *logging.StreamDataBlockedFrame) {
enc.StringKey("frame_type", "stream_data_blocked")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("limit", int64(f.MaximumStreamData))
}
func marshalStreamsBlockedFrame(enc *gojay.Encoder, f *logging.StreamsBlockedFrame) {
enc.StringKey("frame_type", "streams_blocked")
enc.StringKey("stream_type", streamType(f.Type).String())
enc.Int64Key("limit", int64(f.StreamLimit))
}
func marshalNewConnectionIDFrame(enc *gojay.Encoder, f *logging.NewConnectionIDFrame) {
enc.StringKey("frame_type", "new_connection_id")
enc.Int64Key("sequence_number", int64(f.SequenceNumber))
enc.Int64Key("retire_prior_to", int64(f.RetirePriorTo))
enc.IntKey("length", f.ConnectionID.Len())
enc.StringKey("connection_id", f.ConnectionID.String())
enc.StringKey("stateless_reset_token", fmt.Sprintf("%x", f.StatelessResetToken))
}
func marshalRetireConnectionIDFrame(enc *gojay.Encoder, f *logging.RetireConnectionIDFrame) {
enc.StringKey("frame_type", "retire_connection_id")
enc.Int64Key("sequence_number", int64(f.SequenceNumber))
}
func marshalPathChallengeFrame(enc *gojay.Encoder, f *logging.PathChallengeFrame) {
enc.StringKey("frame_type", "path_challenge")
enc.StringKey("data", fmt.Sprintf("%x", f.Data[:]))
}
func marshalPathResponseFrame(enc *gojay.Encoder, f *logging.PathResponseFrame) {
enc.StringKey("frame_type", "path_response")
enc.StringKey("data", fmt.Sprintf("%x", f.Data[:]))
}
func marshalConnectionCloseFrame(enc *gojay.Encoder, f *logging.ConnectionCloseFrame) {
errorSpace := "transport"
if f.IsApplicationError {
errorSpace = "application"
}
enc.StringKey("frame_type", "connection_close")
enc.StringKey("error_space", errorSpace)
if errName := transportError(f.ErrorCode).String(); len(errName) > 0 {
enc.StringKey("error_code", errName)
} else {
enc.Uint64Key("error_code", f.ErrorCode)
}
enc.Uint64Key("raw_error_code", f.ErrorCode)
enc.StringKey("reason", f.ReasonPhrase)
}
func marshalHandshakeDoneFrame(enc *gojay.Encoder, _ *logging.HandshakeDoneFrame) {
enc.StringKey("frame_type", "handshake_done")
}
func marshalDatagramFrame(enc *gojay.Encoder, f *logging.DatagramFrame) {
enc.StringKey("frame_type", "datagram")
enc.Int64Key("length", int64(f.Length))
}