forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_handler.go
108 lines (88 loc) · 3.04 KB
/
client_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2021 CloudWeGo 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 nphttp2
import (
"context"
"net"
"github.com/pjanthony2001/kitex/pkg/kerrors"
"github.com/pjanthony2001/kitex/pkg/remote"
"github.com/pjanthony2001/kitex/pkg/remote/codec/protobuf"
"github.com/pjanthony2001/kitex/pkg/remote/trans/nphttp2/metadata"
"github.com/pjanthony2001/kitex/pkg/rpcinfo"
)
type cliTransHandlerFactory struct{}
// NewCliTransHandlerFactory ...
func NewCliTransHandlerFactory() remote.ClientTransHandlerFactory {
return &cliTransHandlerFactory{}
}
func (f *cliTransHandlerFactory) NewTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) {
return newCliTransHandler(opt)
}
func newCliTransHandler(opt *remote.ClientOption) (*cliTransHandler, error) {
return &cliTransHandler{
opt: opt,
codec: protobuf.NewGRPCCodec(),
}, nil
}
var _ remote.ClientTransHandler = &cliTransHandler{}
type cliTransHandler struct {
opt *remote.ClientOption
codec remote.Codec
}
func (h *cliTransHandler) Write(ctx context.Context, conn net.Conn, msg remote.Message) (nctx context.Context, err error) {
buf := newBuffer(conn.(*clientConn))
defer buf.Release(err)
if err = h.codec.Encode(ctx, msg, buf); err != nil {
return ctx, err
}
return ctx, buf.Flush()
}
func (h *cliTransHandler) Read(ctx context.Context, conn net.Conn, msg remote.Message) (nctx context.Context, err error) {
buf := newBuffer(conn.(*clientConn))
defer buf.Release(err)
err = h.codec.Decode(ctx, msg, buf)
if bizStatusErr, isBizErr := kerrors.FromBizStatusError(err); isBizErr {
if setter, ok := msg.RPCInfo().Invocation().(rpcinfo.InvocationSetter); ok {
setter.SetBizStatusErr(bizStatusErr)
return ctx, nil
}
}
ctx = receiveHeaderAndTrailer(ctx, conn)
return ctx, err
}
func (h *cliTransHandler) OnRead(ctx context.Context, conn net.Conn) (context.Context, error) {
// do nothing
hdr, err := conn.(*clientConn).Header()
if err != nil {
return nil, err
}
return metadata.NewIncomingContext(ctx, hdr), nil
}
func (h *cliTransHandler) OnConnect(ctx context.Context) (context.Context, error) {
return ctx, nil
}
func (h *cliTransHandler) OnInactive(ctx context.Context, conn net.Conn) {
panic("unimplemented")
}
func (h *cliTransHandler) OnError(ctx context.Context, err error, conn net.Conn) {
panic("unimplemented")
}
func (h *cliTransHandler) OnMessage(ctx context.Context, args, result remote.Message) (context.Context, error) {
// do nothing
return ctx, nil
}
func (h *cliTransHandler) SetPipeline(pipeline *remote.TransPipeline) {
}