forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
40 lines (32 loc) · 759 Bytes
/
writer.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
package mux
import "v2ray.com/core/common/buf"
import "v2ray.com/core/common/serial"
type muxWriter struct {
meta *FrameMetadata
writer buf.Writer
}
func (w *muxWriter) Write(b *buf.Buffer) error {
frame := buf.New()
frame.AppendSupplier(w.meta.AsSupplier())
if w.meta.SessionStatus == SessionStatusNew {
w.meta.SessionStatus = SessionStatusKeep
}
frame.AppendSupplier(serial.WriteUint16(0))
lengthBytes := frame.BytesFrom(-2)
nBytes, err := frame.Write(b.Bytes())
if err != nil {
return err
}
serial.Uint16ToBytes(uint16(nBytes), lengthBytes[:0])
if err := w.writer.Write(frame); err != nil {
frame.Release()
b.Release()
return err
}
b.SliceFrom(nBytes)
if !b.IsEmpty() {
return w.Write(b)
}
b.Release()
return nil
}