From 8ff77626d0d4d7718378467c6e0a4f07f7deb990 Mon Sep 17 00:00:00 2001 From: bjdgyc Date: Mon, 19 Jul 2021 12:30:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96byte=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/handler/link_cstp.go | 10 ++++++---- server/handler/link_dtls.go | 11 ++++++----- server/handler/link_tap.go | 10 ++++++---- server/handler/link_tun.go | 5 +++-- server/handler/pool.go | 16 ++++++++-------- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/server/handler/link_cstp.go b/server/handler/link_cstp.go index 431cefb..7df3b29 100644 --- a/server/handler/link_cstp.go +++ b/server/handler/link_cstp.go @@ -34,7 +34,8 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) { return } // hdata := make([]byte, BufferSize) - hdata := getByteFull() + hb := getByteFull() + hdata := *hb n, err = conn.Read(hdata) if err != nil { base.Error("read hdata: ", err) @@ -68,7 +69,7 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) { } } - putByte(hdata) + putByte(hb) } } @@ -98,7 +99,8 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) { } h := []byte{'S', 'T', 'F', 0x01, 0x00, 0x00, payload.PType, 0x00} - header := getByteZero() + hb := getByteZero() + header := *hb header = append(header, h...) if payload.PType == 0x00 { // data binary.BigEndian.PutUint16(header[4:6], uint16(len(payload.Data))) @@ -110,7 +112,7 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) { return } - putByte(header) + putByte(hb) putPayload(payload) // 限流设置 diff --git a/server/handler/link_dtls.go b/server/handler/link_dtls.go index d94daaf..318ba8d 100644 --- a/server/handler/link_dtls.go +++ b/server/handler/link_dtls.go @@ -36,8 +36,8 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) { return } - // hdata := make([]byte, BufferSize) - hdata := getByteFull() + hb := getByteFull() + hdata := *hb n, err := conn.Read(hdata) if err != nil { base.Error("read hdata: ", err) @@ -70,7 +70,7 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) { } } - putByte(hdata) + putByte(hb) } } @@ -99,7 +99,8 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS } // header = []byte{payload.PType} - header := getByteZero() + hb := getByteZero() + header := *hb header = append(header, payload.PType) if payload.PType == 0x00 { // data header = append(header, payload.Data...) @@ -110,7 +111,7 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS return } - putByte(header) + putByte(hb) putPayload(payload) // 限流设置 diff --git a/server/handler/link_tap.go b/server/handler/link_tap.go index a9ff4b0..122ef4c 100644 --- a/server/handler/link_tap.go +++ b/server/handler/link_tap.go @@ -101,7 +101,8 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) { } // var frame ethernet.Frame - frame = getByteFull() + fb := getByteFull() + frame = *fb switch payload.LType { default: // log.Println(payload) @@ -153,7 +154,7 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) { return } - putByte(frame) + putByte(fb) putPayload(payload) } } @@ -174,7 +175,8 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) { for { // var frame ethernet.Frame // frame.Resize(BufferSize) - frame = getByteFull() + fb := getByteFull() + frame = *fb n, err = ifce.Read(frame) if err != nil { base.Error("tap Read err", n, err) @@ -247,6 +249,6 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) { } - putByte(frame) + putByte(fb) } } diff --git a/server/handler/link_tun.go b/server/handler/link_tun.go index 3d0e456..d18c212 100644 --- a/server/handler/link_tun.go +++ b/server/handler/link_tun.go @@ -102,7 +102,8 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) { for { // data := make([]byte, BufferSize) - data := getByteFull() + hb := getByteFull() + data := *hb n, err = ifce.Read(data) if err != nil { base.Error("tun Read err", n, err) @@ -121,6 +122,6 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) { return } - putByte(data) + putByte(hb) } } diff --git a/server/handler/pool.go b/server/handler/pool.go index 27ddffb..86f2102 100644 --- a/server/handler/pool.go +++ b/server/handler/pool.go @@ -32,21 +32,21 @@ var bytePool = sync.Pool{ New: func() interface{} { b := make([]byte, 0, BufferSize) // fmt.Println("bytePool-init") - return b + return &b }, } -func getByteZero() []byte { - b := bytePool.Get().([]byte) +func getByteZero() *[]byte { + b := bytePool.Get().(*[]byte) return b } -func getByteFull() []byte { - b := bytePool.Get().([]byte) - b = b[:BufferSize] +func getByteFull() *[]byte { + b := bytePool.Get().(*[]byte) + *b = (*b)[:BufferSize] return b } -func putByte(b []byte) { - b = b[:0] +func putByte(b *[]byte) { + *b = (*b)[:0] bytePool.Put(b) }