github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/transport/internet/grpc/encoding/customSeviceName.go (about)

     1  package encoding
     2  
     3  import (
     4  	"context"
     5  
     6  	"google.golang.org/grpc"
     7  )
     8  
     9  func ServerDesc(name, tun, tunMulti string) grpc.ServiceDesc {
    10  	return grpc.ServiceDesc{
    11  		ServiceName: name,
    12  		HandlerType: (*GRPCServiceServer)(nil),
    13  		Methods:     []grpc.MethodDesc{},
    14  		Streams: []grpc.StreamDesc{
    15  			{
    16  				StreamName:    tun,
    17  				Handler:       _GRPCService_Tun_Handler,
    18  				ServerStreams: true,
    19  				ClientStreams: true,
    20  			},
    21  			{
    22  				StreamName:    tunMulti,
    23  				Handler:       _GRPCService_TunMulti_Handler,
    24  				ServerStreams: true,
    25  				ClientStreams: true,
    26  			},
    27  		},
    28  		Metadata: "grpc.proto",
    29  	}
    30  }
    31  
    32  func (c *gRPCServiceClient) TunCustomName(ctx context.Context, name, tun string, opts ...grpc.CallOption) (GRPCService_TunClient, error) {
    33  	stream, err := c.cc.NewStream(ctx, &ServerDesc(name, tun, "").Streams[0], "/"+name+"/"+tun, opts...)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	x := &gRPCServiceTunClient{stream}
    38  	return x, nil
    39  }
    40  
    41  func (c *gRPCServiceClient) TunMultiCustomName(ctx context.Context, name, tunMulti string, opts ...grpc.CallOption) (GRPCService_TunMultiClient, error) {
    42  	stream, err := c.cc.NewStream(ctx, &ServerDesc(name, "", tunMulti).Streams[1], "/"+name+"/"+tunMulti, opts...)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	x := &gRPCServiceTunMultiClient{stream}
    47  	return x, nil
    48  }
    49  
    50  type GRPCServiceClientX interface {
    51  	TunCustomName(ctx context.Context, name, tun string, opts ...grpc.CallOption) (GRPCService_TunClient, error)
    52  	TunMultiCustomName(ctx context.Context, name, tunMulti string, opts ...grpc.CallOption) (GRPCService_TunMultiClient, error)
    53  	Tun(ctx context.Context, opts ...grpc.CallOption) (GRPCService_TunClient, error)
    54  	TunMulti(ctx context.Context, opts ...grpc.CallOption) (GRPCService_TunMultiClient, error)
    55  }
    56  
    57  func RegisterGRPCServiceServerX(s *grpc.Server, srv GRPCServiceServer, name, tun, tunMulti string) {
    58  	desc := ServerDesc(name, tun, tunMulti)
    59  	s.RegisterService(&desc, srv)
    60  }