This commit is contained in:
黄思敏 2022-08-26 10:13:48 +08:00
parent 4d0f25400a
commit 216c23f5f7

23
tap.go
View File

@ -21,6 +21,10 @@ import (
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
) )
type ExchangeBuffer struct {
BytesArray [][]byte
}
type NetCard struct { type NetCard struct {
FrameChan chan []byte FrameChan chan []byte
ifce *water.Interface ifce *water.Interface
@ -30,7 +34,7 @@ type NetCard struct {
} }
type RPCClient struct { type RPCClient struct {
FrameChan chan [][]byte FrameChan chan *ExchangeBuffer
conn *grpc.ClientConn conn *grpc.ClientConn
} }
@ -142,7 +146,7 @@ func (nc *NetCard) runRead() {
for { for {
var bytesMap map[string]*[][]byte = make(map[string]*[][]byte) var bytesMap map[string]*ExchangeBuffer = make(map[string]*ExchangeBuffer)
var ok bool var ok bool
var isLoop int32 = 1 var isLoop int32 = 1
@ -183,30 +187,28 @@ func (nc *NetCard) runRead() {
log.Printf("Ethertype: % x %v\n", rframe.Ethertype(), waterutil.IsIPv4(rframe)) log.Printf("Ethertype: % x %v\n", rframe.Ethertype(), waterutil.IsIPv4(rframe))
var buffer *[][]byte var buffer *ExchangeBuffer
if buffer, ok = bytesMap[realAddr]; !ok { if buffer, ok = bytesMap[realAddr]; !ok {
mbuffer := make([][]byte, 0, 100) buffer = &ExchangeBuffer{BytesArray: make([][]byte, 0, 1000)}
buffer = &mbuffer
bytesMap[realAddr] = buffer bytesMap[realAddr] = buffer
} }
*buffer = append(*buffer, []byte(rframe)) buffer.BytesArray = append(buffer.BytesArray, []byte(rframe))
} }
for dst, buffer := range bytesMap { for dst, buffer := range bytesMap {
var cli *RPCClient var cli *RPCClient
if cli, ok = nc.clientMap[dst]; !ok { if cli, ok = nc.clientMap[dst]; !ok {
cli = &RPCClient{FrameChan: make(chan [][]byte, 1000)}
cli = &RPCClient{FrameChan: make(chan *ExchangeBuffer)}
nc.clientMap[dst] = cli nc.clientMap[dst] = cli
} }
if cli.conn == nil { if cli.conn == nil {
cli.connect(dst) cli.connect(dst)
} else { } else {
cli.FrameChan <- *buffer // 网卡数据 发到对方 cli.FrameChan <- buffer // 网卡数据 发到对方
*buffer = (*buffer)[0:]
} }
} }
@ -226,6 +228,7 @@ func (nc *NetCard) runWrite() {
var buf = bytes.NewBuffer(nil) var buf = bytes.NewBuffer(nil)
for wframe := range nc.FrameChan { for wframe := range nc.FrameChan {
log.Printf("get wframes bytes len: %d", len(wframe)) log.Printf("get wframes bytes len: %d", len(wframe))
buf.Reset() buf.Reset()