From 216c23f5f7789a150b0d049e9f8f0950a6f28a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=80=9D=E6=95=8F?= Date: Fri, 26 Aug 2022 10:13:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tap.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tap.go b/tap.go index 4035019..0ebf6d8 100644 --- a/tap.go +++ b/tap.go @@ -21,6 +21,10 @@ import ( "google.golang.org/grpc/credentials/insecure" ) +type ExchangeBuffer struct { + BytesArray [][]byte +} + type NetCard struct { FrameChan chan []byte ifce *water.Interface @@ -30,7 +34,7 @@ type NetCard struct { } type RPCClient struct { - FrameChan chan [][]byte + FrameChan chan *ExchangeBuffer conn *grpc.ClientConn } @@ -142,7 +146,7 @@ func (nc *NetCard) runRead() { for { - var bytesMap map[string]*[][]byte = make(map[string]*[][]byte) + var bytesMap map[string]*ExchangeBuffer = make(map[string]*ExchangeBuffer) var ok bool var isLoop int32 = 1 @@ -183,30 +187,28 @@ func (nc *NetCard) runRead() { log.Printf("Ethertype: % x %v\n", rframe.Ethertype(), waterutil.IsIPv4(rframe)) - var buffer *[][]byte - + var buffer *ExchangeBuffer if buffer, ok = bytesMap[realAddr]; !ok { - mbuffer := make([][]byte, 0, 100) - buffer = &mbuffer + buffer = &ExchangeBuffer{BytesArray: make([][]byte, 0, 1000)} bytesMap[realAddr] = buffer } - *buffer = append(*buffer, []byte(rframe)) + buffer.BytesArray = append(buffer.BytesArray, []byte(rframe)) } for dst, buffer := range bytesMap { var cli *RPCClient if cli, ok = nc.clientMap[dst]; !ok { - cli = &RPCClient{FrameChan: make(chan [][]byte, 1000)} + + cli = &RPCClient{FrameChan: make(chan *ExchangeBuffer)} nc.clientMap[dst] = cli } if cli.conn == nil { cli.connect(dst) } else { - cli.FrameChan <- *buffer // 网卡数据 发到对方 - *buffer = (*buffer)[0:] + cli.FrameChan <- buffer // 网卡数据 发到对方 } } @@ -226,6 +228,7 @@ func (nc *NetCard) runWrite() { var buf = bytes.NewBuffer(nil) for wframe := range nc.FrameChan { + log.Printf("get wframes bytes len: %d", len(wframe)) buf.Reset()