func (self *httpHandler) ServeHTTP(c http.ResponseWriter, req *http.Request) { conn := &httpConnectionBroker{c, req.Body} codec := self.cFactory(conn) if err := rpc.ServeRequest(codec); err != nil { log.Printf("rpcwrap: %v", err) } }
func ListenRPC() { rpc.Register(NewWorker()) l, e := net.Listen("tcp", ":4200") if e != nil { log.Fatal("Error: listen 4200 error:", e) } go func() { for { conn, err := l.Accept() if err != nil { log.Print("Error: accept rpc connection", err.Error()) continue } go func(conn net.Conn) { buf := bufio.NewWriter(conn) srv := &gobServerCodec{ rwc: conn, dec: gob.NewDecoder(conn), enc: gob.NewEncoder(buf), encBuf: buf, } err = rpc.ServeRequest(srv) if err != nil { log.Print("Error: server rpc request", err.Error()) } srv.Close() }(conn) } }() }
func RPCServeWithCode(port string) (net.Listener, error) { rpc.Register(&RPC{}) lis, err := net.Listen("tcp", ":"+port) goutils.CheckErr(err) go func() { for { conn, err := lis.Accept() if err != nil { fmt.Println("Error: accept rpc connection", err.Error()) continue } go func(conn net.Conn) { buf := bufio.NewWriter(conn) srv := &gobServerCodec{ rwc: conn, dec: gob.NewDecoder(conn), enc: gob.NewEncoder(buf), encBuf: buf, } defer srv.Close() err = rpc.ServeRequest(srv) if err != nil { fmt.Println("Error: server rpc request", err.Error()) } }(conn) } }() return lis, nil }
func serveRequests(irwc io.ReadWriteCloser) { for { sc := &conn.ServerCodec{ Rwc: irwc, } rpc.ServeRequest(sc) } }
func serveIt(conn io.ReadWriteCloser) { for { srv := &scodec{ rwc: conn, ebuf: bufio.NewWriter(conn), } rpc.ServeRequest(srv) } }
func RPCServer(w http.ResponseWriter, req *http.Request) { h, buf, err := w.(http.Hijacker).Hijack() if err != nil { log.Print("ERR: ", err) } connected := "200 Connected to JSON-RPC" io.WriteString(h, "HTTP/1.0 "+connected+"\n\n") log.Print("Serving") codec := jsonrpc.NewServerCodec(combo{h, buf}) rpc.ServeRequest(codec) h.Close() }
func TestContextBugNoContextWithoutParams(t *testing.T) { cases := []struct { req string want interface{} }{ { `{"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx"}`, map[string]interface{}{ "Name": " ", "TCPRemoteAddr": "127.0.0.1", "HTTPRemoteAddr": "", }, }, { `{"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx","params":{}}`, map[string]interface{}{ "Name": " ", "TCPRemoteAddr": "127.0.0.1", "HTTPRemoteAddr": "", }, }, { `{"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx","params":{"Fname":"First","Lname":"Last"}}`, map[string]interface{}{ "Name": "First Last", "TCPRemoteAddr": "127.0.0.1", "HTTPRemoteAddr": "", }, }, } for _, v := range cases { buf := bytes.NewBufferString(v.req) rpc.ServeRequest(jsonrpc2.NewServerCodecContext( context.WithValue(context.Background(), remoteAddrContextKey, &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}), &bufReadWriteCloser{buf}, nil, )) var res map[string]interface{} err := json.Unmarshal(buf.Bytes(), &res) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(v.want, res["result"]) { t.Errorf("%q:\n\n\texp: %#v\n\n\tgot: %#v\n\n", v.req, v.want, res["result"]) } } }
func TestContextBatch(t *testing.T) { req := `[ {"jsonrpc":"2.0","id":0,"method":"CtxSvc.NameCtx","params":{}}, {"jsonrpc":"2.0","id":1,"method":"CtxSvc.NameCtx","params":{"Fname":"First","Lname":"Last"}} ]` want := []map[string]interface{}{ { "jsonrpc": "2.0", "id": 0.0, "result": map[string]interface{}{ "Name": " ", "TCPRemoteAddr": "127.0.0.1", "HTTPRemoteAddr": "", }, }, { "jsonrpc": "2.0", "id": 1.0, "result": map[string]interface{}{ "Name": "First Last", "TCPRemoteAddr": "127.0.0.1", "HTTPRemoteAddr": "", }, }, } buf := bytes.NewBufferString(req) rpc.ServeRequest(jsonrpc2.NewServerCodecContext( context.WithValue(context.Background(), remoteAddrContextKey, &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}), &bufReadWriteCloser{buf}, nil, )) var res []map[string]interface{} err := json.Unmarshal(buf.Bytes(), &res) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(want, res) { t.Errorf("%s:\n\n\texp: %#v\n\n\tgot: %#v\n\n", req, want, res) } }