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