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"
)
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()