slimming/rpc.go

63 lines
1.3 KiB
Go
Raw Normal View History

2022-08-25 01:49:42 +00:00
package main
2022-08-23 09:58:07 +00:00
import (
"flag"
"fmt"
"log"
"net"
gen "slimming/proto/gen"
"google.golang.org/grpc"
)
type RPCServer struct {
gen.UnimplementedFrameServiceServer
2022-08-25 01:49:42 +00:00
netCard *NetCard
2022-08-23 09:58:07 +00:00
}
var (
serverPort = flag.Int("port", 50051, "The server port")
2022-08-25 04:21:50 +00:00
cardIP = flag.String("card", "", "The card ip")
2022-08-23 09:58:07 +00:00
othersAddr = flag.String("addr", "", "The other server addr")
)
2022-08-25 01:49:42 +00:00
func newRPCServer(netCard *NetCard) *RPCServer {
return &RPCServer{netCard: netCard}
}
2022-08-23 09:58:07 +00:00
2022-08-25 01:49:42 +00:00
func (rpc *RPCServer) run() {
2022-08-25 04:24:57 +00:00
2022-08-23 09:58:07 +00:00
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())
2022-08-25 01:49:42 +00:00
2022-08-23 09:58:07 +00:00
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 {
2022-08-25 04:12:44 +00:00
log.Printf("Start: %v", stream)
2022-08-23 09:58:07 +00:00
for {
request, err := stream.Recv()
if err != nil {
log.Panic(err)
}
2022-08-25 04:17:25 +00:00
log.Printf("request: %v", len(request.Frames))
2022-08-25 01:49:42 +00:00
s.netCard.FrameChan <- request.GetFrames() // 接受数据 广播到网卡上
2022-08-23 09:58:07 +00:00
}
// err := stream.SendAndClose(&gen.Response{Code: 0})
// if err != nil {
// log.Panic(err)
// }
return nil
}