Hi All, 

I am learning golang and in the process i am trying to decode the buffer 
sent from a C client to a structure in golang. 
however that fails , please guide me through the process. The communication 
uses UNIX domain socket, between a c program and a golang process.

C structure.
struct error_notify_msg_t {
        int type;

        char str[384];

        char name[64];

        char id[256];

        char ip[60];
}

Golang Structure.

type ErrorNotifyMsg struct {
        //Type ...
        Type uint32
        //Str ...
        Str [384]string
        //Name ...
        Name [64]string
        //ID ...
        ID [256]string
        //Ip ...
        IP [60]string
}

func ReadErrorMsg() {
        //var n int
        buf := make([]byte, 1024)
        for {

                n, err := ConnErr.Read(buf[:])
                if err != nil {
                        return
                }
                data := buf[0:n]
                Log.InfoS("DecodeErrorMsg", log.KV{"Bytes read :": n})
                msg, err := DecodeErrorMsg(data)
                if msg != nil {
                        ErrNotify <- *msg
                }
        }
}

var ourEnd = binary.LittleEndian

func DecodeErrorMsg(val []byte) ErrorNotifyMsg {
        ntfy := ErrorNotifyMsg{}
        buf := bytes.NewBuffer(val)
        buf.Next(1)
        _ = binary.Read(buf, ourEnd, &ntfy.Type)
        Log.InfoS("DecodeErrorMsg", log.KV{"ntfy.Type": ntfy.Type})
        buf.Next(384)
        _ = binary.Read(buf, ourEnd, &ntfy.Str)
        Log.InfoS("DecodeErrorMsg", log.KV{"ntfy.Str": ntfy.Str})
        buf.Next(64)
        _ = binary.Read(buf, ourEnd, &ntfy.Name)
        Log.InfoS("DecodeErrorMsg", log.KV{"ntfy.Name": ntfy.Name})
        buf.Next(256)
        _ = binary.Read(buf, ourEnd, &ntfy.ID)
        Log.InfoS("DecodeErrorMsg", log.KV{"ntfy.ID": ntfy.ID})
        buf.Next(60)
        _ = binary.Read(buf, ourEnd, &ntfy.IP)
        Log.InfoS("DecodeErrorMsg", log.KV{"ntfy.IP": ntfy.IP})
        return ntfy
}

Thanks,
Naveen

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to