slimming/rpc.go
2022-08-25 12:17:25 +08:00

62 lines
1.2 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net"
gen "slimming/proto/gen"
"google.golang.org/grpc"
)
type RPCServer struct {
gen.UnimplementedFrameServiceServer
netCard *NetCard
}
var (
serverPort = flag.Int("port", 50051, "The server port")
othersAddr = flag.String("addr", "", "The other server addr")
)
func newRPCServer(netCard *NetCard) *RPCServer {
return &RPCServer{netCard: netCard}
}
func (rpc *RPCServer) run() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *serverPort))
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
}