GithubHelp home page GithubHelp logo

No way to force encode uint32 about go HOT 6 CLOSED

ugorji avatar ugorji commented on July 18, 2024
No way to force encode uint32

from go.

Comments (6)

ugorji avatar ugorji commented on July 18, 2024

Why are you trying to handle the RPC specification yourself? Go already provides a very capable net/rpc library which allows plugging in a specific codec (for encode and decode functions). My go/codec library includes support for this without you having to do the work.

    //RPC Server
    go func() {
        for {
            conn, err := listener.Accept()
            rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
            rpc.ServeCodec(rpcCodec)
        }
    }()

    //RPC Communication (client side)
    conn, err := net.Dial("tcp", "localhost:5555")
    rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
    client := rpc.NewClientWithCodec(rpcCodec)

If you want to force a specific "schema", then you need to use a specific "type" e.g. decode into *Request directly (not a variant of interface{}).

var r Request
err := dec.Decode(&r)

When you decode into an interface{}, you are asking the library to do what it considers best. We provide a few options (RawToString, map and slice type) as hints to it, but we drew the line there. Those options are clearly generally useful, with the understanding that numbers are always decoded schema-less into the largest sized numerical type (int64, uint64, float64). This is sort of in line with the philosophy of other standard packages (encoding/json, reflect, etc).

from go.

maxekman avatar maxekman commented on July 18, 2024

I did think about using that RPC format (and appreciate that it exists), but I consider msgpack-rpc a better choice in my application as it is a cross language RPC format based on msgpack. I also use 0mq for trasport atm which means I can't use net/rpc (which is tcp.)

The thing is that it expects it to be exactly as in the format spec, which unfortunately leaves me to put it into and take it out of the array myself. If I would do my own RPC format I could easily encode and decode a custom message struct as I see fit! I have tried that with your library and that is really smooth!

In the end I feel I still need to support msgpack-rpc, in which case I need to make sure a Go uint32 is encoded as msgpack uint32: 0xce (I think it was) and not 0xcc (as it got optimized to now.)

Hope you understand my dilemma.

from go.

ugorji avatar ugorji commented on July 18, 2024

This functionality is already provided.

There's codec.GoRpc (which implements the net/rpc communication protocol) and codec.MsgpackSpecRpc (which implements the msgpack defined spec rpc communication protocol, which you are now trying to re-implement).

http://godoc.org/github.com/ugorji/go/codec#MsgpackSpecRpc

In the example snippet I posted above, I used the MsgpackSpecRpc, which is what you need. The sample usage on the docs show this also.

The net/rpc library is flexible enough to allow us configure both the communication protocol and the encoding format so that integrating msgpack was seamless on both parts.

from go.

maxekman avatar maxekman commented on July 18, 2024

Oh, I missed that it was actually implementing the msgpack-rpc, I thought it was a Go specific rpc packed with msgpack. I'm not familiar with GoRpc so I didn't know it used a reader/writer.

I guess my task will instead be to create a 0mq reader/writer interface. Thanks for pointing that out. :)

from go.

maxekman avatar maxekman commented on July 18, 2024

For my purpose at the moment (internally in the application) I will use a custom message format, which will be easier to encode and decode, and almost the same size as a msgpack-rpc version. I will for sure use the built-in rpc version for the client API later, thanks again for pointing me in that direction. Here is the short message version:

type Message struct {
    Command string      `codec:"c"`
    Args    interface{} `codec:"a,omitempty"`
}

from go.

ugorji avatar ugorji commented on July 18, 2024

BTW,

RE: #12 (comment)

msgpack format strives to store numbers using the minimum number of bytes. Consequently, unsigned 0-255 will always be stored as msgpack uint8 (even if Go source was a uint64). Thus, a uint32 will not necessarily be encoded as 0xce. Remember that msgpack is used even by languages which don't have defined-precision types (e.g. python, ruby), and all implementations use the value to determine how to encode it.

Also, look at msgpack.go parseCustomHeader method at https://github.com/ugorji/go/blob/master/codec/msgpack.go#L673 to see how I parsed the request by hand. Had to do this, because we wanted to read the header byte-by-byte so as not to hang forever waiting for input which may never come (testing some corner cases turned this up - but I don't remember the details).

from go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.