slimming/rpc.go
2022-08-26 11:00:18 +08:00

57 lines
1.1 KiB
Go

package main
import (
"log"
"net"
gen "slimming/proto/gen"
"google.golang.org/grpc"
)
type RPCServer struct {
gen.UnimplementedFrameServiceServer
netCard *NetCard
}
var ()
func newRPCServer(netCard *NetCard) *RPCServer {
return &RPCServer{netCard: netCard}
}
func (rpc *RPCServer) run() {
lis, err := net.Listen("tcp", config.Network.Self.Real)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
gen.RegisterFrameServiceServer(s, rpc)
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
// SayHello implements helloworld.GreeterServer
func (s *RPCServer) SendFrames(stream gen.FrameService_SendFramesServer) error {
log.Printf("Start: %v", stream)
for {
request, err := stream.Recv()
if err != nil {
log.Panic(err)
}
log.Printf("request: %v", len(request.Frames))
s.netCard.FrameChan <- request.GetFrames() // 接受数据 广播到网卡上
}
// err := stream.SendAndClose(&gen.Response{Code: 0})
// if err != nil {
// log.Panic(err)
// }
return nil
}