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"
func main() {
NewNetCard().Run()
}

24
tap.go
View File

@ -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服务