github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/transport/tcp_request.go (about)

     1  package transport
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/volts-dev/volts/codec"
     7  	"github.com/volts-dev/volts/internal/body"
     8  	"github.com/volts-dev/volts/internal/header"
     9  )
    10  
    11  type RpcRequest struct {
    12  	Message    *Message
    13  	RemoteAddr string
    14  
    15  	// Context is either the client or server context. It should only
    16  	// be modified via copying the whole Request using WithContext.
    17  	// It is unexported to prevent people from using Context wrong
    18  	// and mutating the contexts held by callers of the same request.
    19  	Context context.Context
    20  
    21  	service     string
    22  	method      string
    23  	endpoint    string
    24  	contentType string
    25  	socket      ISocket //
    26  	codec       codec.ICodec
    27  	header      header.Header
    28  	body        *body.TBody //
    29  	rawBody     interface{}
    30  	stream      bool
    31  	first       bool
    32  }
    33  
    34  // 提供给Router的context使用
    35  func NewRpcRequest(ctx context.Context, message *Message, socket ISocket) *RpcRequest {
    36  	r := &RpcRequest{
    37  		Message: message,
    38  		Context: ctx,
    39  		socket:  socket,
    40  	}
    41  
    42  	// new a body
    43  	body := body.New(r.Codec())
    44  	body.Data.Write(message.Payload)
    45  	r.body = body
    46  
    47  	return r
    48  }
    49  
    50  func (self *RpcRequest) Codec() codec.ICodec {
    51  	if self.codec == nil {
    52  		self.codec = codec.IdentifyCodec(self.Message.SerializeType())
    53  	}
    54  
    55  	return self.codec
    56  }
    57  
    58  func (self *RpcRequest) Body() *body.TBody {
    59  	return self.body
    60  }
    61  
    62  func (self *RpcRequest) ContentType() string {
    63  	return self.contentType
    64  }
    65  
    66  func (self *RpcRequest) Service() string {
    67  	return self.service
    68  }
    69  
    70  func (self *RpcRequest) Method() string {
    71  	return self.method
    72  }
    73  
    74  func (self *RpcRequest) Endpoint() string {
    75  	return self.endpoint
    76  }
    77  
    78  // header 这是通讯协议包中附带的数据,有区别于body内
    79  func (self *RpcRequest) Header() header.Header {
    80  	if self.header == nil {
    81  		self.header = make(header.Header)
    82  		for k, v := range self.Message.Header {
    83  			self.header.Add(k, v)
    84  		}
    85  	}
    86  
    87  	return self.header
    88  }
    89  
    90  func (self *RpcRequest) ___Read() ([]byte, error) {
    91  	// got a body
    92  	if self.first {
    93  		b := self.Body()
    94  		self.first = false
    95  		return b.AsBytes(), nil
    96  	}
    97  
    98  	var msg Message
    99  	err := self.socket.Recv(&msg)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	//self.header = msg.Header
   104  
   105  	return msg.Body, nil
   106  }
   107  
   108  func (self *RpcRequest) Stream() bool {
   109  	return self.stream
   110  }