2019-02-09 09:07:47 +00:00
|
|
|
package conn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cnlh/nps/lib/pool"
|
2019-08-10 03:10:01 +00:00
|
|
|
"github.com/golang/snappy"
|
2019-03-01 09:23:14 +00:00
|
|
|
"io"
|
2019-02-09 09:07:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SnappyConn struct {
|
2019-03-29 02:41:57 +00:00
|
|
|
w *snappy.Writer
|
|
|
|
r *snappy.Reader
|
2019-02-09 09:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 02:44:12 +00:00
|
|
|
func NewSnappyConn(conn io.ReadWriteCloser) *SnappyConn {
|
2019-02-09 09:07:47 +00:00
|
|
|
c := new(SnappyConn)
|
|
|
|
c.w = snappy.NewBufferedWriter(conn)
|
|
|
|
c.r = snappy.NewReader(conn)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-17 17:05:05 +00:00
|
|
|
//snappy压缩写
|
2019-02-09 09:07:47 +00:00
|
|
|
func (s *SnappyConn) Write(b []byte) (n int, err error) {
|
2019-02-17 17:05:05 +00:00
|
|
|
if n, err = s.w.Write(b); err != nil {
|
2019-02-09 09:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = s.w.Flush(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-17 17:05:05 +00:00
|
|
|
//snappy压缩读
|
2019-02-09 09:07:47 +00:00
|
|
|
func (s *SnappyConn) Read(b []byte) (n int, err error) {
|
|
|
|
buf := pool.BufPool.Get().([]byte)
|
|
|
|
defer pool.BufPool.Put(buf)
|
|
|
|
if n, err = s.r.Read(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-17 17:05:05 +00:00
|
|
|
copy(b, buf[:n])
|
2019-02-09 09:07:47 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-01 09:23:14 +00:00
|
|
|
|
|
|
|
func (s *SnappyConn) Close() error {
|
|
|
|
s.w.Close()
|
|
|
|
return s.w.Close()
|
|
|
|
}
|