/*
 *
 * Copyright 2014 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package grpc

import (
	
	
	
	
	rand 
	
	
	

	
	
	
	
	
	
	
	
	imetadata 
	iresolver 
	
	istatus 
	
	
	
	
	
	
)

var metadataFromOutgoingContextRaw = internal.FromOutgoingContextRaw.(func(context.Context) (metadata.MD, [][]string, bool))

// StreamHandler defines the handler called by gRPC server to complete the
// execution of a streaming RPC.
//
// If a StreamHandler returns an error, it should either be produced by the
// status package, or be one of the context errors. Otherwise, gRPC will use
// codes.Unknown as the status code and err.Error() as the status message of the
// RPC.
type StreamHandler func(srv any, stream ServerStream) error

// StreamDesc represents a streaming RPC service's method specification.  Used
// on the server when registering services and on the client when initiating
// new streams.
type StreamDesc struct {
	// StreamName and Handler are only used when registering handlers on a
	// server.
	StreamName string        // the name of the method excluding the service
	Handler    StreamHandler // the handler called for the method

	// ServerStreams and ClientStreams are used for registering handlers on a
	// server as well as defining RPC behavior when passed to NewClientStream
	// and ClientConn.NewStream.  At least one must be true.
	ServerStreams bool // indicates the server can perform streaming sends
	ClientStreams bool // indicates the client can perform streaming sends
}

// Stream defines the common interface a client or server stream has to satisfy.
//
// Deprecated: See ClientStream and ServerStream documentation instead.
type Stream interface {
	// Deprecated: See ClientStream and ServerStream documentation instead.
	Context() context.Context
	// Deprecated: See ClientStream and ServerStream documentation instead.
	SendMsg(m any) error
	// Deprecated: See ClientStream and ServerStream documentation instead.
	RecvMsg(m any) error
}

// ClientStream defines the client-side behavior of a streaming RPC.
//
// All errors returned from ClientStream methods are compatible with the
// status package.
type ClientStream interface {
	// Header returns the header metadata received from the server if there
	// is any. It blocks if the metadata is not ready to read.  If the metadata
	// is nil and the error is also nil, then the stream was terminated without
	// headers, and the status can be discovered by calling RecvMsg.
	Header() (metadata.MD, error)
	// Trailer returns the trailer metadata from the server, if there is any.
	// It must only be called after stream.CloseAndRecv has returned, or
	// stream.Recv has returned a non-nil error (including io.EOF).
	Trailer() metadata.MD
	// CloseSend closes the send direction of the stream. This method always
	// returns a nil error. The status of the stream may be discovered using
	// RecvMsg. It is also not safe to call CloseSend concurrently with SendMsg.
	CloseSend() error
	// Context returns the context for this stream.
	//
	// It should not be called until after Header or RecvMsg has returned. Once
	// called, subsequent client-side retries are disabled.
	Context() context.Context
	// SendMsg is generally called by generated code. On error, SendMsg aborts
	// the stream. If the error was generated by the client, the status is
	// returned directly; otherwise, io.EOF is returned and the status of
	// the stream may be discovered using RecvMsg. For unary or server-streaming
	// RPCs (StreamDesc.ClientStreams is false), a nil error is returned
	// unconditionally.
	//
	// SendMsg blocks until:
	//   - There is sufficient flow control to schedule m with the transport, or
	//   - The stream is done, or
	//   - The stream breaks.
	//
	// SendMsg does not wait until the message is received by the server. An
	// untimely stream closure may result in lost messages. To ensure delivery,
	// users should ensure the RPC completed successfully using RecvMsg.
	//
	// It is safe to have a goroutine calling SendMsg and another goroutine
	// calling RecvMsg on the same stream at the same time, but it is not safe
	// to call SendMsg on the same stream in different goroutines. It is also
	// not safe to call CloseSend concurrently with SendMsg.
	//
	// It is not safe to modify the message after calling SendMsg. Tracing
	// libraries and stats handlers may use the message lazily.
	SendMsg(m any) error
	// RecvMsg blocks until it receives a message into m or the stream is
	// done. It returns io.EOF when the stream completes successfully. On
	// any other error, the stream is aborted and the error contains the RPC
	// status.
	//
	// It is safe to have a goroutine calling SendMsg and another goroutine
	// calling RecvMsg on the same stream at the same time, but it is not
	// safe to call RecvMsg on the same stream in different goroutines.
	RecvMsg(m any) error
}

// NewStream creates a new Stream for the client side. This is typically
// called by generated code. ctx is used for the lifetime of the stream.
//
// To ensure resources are not leaked due to the stream returned, one of the following
// actions must be performed:
//
//  1. Call Close on the ClientConn.
//  2. Cancel the context provided.
//  3. Call RecvMsg until a non-nil error is returned. A protobuf-generated
//     client-streaming RPC, for instance, might use the helper function
//     CloseAndRecv (note that CloseSend does not Recv, therefore is not
//     guaranteed to release all resources).
//  4. Receive a non-nil, non-io.EOF error from Header or SendMsg.
//
// If none of the above happen, a goroutine and a context will be leaked, and grpc
// will not call the optionally-configured stats handler with a stats.End message.
func ( *ClientConn) ( context.Context,  *StreamDesc,  string,  ...CallOption) (ClientStream, error) {
	// allow interceptor to see all applicable call options, which means those
	// configured as defaults from dial option as well as per-call options
	 = combine(.dopts.callOptions, )

	if .dopts.streamInt != nil {
		return .dopts.streamInt(, , , , newClientStream, ...)
	}
	return newClientStream(, , , , ...)
}

// NewClientStream is a wrapper for ClientConn.NewStream.
func ( context.Context,  *StreamDesc,  *ClientConn,  string,  ...CallOption) (ClientStream, error) {
	return .NewStream(, , , ...)
}

func ( context.Context,  *StreamDesc,  *ClientConn,  string,  ...CallOption) ( ClientStream,  error) {
	// Start tracking the RPC for idleness purposes. This is where a stream is
	// created for both streaming and unary RPCs, and hence is a good place to
	// track active RPC count.
	if  := .idlenessMgr.OnCallBegin();  != nil {
		return nil, 
	}
	// Add a calloption, to decrement the active call count, that gets executed
	// when the RPC completes.
	 = append([]CallOption{OnFinish(func(error) { .idlenessMgr.OnCallEnd() })}, ...)

	if , ,  := metadataFromOutgoingContextRaw();  {
		// validate md
		if  := imetadata.Validate();  != nil {
			return nil, status.Error(codes.Internal, .Error())
		}
		// validate added
		for ,  := range  {
			for  := 0;  < len();  += 2 {
				if  := imetadata.ValidatePair([], [+1]);  != nil {
					return nil, status.Error(codes.Internal, .Error())
				}
			}
		}
	}
	if channelz.IsOn() {
		.incrCallsStarted()
		defer func() {
			if  != nil {
				.incrCallsFailed()
			}
		}()
	}
	// Provide an opportunity for the first RPC to see the first service config
	// provided by the resolver.
	,  := .waitForResolvedAddrs()
	if  != nil {
		return nil, 
	}

	var  serviceconfig.MethodConfig
	var  func()
	 := func( context.Context,  func()) (iresolver.ClientStream, error) {
		return newClientStreamWithParams(, , , , , , , , ...)
	}

	 := iresolver.RPCInfo{Context: , Method: }
	,  := .safeConfigSelector.SelectConfig()
	if  != nil {
		if ,  := status.FromError();  {
			// Restrict the code to the list allowed by gRFC A54.
			if istatus.IsRestrictedControlPlaneCode() {
				 = status.Errorf(codes.Internal, "config selector returned illegal status: %v", )
			}
			return nil, 
		}
		return nil, toRPCErr()
	}

	if  != nil {
		if .Context != nil {
			 = .Context
		}
		 = .MethodConfig
		 = .OnCommitted
		if .Interceptor != nil {
			.Context = nil
			 := 
			 = func( context.Context,  func()) (iresolver.ClientStream, error) {
				,  := .Interceptor.NewStream(, , , )
				if  != nil {
					return nil, toRPCErr()
				}
				return , nil
			}
		}
	}

	return (, func() {})
}

func ( context.Context,  *StreamDesc,  *ClientConn,  string,  serviceconfig.MethodConfig, ,  func(),  bool,  ...CallOption) ( iresolver.ClientStream,  error) {
	 := defaultCallInfo()
	if .WaitForReady != nil {
		.failFast = !*.WaitForReady
	}

	// Possible context leak:
	// The cancel function for the child context we create will only be called
	// when RecvMsg returns a non-nil error, if the ClientConn is closed, or if
	// an error is generated by SendMsg.
	// https://github.com/grpc/grpc-go/issues/1818.
	var  context.CancelFunc
	if .Timeout != nil && *.Timeout >= 0 {
		,  = context.WithTimeout(, *.Timeout)
	} else {
		,  = context.WithCancel()
	}
	defer func() {
		if  != nil {
			()
		}
	}()

	for ,  := range  {
		if  := .before();  != nil {
			return nil, toRPCErr()
		}
	}
	.maxSendMessageSize = getMaxSize(.MaxReqSize, .maxSendMessageSize, defaultClientMaxSendMessageSize)
	.maxReceiveMessageSize = getMaxSize(.MaxRespSize, .maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
	if  := setCallInfoCodec();  != nil {
		return nil, 
	}

	 := &transport.CallHdr{
		Host:           .authority,
		Method:         ,
		ContentSubtype: .contentSubtype,
		DoneFunc:       ,
		Authority:      .authority,
	}

	// Set our outgoing compression according to the UseCompressor CallOption, if
	// set.  In that case, also find the compressor from the encoding package.
	// Otherwise, use the compressor configured by the WithCompressor DialOption,
	// if set.
	var  Compressor
	var  encoding.Compressor
	if  := .compressorName;  != "" {
		.SendCompress = 
		if  != encoding.Identity {
			 = encoding.GetCompressor()
			if  == nil {
				return nil, status.Errorf(codes.Internal, "grpc: Compressor is not installed for requested grpc-encoding %q", )
			}
		}
	} else if .dopts.compressorV0 != nil {
		.SendCompress = .dopts.compressorV0.Type()
		 = .dopts.compressorV0
	}
	if .creds != nil {
		.Creds = .creds
	}

	 := &clientStream{
		callHdr:             ,
		ctx:                 ,
		methodConfig:        &,
		opts:                ,
		callInfo:            ,
		cc:                  ,
		desc:                ,
		codec:               .codec,
		compressorV0:        ,
		compressorV1:        ,
		cancel:              ,
		firstAttempt:        true,
		onCommit:            ,
		nameResolutionDelay: ,
	}
	if !.dopts.disableRetry {
		.retryThrottler = .retryThrottler.Load().(*retryThrottler)
	}
	if  := binarylog.GetMethodLogger();  != nil {
		.binlogs = append(.binlogs, )
	}
	if .dopts.binaryLogger != nil {
		if  := .dopts.binaryLogger.GetMethodLogger();  != nil {
			.binlogs = append(.binlogs, )
		}
	}

	// Pick the transport to use and create a new stream on the transport.
	// Assign cs.attempt upon success.
	 := func( *csAttempt) error {
		if  := .getTransport();  != nil {
			return 
		}
		if  := .newStream();  != nil {
			return 
		}
		// Because this operation is always called either here (while creating
		// the clientStream) or by the retry code while locked when replaying
		// the operation, it is safe to access cs.attempt directly.
		.attempt = 
		return nil
	}
	if  := .withRetry(, func() { .bufferForRetryLocked(0, , nil) });  != nil {
		return nil, 
	}

	if len(.binlogs) != 0 {
		,  := metadata.FromOutgoingContext()
		 := &binarylog.ClientHeader{
			OnClientSide: true,
			Header:       ,
			MethodName:   ,
			Authority:    .cc.authority,
		}
		if ,  := .Deadline();  {
			.Timeout = time.Until()
			if .Timeout < 0 {
				.Timeout = 0
			}
		}
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}

	if  != unaryStreamDesc {
		// Listen on cc and stream contexts to cleanup when the user closes the
		// ClientConn or cancels the stream context.  In all other cases, an error
		// should already be injected into the recv buffer by the transport, which
		// the client will eventually receive, and then we will cancel the stream's
		// context in clientStream.finish.
		go func() {
			select {
			case <-.ctx.Done():
				.finish(ErrClientConnClosing)
			case <-.Done():
				.finish(toRPCErr(.Err()))
			}
		}()
	}
	return , nil
}

// newAttemptLocked creates a new csAttempt without a transport or stream.
func ( *clientStream) ( bool) (*csAttempt, error) {
	if  := .ctx.Err();  != nil {
		return nil, toRPCErr()
	}
	if  := .cc.ctx.Err();  != nil {
		return nil, ErrClientConnClosing
	}

	 := newContextWithRPCInfo(.ctx, .callInfo.failFast, .callInfo.codec, .compressorV0, .compressorV1)
	 := .callHdr.Method
	var  time.Time
	 := .cc.dopts.copts.StatsHandlers
	for ,  := range  {
		 = .TagRPC(, &stats.RPCTagInfo{FullMethodName: , FailFast: .callInfo.failFast, NameResolutionDelay: .nameResolutionDelay})
		 = time.Now()
		 := &stats.Begin{
			Client:                    true,
			BeginTime:                 ,
			FailFast:                  .callInfo.failFast,
			IsClientStream:            .desc.ClientStreams,
			IsServerStream:            .desc.ServerStreams,
			IsTransparentRetryAttempt: ,
		}
		.HandleRPC(, )
	}

	var  *traceInfo
	if EnableTracing {
		 = &traceInfo{
			tr: newTrace("grpc.Sent."+methodFamily(), ),
			firstLine: firstLine{
				client: true,
			},
		}
		if ,  := .Deadline();  {
			.firstLine.deadline = time.Until()
		}
		.tr.LazyLog(&.firstLine, false)
		 = newTraceContext(, .tr)
	}

	if .cc.parsedTarget.URL.Scheme == internal.GRPCResolverSchemeExtraMetadata {
		// Add extra metadata (metadata that will be added by transport) to context
		// so the balancer can see them.
		 = grpcutil.WithExtraMetadata(, metadata.Pairs(
			"content-type", grpcutil.ContentType(.callHdr.ContentSubtype),
		))
	}

	return &csAttempt{
		ctx:            ,
		beginTime:      ,
		cs:             ,
		decompressorV0: .cc.dopts.dc,
		statsHandlers:  ,
		trInfo:         ,
	}, nil
}

func ( *csAttempt) () error {
	 := .cs

	 := balancer.PickInfo{Ctx: .ctx, FullMethodName: .callHdr.Method}
	,  := .cc.pickerWrapper.pick(.ctx, .callInfo.failFast, )
	.transport, .pickResult = .transport, .result
	if  != nil {
		if ,  := .(dropError);  {
			 = .error
			.drop = true
		}
		return 
	}
	if .trInfo != nil {
		.trInfo.firstLine.SetRemoteAddr(.transport.RemoteAddr())
	}
	if .blocked {
		for ,  := range .statsHandlers {
			.HandleRPC(.ctx, &stats.DelayedPickComplete{})
		}
	}
	return nil
}

func ( *csAttempt) () error {
	 := .cs
	.callHdr.PreviousAttempts = .numRetries

	// Merge metadata stored in PickResult, if any, with existing call metadata.
	// It is safe to overwrite the csAttempt's context here, since all state
	// maintained in it are local to the attempt. When the attempt has to be
	// retried, a new instance of csAttempt will be created.
	if .pickResult.Metadata != nil {
		// We currently do not have a function it the metadata package which
		// merges given metadata with existing metadata in a context. Existing
		// function `AppendToOutgoingContext()` takes a variadic argument of key
		// value pairs.
		//
		// TODO: Make it possible to retrieve key value pairs from metadata.MD
		// in a form passable to AppendToOutgoingContext(), or create a version
		// of AppendToOutgoingContext() that accepts a metadata.MD.
		,  := metadata.FromOutgoingContext(.ctx)
		 = metadata.Join(, .pickResult.Metadata)
		.ctx = metadata.NewOutgoingContext(.ctx, )
	}

	,  := .transport.NewStream(.ctx, .callHdr)
	if  != nil {
		,  := .(*transport.NewStreamError)
		if ! {
			// Unexpected.
			return 
		}

		if .AllowTransparentRetry {
			.allowTransparentRetry = true
		}

		// Unwrap and convert error.
		return toRPCErr(.Err)
	}
	.transportStream = 
	.ctx = .Context()
	.parser = &parser{r: , bufferPool: .cs.cc.dopts.copts.BufferPool}
	return nil
}

// clientStream implements a client side Stream.
type clientStream struct {
	callHdr  *transport.CallHdr
	opts     []CallOption
	callInfo *callInfo
	cc       *ClientConn
	desc     *StreamDesc

	codec        baseCodec
	compressorV0 Compressor
	compressorV1 encoding.Compressor

	cancel context.CancelFunc // cancels all attempts

	sentLast bool // sent an end stream

	methodConfig *MethodConfig

	ctx context.Context // the application's context, wrapped by stats/tracing

	retryThrottler *retryThrottler // The throttler active when the RPC began.

	binlogs []binarylog.MethodLogger
	// serverHeaderBinlogged is a boolean for whether server header has been
	// logged. Server header will be logged when the first time one of those
	// happens: stream.Header(), stream.Recv().
	//
	// It's only read and used by Recv() and Header(), so it doesn't need to be
	// synchronized.
	serverHeaderBinlogged bool

	mu                      sync.Mutex
	firstAttempt            bool // if true, transparent retry is valid
	numRetries              int  // exclusive of transparent retry attempt(s)
	numRetriesSincePushback int  // retries since pushback; to reset backoff
	finished                bool // TODO: replace with atomic cmpxchg or sync.Once?
	// attempt is the active client stream attempt.
	// The only place where it is written is the newAttemptLocked method and this method never writes nil.
	// So, attempt can be nil only inside newClientStream function when clientStream is first created.
	// One of the first things done after clientStream's creation, is to call newAttemptLocked which either
	// assigns a non nil value to the attempt or returns an error. If an error is returned from newAttemptLocked,
	// then newClientStream calls finish on the clientStream and returns. So, finish method is the only
	// place where we need to check if the attempt is nil.
	attempt *csAttempt
	// TODO(hedging): hedging will have multiple attempts simultaneously.
	committed        bool // active attempt committed for retry?
	onCommit         func()
	replayBuffer     []replayOp // operations to replay on retry
	replayBufferSize int        // current size of replayBuffer
	// nameResolutionDelay indicates if there was a delay in the name resolution.
	// This field is only valid on client side, it's always false on server side.
	nameResolutionDelay bool
}

type replayOp struct {
	op      func(a *csAttempt) error
	cleanup func()
}

// csAttempt implements a single transport stream attempt within a
// clientStream.
type csAttempt struct {
	ctx             context.Context
	cs              *clientStream
	transport       transport.ClientTransport
	transportStream *transport.ClientStream
	parser          *parser
	pickResult      balancer.PickResult

	finished        bool
	decompressorV0  Decompressor
	decompressorV1  encoding.Compressor
	decompressorSet bool

	mu sync.Mutex // guards trInfo.tr
	// trInfo may be nil (if EnableTracing is false).
	// trInfo.tr is set when created (if EnableTracing is true),
	// and cleared when the finish method is called.
	trInfo *traceInfo

	statsHandlers []stats.Handler
	beginTime     time.Time

	// set for newStream errors that may be transparently retried
	allowTransparentRetry bool
	// set for pick errors that are returned as a status
	drop bool
}

func ( *clientStream) () {
	if !.committed && .onCommit != nil {
		.onCommit()
	}
	.committed = true
	for ,  := range .replayBuffer {
		if .cleanup != nil {
			.cleanup()
		}
	}
	.replayBuffer = nil
}

func ( *clientStream) () {
	.mu.Lock()
	.commitAttemptLocked()
	.mu.Unlock()
}

// shouldRetry returns nil if the RPC should be retried; otherwise it returns
// the error that should be returned by the operation.  If the RPC should be
// retried, the bool indicates whether it is being retried transparently.
func ( *csAttempt) ( error) (bool, error) {
	 := .cs

	if .finished || .committed || .drop {
		// RPC is finished or committed or was dropped by the picker; cannot retry.
		return false, 
	}
	if .transportStream == nil && .allowTransparentRetry {
		return true, nil
	}
	// Wait for the trailers.
	 := false
	if .transportStream != nil {
		<-.transportStream.Done()
		 = .transportStream.Unprocessed()
	}
	if .firstAttempt &&  {
		// First attempt, stream unprocessed: transparently retry.
		return true, nil
	}
	if .cc.dopts.disableRetry {
		return false, 
	}

	 := 0
	 := false
	if .transportStream != nil {
		if !.transportStream.TrailersOnly() {
			return false, 
		}

		// TODO(retry): Move down if the spec changes to not check server pushback
		// before considering this a failure for throttling.
		 := .transportStream.Trailer()["grpc-retry-pushback-ms"]
		if len() == 1 {
			var  error
			if ,  = strconv.Atoi([0]);  != nil ||  < 0 {
				channelz.Infof(logger, .cc.channelz, "Server retry pushback specified to abort (%q).", [0])
				.retryThrottler.throttle() // This counts as a failure for throttling.
				return false, 
			}
			 = true
		} else if len() > 1 {
			channelz.Warningf(logger, .cc.channelz, "Server retry pushback specified multiple values (%q); not retrying.", )
			.retryThrottler.throttle() // This counts as a failure for throttling.
			return false, 
		}
	}

	var  codes.Code
	if .transportStream != nil {
		 = .transportStream.Status().Code()
	} else {
		 = status.Code()
	}

	 := .methodConfig.RetryPolicy
	if  == nil || !.RetryableStatusCodes[] {
		return false, 
	}

	// Note: the ordering here is important; we count this as a failure
	// only if the code matched a retryable code.
	if .retryThrottler.throttle() {
		return false, 
	}
	if .numRetries+1 >= .MaxAttempts {
		return false, 
	}

	var  time.Duration
	if  {
		 = time.Millisecond * time.Duration()
		.numRetriesSincePushback = 0
	} else {
		 := math.Pow(.BackoffMultiplier, float64(.numRetriesSincePushback))
		 := min(float64(.InitialBackoff)*, float64(.MaxBackoff))
		// Apply jitter by multiplying with a random factor between 0.8 and 1.2
		 *= 0.8 + 0.4*rand.Float64()
		 = time.Duration(int64())
		.numRetriesSincePushback++
	}

	// TODO(dfawley): we could eagerly fail here if dur puts us past the
	// deadline, but unsure if it is worth doing.
	 := time.NewTimer()
	select {
	case <-.C:
		.numRetries++
		return false, nil
	case <-.ctx.Done():
		.Stop()
		return false, status.FromContextError(.ctx.Err()).Err()
	}
}

// Returns nil if a retry was performed and succeeded; error otherwise.
func ( *clientStream) ( *csAttempt,  error) error {
	for {
		.finish(toRPCErr())
		,  := .shouldRetry()
		if  != nil {
			.commitAttemptLocked()
			return 
		}
		.firstAttempt = false
		,  = .newAttemptLocked()
		if  != nil {
			// Only returns error if the clientconn is closed or the context of
			// the stream is canceled.
			return 
		}
		// Note that the first op in replayBuffer always sets cs.attempt
		// if it is able to pick a transport and create a stream.
		if  = .replayBufferLocked();  == nil {
			return nil
		}
	}
}

func ( *clientStream) () context.Context {
	.commitAttempt()
	// No need to lock before using attempt, since we know it is committed and
	// cannot change.
	if .attempt.transportStream != nil {
		return .attempt.transportStream.Context()
	}
	return .ctx
}

func ( *clientStream) ( func( *csAttempt) error,  func()) error {
	.mu.Lock()
	for {
		if .committed {
			.mu.Unlock()
			// toRPCErr is used in case the error from the attempt comes from
			// NewClientStream, which intentionally doesn't return a status
			// error to allow for further inspection; all other errors should
			// already be status errors.
			return toRPCErr((.attempt))
		}
		if len(.replayBuffer) == 0 {
			// For the first op, which controls creation of the stream and
			// assigns cs.attempt, we need to create a new attempt inline
			// before executing the first op.  On subsequent ops, the attempt
			// is created immediately before replaying the ops.
			var  error
			if .attempt,  = .newAttemptLocked(false /* isTransparent */);  != nil {
				.mu.Unlock()
				.finish()
				return 
			}
		}
		 := .attempt
		.mu.Unlock()
		 := ()
		.mu.Lock()
		if  != .attempt {
			// We started another attempt already.
			continue
		}
		if  == io.EOF {
			<-.transportStream.Done()
		}
		if  == nil || ( == io.EOF && .transportStream.Status().Code() == codes.OK) {
			()
			.mu.Unlock()
			return 
		}
		if  := .retryLocked(, );  != nil {
			.mu.Unlock()
			return 
		}
	}
}

