新TAP测试(TODO 解析arp)
This commit is contained in:
parent
1761d52c40
commit
8fa6a1bf96
13
netcard.go
13
netcard.go
|
@ -208,13 +208,14 @@ func (nt *NetTunnel) SendFrames(stream gen.FrameService_SendFramesServer) error
|
|||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
nt.writer <- request.GetFrames()
|
||||
|
||||
frames := Decompress(request.GetFrames())
|
||||
if len(frames) > 0 {
|
||||
for _, frame := range frames {
|
||||
nt.writer <- frame
|
||||
}
|
||||
}
|
||||
// frames := Decompress(request.GetFrames())
|
||||
// if len(frames) > 0 {
|
||||
// for _, frame := range frames {
|
||||
// nt.writer <- frame
|
||||
// }
|
||||
// }
|
||||
})
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log"
|
||||
gen "slimming/proto/gen"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
type RPCClient struct {
|
||||
Frames [][]byte
|
||||
Frames chan []byte
|
||||
|
||||
realAddr string
|
||||
conn *grpc.ClientConn
|
||||
|
@ -26,7 +24,7 @@ func NewRPCClient(realAddr string) *RPCClient {
|
|||
return &RPCClient{
|
||||
realAddr: realAddr,
|
||||
|
||||
Frames: make([][]byte, 0, 1000),
|
||||
Frames: make(chan []byte),
|
||||
conn: nil,
|
||||
lock: sync.Mutex{},
|
||||
trigger: make(chan bool),
|
||||
|
@ -67,13 +65,15 @@ func (cli *RPCClient) CheckConnect() {
|
|||
}
|
||||
|
||||
func (cli *RPCClient) Push(frame []byte) {
|
||||
cli.Lock(func() bool {
|
||||
cli.Frames = append(cli.Frames, frame)
|
||||
if len(cli.Frames) >= 1000 {
|
||||
cli.trigger <- true
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
cli.Frames <- frame
|
||||
// cli.Lock(func() bool {
|
||||
// cli.Frames = append(cli.Frames, frame)
|
||||
// if len(cli.Frames) >= 1000 {
|
||||
// cli.trigger <- true
|
||||
// }
|
||||
// return true
|
||||
// })
|
||||
}
|
||||
|
||||
func (cli *RPCClient) run() {
|
||||
|
@ -99,44 +99,44 @@ func (cli *RPCClient) run() {
|
|||
}
|
||||
defer stream.CloseSend()
|
||||
|
||||
var ticker = time.NewTicker(time.Millisecond * 1)
|
||||
// var ticker = time.NewTicker(time.Millisecond * 1)
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
for {
|
||||
// buf := bytes.NewBuffer(nil)
|
||||
for buf := range cli.Frames {
|
||||
|
||||
select {
|
||||
case <-cli.trigger:
|
||||
case <-ticker.C:
|
||||
}
|
||||
// select {
|
||||
// case <-cli.trigger:
|
||||
// case <-ticker.C:
|
||||
// }
|
||||
|
||||
if !cli.Lock(func() bool {
|
||||
if len(cli.Frames) == 0 {
|
||||
return false
|
||||
}
|
||||
// if !cli.Lock(func() bool {
|
||||
// if len(cli.Frames) == 0 {
|
||||
// return false
|
||||
// }
|
||||
|
||||
Compress(buf, cli.Frames)
|
||||
// Compress(buf, cli.Frames)
|
||||
|
||||
if len(cli.Frames) >= 2 {
|
||||
var countbuf = 0
|
||||
for _, frame := range cli.Frames {
|
||||
countbuf += len(frame)
|
||||
}
|
||||
log.Printf("src size: %d compressed size: %d", countbuf, len(buf.Bytes()))
|
||||
}
|
||||
// if len(cli.Frames) >= 2 {
|
||||
// var countbuf = 0
|
||||
// for _, frame := range cli.Frames {
|
||||
// countbuf += len(frame)
|
||||
// }
|
||||
// log.Printf("src size: %d compressed size: %d", countbuf, len(buf.Bytes()))
|
||||
// }
|
||||
|
||||
cli.Frames = cli.Frames[:0]
|
||||
return true
|
||||
}) {
|
||||
continue
|
||||
}
|
||||
// cli.Frames = cli.Frames[:0]
|
||||
// return true
|
||||
// }) {
|
||||
// continue
|
||||
// }
|
||||
|
||||
// 发到对面的网卡
|
||||
err = stream.Send(&gen.RequestFrames{
|
||||
Frames: buf.Bytes(),
|
||||
Frames: buf,
|
||||
})
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
buf.Reset()
|
||||
// buf.Reset()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user