This commit is contained in:
黄思敏 2022-08-25 10:12:43 +08:00
parent 068c31ec20
commit 95834d2324
2 changed files with 17 additions and 8 deletions

View File

@ -8,6 +8,5 @@ func init() {
//go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto" //go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto"
func main() { func main() {
NewNetCard().Run() NewNetCard().Run()
} }

24
tap.go
View File

@ -21,9 +21,10 @@ type NetCard struct {
type RPCClient struct { type RPCClient struct {
FrameChan chan [][]byte FrameChan chan [][]byte
conn *grpc.ClientConn
} }
func (cli *RPCClient) run() { func (cli *RPCClient) connect() {
log.Println("rpcclient start") log.Println("rpcclient start")
defer log.Println("rpcclient exit") defer log.Println("rpcclient exit")
@ -31,11 +32,16 @@ func (cli *RPCClient) run() {
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
) )
if err != nil { if err != nil {
log.Fatalf("did not connect: %v", err) log.Printf("did not connect: %v", err)
return
} }
defer conn.Close() cli.conn = conn
c := gen.NewFrameServiceClient(conn) go cli.run()
}
func (cli *RPCClient) run() {
c := gen.NewFrameServiceClient(cli.conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel() defer cancel()
stream, err := c.SendFrames(ctx) stream, err := c.SendFrames(ctx)
@ -55,13 +61,12 @@ func (cli *RPCClient) run() {
panic(err) panic(err)
} }
} }
} }
func (nc *NetCard) Run() { func (nc *NetCard) Run() {
go nc.runRead() go nc.runRead()
go nc.runWrite() go nc.runWrite()
go nc.cli.run() // go nc.cli.run()
nc.server.run() nc.server.run()
} }
@ -105,7 +110,12 @@ func (nc *NetCard) runRead() {
} }
if len(framesBytes) > 0 { if len(framesBytes) > 0 {
nc.cli.FrameChan <- framesBytes // 网卡数据 发到对方 if nc.cli.conn == nil {
nc.cli.connect()
}
if nc.cli.conn != nil {
nc.cli.FrameChan <- framesBytes // 网卡数据 发到对方
}
} }
// 写到grpc服务 // 写到grpc服务