func ( *clientStream) () (metadata.MD, error) {
	var  metadata.MD
	 := .withRetry(func( *csAttempt) error {
		var  error
		,  = .transportStream.Header()
		return toRPCErr()
	}, .commitAttemptLocked)

	if  == nil &&  == nil {
		// The stream ended with success.  Finish the clientStream.
		 = io.EOF
	}

	if  != nil {
		.finish()
		// Do not return the error.  The user should get it by calling Recv().
		return nil, nil
	}

	if len(.binlogs) != 0 && !.serverHeaderBinlogged &&  != nil {
		// Only log if binary log is on and header has not been logged, and
		// there is actually headers to log.
		 := &binarylog.ServerHeader{
			OnClientSide: true,
			Header:       ,
			PeerAddr:     nil,
		}
		if ,  := peer.FromContext(.Context());  {
			.PeerAddr = .Addr
		}
		.serverHeaderBinlogged = true
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}

	return , nil
}

func ( *clientStream) () metadata.MD {
	// On RPC failure, we never need to retry, because usage requires that
	// RecvMsg() returned a non-nil error before calling this function is valid.
	// We would have retried earlier if necessary.
	//
	// Commit the attempt anyway, just in case users are not following those
	// directions -- it will prevent races and should not meaningfully impact
	// performance.
	.commitAttempt()
	if .attempt.transportStream == nil {
		return nil
	}
	return .attempt.transportStream.Trailer()
}

