Files
umbrix-libcore/v2/grpc_server.go

60 lines
1.3 KiB
Go
Raw Normal View History

2024-03-10 19:45:03 +01:00
package v2
/*
#include "stdint.h"
*/
import (
"log"
"net"
2024-03-22 16:25:56 +00:00
pb "github.com/hiddify/hiddify-core/hiddifyrpc"
2024-03-10 19:45:03 +01:00
"google.golang.org/grpc"
)
type HelloService struct {
pb.UnimplementedHelloServer
}
type CoreService struct {
pb.UnimplementedCoreServer
2024-03-10 19:45:03 +01:00
}
type TunnelService struct {
pb.UnimplementedTunnelServiceServer
2024-03-10 19:45:03 +01:00
}
func StartGrpcServer(listenAddressG string, service string) error {
2024-03-10 19:45:03 +01:00
lis, err := net.Listen("tcp", listenAddressG)
if err != nil {
log.Printf("failed to listen: %v", err)
return err
}
s := grpc.NewServer()
if service == "core" {
pb.RegisterCoreServer(s, &CoreService{})
} else if service == "hello" {
pb.RegisterHelloServer(s, &HelloService{})
} else if service == "tunnel" {
pb.RegisterTunnelServiceServer(s, &TunnelService{})
}
2024-03-10 19:45:03 +01:00
log.Printf("Server listening on %s", listenAddressG)
go func() {
if err := s.Serve(lis); err != nil {
log.Printf("failed to serve: %v", err)
}
}()
return nil
}
func StartCoreGrpcServer(listenAddressG string) error {
return StartGrpcServer(listenAddressG, "core")
}
func StartHelloGrpcServer(listenAddressG string) error {
return StartGrpcServer(listenAddressG, "hello")
}
func StartTunnelGrpcServer(listenAddressG string) error {
return StartGrpcServer(listenAddressG, "tunnel")
}