|
| 1 | +/* |
| 2 | + * Copyright 2021 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package unknownservice |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/binary" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "github.com/cloudwego/kitex/pkg/protocol/bthrift" |
| 26 | + thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" |
| 27 | + |
| 28 | + "github.com/cloudwego/kitex/pkg/remote" |
| 29 | + "github.com/cloudwego/kitex/pkg/remote/codec" |
| 30 | + "github.com/cloudwego/kitex/pkg/remote/codec/perrors" |
| 31 | + "github.com/cloudwego/kitex/pkg/rpcinfo" |
| 32 | + "github.com/cloudwego/kitex/pkg/serviceinfo" |
| 33 | + unknownservice "github.com/cloudwego/kitex/pkg/unknownservice/service" |
| 34 | +) |
| 35 | + |
| 36 | +// UnknownCodec implements PayloadCodec |
| 37 | +type unknownCodec struct { |
| 38 | + Codec remote.PayloadCodec |
| 39 | +} |
| 40 | + |
| 41 | +// NewUnknownServiceCodec creates the unknown binary codec. |
| 42 | +func NewUnknownServiceCodec(code remote.PayloadCodec) remote.PayloadCodec { |
| 43 | + return &unknownCodec{code} |
| 44 | +} |
| 45 | + |
| 46 | +// Marshal implements the remote.PayloadCodec interface. |
| 47 | +func (c unknownCodec) Marshal(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error { |
| 48 | + ink := msg.RPCInfo().Invocation() |
| 49 | + data := msg.Data() |
| 50 | + |
| 51 | + res, ok := data.(*unknownservice.Result) |
| 52 | + if !ok { |
| 53 | + return c.Codec.Marshal(ctx, msg, out) |
| 54 | + } |
| 55 | + if len(res.Success) == 0 { |
| 56 | + return errors.New("unknown messages cannot be empty") |
| 57 | + } |
| 58 | + if msg.MessageType() == remote.Exception { |
| 59 | + return c.Codec.Marshal(ctx, msg, out) |
| 60 | + } |
| 61 | + if ink, ok := ink.(rpcinfo.InvocationSetter); ok { |
| 62 | + ink.SetMethodName(res.Method) |
| 63 | + ink.SetServiceName(res.ServiceName) |
| 64 | + } else { |
| 65 | + return errors.New("the interface Invocation doesn't implement InvocationSetter") |
| 66 | + } |
| 67 | + if err := encode(res, msg, out); err != nil { |
| 68 | + return c.Codec.Marshal(ctx, msg, out) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// Unmarshal implements the remote.PayloadCodec interface. |
| 74 | +func (c unknownCodec) Unmarshal(ctx context.Context, message remote.Message, in remote.ByteBuffer) error { |
| 75 | + ink := message.RPCInfo().Invocation() |
| 76 | + magicAndMsgType, err := codec.PeekUint32(in) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + msgType := magicAndMsgType & codec.FrontMask |
| 81 | + if msgType == uint32(remote.Exception) { |
| 82 | + return c.Codec.Unmarshal(ctx, message, in) |
| 83 | + } |
| 84 | + if err = codec.UpdateMsgType(msgType, message); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + service, method, err := readDecode(message, in) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + err = codec.SetOrCheckMethodName(method, message) |
| 92 | + var te *remote.TransError |
| 93 | + if errors.As(err, &te) && (te.TypeID() == remote.UnknownMethod || te.TypeID() == remote.UnknownService) { |
| 94 | + svcInfo, err := message.SpecifyServiceInfo(unknownservice.UnknownService, unknownservice.UnknownMethod) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + if ink, ok := ink.(rpcinfo.InvocationSetter); ok { |
| 100 | + ink.SetMethodName(unknownservice.UnknownMethod) |
| 101 | + ink.SetPackageName(svcInfo.GetPackageName()) |
| 102 | + ink.SetServiceName(unknownservice.UnknownService) |
| 103 | + } else { |
| 104 | + return errors.New("the interface Invocation doesn't implement InvocationSetter") |
| 105 | + } |
| 106 | + if err = codec.NewDataIfNeeded(unknownservice.UnknownMethod, message); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + data := message.Data() |
| 111 | + |
| 112 | + if data, ok := data.(*unknownservice.Args); ok { |
| 113 | + data.Method = method |
| 114 | + data.ServiceName = service |
| 115 | + buf, err := in.Next(in.ReadableLen()) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + data.Request = buf |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + return c.Codec.Unmarshal(ctx, message, in) |
| 125 | +} |
| 126 | + |
| 127 | +// Name implements the remote.PayloadCodec interface. |
| 128 | +func (c unknownCodec) Name() string { |
| 129 | + return "unknownMethodCodec" |
| 130 | +} |
| 131 | + |
| 132 | +func write(dst, src []byte) { |
| 133 | + copy(dst, src) |
| 134 | +} |
| 135 | + |
| 136 | +func readDecode(message remote.Message, in remote.ByteBuffer) (string, string, error) { |
| 137 | + code := message.ProtocolInfo().CodecType |
| 138 | + if code == serviceinfo.Thrift || code == serviceinfo.Protobuf { |
| 139 | + method, size, err := peekMethod(in) |
| 140 | + if err != nil { |
| 141 | + return "", "", err |
| 142 | + } |
| 143 | + |
| 144 | + seqID, err := peekSeqID(in, size) |
| 145 | + if err != nil { |
| 146 | + return "", "", err |
| 147 | + } |
| 148 | + if err = codec.SetOrCheckSeqID(seqID, message); err != nil { |
| 149 | + return "", "", err |
| 150 | + } |
| 151 | + return message.RPCInfo().Invocation().ServiceName(), method, nil |
| 152 | + } |
| 153 | + return "", "", nil |
| 154 | +} |
| 155 | + |
| 156 | +func peekMethod(in remote.ByteBuffer) (string, int32, error) { |
| 157 | + buf, err := in.Peek(8) |
| 158 | + if err != nil { |
| 159 | + return "", 0, err |
| 160 | + } |
| 161 | + buf = buf[4:] |
| 162 | + size := int32(binary.BigEndian.Uint32(buf)) |
| 163 | + buf, err = in.Peek(int(size + 8)) |
| 164 | + if err != nil { |
| 165 | + return "", 0, perrors.NewProtocolError(err) |
| 166 | + } |
| 167 | + buf = buf[8:] |
| 168 | + method := string(buf) |
| 169 | + return method, size + 8, nil |
| 170 | +} |
| 171 | + |
| 172 | +func peekSeqID(in remote.ByteBuffer, size int32) (int32, error) { |
| 173 | + buf, err := in.Peek(int(size + 4)) |
| 174 | + if err != nil { |
| 175 | + return 0, perrors.NewProtocolError(err) |
| 176 | + } |
| 177 | + buf = buf[size:] |
| 178 | + seqID := int32(binary.BigEndian.Uint32(buf)) |
| 179 | + return seqID, nil |
| 180 | +} |
| 181 | + |
| 182 | +func encode(res *unknownservice.Result, msg remote.Message, out remote.ByteBuffer) error { |
| 183 | + if msg.ProtocolInfo().CodecType == serviceinfo.Thrift { |
| 184 | + return encodeThrift(res, msg, out) |
| 185 | + } |
| 186 | + if msg.ProtocolInfo().CodecType == serviceinfo.Protobuf { |
| 187 | + return encodeKitexProtobuf(res, msg, out) |
| 188 | + } |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +// encodeThrift Thrift encoder |
| 193 | +func encodeThrift(res *unknownservice.Result, msg remote.Message, out remote.ByteBuffer) error { |
| 194 | + nw, _ := out.(remote.NocopyWrite) |
| 195 | + msgType := msg.MessageType() |
| 196 | + ink := msg.RPCInfo().Invocation() |
| 197 | + msgBeginLen := bthrift.Binary.MessageBeginLength(res.Method, thrift.TMessageType(msgType), ink.SeqID()) |
| 198 | + msgEndLen := bthrift.Binary.MessageEndLength() |
| 199 | + |
| 200 | + buf, err := out.Malloc(msgBeginLen + len(res.Success) + msgEndLen) |
| 201 | + if err != nil { |
| 202 | + return perrors.NewProtocolErrorWithMsg(fmt.Sprintf("thrift marshal, Malloc failed: %s", err.Error())) |
| 203 | + } |
| 204 | + offset := bthrift.Binary.WriteMessageBegin(buf, res.Method, thrift.TMessageType(msgType), ink.SeqID()) |
| 205 | + write(buf[offset:], res.Success) |
| 206 | + bthrift.Binary.WriteMessageEnd(buf[offset:]) |
| 207 | + if nw == nil { |
| 208 | + // if nw is nil, FastWrite will act in Copy mode. |
| 209 | + return nil |
| 210 | + } |
| 211 | + return nw.MallocAck(out.MallocLen()) |
| 212 | +} |
| 213 | + |
| 214 | +// encodeKitexProtobuf Kitex Protobuf encoder |
| 215 | +func encodeKitexProtobuf(res *unknownservice.Result, msg remote.Message, out remote.ByteBuffer) error { |
| 216 | + ink := msg.RPCInfo().Invocation() |
| 217 | + // 3.1 magic && msgType |
| 218 | + if err := codec.WriteUint32(codec.ProtobufV1Magic+uint32(msg.MessageType()), out); err != nil { |
| 219 | + return perrors.NewProtocolErrorWithMsg(fmt.Sprintf("protobuf marshal, write meta info failed: %s", err.Error())) |
| 220 | + } |
| 221 | + // 3.2 methodName |
| 222 | + if _, err := codec.WriteString(res.Method, out); err != nil { |
| 223 | + return perrors.NewProtocolErrorWithMsg(fmt.Sprintf("protobuf marshal, write method name failed: %s", err.Error())) |
| 224 | + } |
| 225 | + // 3.3 seqID |
| 226 | + if err := codec.WriteUint32(uint32(ink.SeqID()), out); err != nil { |
| 227 | + return perrors.NewProtocolErrorWithMsg(fmt.Sprintf("protobuf marshal, write seqID failed: %s", err.Error())) |
| 228 | + } |
| 229 | + dataLen := len(res.Success) |
| 230 | + buf, err := out.Malloc(dataLen) |
| 231 | + if err != nil { |
| 232 | + return perrors.NewProtocolErrorWithErrMsg(err, fmt.Sprintf("protobuf malloc size %d failed: %s", dataLen, err.Error())) |
| 233 | + } |
| 234 | + write(buf, res.Success) |
| 235 | + return nil |
| 236 | +} |
0 commit comments