func ( *clientStream) ( *csAttempt) error {
	for ,  := range .replayBuffer {
		if  := .op();  != nil {
			return 
		}
	}
	return nil
}

func ( *clientStream) ( int,  func( *csAttempt) error,  func()) {
	// Note: we still will buffer if retry is disabled (for transparent retries).
	if .committed {
		return
	}
	.replayBufferSize += 
	if .replayBufferSize > .callInfo.maxRetryRPCBufferSize {
		.commitAttemptLocked()
		()
		return
	}
	.replayBuffer = append(.replayBuffer, replayOp{op: , cleanup: })
}

func ( *clientStream) ( any) ( error) {
	defer func() {
		if  != nil &&  != io.EOF {
			// Call finish on the client stream for errors generated by this SendMsg
			// call, as these indicate problems created by this client.  (Transport
			// errors are converted to an io.EOF error in csAttempt.sendMsg; the real
			// error will be returned from RecvMsg eventually in that case, or be
			// retried.)
			.finish()
		}
	}()
	if .sentLast {
		return status.Errorf(codes.Internal, "SendMsg called after CloseSend")
	}
	if !.desc.ClientStreams {
		.sentLast = true
	}

	// load hdr, payload, data
	, , , ,  := prepareMsg(, .codec, .compressorV0, .compressorV1, .cc.dopts.copts.BufferPool)
	if  != nil {
		return 
	}

	defer func() {
		.Free()
		// only free payload if compression was made, and therefore it is a different set
		// of buffers from data.
		if .isCompressed() {
			.Free()
		}
	}()

	 := .Len()
	 := .Len()
	// TODO(dfawley): should we be checking len(data) instead?
	if  > *.callInfo.maxSendMessageSize {
		return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", , *.callInfo.maxSendMessageSize)
	}

	// always take an extra ref in case data == payload (i.e. when the data isn't
	// compressed). The original ref will always be freed by the deferred free above.
	.Ref()
	 := func( *csAttempt) error {
		return .sendMsg(, , , , )
	}

	// onSuccess is invoked when the op is captured for a subsequent retry. If the
	// stream was established by a previous message and therefore retries are
	// disabled, onSuccess will not be invoked, and payloadRef can be freed
	// immediately.
	 := false
	 = .withRetry(, func() {
		.bufferForRetryLocked(len()+, , .Free)
		 = true
	})
	if ! {
		.Free()
	}
	if len(.binlogs) != 0 &&  == nil {
		 := &binarylog.ClientMessage{
			OnClientSide: true,
			Message:      .Materialize(),
		}
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}
	return 
}

