vendor: add package golib/net

This commit is contained in:
fatedier
2018-05-09 00:23:42 +08:00
parent a27e3dda88
commit 20fcb58437
10 changed files with 103 additions and 159 deletions

View File

@@ -15,7 +15,6 @@
package net
import (
"bytes"
"errors"
"fmt"
"io"
@@ -133,49 +132,6 @@ func ConnectServerByProxy(proxyUrl string, protocol string, addr string) (c Conn
}
}
type SharedConn struct {
Conn
buf *bytes.Buffer
}
// the bytes you read in io.Reader, will be reserved in SharedConn
func NewShareConn(conn Conn) (*SharedConn, io.Reader) {
sc := &SharedConn{
Conn: conn,
buf: bytes.NewBuffer(make([]byte, 0, 1024)),
}
return sc, io.TeeReader(conn, sc.buf)
}
func NewShareConnSize(conn Conn, bufSize int) (*SharedConn, io.Reader) {
sc := &SharedConn{
Conn: conn,
buf: bytes.NewBuffer(make([]byte, 0, bufSize)),
}
return sc, io.TeeReader(conn, sc.buf)
}
// Not thread safety.
func (sc *SharedConn) Read(p []byte) (n int, err error) {
if sc.buf == nil {
return sc.Conn.Read(p)
}
n, err = sc.buf.Read(p)
if err == io.EOF {
sc.buf = nil
var n2 int
n2, err = sc.Conn.Read(p[n:])
n += n2
}
return
}
func (sc *SharedConn) WriteBuff(buffer []byte) (err error) {
sc.buf.Reset()
_, err = sc.buf.Write(buffer)
return err
}
type StatsConn struct {
Conn