slimming/rpc.go

59 lines
1.1 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 (
"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
}
2022-08-25 07:31:51 +00:00
var ()
2022-08-23 09:58:07 +00:00
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-25 07:39:22 +00:00
lis, err := net.Listen("tcp", fmt.Sprintf("%s", config.Network.Self.Real))
2022-08-23 09:58:07 +00:00
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 07:31:51 +00:00
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
}