github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/network/transport/grpc/socket.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/network/transport/grpc/socket.go
    14  
    15  package grpc
    16  
    17  import (
    18  	pb "github.com/tickoalcantara12/micro/v3/proto/transport"
    19  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    20  	"google.golang.org/grpc"
    21  )
    22  
    23  type grpcTransportClient struct {
    24  	conn   *grpc.ClientConn
    25  	stream pb.Transport_StreamClient
    26  
    27  	local  string
    28  	remote string
    29  }
    30  
    31  type grpcTransportSocket struct {
    32  	stream pb.Transport_StreamServer
    33  	local  string
    34  	remote string
    35  }
    36  
    37  func (g *grpcTransportClient) Local() string {
    38  	return g.local
    39  }
    40  
    41  func (g *grpcTransportClient) Remote() string {
    42  	return g.remote
    43  }
    44  
    45  func (g *grpcTransportClient) Recv(m *transport.Message) error {
    46  	if m == nil {
    47  		return nil
    48  	}
    49  
    50  	msg, err := g.stream.Recv()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	m.Header = msg.Header
    56  	m.Body = msg.Body
    57  	return nil
    58  }
    59  
    60  func (g *grpcTransportClient) Send(m *transport.Message) error {
    61  	if m == nil {
    62  		return nil
    63  	}
    64  
    65  	return g.stream.Send(&pb.Message{
    66  		Header: m.Header,
    67  		Body:   m.Body,
    68  	})
    69  }
    70  
    71  func (g *grpcTransportClient) Close() error {
    72  	return g.conn.Close()
    73  }
    74  
    75  func (g *grpcTransportSocket) Local() string {
    76  	return g.local
    77  }
    78  
    79  func (g *grpcTransportSocket) Remote() string {
    80  	return g.remote
    81  }
    82  
    83  func (g *grpcTransportSocket) Recv(m *transport.Message) error {
    84  	if m == nil {
    85  		return nil
    86  	}
    87  
    88  	msg, err := g.stream.Recv()
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	m.Header = msg.Header
    94  	m.Body = msg.Body
    95  	return nil
    96  }
    97  
    98  func (g *grpcTransportSocket) Send(m *transport.Message) error {
    99  	if m == nil {
   100  		return nil
   101  	}
   102  
   103  	return g.stream.Send(&pb.Message{
   104  		Header: m.Header,
   105  		Body:   m.Body,
   106  	})
   107  }
   108  
   109  func (g *grpcTransportSocket) Close() error {
   110  	return nil
   111  }