blob: 1e783febf92def634704b28e243bb4bf25f9811e [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 Kumar40252eb2025-10-13 13:25:53 +000023 "google.golang.org/grpc/mem"
Scott Baker2c1c4822019-10-16 11:02:41 -070024 "google.golang.org/grpc/status"
25)
26
27// PreparedMsg is responsible for creating a Marshalled and Compressed object.
28//
Abhay Kumar40252eb2025-10-13 13:25:53 +000029// # Experimental
30//
31// Notice: This type is EXPERIMENTAL and may be changed or removed in a
32// later release.
Scott Baker2c1c4822019-10-16 11:02:41 -070033type PreparedMsg struct {
34 // Struct for preparing msg before sending them
Abhay Kumar40252eb2025-10-13 13:25:53 +000035 encodedData mem.BufferSlice
Scott Baker2c1c4822019-10-16 11:02:41 -070036 hdr []byte
Abhay Kumar40252eb2025-10-13 13:25:53 +000037 payload mem.BufferSlice
38 pf payloadFormat
Scott Baker2c1c4822019-10-16 11:02:41 -070039}
40
41// Encode marshalls and compresses the message using the codec and compressor for the stream.
Abhay Kumar40252eb2025-10-13 13:25:53 +000042func (p *PreparedMsg) Encode(s Stream, msg any) error {
Scott Baker2c1c4822019-10-16 11:02:41 -070043 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
Scott Baker2c1c4822019-10-16 11:02:41 -070050 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 Kumar40252eb2025-10-13 13:25:53 +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())
Scott Baker2c1c4822019-10-16 11:02:41 -070069 if err != nil {
70 return err
71 }
Abhay Kumar40252eb2025-10-13 13:25:53 +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
Scott Baker2c1c4822019-10-16 11:02:41 -070081 return nil
82}