duandgfh0506 2015-09-05 09:00
浏览 44
已采纳

使用JSON-RPC编组数据时出现bug-我是傻瓜吗?

So I'm working with code similar to the following and can't get the RPC to work in the FooHolder where it holds objects that meets an interface. I might be missing something but it looks like it should work - using the reflect package it correctly identifies the types but there's something wrong when the function returns....

package main

import (
    "log"
    "net"
    "net/rpc"
    "net/rpc/jsonrpc"
    "reflect"
)

type FooInterface interface {
    DoTheBartman(in *string, out *string) error
    DoThis(in *[]string, out *[]string) error
    NoProblems(in *int, out *int) error
}

type Foo struct {
    wibble string
}

func (f *Foo) DoTheBartman(in *string, out *string) error {
    log.Println("do the bartman")
    *out = "^^[ " + *in + "]^^"
    return nil
}

func (f *Foo) DoThis(in *[]string, out *[]string) error {
    log.Println("Doing this")
    log.Println("Type in - ", reflect.TypeOf(in))
    log.Println("Type *in - ", reflect.TypeOf(*in))
    log.Println("Type out - ", reflect.TypeOf(out))
    log.Println("Type *out - ", reflect.TypeOf(*out))

    *out = append(*in, "Hello")

    return nil
}

type FooHolder struct {
    TheFoos []FooInterface
}

func (fh *FooHolder) GetFoos(in *[]Foo, out *[]Foo) error {
    log.Println("Getting Foos...")
    log.Println("Type in - ", reflect.TypeOf(in))
    log.Println("Type *in - ", reflect.TypeOf(*in))
    log.Println("Type out - ", reflect.TypeOf(out))
    log.Println("Type *out - ", reflect.TypeOf(*out))

    return nil // This fails but with a nil... what's up with that.
}

^^ This is the function that fails, not sure why? It returns a nil (and it's reported as a nil but errors out with a exit 1

func (f *Foo) NoProblems(in *int, out *int) error {
    log.Println("No Problems doing maths")
    log.Println("Type in - ", reflect.TypeOf(in))
    log.Println("Type *in - ", reflect.TypeOf(*in))
    log.Println("Type out - ", reflect.TypeOf(out))
    log.Println("Type *out - ", reflect.TypeOf(*out))

    *out = 42 - (*in)
    return nil
}

// This all works just fine
func startServer(f FooInterface) {
    server := rpc.NewServer()
    server.Register(f)

    l, e := net.Listen("tcp", ":8222")
    if e != nil {
        log.Fatal("listen error:", e)
    }

    for {
        conn, err := l.Accept()
        if err != nil {
            log.Fatal(err)
        }
        log.Println("Incoming!")

        go server.ServeCodec(jsonrpc.NewServerCodec(conn))
    }
}

// It starts to go wrong here...
func startFooHolderServer(f *FooHolder) {
    server := rpc.NewServer()
    server.Register(f)

    l, e := net.Listen("tcp", ":8222")
    if e != nil {
        log.Fatal("listen error:", e)
    }

    for {
        conn, err := l.Accept()
        if err != nil {
            log.Fatal(err)
        }
        log.Println("Incoming!")

        go server.ServeCodec(jsonrpc.NewServerCodec(conn))
    }
}

func main() {

    foo1 := &Foo{}

    fooHolder := &FooHolder{}

    fooHolder.TheFoos = append(fooHolder.TheFoos, foo1)

    // go startServer(foo1)
    go startFooHolderServer(fooHolder)

    conn, err := net.Dial("tcp", "localhost:8222")

    if err != nil {
        panic(err)
    }
    defer conn.Close()

    c := jsonrpc.NewClient(conn)

    var foo []Foo
    log.Println("Type - ", reflect.TypeOf(foo))
    log.Println("Type - ", reflect.TypeOf(&foo))
    err = c.Call("FooHolder.GetFoos", foo, &foo)
    if err != nil {
        log.Fatal("RPC error:", err)
    }

    // No Problems?
    var baz int
    err = c.Call("Foo.NoProblems", baz, &baz)
    if err != nil {
        log.Fatal("RPC error:", err)
    }
    log.Println("Yay - baz is now", baz)

    stringArg := "Put stuff around me"
    err = c.Call("Foo.DoTheBartman", stringArg, &stringArg)
    if err != nil {
        log.Fatal("RPC error:", err)
    }
    log.Println("Yay - stringArg is now", stringArg)

    // Also No Problem
    bar := append([]string{"Hello"}, stringArg)
    log.Println("Type - ", reflect.TypeOf(bar))
    err = c.Call("Foo.DoThis", bar, &bar)
    if err != nil {
        log.Fatal("RPC error:", err)
    }
    log.Println("Yay - bar is now", bar)
}

Output:

rpc-demo $ go run demo.go 
2015/09/05 20:48:32 Incoming!
2015/09/05 20:48:32 Type -  []main.Foo
2015/09/05 20:48:32 Type -  *[]main.Foo
2015/09/05 20:48:32 Getting Foos...
2015/09/05 20:48:32 Type in -  *[]main.Foo
2015/09/05 20:48:32 Type *in -  []main.Foo
2015/09/05 20:48:32 Type out -  *[]main.Foo
2015/09/05 20:48:32 Type *out -  []main.Foo
2015/09/05 20:48:32 RPC error:invalid error <nil>
exit status 1
rpc-demo $ 

Has anyone experienced similar - it doesn't help if I declare a new type and throw that around either and I'm not seeing a way forward.

Tangentially, but possibly related, I compiled a Go app to archive and built it into a C program before changing the interface to match a Foo rather than a Foo interface and that worked! Weird.

  • 写回答

1条回答 默认 最新

  • dongqun5769 2015-09-05 10:24
    关注

    Here is the line throwing that error : https://github.com/golang/go/blob/master/src/net/rpc/jsonrpc/client.go#L89

    It is client-side, and you can see it is reached if c.resp.Error != nil || c.resp.Result == nil.

    Now you don't return any error, so the problem is not here.

    I believe the problem is that you do not set any result. GetFoos leaves the out pointer nil. Set out to a non-nil value and it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波