mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
refactor
This commit is contained in:
parent
e87b0ca848
commit
cea6e28634
@ -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().
|
||||
|
@ -3,7 +3,7 @@ package buf
|
||||
import "net"
|
||||
|
||||
type MultiBufferWriter interface {
|
||||
WriteMultiBuffer(MultiBuffer) (int, error)
|
||||
WriteMultiBuffer(MultiBuffer) error
|
||||
}
|
||||
|
||||
type MultiBufferReader interface {
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user