1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -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)
}
var (
_ buf.MultiBufferReader = (*Connection)(nil)
_ buf.MultiBufferWriter = (*Connection)(nil)
)
type Connection struct {
stream ray.Ray
closed bool
@ -170,11 +175,11 @@ func (v *Connection) Write(b []byte) (int, error) {
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 {
return 0, io.ErrClosedPipe
return io.ErrClosedPipe
}
return mb.Len(), v.writer.Write(mb)
return v.writer.Write(mb)
}
// Close implements net.Conn.Close().

View File

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

View File

@ -22,8 +22,7 @@ type writerAdapter struct {
// Write implements buf.MultiBufferWriter.
func (w *writerAdapter) Write(mb MultiBuffer) error {
_, err := w.writer.WriteMultiBuffer(mb)
return err
return w.writer.WriteMultiBuffer(mb)
}
type mergingWriter struct {
@ -62,6 +61,10 @@ func (w *seqWriter) Write(mb MultiBuffer) error {
return nil
}
var (
_ MultiBufferWriter = (*bytesToBufferWriter)(nil)
)
type bytesToBufferWriter struct {
writer Writer
}
@ -81,8 +84,8 @@ func (w *bytesToBufferWriter) Write(payload []byte) (int, error) {
return len(payload), nil
}
func (w *bytesToBufferWriter) WriteMultiBuffer(mb MultiBuffer) (int, error) {
return mb.Len(), w.writer.Write(mb)
func (w *bytesToBufferWriter) WriteMultiBuffer(mb MultiBuffer) error {
return w.writer.Write(mb)
}
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
}
var (
_ buf.MultiBufferWriter = (*CryptionWriter)(nil)
)
type CryptionWriter struct {
stream cipher.Stream
writer io.Writer
@ -46,11 +50,11 @@ func (w *CryptionWriter) Write(data []byte) (int, error) {
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()
for _, b := range bs {
w.stream.XORKeyStream(b, b)
}
nBytes, err := bs.WriteTo(w.writer)
return int(nBytes), err
_, err := bs.WriteTo(w.writer)
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 {
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) {

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
var (
_ buf.MultiBufferReader = (*conn)(nil)
_ buf.MultiBufferWriter = (*conn)(nil)
)
type conn struct {
net.Conn
@ -23,11 +28,11 @@ func (c *conn) ReadMultiBuffer() (buf.MultiBuffer, error) {
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 {
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 {

View File

@ -69,11 +69,11 @@ func (c *connection) Write(b []byte) (int, error) {
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 {
c.mergingWriter = buf.NewMergingWriter(c)
}
return mb.Len(), c.mergingWriter.Write(mb)
return c.mergingWriter.Write(mb)
}
func (c *connection) Close() error {