func ( *clientStream) ( any) error {
	if len(.binlogs) != 0 && !.serverHeaderBinlogged {
		// Call Header() to binary log header if it's not already logged.
		.Header()
	}
	var  *payloadInfo
	if len(.binlogs) != 0 {
		 = &payloadInfo{}
		defer .free()
	}
	 := .withRetry(func( *csAttempt) error {
		return .recvMsg(, )
	}, .commitAttemptLocked)
	if len(.binlogs) != 0 &&  == nil {
		 := &binarylog.ServerMessage{
			OnClientSide: true,
			Message:      .uncompressedBytes.Materialize(),
		}
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}
	if  != nil || !.desc.ServerStreams {
		// err != nil or non-server-streaming indicates end of stream.
		.finish()
	}
	return 
}

func ( *clientStream) () error {
	if .sentLast {
		// Return a nil error on repeated calls to this method.
		return nil
	}
	.sentLast = true
	 := func( *csAttempt) error {
		.transportStream.Write(nil, nil, &transport.WriteOptions{Last: true})
		// Always return nil; io.EOF is the only error that might make sense
		// instead, but there is no need to signal the client to call RecvMsg
		// as the only use left for the stream after CloseSend is to call
		// RecvMsg.  This also matches historical behavior.
		return nil
	}
	.withRetry(, func() { .bufferForRetryLocked(0, , nil) })
	if len(.binlogs) != 0 {
		 := &binarylog.ClientHalfClose{
			OnClientSide: true,
		}
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}
	// We don't return an error here as we expect users to read all messages
	// from the stream and get the RPC status from RecvMsg().  Note that
	// SendMsg() must return an error when one occurs so the application
	// knows to stop sending messages, but that does not apply here.
	return nil
}

