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
| type request struct { h *codec.Header argv, replyv reflect.Value }
func (server *Server) readRequestHeader(cc codec.Codec) (*codec.Header, error) { var h codec.Header if err := cc.ReadHeader(&h); err != nil { if err != io.EOF && err != io.ErrUnexpectedEOF { log.Println("rpc server: read header error:", err) } return nil, err } return &h, nil }
func (server *Server) readRequest(cc codec.Codec) (*request, error) { h, err := server.readRequestHeader(cc) if err != nil { return nil, err } req := &request{h: h} req.argv = reflect.New(reflect.TypeOf("")) if err = cc.ReadBody(req.argv.Interface()); err != nil { log.Println("rpc server: read argv err:", err) } return req, nil }
func (server *Server) sendResponse(cc codec.Codec, h *codec.Header, body interface{}, sending *sync.Mutex) { sending.Lock() defer sending.Unlock() if err := cc.Write(h, body); err != nil { log.Println("rpc server: write response error:", err) } }
func (server *Server) handleRequest(cc codec.Codec, req *request, sending *sync.Mutex, wg *sync.WaitGroup) { defer wg.Done() log.Println(req.h, req.argv.Elem()) req.replyv = reflect.ValueOf(fmt.Sprintf("geerpc resp %d", req.h.Seq)) server.sendResponse(cc, req.h, req.replyv.Interface(), sending) }
|