mirror of
https://github.com/xtaci/kcptun.git
synced 2024-04-21 12:32:32 +00:00
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
package generic
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/golang/snappy"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type CompStream struct {
|
|
conn net.Conn
|
|
w *snappy.Writer
|
|
r *snappy.Reader
|
|
}
|
|
|
|
func (c *CompStream) Read(p []byte) (n int, err error) {
|
|
return c.r.Read(p)
|
|
}
|
|
|
|
func (c *CompStream) Write(p []byte) (n int, err error) {
|
|
if _, err := c.w.Write(p); err != nil {
|
|
return 0, errors.WithStack(err)
|
|
}
|
|
|
|
if err := c.w.Flush(); err != nil {
|
|
return 0, errors.WithStack(err)
|
|
}
|
|
return len(p), err
|
|
}
|
|
|
|
func (c *CompStream) Close() error {
|
|
return c.conn.Close()
|
|
}
|
|
|
|
func (c *CompStream) LocalAddr() net.Addr {
|
|
if ts, ok := c.conn.(interface {
|
|
LocalAddr() net.Addr
|
|
}); ok {
|
|
return ts.LocalAddr()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *CompStream) RemoteAddr() net.Addr {
|
|
if ts, ok := c.conn.(interface {
|
|
RemoteAddr() net.Addr
|
|
}); ok {
|
|
return ts.RemoteAddr()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewCompStream(conn net.Conn) *CompStream {
|
|
c := new(CompStream)
|
|
c.conn = conn
|
|
c.w = snappy.NewBufferedWriter(conn)
|
|
c.r = snappy.NewReader(conn)
|
|
return c
|
|
}
|