mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
P2p first version
This commit is contained in:
@@ -69,6 +69,15 @@ func (s *Conn) GetHost() (method, address string, rb []byte, err error, r *http.
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Conn) GetLenContent() (b []byte, err error) {
|
||||
var l int
|
||||
if l, err = s.GetLen(); err != nil {
|
||||
return
|
||||
}
|
||||
b, err = s.ReadLen(l)
|
||||
return
|
||||
}
|
||||
|
||||
//读取指定长度内容
|
||||
func (s *Conn) ReadLen(cLen int) ([]byte, error) {
|
||||
if cLen > pool.PoolSize {
|
||||
@@ -77,10 +86,11 @@ func (s *Conn) ReadLen(cLen int) ([]byte, error) {
|
||||
var buf []byte
|
||||
if cLen < pool.PoolSizeSmall {
|
||||
buf = pool.BufPoolSmall.Get().([]byte)[:cLen]
|
||||
defer pool.PutBufPoolSmall(buf)
|
||||
//TODO 回收
|
||||
//defer pool.PutBufPoolSmall(buf)
|
||||
} else {
|
||||
buf = pool.BufPoolMax.Get().([]byte)[:cLen]
|
||||
defer pool.PutBufPoolMax(buf)
|
||||
//defer pool.PutBufPoolMax(buf)
|
||||
}
|
||||
if n, err := io.ReadFull(s, buf); err != nil || n != cLen {
|
||||
return buf, errors.New("Error reading specified length " + err.Error())
|
||||
@@ -95,6 +105,14 @@ func (s *Conn) GetLen() (int, error) {
|
||||
return int(l), err
|
||||
}
|
||||
|
||||
func (s *Conn) WriteLenContent(buf []byte) (err error) {
|
||||
var b []byte
|
||||
if b, err = GetLenBytes(buf); err != nil {
|
||||
return
|
||||
}
|
||||
return binary.Write(s.Conn, binary.LittleEndian, b)
|
||||
}
|
||||
|
||||
//read flag
|
||||
func (s *Conn) ReadFlag() (string, error) {
|
||||
val, err := s.ReadLen(4)
|
||||
@@ -477,7 +495,6 @@ func (s *Conn) WriteChan() (int, error) {
|
||||
defer s.Unlock()
|
||||
return s.Write([]byte(common.WORK_CHAN))
|
||||
}
|
||||
|
||||
//获取长度+内容
|
||||
func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
raw := bytes.NewBuffer([]byte{})
|
||||
@@ -491,6 +508,7 @@ func GetLenBytes(buf []byte) (b []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
//解析出长度
|
||||
func GetLenByBytes(buf []byte) (int, error) {
|
||||
nlen := binary.LittleEndian.Uint32(buf)
|
||||
@@ -508,4 +526,5 @@ func SetUdpSession(sess *kcp.UDPSession) {
|
||||
sess.SetNoDelay(1, 10, 2, 1)
|
||||
sess.SetMtu(1600)
|
||||
sess.SetACKNoDelay(true)
|
||||
sess.SetWriteDelay(false)
|
||||
}
|
||||
|
@@ -56,7 +56,7 @@ func NewLink(id int, connType string, host string, en, de int, crypt bool, c *Co
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Link) Run(flow bool) {
|
||||
func (s *Link) RunWrite() {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
@@ -76,7 +76,7 @@ func (s *Link) Run(flow bool) {
|
||||
} else {
|
||||
s.Conn.Write(content)
|
||||
}
|
||||
if flow {
|
||||
if s.Flow != nil {
|
||||
s.Flow.Add(0, len(content))
|
||||
}
|
||||
if s.ConnType == common.CONN_UDP {
|
||||
@@ -89,3 +89,25 @@ func (s *Link) Run(flow bool) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
func (s *Link) RunRead(msgConn *Conn) {
|
||||
buf := pool.BufPoolCopy.Get().([]byte)
|
||||
for {
|
||||
if n, err := s.Conn.Read(buf); err != nil {
|
||||
msgConn.SendMsg([]byte(common.IO_EOF), s)
|
||||
break
|
||||
} else {
|
||||
if _, err := msgConn.SendMsg(buf[:n], s); err != nil {
|
||||
msgConn.Close()
|
||||
break
|
||||
}
|
||||
if s.ConnType == common.CONN_UDP {
|
||||
break
|
||||
}
|
||||
if s.Flow != nil {
|
||||
s.Flow.Add(n, 0)
|
||||
}
|
||||
}
|
||||
<-s.StatusCh
|
||||
}
|
||||
pool.PutBufPoolCopy(buf)
|
||||
}
|
||||
|
Reference in New Issue
Block a user