v2ray-core/app/proxyman/mux/writer.go

106 lines
2.0 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-04-19 16:00:11 +00:00
"runtime"
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-03-26 23:47:01 +00:00
"v2ray.com/core/common/serial"
)
2017-02-07 20:11:47 +00:00
2017-04-02 11:43:24 +00:00
type Writer struct {
2017-03-31 22:53:01 +00:00
id uint16
dest net.Destination
writer buf.Writer
followup bool
2017-02-07 20:11:47 +00:00
}
2017-04-02 11:43:24 +00:00
func NewWriter(id uint16, dest net.Destination, writer buf.Writer) *Writer {
return &Writer{
id: id,
dest: dest,
writer: writer,
followup: false,
2017-04-02 07:48:30 +00:00
}
}
2017-04-03 10:55:46 +00:00
func NewResponseWriter(id uint16, writer buf.Writer) *Writer {
return &Writer{
id: id,
writer: writer,
followup: true,
}
}
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,
}
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()
if err := b.AppendSupplier(meta.AsSupplier()); err != nil {
return err
2017-03-31 22:53:01 +00:00
}
2017-04-19 16:00:11 +00:00
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)
2017-02-07 20:11:47 +00:00
2017-03-31 22:53:01 +00:00
frame := buf.New()
2017-04-19 16:00:11 +00:00
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
2017-02-07 20:11:47 +00:00
}
2017-04-19 16:00:11 +00:00
2017-04-19 19:27:21 +00:00
mb2 := buf.NewMultiBuffer()
mb2.Append(frame)
2017-04-19 16:00:11 +00:00
mb2.AppendMulti(mb)
2017-04-15 19:07:23 +00:00
return w.writer.Write(mb2)
2017-03-31 22:53:01 +00:00
}
2017-02-07 20:11:47 +00:00
func (w *Writer) Write(mb buf.MultiBuffer) error {
2017-04-19 16:00:11 +00:00
if mb.IsEmpty() {
return w.writeMetaOnly()
}
const chunkSize = 8 * 1024
2017-04-19 16:00:11 +00:00
for !mb.IsEmpty() {
slice := mb.SliceBySize(chunkSize)
2017-04-19 16:00:11 +00:00
if err := w.writeData(slice); err != nil {
return err
}
}
return nil
}
2017-04-02 11:43:24 +00:00
func (w *Writer) Close() {
2017-03-31 22:53:01 +00:00
meta := FrameMetadata{
SessionID: w.id,
SessionStatus: SessionStatusEnd,
2017-02-07 20:11:47 +00:00
}
2017-03-31 22:53:01 +00:00
frame := buf.New()
frame.AppendSupplier(meta.AsSupplier())
2017-04-19 16:00:11 +00:00
runtime.KeepAlive(meta)
2017-03-31 22:53:01 +00:00
2017-04-16 11:17:35 +00:00
w.writer.Write(buf.NewMultiBufferValue(frame))
2017-02-07 20:11:47 +00:00
}