forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
119 lines (102 loc) · 2.43 KB
/
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
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
package mux
import (
"runtime"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
)
type Writer struct {
id uint16
dest net.Destination
writer buf.Writer
followup bool
transferType protocol.TransferType
}
func NewWriter(id uint16, dest net.Destination, writer buf.Writer, transferType protocol.TransferType) *Writer {
return &Writer{
id: id,
dest: dest,
writer: writer,
followup: false,
transferType: transferType,
}
}
func NewResponseWriter(id uint16, writer buf.Writer, transferType protocol.TransferType) *Writer {
return &Writer{
id: id,
writer: writer,
followup: true,
transferType: transferType,
}
}
func (w *Writer) getNextFrameMeta() FrameMetadata {
meta := FrameMetadata{
SessionID: w.id,
Target: w.dest,
}
if w.followup {
meta.SessionStatus = SessionStatusKeep
} else {
w.followup = true
meta.SessionStatus = SessionStatusNew
}
return meta
}
func (w *Writer) writeMetaOnly() error {
meta := w.getNextFrameMeta()
b := buf.New()
if err := b.AppendSupplier(meta.AsSupplier()); err != nil {
return err
}
runtime.KeepAlive(meta)
return w.writer.Write(buf.NewMultiBufferValue(b))
}
func (w *Writer) writeData(mb buf.MultiBuffer) error {
meta := w.getNextFrameMeta()
meta.Option.Add(OptionData)
frame := buf.New()
if err := frame.AppendSupplier(meta.AsSupplier()); err != nil {
return err
}
runtime.KeepAlive(meta)
if err := frame.AppendSupplier(serial.WriteUint16(uint16(mb.Len()))); err != nil {
return err
}
mb2 := buf.NewMultiBuffer()
mb2.Append(frame)
mb2.AppendMulti(mb)
return w.writer.Write(mb2)
}
// Write implements buf.MultiBufferWriter.
func (w *Writer) Write(mb buf.MultiBuffer) error {
if mb.IsEmpty() {
return w.writeMetaOnly()
}
if w.transferType == protocol.TransferTypeStream {
const chunkSize = 8 * 1024
for !mb.IsEmpty() {
slice := mb.SliceBySize(chunkSize)
if err := w.writeData(slice); err != nil {
return err
}
}
} else {
for _, b := range mb {
if err := w.writeData(buf.NewMultiBufferValue(b)); err != nil {
return err
}
}
}
return nil
}
func (w *Writer) Close() {
meta := FrameMetadata{
SessionID: w.id,
SessionStatus: SessionStatusEnd,
}
frame := buf.New()
frame.AppendSupplier(meta.AsSupplier())
runtime.KeepAlive(meta)
w.writer.Write(buf.NewMultiBufferValue(frame))
}