func ( *clientStream) ( error) {
	if  == io.EOF {
		// Ending a stream with EOF indicates a success.
		 = nil
	}
	.mu.Lock()
	if .finished {
		.mu.Unlock()
		return
	}
	.finished = true
	for ,  := range .callInfo.onFinish {
		()
	}
	.commitAttemptLocked()
	if .attempt != nil {
		.attempt.finish()
		// after functions all rely upon having a stream.
		if .attempt.transportStream != nil {
			for ,  := range .opts {
				.after(.callInfo, .attempt)
			}
		}
	}

	.mu.Unlock()
	// Only one of cancel or trailer needs to be logged.
	if len(.binlogs) != 0 {
		switch  {
		case errContextCanceled, errContextDeadline, ErrClientConnClosing:
			 := &binarylog.Cancel{
				OnClientSide: true,
			}
			for ,  := range .binlogs {
				.Log(.ctx, )
			}
		default:
			 := &binarylog.ServerTrailer{
				OnClientSide: true,
				Trailer:      .Trailer(),
				Err:          ,
			}
			if ,  := peer.FromContext(.Context());  {
				.PeerAddr = .Addr
			}
			for ,  := range .binlogs {
				.Log(.ctx, )
			}
		}
	}
	if  == nil {
		.retryThrottler.successfulRPC()
	}
	if channelz.IsOn() {
		if  != nil {
			.cc.incrCallsFailed()
		} else {
			.cc.incrCallsSucceeded()
		}
	}
	.cancel()
}

func ( *csAttempt) ( any,  []byte,  mem.BufferSlice, ,  int) error {
	 := .cs
	if .trInfo != nil {
		.mu.Lock()
		if .trInfo.tr != nil {
			.trInfo.tr.LazyLog(&payload{sent: true, msg: }, true)
		}
		.mu.Unlock()
	}
	if  := .transportStream.Write(, , &transport.WriteOptions{Last: !.desc.ClientStreams});  != nil {
		if !.desc.ClientStreams {
			// For non-client-streaming RPCs, we return nil instead of EOF on error
			// because the generated code requires it.  finish is not called; RecvMsg()
			// will call it with the stream's status independently.
			return nil
		}
		return io.EOF
	}
	if len(.statsHandlers) != 0 {
		for ,  := range .statsHandlers {
			.HandleRPC(.ctx, outPayload(true, , , , time.Now()))
		}
	}
	return nil
}

func ( *csAttempt) ( any,  *payloadInfo) ( error) {
	 := .cs
	if len(.statsHandlers) != 0 &&  == nil {
		 = &payloadInfo{}
		defer .free()
	}

	if !.decompressorSet {
		// Block until we receive headers containing received message encoding.
		if  := .transportStream.RecvCompress();  != "" &&  != encoding.Identity {
			if .decompressorV0 == nil || .decompressorV0.Type() !=  {
				// No configured decompressor, or it does not match the incoming
				// message encoding; attempt to find a registered compressor that does.
				.decompressorV0 = nil
				.decompressorV1 = encoding.GetCompressor()
			}
		} else {
			// No compression is used; disable our decompressor.
			.decompressorV0 = nil
		}
		// Only initialize this state once per stream.
		.decompressorSet = true
	}
	if  := recv(.parser, .codec, .transportStream, .decompressorV0, , *.callInfo.maxReceiveMessageSize, , .decompressorV1, false);  != nil {
		if  == io.EOF {
			if  := .transportStream.Status().Err();  != nil {
				return 
			}
			return io.EOF // indicates successful end of stream.
		}

		return toRPCErr()
	}
	if .trInfo != nil {
		.mu.Lock()
		if .trInfo.tr != nil {
			.trInfo.tr.LazyLog(&payload{sent: false, msg: }, true)
		}
		.mu.Unlock()
	}
	for ,  := range .statsHandlers {
		.HandleRPC(.ctx, &stats.InPayload{
			Client:           true,
			RecvTime:         time.Now(),
			Payload:          ,
			WireLength:       .compressedLength + headerLen,
			CompressedLength: .compressedLength,
			Length:           .uncompressedBytes.Len(),
		})
	}
	if .desc.ServerStreams {
		// Subsequent messages should be received by subsequent RecvMsg calls.
		return nil
	}
	// Special handling for non-server-stream rpcs.
	// This recv expects EOF or errors, so we don't collect inPayload.
	if  := recv(.parser, .codec, .transportStream, .decompressorV0, , *.callInfo.maxReceiveMessageSize, nil, .decompressorV1, false);  == io.EOF {
		return .transportStream.Status().Err() // non-server streaming Recv returns nil on success
	} else if  != nil {
		return toRPCErr()
	}
	return status.Errorf(codes.Internal, "cardinality violation: expected <EOF> for non server-streaming RPCs, but received another message")
}

