github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/router/rpc.go (about) 1 package router 2 3 import ( 4 "context" 5 "encoding/json" 6 "reflect" 7 8 "github.com/volts-dev/volts/codec" 9 "github.com/volts-dev/volts/internal/body" 10 "github.com/volts-dev/volts/logger" 11 "github.com/volts-dev/volts/transport" 12 ) 13 14 var ( 15 RpcContextType = reflect.TypeOf(&TRpcContext{}) // must be a pointer 16 RpcContext = "RpcContext" // 标识用于判断String() 17 ) 18 19 type ( 20 // 代表一个控制集 21 TRpcContext struct { 22 logger.ILogger 23 context context.Context 24 response *transport.RpcResponse //http.ResponseWriter 25 request *transport.RpcRequest // 26 router *TRouter 27 data *TParamsSet // 数据缓存在各个Controler间调用 28 pathParams *TParamsSet 29 route route //执行本次Handle的Route 30 inited bool 31 handlerIndex int 32 handler *handler 33 name string // name of service 34 __rcvr reflect.Value // receiver of methods for the service 35 val reflect.Value 36 typ reflect.Type // type of the receiver 37 //method map[string]*methodType // registered methods 38 //function map[string]*functionType // registered functions 39 isDone bool // -- 已经提交过 40 41 argv reflect.Value 42 replyv reflect.Value 43 } 44 ) 45 46 func handleError(res *transport.Message, err error) (*transport.Message, error) { 47 res.SetMessageStatusType(transport.StatusError) 48 if res.Header == nil { 49 res.Header = make(map[string]string) 50 } 51 res.Header["__rpc_error__"] = err.Error() 52 return res, err 53 } 54 55 func NewRpcHandler(router *TRouter) *TRpcContext { 56 handler := &TRpcContext{ 57 ILogger: log, 58 router: router, 59 } 60 handler.val = reflect.ValueOf(handler) 61 handler.typ = handler.val.Type() 62 return handler 63 } 64 func (self *TRpcContext) Router() IRouter { 65 return self.router 66 } 67 func (self *TRpcContext) Request() *transport.RpcRequest { 68 return self.request 69 } 70 71 func (self *TRpcContext) Response() *transport.RpcResponse { 72 return self.response 73 } 74 75 func (self *TRpcContext) Route() route { 76 return self.route 77 } 78 79 func (self *TRpcContext) HandlerIndex() int { 80 return self.handlerIndex 81 } 82 83 func (self *TRpcContext) Handler(index ...int) *handler { 84 idx := self.handlerIndex 85 if len(index) > 0 { 86 idx = index[0] 87 } 88 89 if idx == self.handlerIndex { 90 return self.handler 91 } 92 93 return self.route.handlers[idx] 94 } 95 96 func (self *TRpcContext) Context() context.Context { 97 return self.context 98 } 99 100 func (self *TRpcContext) IsDone() bool { 101 return self.isDone 102 } 103 104 // the reflect model of Value 105 func (self *TRpcContext) ValueModel() reflect.Value { 106 return self.val 107 } 108 109 // the reflect model of Type 110 func (self *TRpcContext) TypeModel() reflect.Type { 111 return self.typ 112 } 113 114 func (self *TRpcContext) reset(rw *transport.RpcResponse, req *transport.RpcRequest, Router IRouter, Route *route) { 115 self.request = req 116 self.response = rw 117 self.data = nil // 清空 118 } 119 120 func (self *TRpcContext) setData(v interface{}) { 121 122 } 123 124 func (self *TRpcContext) String() string { 125 return RpcContext 126 } 127 128 func (self *TRpcContext) Body() *body.TBody { 129 return self.request.Body() 130 } 131 132 func (self *TRpcContext) Write(data []byte) (int, error) { 133 return self.response.Write(data) 134 } 135 136 func (self *TRpcContext) WriteStream(data interface{}) error { 137 self.isDone = true 138 return self.response.WriteStream(data) 139 } 140 141 func (self *TRpcContext) WriteStreamWithCodec(data interface{}, c codec.ICodec) error { 142 self.isDone = true 143 self.response.Body().Codec = c 144 return self.response.WriteStream(data) 145 } 146 147 func (self *TRpcContext) RespondByJson(data interface{}) { 148 js, err := json.Marshal(data) 149 if err != nil { 150 self.response.Write([]byte(err.Error())) 151 return 152 } 153 154 self.response.Write(js) 155 } 156 157 func (self *TRpcContext) Next() { 158 self.handler.Invoke(self) 159 } 160 161 func (self *TRpcContext) Data() *TParamsSet { 162 if self.data == nil { 163 self.data = NewParamsSet(self) 164 } 165 166 return self.data 167 } 168 169 func (self *TRpcContext) PathParams() *TParamsSet { 170 return self.pathParams 171 } 172 173 func (self *TRpcContext) Abort(message ...string) { 174 self.isDone = true 175 self.response.WriteHeader(transport.StatusAborted) 176 if len(message) > 0 { 177 self.Write([]byte(message[0])) 178 } 179 self.response.Write(nil) 180 } 181 182 func (self *TRpcContext) NotFound(message ...string) { 183 self.isDone = true 184 self.response.WriteHeader(transport.StatusNotFound) 185 if len(message) > 0 { 186 self.Write([]byte(message[0])) 187 } 188 self.response.Write(nil) 189 } 190 191 func (self *TRpcContext) Forbidden(message ...string) { 192 self.isDone = true 193 self.response.WriteHeader(transport.StatusForbidden) 194 if len(message) > 0 { 195 self.Write([]byte(message[0])) 196 } 197 self.response.Write(nil) 198 } 199 200 func (self *TRpcContext) setControllerIndex(num int) { 201 self.handlerIndex = num 202 } 203 204 func (self *TRpcContext) setPathParams(p Params) { 205 // init dy url parm to handler 206 if len(p) > 0 { 207 self.pathParams = NewParamsSet(self) 208 } 209 210 for _, param := range p { 211 self.pathParams.SetByField(param.Name, param.Value) 212 } 213 } 214 215 func (self *TRpcContext) setHandler(h *handler) { 216 self.handler = h 217 }