bug repair when high concurrent

This commit is contained in:
刘河
2019-02-18 01:05:05 +08:00
parent 48c7309973
commit dab51c32a2
14 changed files with 229 additions and 242 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/binary"
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/lg"
"io/ioutil"
"net"
"net/http"
@@ -23,7 +22,7 @@ func GetCompressType(compress string) (int, int) {
case "snappy":
return COMPRESS_SNAPY_DECODE, COMPRESS_SNAPY_ENCODE
default:
lg.Fatalln("数据压缩格式错误")
return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
}
return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
}
@@ -184,7 +183,7 @@ func BinaryWrite(raw *bytes.Buffer, v ...string) {
binary.Write(raw, binary.LittleEndian, buffer.Bytes())
}
func InArr(arr []string, val string) bool {
func InStrArr(arr []string, val string) bool {
for _, v := range arr {
if v == val {
return true
@@ -224,6 +223,7 @@ func GetPorts(p string) []int {
}
return ps
}
func IsPort(p string) bool {
pi, err := strconv.Atoi(p)
if err != nil {

View File

@@ -89,11 +89,9 @@ func (s *Conn) ReadLen(cLen int) ([]byte, error) {
//read length or id (content length=4)
func (s *Conn) GetLen() (int, error) {
val, err := s.ReadLen(4)
if err != nil {
return 0, err
}
return GetLenByBytes(val)
var l int32
err := binary.Read(s, binary.LittleEndian, &l)
return int(l), err
}
//read flag

View File

@@ -1,41 +1,29 @@
package conn
import (
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/lib/pool"
"github.com/cnlh/nps/lib/rate"
"github.com/cnlh/nps/vender/github.com/golang/snappy"
"log"
"net"
)
type SnappyConn struct {
w *snappy.Writer
r *snappy.Reader
crypt bool
rate *rate.Rate
w *snappy.Writer
r *snappy.Reader
rate *rate.Rate
}
func NewSnappyConn(conn net.Conn, crypt bool, rate *rate.Rate) *SnappyConn {
c := new(SnappyConn)
c.w = snappy.NewBufferedWriter(conn)
c.r = snappy.NewReader(conn)
c.crypt = crypt
c.rate = rate
return c
}
//snappy压缩写 包含加密
//snappy压缩写
func (s *SnappyConn) Write(b []byte) (n int, err error) {
n = len(b)
if s.crypt {
if b, err = crypt.AesEncrypt(b, []byte(cryptKey)); err != nil {
lg.Println("encode crypt error:", err)
return
}
}
if _, err = s.w.Write(b); err != nil {
if n, err = s.w.Write(b); err != nil {
return
}
if err = s.w.Flush(); err != nil {
@@ -47,24 +35,14 @@ func (s *SnappyConn) Write(b []byte) (n int, err error) {
return
}
//snappy压缩读 包含解密
//snappy压缩读
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
}
var bs []byte
if s.crypt {
if bs, err = crypt.AesDecrypt(buf[:n], []byte(cryptKey)); err != nil {
log.Println("decode crypt error:", err)
return
}
} else {
bs = buf[:n]
}
n = len(bs)
copy(b, bs)
copy(b, buf[:n])
if s.rate != nil {
s.rate.Get(int64(n))
}

View File

@@ -157,7 +157,7 @@ func (s *Csv) UpdateTask(t *Tunnel) error {
return nil
}
}
return errors.New("不存在")
return errors.New("the task is not exist")
}
func (s *Csv) DelTask(id int) error {