func ( *csAttempt) ( error) {
	.mu.Lock()
	if .finished {
		.mu.Unlock()
		return
	}
	.finished = true
	if  == io.EOF {
		// Ending a stream with EOF indicates a success.
		 = nil
	}
	var  metadata.MD
	if .transportStream != nil {
		.transportStream.Close()
		 = .transportStream.Trailer()
	}

	if .pickResult.Done != nil {
		 := false
		if .transportStream != nil {
			 = .transportStream.BytesReceived()
		}
		.pickResult.Done(balancer.DoneInfo{
			Err:           ,
			Trailer:       ,
			BytesSent:     .transportStream != nil,
			BytesReceived: ,
			ServerLoad:    balancerload.Parse(),
		})
	}
	for ,  := range .statsHandlers {
		 := &stats.End{
			Client:    true,
			BeginTime: .beginTime,
			EndTime:   time.Now(),
			Trailer:   ,
			Error:     ,
		}
		.HandleRPC(.ctx, )
	}
	if .trInfo != nil && .trInfo.tr != nil {
		if  == nil {
			.trInfo.tr.LazyPrintf("RPC: [OK]")
		} else {
			.trInfo.tr.LazyPrintf("RPC: [%v]", )
			.trInfo.tr.SetError()
		}
		.trInfo.tr.Finish()
		.trInfo.tr = nil
	}
	.mu.Unlock()
}

