2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-11-26 01:18:35 +03:00
|
|
|
"io"
|
|
|
|
|
2020-09-18 18:27:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2019-06-17 20:05:58 +01:00
|
|
|
"google.golang.org/grpc"
|
2019-06-18 18:51:52 +01:00
|
|
|
"google.golang.org/grpc/encoding"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
type wrapStream struct{ grpc.ClientStream }
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (w *wrapStream) Write(d []byte) (int, error) {
|
|
|
|
n := len(d)
|
|
|
|
err := w.ClientStream.SendMsg(&codec.Frame{Data: d})
|
|
|
|
return n, err
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (w *wrapStream) Read(d []byte) (int, error) {
|
|
|
|
m := &codec.Frame{}
|
|
|
|
err := w.ClientStream.RecvMsg(m)
|
|
|
|
d = m.Data
|
|
|
|
return len(d), err
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
type wrapMicroCodec struct{ codec.Codec }
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (w *wrapMicroCodec) Name() string {
|
|
|
|
return w.Codec.String()
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
type wrapGrpcCodec struct{ encoding.Codec }
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (w *wrapGrpcCodec) String() string {
|
|
|
|
return w.Codec.Name()
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (w *wrapGrpcCodec) Marshal(v interface{}) ([]byte, error) {
|
2020-09-18 18:27:29 +03:00
|
|
|
switch m := v.(type) {
|
2020-11-26 01:18:35 +03:00
|
|
|
case *codec.Frame:
|
2020-09-18 18:27:29 +03:00
|
|
|
return m.Data, nil
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2020-11-26 01:18:35 +03:00
|
|
|
return w.Codec.Marshal(v)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
func (w wrapGrpcCodec) Unmarshal(d []byte, v interface{}) error {
|
|
|
|
if d == nil || v == nil {
|
2020-03-10 15:21:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-18 18:27:29 +03:00
|
|
|
switch m := v.(type) {
|
2020-11-26 01:18:35 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = d
|
2020-09-18 18:27:29 +03:00
|
|
|
return nil
|
2019-07-04 16:43:36 +08:00
|
|
|
}
|
2020-11-26 01:18:35 +03:00
|
|
|
return w.Codec.Unmarshal(d, v)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
2019-06-17 20:05:58 +01:00
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
/*
|
2019-06-17 20:05:58 +01:00
|
|
|
type grpcCodec struct {
|
2020-11-26 01:18:35 +03:00
|
|
|
grpc.ServerStream
|
2019-06-17 20:05:58 +01:00
|
|
|
// headers
|
2019-06-18 18:51:52 +01:00
|
|
|
id string
|
|
|
|
target string
|
|
|
|
method string
|
2019-06-17 20:05:58 +01:00
|
|
|
endpoint string
|
|
|
|
|
|
|
|
c encoding.Codec
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:18:35 +03:00
|
|
|
*/
|
|
|
|
|
2020-12-28 15:33:19 +03:00
|
|
|
func (g *wrapGrpcCodec) ReadHeader(conn io.Reader, m *codec.Message, mt codec.MessageType) error {
|
2020-11-26 01:18:35 +03:00
|
|
|
/*
|
|
|
|
if m == nil {
|
|
|
|
m = codec.NewMessage(codec.Request)
|
|
|
|
}
|
|
|
|
|
|
|
|
if md, ok := metadata.FromIncomingContext(g.ServerStream.Context()); ok {
|
|
|
|
if m.Header == nil {
|
|
|
|
m.Header = meta.New(len(md))
|
|
|
|
}
|
|
|
|
for k, v := range md {
|
|
|
|
m.Header[k] = strings.Join(v, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Id = g.id
|
|
|
|
m.Target = g.target
|
|
|
|
m.Method = g.method
|
|
|
|
m.Endpoint = g.endpoint
|
|
|
|
*/
|
2019-06-17 20:05:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:33:19 +03:00
|
|
|
func (g *wrapGrpcCodec) ReadBody(conn io.Reader, v interface{}) error {
|
2020-11-26 01:18:35 +03:00
|
|
|
// caller has requested a frame
|
2020-09-18 18:27:29 +03:00
|
|
|
switch m := v.(type) {
|
2020-11-26 01:18:35 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Read(m.Data)
|
|
|
|
return err
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
2020-11-26 01:18:35 +03:00
|
|
|
return codec.ErrInvalidMessage
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 15:33:19 +03:00
|
|
|
func (g *wrapGrpcCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
2019-06-17 20:05:58 +01:00
|
|
|
// if we don't have a body
|
2019-06-18 18:51:52 +01:00
|
|
|
if v != nil {
|
2020-11-26 01:18:35 +03:00
|
|
|
b, err := g.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Body = b
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|
|
|
|
// write the body using the framing codec
|
2020-11-26 01:18:35 +03:00
|
|
|
_, err := conn.Write(m.Body)
|
|
|
|
return err
|
2019-06-17 20:05:58 +01:00
|
|
|
}
|