blob: 1e783febf92def634704b28e243bb4bf25f9811e [file] [log] [blame]
Abhilash S.L3b494632019-07-16 15:51:09 +05301/*
2 *
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package grpc
20
21import (
22 "google.golang.org/grpc/codes"
Abhay Kumara61c5222025-11-10 07:32:50 +000023 "google.golang.org/grpc/mem"
Abhilash S.L3b494632019-07-16 15:51:09 +053024 "google.golang.org/grpc/status"
25)
26
27// PreparedMsg is responsible for creating a Marshalled and Compressed object.
28//
Abhay Kumara61c5222025-11-10 07:32:50 +000029// # Experimental
30//
31// Notice: This type is EXPERIMENTAL and may be changed or removed in a
32// later release.
Abhilash S.L3b494632019-07-16 15:51:09 +053033type PreparedMsg struct {
34 // Struct for preparing msg before sending them
Abhay Kumara61c5222025-11-10 07:32:50 +000035 encodedData mem.BufferSlice
Abhilash S.L3b494632019-07-16 15:51:09 +053036 hdr []byte
Abhay Kumara61c5222025-11-10 07:32:50 +000037 payload mem.BufferSlice
38 pf payloadFormat
Abhilash S.L3b494632019-07-16 15:51:09 +053039}
40
41// Encode marshalls and compresses the message using the codec and compressor for the stream.
Abhay Kumara61c5222025-11-10 07:32:50 +000042func (p *PreparedMsg) Encode(s Stream, msg any) error {
Abhilash S.L3b494632019-07-16 15:51:09 +053043 ctx := s.Context()
44 rpcInfo, ok := rpcInfoFromContext(ctx)
45 if !ok {
46 return status.Errorf(codes.Internal, "grpc: unable to get rpcInfo")
47 }
48
49 // check if the context has the relevant information to prepareMsg
Abhilash S.L3b494632019-07-16 15:51:09 +053050 if rpcInfo.preloaderInfo.codec == nil {
51 return status.Errorf(codes.Internal, "grpc: rpcInfo.preloaderInfo.codec is nil")
52 }
53
54 // prepare the msg
55 data, err := encode(rpcInfo.preloaderInfo.codec, msg)
56 if err != nil {
57 return err
58 }
Abhay Kumara61c5222025-11-10 07:32:50 +000059
60 materializedData := data.Materialize()
61 data.Free()
62 p.encodedData = mem.BufferSlice{mem.SliceBuffer(materializedData)}
63
64 // TODO: it should be possible to grab the bufferPool from the underlying
65 // stream implementation with a type cast to its actual type (such as
66 // addrConnStream) and accessing the buffer pool directly.
67 var compData mem.BufferSlice
68 compData, p.pf, err = compress(p.encodedData, rpcInfo.preloaderInfo.cp, rpcInfo.preloaderInfo.comp, mem.DefaultBufferPool())
Abhilash S.L3b494632019-07-16 15:51:09 +053069 if err != nil {
70 return err
71 }
Abhay Kumara61c5222025-11-10 07:32:50 +000072
73 if p.pf.isCompressed() {
74 materializedCompData := compData.Materialize()
75 compData.Free()
76 compData = mem.BufferSlice{mem.SliceBuffer(materializedCompData)}
77 }
78
79 p.hdr, p.payload = msgHeader(p.encodedData, compData, p.pf)
80
Abhilash S.L3b494632019-07-16 15:51:09 +053081 return nil
82}