v2ray-core/common/mux/writer.go

127 lines
2.6 KiB
Go
Raw Normal View History

2017-02-07 20:11:47 +00:00
package mux
2017-03-26 23:47:01 +00:00
import (
2017-11-07 21:31:47 +00:00
"v2ray.com/core/common"
2017-03-26 23:47:01 +00:00
"v2ray.com/core/common/buf"
2017-03-31 22:53:01 +00:00
"v2ray.com/core/common/net"
2017-05-02 20:23:07 +00:00
"v2ray.com/core/common/protocol"
2018-11-03 12:03:02 +00:00
"v2ray.com/core/common/serial"
2017-03-26 23:47:01 +00:00
)
2017-02-07 20:11:47 +00:00
2017-04-02 11:43:24 +00:00
type Writer struct {
2017-05-02 20:23:07 +00:00
dest net.Destination
writer buf.Writer
id uint16
2017-05-02 20:23:07 +00:00
followup bool
2018-04-04 19:33:33 +00:00
hasError bool
2017-05-02 20:23:07 +00:00
transferType protocol.TransferType
2017-02-07 20:11:47 +00:00
}
2017-05-02 20:23:07 +00:00
func NewWriter(id uint16, dest net.Destination, writer buf.Writer, transferType protocol.TransferType) *Writer {
2017-04-02 11:43:24 +00:00
return &Writer{
2017-05-02 20:23:07 +00:00
id: id,
dest: dest,
writer: writer,
followup: false,
transferType: transferType,
2017-04-02 07:48:30 +00:00
}
}
2017-05-02 20:23:07 +00:00
func NewResponseWriter(id uint16, writer buf.Writer, transferType protocol.TransferType) *Writer {
2017-04-03 10:55:46 +00:00
return &Writer{
2017-05-02 20:23:07 +00:00
id: id,
writer: writer,
followup: true,
transferType: transferType,
2017-04-03 10:55:46 +00:00
}
}
2017-04-19 16:00:11 +00:00
func (w *Writer) getNextFrameMeta() FrameMetadata {
2017-03-31 22:53:01 +00:00
meta := FrameMetadata{
SessionID: w.id,
Target: w.dest,
}
2018-04-04 19:33:33 +00:00
2017-03-31 22:53:01 +00:00
if w.followup {
meta.SessionStatus = SessionStatusKeep
} else {
w.followup = true
meta.SessionStatus = SessionStatusNew
2017-02-07 20:11:47 +00:00
}
2017-04-19 16:00:11 +00:00
return meta
}
2017-04-19 09:20:08 +00:00
2017-04-19 16:00:11 +00:00
func (w *Writer) writeMetaOnly() error {
meta := w.getNextFrameMeta()
b := buf.New()
2018-02-23 23:57:54 +00:00
if err := meta.WriteTo(b); err != nil {
2017-04-19 16:00:11 +00:00
return err
2017-03-31 22:53:01 +00:00
}
2018-11-16 10:08:12 +00:00
return w.writer.WriteMultiBuffer(buf.MultiBuffer{b})
2017-04-19 16:00:11 +00:00
}
2018-08-31 13:15:40 +00:00
func writeMetaWithFrame(writer buf.Writer, meta FrameMetadata, data buf.MultiBuffer) error {
2017-03-31 22:53:01 +00:00
frame := buf.New()
2018-02-23 23:57:54 +00:00
if err := meta.WriteTo(frame); err != nil {
2017-04-19 16:00:11 +00:00
return err
}
2018-11-03 12:03:02 +00:00
if _, err := serial.WriteUint16(frame, uint16(data.Len())); err != nil {
2017-04-19 16:00:11 +00:00
return err
2017-02-07 20:11:47 +00:00
}
2017-04-19 16:00:11 +00:00
2018-11-16 10:29:16 +00:00
mb2 := make(buf.MultiBuffer, 0, len(data)+1)
2018-11-17 08:12:20 +00:00
mb2 = append(mb2, frame)
mb2 = append(mb2, data...)
2018-08-31 13:15:40 +00:00
return writer.WriteMultiBuffer(mb2)
}
func (w *Writer) writeData(mb buf.MultiBuffer) error {
meta := w.getNextFrameMeta()
meta.Option.Set(OptionData)
return writeMetaWithFrame(w.writer, meta, mb)
2017-03-31 22:53:01 +00:00
}
2017-02-07 20:11:47 +00:00
2017-11-09 21:33:15 +00:00
// WriteMultiBuffer implements buf.Writer.
func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-11-17 21:45:07 +00:00
defer buf.ReleaseMulti(mb)
2017-11-07 10:58:43 +00:00
2017-04-19 16:00:11 +00:00
if mb.IsEmpty() {
return w.writeMetaOnly()
}
2017-11-07 10:58:43 +00:00
for !mb.IsEmpty() {
var chunk buf.MultiBuffer
if w.transferType == protocol.TransferTypeStream {
2018-11-18 19:16:14 +00:00
mb, chunk = buf.SplitSize(mb, 8*1024)
2017-11-07 10:58:43 +00:00
} else {
2018-11-18 18:57:29 +00:00
mb2, b := buf.SplitFirst(mb)
mb = mb2
chunk = buf.MultiBuffer{b}
2017-05-02 20:23:07 +00:00
}
2017-11-07 10:58:43 +00:00
if err := w.writeData(chunk); err != nil {
return err
}
}
2017-05-02 20:23:07 +00:00
return nil
}
2018-05-26 14:32:55 +00:00
// Close implements common.Closable.
2018-02-08 14:39:46 +00:00
func (w *Writer) Close() error {
2017-03-31 22:53:01 +00:00
meta := FrameMetadata{
SessionID: w.id,
SessionStatus: SessionStatusEnd,
2017-02-07 20:11:47 +00:00
}
2018-04-04 19:33:33 +00:00
if w.hasError {
meta.Option.Set(OptionError)
2018-04-04 15:20:45 +00:00
}
frame := buf.New()
common.Must(meta.WriteTo(frame))
2018-11-16 10:08:12 +00:00
w.writer.WriteMultiBuffer(buf.MultiBuffer{frame}) // nolint: errcheck
2018-04-04 15:20:45 +00:00
return nil
}