1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00
go-micro/server/rpc_request.go
2023-04-26 02:16:34 +02:00

108 lines
1.7 KiB
Go

package server
import (
"bytes"
"go-micro.dev/v4/codec"
"go-micro.dev/v4/transport"
"go-micro.dev/v4/util/buf"
)
type rpcRequest struct {
socket transport.Socket
codec codec.Codec
rawBody interface{}
header map[string]string
service string
method string
endpoint string
contentType string
body []byte
stream bool
first bool
}
type rpcMessage struct {
payload interface{}
header map[string]string
codec codec.NewCodec
topic string
contentType string
body []byte
}
func (r *rpcRequest) Codec() codec.Reader {
return r.codec
}
func (r *rpcRequest) ContentType() string {
return r.contentType
}
func (r *rpcRequest) Service() string {
return r.service
}
func (r *rpcRequest) Method() string {
return r.method
}
func (r *rpcRequest) Endpoint() string {
return r.endpoint
}
func (r *rpcRequest) Header() map[string]string {
return r.header
}
func (r *rpcRequest) Body() interface{} {
return r.rawBody
}
func (r *rpcRequest) Read() ([]byte, error) {
// got a body
if r.first {
b := r.body
r.first = false
return b, nil
}
var msg transport.Message
err := r.socket.Recv(&msg)
if err != nil {
return nil, err
}
r.header = msg.Header
return msg.Body, nil
}
func (r *rpcRequest) Stream() bool {
return r.stream
}
func (r *rpcMessage) ContentType() string {
return r.contentType
}
func (r *rpcMessage) Topic() string {
return r.topic
}
func (r *rpcMessage) Payload() interface{} {
return r.payload
}
func (r *rpcMessage) Header() map[string]string {
return r.header
}
func (r *rpcMessage) Body() []byte {
return r.body
}
func (r *rpcMessage) Codec() codec.Reader {
b := buf.New(bytes.NewBuffer(r.body))
return r.codec(b)
}