From 95834d2324b05637a6395d7d3809e7ccc00238f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=80=9D=E6=95=8F?= Date: Thu, 25 Aug 2022 10:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 1 - tap.go | 24 +++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 10e40db..74a18ac 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,5 @@ func init() { //go:generate bash -c "protoc --go_out=plugins=grpc:. proto/*.proto" func main() { - NewNetCard().Run() } diff --git a/tap.go b/tap.go index d8bde49..59d039c 100644 --- a/tap.go +++ b/tap.go @@ -21,9 +21,10 @@ type NetCard struct { type RPCClient struct { FrameChan chan [][]byte + conn *grpc.ClientConn } -func (cli *RPCClient) run() { +func (cli *RPCClient) connect() { log.Println("rpcclient start") defer log.Println("rpcclient exit") @@ -31,11 +32,16 @@ func (cli *RPCClient) run() { grpc.WithTransportCredentials(insecure.NewCredentials()), ) if err != nil { - log.Fatalf("did not connect: %v", err) + log.Printf("did not connect: %v", err) + return } - defer conn.Close() - c := gen.NewFrameServiceClient(conn) + cli.conn = conn + go cli.run() +} +func (cli *RPCClient) run() { + + c := gen.NewFrameServiceClient(cli.conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() stream, err := c.SendFrames(ctx) @@ -55,13 +61,12 @@ func (cli *RPCClient) run() { panic(err) } } - } func (nc *NetCard) Run() { go nc.runRead() go nc.runWrite() - go nc.cli.run() + // go nc.cli.run() nc.server.run() } @@ -105,7 +110,12 @@ func (nc *NetCard) runRead() { } 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服务