1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00
This commit is contained in:
Darien Raymond 2017-04-23 13:41:52 +02:00
parent e87b0ca848
commit cea6e28634
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 34 additions and 17 deletions

View File

@ -122,6 +122,11 @@ func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Co
return internet.Dial(ctx, dest) return internet.Dial(ctx, dest)
} }
var (
_ buf.MultiBufferReader = (*Connection)(nil)
_ buf.MultiBufferWriter = (*Connection)(nil)
)
type Connection struct { type Connection struct {
stream ray.Ray stream ray.Ray
closed bool closed bool
@ -170,11 +175,11 @@ func (v *Connection) Write(b []byte) (int, error) {
return buf.ToBytesWriter(v.writer).Write(b) return buf.ToBytesWriter(v.writer).Write(b)
} }
func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) { func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
if v.closed { if v.closed {
return 0, io.ErrClosedPipe return io.ErrClosedPipe
} }
return mb.Len(), v.writer.Write(mb) return v.writer.Write(mb)
} }
// Close implements net.Conn.Close(). // Close implements net.Conn.Close().

View File

@ -3,7 +3,7 @@ package buf
import "net" import "net"
type MultiBufferWriter interface { type MultiBufferWriter interface {
WriteMultiBuffer(MultiBuffer) (int, error) WriteMultiBuffer(MultiBuffer) error
} }
type MultiBufferReader interface { type MultiBufferReader interface {

View File

@ -22,8 +22,7 @@ type writerAdapter struct {
// Write implements buf.MultiBufferWriter. // Write implements buf.MultiBufferWriter.
func (w *writerAdapter) Write(mb MultiBuffer) error { func (w *writerAdapter) Write(mb MultiBuffer) error {
_, err := w.writer.WriteMultiBuffer(mb) return w.writer.WriteMultiBuffer(mb)
return err
} }
type mergingWriter struct { type mergingWriter struct {
@ -62,6 +61,10 @@ func (w *seqWriter) Write(mb MultiBuffer) error {
return nil return nil
} }
var (
_ MultiBufferWriter = (*bytesToBufferWriter)(nil)
)
type bytesToBufferWriter struct { type bytesToBufferWriter struct {
writer Writer writer Writer
} }
@ -81,8 +84,8 @@ func (w *bytesToBufferWriter) Write(payload []byte) (int, error) {
return len(payload), nil return len(payload), nil
} }
func (w *bytesToBufferWriter) WriteMultiBuffer(mb MultiBuffer) (int, error) { func (w *bytesToBufferWriter) WriteMultiBuffer(mb MultiBuffer) error {
return mb.Len(), w.writer.Write(mb) return w.writer.Write(mb)
} }
func (w *bytesToBufferWriter) ReadFrom(reader io.Reader) (int64, error) { func (w *bytesToBufferWriter) ReadFrom(reader io.Reader) (int64, error) {

View File

@ -27,6 +27,10 @@ func (r *CryptionReader) Read(data []byte) (int, error) {
return nBytes, err return nBytes, err
} }
var (
_ buf.MultiBufferWriter = (*CryptionWriter)(nil)
)
type CryptionWriter struct { type CryptionWriter struct {
stream cipher.Stream stream cipher.Stream
writer io.Writer writer io.Writer
@ -46,11 +50,11 @@ func (w *CryptionWriter) Write(data []byte) (int, error) {
return w.writer.Write(data) return w.writer.Write(data)
} }
func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) { func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
bs := mb.ToNetBuffers() bs := mb.ToNetBuffers()
for _, b := range bs { for _, b := range bs {
w.stream.XORKeyStream(b, b) w.stream.XORKeyStream(b, b)
} }
nBytes, err := bs.WriteTo(w.writer) _, err := bs.WriteTo(w.writer)
return int(nBytes), err return err
} }

View File

@ -375,11 +375,11 @@ func (v *Connection) Write(b []byte) (int, error) {
} }
} }
func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) { func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
if c.mergingWriter == nil { if c.mergingWriter == nil {
c.mergingWriter = buf.NewMergingWriterSize(c, c.mss) c.mergingWriter = buf.NewMergingWriterSize(c, c.mss)
} }
return mb.Len(), c.mergingWriter.Write(mb) return c.mergingWriter.Write(mb)
} }
func (v *Connection) SetState(state State) { func (v *Connection) SetState(state State) {

View File

@ -9,6 +9,11 @@ import (
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg tls -path Transport,Internet,TLS //go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg tls -path Transport,Internet,TLS
var (
_ buf.MultiBufferReader = (*conn)(nil)
_ buf.MultiBufferWriter = (*conn)(nil)
)
type conn struct { type conn struct {
net.Conn net.Conn
@ -23,11 +28,11 @@ func (c *conn) ReadMultiBuffer() (buf.MultiBuffer, error) {
return c.mergingReader.Read() return c.mergingReader.Read()
} }
func (c *conn) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) { func (c *conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
if c.mergingWriter == nil { if c.mergingWriter == nil {
c.mergingWriter = buf.NewMergingWriter(c.Conn) c.mergingWriter = buf.NewMergingWriter(c.Conn)
} }
return mb.Len(), c.mergingWriter.Write(mb) return c.mergingWriter.Write(mb)
} }
func Client(c net.Conn, config *tls.Config) net.Conn { func Client(c net.Conn, config *tls.Config) net.Conn {

View File

@ -69,11 +69,11 @@ func (c *connection) Write(b []byte) (int, error) {
return len(b), nil return len(b), nil
} }
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) { func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
if c.mergingWriter == nil { if c.mergingWriter == nil {
c.mergingWriter = buf.NewMergingWriter(c) c.mergingWriter = buf.NewMergingWriter(c)
} }
return mb.Len(), c.mergingWriter.Write(mb) return c.mergingWriter.Write(mb)
} }
func (c *connection) Close() error { func (c *connection) Close() error {