// newNonRetryClientStream creates a ClientStream with the specified transport, on the
// given addrConn.
//
// It's expected that the given transport is either the same one in addrConn, or
// is already closed. To avoid race, transport is specified separately, instead
// of using ac.transport.
//
// Main difference between this and ClientConn.NewStream:
// - no retry
// - no service config (or wait for service config)
// - no tracing or stats
func ( context.Context,  *StreamDesc,  string,  transport.ClientTransport,  *addrConn,  ...CallOption) ( ClientStream,  error) {
	if  == nil {
		// TODO: return RPC error here?
		return nil, errors.New("transport provided is nil")
	}
	// defaultCallInfo contains unnecessary info(i.e. failfast, maxRetryRPCBufferSize), so we just initialize an empty struct.
	 := &callInfo{}

	// Possible context leak:
	// The cancel function for the child context we create will only be called
	// when RecvMsg returns a non-nil error, if the ClientConn is closed, or if
	// an error is generated by SendMsg.
	// https://github.com/grpc/grpc-go/issues/1818.
	,  := context.WithCancel()
	defer func() {
		if  != nil {
			()
		}
	}()

	for ,  := range  {
		if  := .before();  != nil {
			return nil, toRPCErr()
		}
	}
	.maxReceiveMessageSize = getMaxSize(nil, .maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
	.maxSendMessageSize = getMaxSize(nil, .maxSendMessageSize, defaultServerMaxSendMessageSize)
	if  := setCallInfoCodec();  != nil {
		return nil, 
	}

	 := &transport.CallHdr{
		Host:           .cc.authority,
		Method:         ,
		ContentSubtype: .contentSubtype,
	}

	// Set our outgoing compression according to the UseCompressor CallOption, if
	// set.  In that case, also find the compressor from the encoding package.
	// Otherwise, use the compressor configured by the WithCompressor DialOption,
	// if set.
	var  Compressor
	var  encoding.Compressor
	if  := .compressorName;  != "" {
		.SendCompress = 
		if  != encoding.Identity {
			 = encoding.GetCompressor()
			if  == nil {
				return nil, status.Errorf(codes.Internal, "grpc: Compressor is not installed for requested grpc-encoding %q", )
			}
		}
	} else if .cc.dopts.compressorV0 != nil {
		.SendCompress = .cc.dopts.compressorV0.Type()
		 = .cc.dopts.compressorV0
	}
	if .creds != nil {
		.Creds = .creds
	}

	// Use a special addrConnStream to avoid retry.
	 := &addrConnStream{
		callHdr:          ,
		ac:               ,
		ctx:              ,
		cancel:           ,
		opts:             ,
		callInfo:         ,
		desc:             ,
		codec:            .codec,
		sendCompressorV0: ,
		sendCompressorV1: ,
		transport:        ,
	}

	,  := .transport.NewStream(.ctx, .callHdr)
	if  != nil {
		 = toRPCErr()
		return nil, 
	}
	.transportStream = 
	.parser = &parser{r: , bufferPool: .dopts.copts.BufferPool}
	.incrCallsStarted()
	if  != unaryStreamDesc {
		// Listen on stream context to cleanup when the stream context is
		// canceled.  Also listen for the addrConn's context in case the
		// addrConn is closed or reconnects to a different address.  In all
		// other cases, an error should already be injected into the recv
		// buffer by the transport, which the client will eventually receive,
		// and then we will cancel the stream's context in
		// addrConnStream.finish.
		go func() {
			.mu.Lock()
			 := .ctx
			.mu.Unlock()
			select {
			case <-.Done():
				.finish(status.Error(codes.Canceled, "grpc: the SubConn is closing"))
			case <-.Done():
				.finish(toRPCErr(.Err()))
			}
		}()
	}
	return , nil
}

type addrConnStream struct {
	transportStream  *transport.ClientStream
	ac               *addrConn
	callHdr          *transport.CallHdr
	cancel           context.CancelFunc
	opts             []CallOption
	callInfo         *callInfo
	transport        transport.ClientTransport
	ctx              context.Context
	sentLast         bool
	desc             *StreamDesc
	codec            baseCodec
	sendCompressorV0 Compressor
	sendCompressorV1 encoding.Compressor
	decompressorSet  bool
	decompressorV0   Decompressor
	decompressorV1   encoding.Compressor
	parser           *parser

	// mu guards finished and is held for the entire finish method.
	mu       sync.Mutex
	finished bool
}

func ( *addrConnStream) () (metadata.MD, error) {
	,  := .transportStream.Header()
	if  != nil {
		.finish(toRPCErr())
	}
	return , 
}

func ( *addrConnStream) () metadata.MD {
	return .transportStream.Trailer()
}

func ( *addrConnStream) () error {
	if .sentLast {
		// Return a nil error on repeated calls to this method.
		return nil
	}
	.sentLast = true

	.transportStream.Write(nil, nil, &transport.WriteOptions{Last: true})
	// Always return nil; io.EOF is the only error that might make sense
	// instead, but there is no need to signal the client to call RecvMsg
	// as the only use left for the stream after CloseSend is to call
	// RecvMsg.  This also matches historical behavior.
	return nil
}

func ( *addrConnStream) () context.Context {
	return .transportStream.Context()
}

func ( *addrConnStream) ( any) ( error) {
	defer func() {
		if  != nil &&  != io.EOF {
			// Call finish on the client stream for errors generated by this SendMsg
			// call, as these indicate problems created by this client.  (Transport
			// errors are converted to an io.EOF error in csAttempt.sendMsg; the real
			// error will be returned from RecvMsg eventually in that case, or be
			// retried.)
			.finish()
		}
	}()
	if .sentLast {
		return status.Errorf(codes.Internal, "SendMsg called after CloseSend")
	}
	if !.desc.ClientStreams {
		.sentLast = true
	}

	// load hdr, payload, data
	, , , ,  := prepareMsg(, .codec, .sendCompressorV0, .sendCompressorV1, .ac.dopts.copts.BufferPool)
	if  != nil {
		return 
	}

	defer func() {
		.Free()
		// only free payload if compression was made, and therefore it is a different set
		// of buffers from data.
		if .isCompressed() {
			.Free()
		}
	}()

	// TODO(dfawley): should we be checking len(data) instead?
	if .Len() > *.callInfo.maxSendMessageSize {
		return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", .Len(), *.callInfo.maxSendMessageSize)
	}

	if  := .transportStream.Write(, , &transport.WriteOptions{Last: !.desc.ClientStreams});  != nil {
		if !.desc.ClientStreams {
			// For non-client-streaming RPCs, we return nil instead of EOF on error
			// because the generated code requires it.  finish is not called; RecvMsg()
			// will call it with the stream's status independently.
			return nil
		}
		return io.EOF
	}

	return nil
}

func ( *addrConnStream) ( any) ( error) {
	defer func() {
		if  != nil || !.desc.ServerStreams {
			// err != nil or non-server-streaming indicates end of stream.
			.finish()
		}
	}()

	if !.decompressorSet {
		// Block until we receive headers containing received message encoding.
		if  := .transportStream.RecvCompress();  != "" &&  != encoding.Identity {
			if .decompressorV0 == nil || .decompressorV0.Type() !=  {
				// No configured decompressor, or it does not match the incoming
				// message encoding; attempt to find a registered compressor that does.
				.decompressorV0 = nil
				.decompressorV1 = encoding.GetCompressor()
			}
		} else {
			// No compression is used; disable our decompressor.
			.decompressorV0 = nil
		}
		// Only initialize this state once per stream.
		.decompressorSet = true
	}
	if  := recv(.parser, .codec, .transportStream, .decompressorV0, , *.callInfo.maxReceiveMessageSize, nil, .decompressorV1, false);  != nil {
		if  == io.EOF {
			if  := .transportStream.Status().Err();  != nil {
				return 
			}
			return io.EOF // indicates successful end of stream.
		}
		return toRPCErr()
	}

	if .desc.ServerStreams {
		// Subsequent messages should be received by subsequent RecvMsg calls.
		return nil
	}

	// Special handling for non-server-stream rpcs.
	// This recv expects EOF or errors, so we don't collect inPayload.
	if  := recv(.parser, .codec, .transportStream, .decompressorV0, , *.callInfo.maxReceiveMessageSize, nil, .decompressorV1, false);  == io.EOF {
		return .transportStream.Status().Err() // non-server streaming Recv returns nil on success
	} else if  != nil {
		return toRPCErr()
	}
	return status.Errorf(codes.Internal, "cardinality violation: expected <EOF> for non server-streaming RPCs, but received another message")
}

func ( *addrConnStream) ( error) {
	.mu.Lock()
	if .finished {
		.mu.Unlock()
		return
	}
	.finished = true
	if  == io.EOF {
		// Ending a stream with EOF indicates a success.
		 = nil
	}
	if .transportStream != nil {
		.transportStream.Close()
	}

	if  != nil {
		.ac.incrCallsFailed()
	} else {
		.ac.incrCallsSucceeded()
	}
	.cancel()
	.mu.Unlock()
}

// ServerStream defines the server-side behavior of a streaming RPC.
//
// Errors returned from ServerStream methods are compatible with the status
// package.  However, the status code will often not match the RPC status as
// seen by the client application, and therefore, should not be relied upon for
// this purpose.
type ServerStream interface {
	// SetHeader sets the header metadata. It may be called multiple times.
	// When call multiple times, all the provided metadata will be merged.
	// All the metadata will be sent out when one of the following happens:
	//  - ServerStream.SendHeader() is called;
	//  - The first response is sent out;
	//  - An RPC status is sent out (error or success).
	SetHeader(metadata.MD) error
	// SendHeader sends the header metadata.
	// The provided md and headers set by SetHeader() will be sent.
	// It fails if called multiple times.
	SendHeader(metadata.MD) error
	// SetTrailer sets the trailer metadata which will be sent with the RPC status.
	// When called more than once, all the provided metadata will be merged.
	SetTrailer(metadata.MD)
	// Context returns the context for this stream.
	Context() context.Context
	// SendMsg sends a message. On error, SendMsg aborts the stream and the
	// error is returned directly.
	//
	// SendMsg blocks until:
	//   - There is sufficient flow control to schedule m with the transport, or
	//   - The stream is done, or
	//   - The stream breaks.
	//
	// SendMsg does not wait until the message is received by the client. An
	// untimely stream closure may result in lost messages.
	//
	// It is safe to have a goroutine calling SendMsg and another goroutine
	// calling RecvMsg on the same stream at the same time, but it is not safe
	// to call SendMsg on the same stream in different goroutines.
	//
	// It is not safe to modify the message after calling SendMsg. Tracing
	// libraries and stats handlers may use the message lazily.
	SendMsg(m any) error
	// RecvMsg blocks until it receives a message into m or the stream is
	// done. It returns io.EOF when the client has performed a CloseSend. On
	// any non-EOF error, the stream is aborted and the error contains the
	// RPC status.
	//
	// It is safe to have a goroutine calling SendMsg and another goroutine
	// calling RecvMsg on the same stream at the same time, but it is not
	// safe to call RecvMsg on the same stream in different goroutines.
	RecvMsg(m any) error
}

// serverStream implements a server side Stream.
type serverStream struct {
	ctx   context.Context
	s     *transport.ServerStream
	p     *parser
	codec baseCodec
	desc  *StreamDesc

	compressorV0   Compressor
	compressorV1   encoding.Compressor
	decompressorV0 Decompressor
	decompressorV1 encoding.Compressor

	sendCompressorName string

	recvFirstMsg bool // set after the first message is received

	maxReceiveMessageSize int
	maxSendMessageSize    int
	trInfo                *traceInfo

	statsHandler []stats.Handler

	binlogs []binarylog.MethodLogger
	// serverHeaderBinlogged indicates whether server header has been logged. It
	// will happen when one of the following two happens: stream.SendHeader(),
	// stream.Send().
	//
	// It's only checked in send and sendHeader, doesn't need to be
	// synchronized.
	serverHeaderBinlogged bool

	mu sync.Mutex // protects trInfo.tr after the service handler runs.
}

func ( *serverStream) () context.Context {
	return .ctx
}

func ( *serverStream) ( metadata.MD) error {
	if .Len() == 0 {
		return nil
	}
	 := imetadata.Validate()
	if  != nil {
		return status.Error(codes.Internal, .Error())
	}
	return .s.SetHeader()
}

func ( *serverStream) ( metadata.MD) error {
	 := imetadata.Validate()
	if  != nil {
		return status.Error(codes.Internal, .Error())
	}

	 = .s.SendHeader()
	if len(.binlogs) != 0 && !.serverHeaderBinlogged {
		,  := .s.Header()
		 := &binarylog.ServerHeader{
			Header: ,
		}
		.serverHeaderBinlogged = true
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}
	return 
}

func ( *serverStream) ( metadata.MD) {
	if .Len() == 0 {
		return
	}
	if  := imetadata.Validate();  != nil {
		logger.Errorf("stream: failed to validate md when setting trailer, err: %v", )
	}
	.s.SetTrailer()
}

func ( *serverStream) ( any) ( error) {
	defer func() {
		if .trInfo != nil {
			.mu.Lock()
			if .trInfo.tr != nil {
				if  == nil {
					.trInfo.tr.LazyLog(&payload{sent: true, msg: }, true)
				} else {
					.trInfo.tr.LazyLog(&fmtStringer{"%v", []any{}}, true)
					.trInfo.tr.SetError()
				}
			}
			.mu.Unlock()
		}
		if  != nil &&  != io.EOF {
			,  := status.FromError(toRPCErr())
			.s.WriteStatus()
			// Non-user specified status was sent out. This should be an error
			// case (as a server side Cancel maybe).
			//
			// This is not handled specifically now. User will return a final
			// status from the service handler, we will log that error instead.
			// This behavior is similar to an interceptor.
		}
	}()

	// Server handler could have set new compressor by calling SetSendCompressor.
	// In case it is set, we need to use it for compressing outbound message.
	if  := .s.SendCompress();  != .sendCompressorName {
		.compressorV1 = encoding.GetCompressor()
		.sendCompressorName = 
	}

	// load hdr, payload, data
	, , , ,  := prepareMsg(, .codec, .compressorV0, .compressorV1, .p.bufferPool)
	if  != nil {
		return 
	}

	defer func() {
		.Free()
		// only free payload if compression was made, and therefore it is a different set
		// of buffers from data.
		if .isCompressed() {
			.Free()
		}
	}()

	 := .Len()
	 := .Len()

	// TODO(dfawley): should we be checking len(data) instead?
	if  > .maxSendMessageSize {
		return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", , .maxSendMessageSize)
	}
	if  := .s.Write(, , &transport.WriteOptions{Last: false});  != nil {
		return toRPCErr()
	}

	if len(.binlogs) != 0 {
		if !.serverHeaderBinlogged {
			,  := .s.Header()
			 := &binarylog.ServerHeader{
				Header: ,
			}
			.serverHeaderBinlogged = true
			for ,  := range .binlogs {
				.Log(.ctx, )
			}
		}
		 := &binarylog.ServerMessage{
			Message: .Materialize(),
		}
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}
	if len(.statsHandler) != 0 {
		for ,  := range .statsHandler {
			.HandleRPC(.s.Context(), outPayload(false, , , , time.Now()))
		}
	}
	return nil
}

func ( *serverStream) ( any) ( error) {
	defer func() {
		if .trInfo != nil {
			.mu.Lock()
			if .trInfo.tr != nil {
				if  == nil {
					.trInfo.tr.LazyLog(&payload{sent: false, msg: }, true)
				} else if  != io.EOF {
					.trInfo.tr.LazyLog(&fmtStringer{"%v", []any{}}, true)
					.trInfo.tr.SetError()
				}
			}
			.mu.Unlock()
		}
		if  != nil &&  != io.EOF {
			,  := status.FromError(toRPCErr())
			.s.WriteStatus()
			// Non-user specified status was sent out. This should be an error
			// case (as a server side Cancel maybe).
			//
			// This is not handled specifically now. User will return a final
			// status from the service handler, we will log that error instead.
			// This behavior is similar to an interceptor.
		}
	}()
	var  *payloadInfo
	if len(.statsHandler) != 0 || len(.binlogs) != 0 {
		 = &payloadInfo{}
		defer .free()
	}
	if  := recv(.p, .codec, .s, .decompressorV0, , .maxReceiveMessageSize, , .decompressorV1, true);  != nil {
		if  == io.EOF {
			if len(.binlogs) != 0 {
				 := &binarylog.ClientHalfClose{}
				for ,  := range .binlogs {
					.Log(.ctx, )
				}
			}
			// Received no request msg for non-client streaming rpcs.
			if !.desc.ClientStreams && !.recvFirstMsg {
				return status.Error(codes.Internal, "cardinality violation: received no request message from non-client-streaming RPC")
			}
			return 
		}
		if  == io.ErrUnexpectedEOF {
			 = status.Error(codes.Internal, io.ErrUnexpectedEOF.Error())
		}
		return toRPCErr()
	}
	.recvFirstMsg = true
	if len(.statsHandler) != 0 {
		for ,  := range .statsHandler {
			.HandleRPC(.s.Context(), &stats.InPayload{
				RecvTime:         time.Now(),
				Payload:          ,
				Length:           .uncompressedBytes.Len(),
				WireLength:       .compressedLength + headerLen,
				CompressedLength: .compressedLength,
			})
		}
	}
	if len(.binlogs) != 0 {
		 := &binarylog.ClientMessage{
			Message: .uncompressedBytes.Materialize(),
		}
		for ,  := range .binlogs {
			.Log(.ctx, )
		}
	}

	if .desc.ClientStreams {
		// Subsequent messages should be received by subsequent RecvMsg calls.
		return nil
	}
	// Special handling for non-client-stream rpcs.
	// This recv expects EOF or errors, so we don't collect inPayload.
	if  := recv(.p, .codec, .s, .decompressorV0, , .maxReceiveMessageSize, nil, .decompressorV1, true);  == io.EOF {
		return nil
	} else if  != nil {
		return 
	}
	return status.Error(codes.Internal, "cardinality violation: received multiple request messages for non-client-streaming RPC")
}

// MethodFromServerStream returns the method string for the input stream.
// The returned string is in the format of "/service/method".
func ( ServerStream) (string, bool) {
	return Method(.Context())
}

// prepareMsg returns the hdr, payload and data using the compressors passed or
// using the passed preparedmsg. The returned boolean indicates whether
// compression was made and therefore whether the payload needs to be freed in
// addition to the returned data. Freeing the payload if the returned boolean is
// false can lead to undefined behavior.
func ( any,  baseCodec,  Compressor,  encoding.Compressor,  mem.BufferPool) ( []byte, ,  mem.BufferSlice,  payloadFormat,  error) {
	if ,  := .(*PreparedMsg);  {
		return .hdr, .encodedData, .payload, .pf, nil
	}
	// The input interface is not a prepared msg.
	// Marshal and Compress the data at this point
	,  = encode(, )
	if  != nil {
		return nil, nil, nil, 0, 
	}
	, ,  := compress(, , , )
	if  != nil {
		.Free()
		return nil, nil, nil, 0, 
	}
	,  = msgHeader(, , )
	return , , , , nil
}