2020-08-23 20:37:22 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2021-01-29 18:47:28 +03:00
|
|
|
pb "github.com/unistack-org/micro-network-transport-grpc/v3/proto"
|
2020-09-20 17:04:25 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
2020-08-23 20:37:22 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type grpcTransportClient struct {
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
stream pb.Transport_StreamClient
|
|
|
|
|
|
|
|
local string
|
|
|
|
remote string
|
|
|
|
}
|
|
|
|
|
|
|
|
type grpcTransportSocket struct {
|
|
|
|
stream pb.Transport_StreamServer
|
|
|
|
local string
|
|
|
|
remote string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportClient) Local() string {
|
|
|
|
return g.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportClient) Remote() string {
|
|
|
|
return g.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportClient) Recv(m *transport.Message) error {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := g.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Header = msg.Header
|
|
|
|
m.Body = msg.Body
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportClient) Send(m *transport.Message) error {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.stream.Send(&pb.Message{
|
|
|
|
Header: m.Header,
|
|
|
|
Body: m.Body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportClient) Close() error {
|
|
|
|
return g.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportSocket) Local() string {
|
|
|
|
return g.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportSocket) Remote() string {
|
|
|
|
return g.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportSocket) Recv(m *transport.Message) error {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := g.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Header = msg.Header
|
|
|
|
m.Body = msg.Body
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportSocket) Send(m *transport.Message) error {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.stream.Send(&pb.Message{
|
|
|
|
Header: m.Header,
|
|
|
|
Body: m.Body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *grpcTransportSocket) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|