// CodecDecodeSelf implements codec.Selfer func (n *NodeSet) CodecDecodeSelf(decoder *codec.Decoder) { in := []Node{} if err := decoder.Decode(&in); err != nil { return } *n = NodeSet{}.fromIntermediate(in) }
// CodecDecodeSelf implements codec.Selfer func (n *PluginSpecs) CodecDecodeSelf(decoder *codec.Decoder) { in := []PluginSpec{} if err := decoder.Decode(&in); err != nil { return } *n = PluginSpecs{}.fromIntermediate(in) }
// CodecDecodeSelf implements codec.Selfer func (c *Counters) CodecDecodeSelf(decoder *codec.Decoder) { in := map[string]int{} if err := decoder.Decode(&in); err != nil { return } *c = Counters{}.fromIntermediate(in) }
// CodecDecodeSelf implements codec.Selfer func (m *Metric) CodecDecodeSelf(decoder *codec.Decoder) { in := WireMetrics{} if err := decoder.Decode(&in); err != nil { return } *m = in.FromIntermediate() }
// CodecDecodeSelf implements codec.Selfer func (m *LatestMap) CodecDecodeSelf(decoder *codec.Decoder) { in := map[string]LatestEntry{} if err := decoder.Decode(&in); err != nil { return } *m = LatestMap{}.fromIntermediate(in) }
// CodecDecodeSelf implements codec.Selfer func (s *Sets) CodecDecodeSelf(decoder *codec.Decoder) { in := map[string]StringSet{} if err := decoder.Decode(&in); err != nil { return } *s = Sets{}.fromIntermediate(in) }
// readUserMsg is used to decode a UserMsg from a TCP stream func (m *Memberlist) readUserMsg(bufConn io.Reader, dec *codec.Decoder) error { // Read the user message header var header userMsgHeader if err := dec.Decode(&header); err != nil { return err } // Read the user message into a buffer var userBuf []byte if header.UserMsgLen > 0 { userBuf = make([]byte, header.UserMsgLen) bytes, err := io.ReadAtLeast(bufConn, userBuf, header.UserMsgLen) if err == nil && bytes != header.UserMsgLen { err = fmt.Errorf( "Failed to read full user message (%d / %d)", bytes, header.UserMsgLen) } if err != nil { return err } d := m.config.Delegate if d != nil { d.NotifyMsg(userBuf) } } return nil }
// CodecDecodeSelf implements codec.Selfer func (c *EdgeMetadatas) CodecDecodeSelf(decoder *codec.Decoder) { in := map[string]EdgeMetadata{} if err := decoder.Decode(&in); err != nil { return } *c = EdgeMetadatas{}.fromIntermediate(in) }
// CodecDecodeSelf is part of the codec.Selfer interface. It is overwritten here to make sure // that an empty verbs list is not decoded as nil. On the other hand, an undefined verbs list // will lead to nil because this decoding for Verbs is not invoked. // // TODO(sttts): this is due to a ugorji regression: https://github.com/ugorji/go/issues/119. Remove the // workaround when the regression is fixed. func (vs *Verbs) CodecDecodeSelf(decoder *codec.Decoder) { m := []string{} decoder.Decode(&m) if len(m) == 0 { *vs = []string{} } else { *vs = m } }
// CodecDecodeSelf implements codec.Selfer func (nc *NodeControls) CodecDecodeSelf(decoder *codec.Decoder) { in := WireNodeControls{} if err := decoder.Decode(&in); err != nil { return } *nc = NodeControls{ Timestamp: parseTime(in.Timestamp), Controls: in.Controls, } }
// CodecDecodeSelf implements codec.Selfer func (c *ControlInstance) CodecDecodeSelf(decoder *codec.Decoder) { var in wiredControlInstance decoder.Decode(&in) *c = ControlInstance{ ProbeID: in.ProbeID, NodeID: in.NodeID, Control: report.Control{ ID: in.ID, Human: in.Human, Icon: in.Icon, Rank: in.Rank, }, } }
// Deserialize is used to deserialize the time table // and restore the state func (t *TimeTable) Deserialize(dec *codec.Decoder) error { // Decode the table var table []TimeTableEntry if err := dec.Decode(&table); err != nil { return err } // Witness from oldest to newest n := len(table) for i := n - 1; i >= 0; i-- { t.Witness(table[i].Index, table[i].Time) } return nil }
func main() { log.Println("MsgPack test begin==>") bookNode := Book{"web-founding", 36.5} for i := 0; i < 200; i++ { bookList = append(bookList, bookNode) } log.Println("json test=>begin") jbook, err := json.Marshal(bookList) if err != nil { log.Println("json failed=>", err) return } log.Println("length[", len(jbook), "]") fh, _ := os.OpenFile("json.txt", os.O_CREATE|os.O_WRONLY, 0666) nlen, _ := fh.Write(jbook) log.Println("acive write byte length[", nlen, "]") fh.Close() log.Println("json test=>end") log.Println("==================================") log.Println("msgpack test=>begin") log.Println("msgpack encoding...") var h codec.Handle = new(codec.MsgpackHandle) var buf []byte var enc *codec.Encoder = codec.NewEncoderBytes(&buf, h) err = enc.Encode(bookList) if err != nil { log.Println("msgpack encode err:", err) return } // hlen := hex.Encode(dst, buf) log.Println("buf:=>", len(hex.EncodeToString(buf))) fmsg, _ := os.OpenFile("msg.txt", os.O_CREATE|os.O_WRONLY, 0666) nlen, _ = fmsg.Write(buf) log.Println("acive write byte length[", nlen, "]") fmsg.Close() log.Println("msgpack decoding...") var dec *codec.Decoder = codec.NewDecoderBytes(buf, h) v2 := new([]Book) log.Println("src data len=>", len(buf)) err = dec.Decode(v2) if err != nil { log.Println("msgpack decode err:", err) return } // log.Println("decode=>", *v2) log.Println("msgpack test=>end") log.Println("MsgPack test end==>") }
// readRemoteState is used to read the remote state from a connection func (m *Memberlist) readRemoteState(bufConn io.Reader, dec *codec.Decoder) (bool, []pushNodeState, []byte, error) { // Read the push/pull header var header pushPullHeader if err := dec.Decode(&header); err != nil { return false, nil, nil, err } // Allocate space for the transfer remoteNodes := make([]pushNodeState, header.Nodes) // Try to decode all the states for i := 0; i < header.Nodes; i++ { if err := dec.Decode(&remoteNodes[i]); err != nil { return false, nil, nil, err } } // Read the remote user state into a buffer var userBuf []byte if header.UserStateLen > 0 { userBuf = make([]byte, header.UserStateLen) bytes, err := io.ReadAtLeast(bufConn, userBuf, header.UserStateLen) if err == nil && bytes != header.UserStateLen { err = fmt.Errorf( "Failed to read full user state (%d / %d)", bytes, header.UserStateLen) } if err != nil { return false, nil, nil, err } } // For proto versions < 2, there is no port provided. Mask old // behavior by using the configured port for idx := range remoteNodes { if m.ProtocolVersion() < 2 || remoteNodes[idx].Port == 0 { remoteNodes[idx].Port = uint16(m.config.BindPort) } } return header.Join, remoteNodes, userBuf, nil }
// CodecDecodeSelf implements codec.Selfer func (m *MetricRow) CodecDecodeSelf(decoder *codec.Decoder) { var in wiredMetricRow decoder.Decode(&in) w := WireMetrics{ Samples: in.Samples, Min: in.Min, Max: in.Max, First: in.First, Last: in.Last, } metric := w.FromIntermediate() *m = MetricRow{ ID: in.ID, Label: in.Label, Format: in.Format, Group: in.Group, Value: in.Value, Priority: in.Priority, Metric: &metric, } }
// CodecDecodeSelf for ugorji.go codec package @todo write test ... not sure if ok func (n *NullFloat64) CodecDecodeSelf(d *codec.Decoder) { if err := d.Decode(&n.Float64); err != nil { PkgLog.Debug("dbr.NullFloat64.CodecDecodeSelf", "err", err, "n", n) } }
// handleCommand is used to decode and dispatch a single command func (n *NetworkTransport) handleCommand(r *bufio.Reader, dec *codec.Decoder, enc *codec.Encoder) error { // Get the rpc type rpcType, err := r.ReadByte() if err != nil { return err } // Create the RPC object respCh := make(chan RPCResponse, 1) rpc := RPC{ RespChan: respCh, } // Decode the command switch rpcType { case rpcAppendEntries: var req AppendEntriesRequest if err := dec.Decode(&req); err != nil { return err } rpc.Command = &req case rpcRequestVote: var req RequestVoteRequest if err := dec.Decode(&req); err != nil { return err } rpc.Command = &req case rpcInstallSnapshot: var req InstallSnapshotRequest if err := dec.Decode(&req); err != nil { return err } rpc.Command = &req rpc.Reader = io.LimitReader(r, req.Size) default: return fmt.Errorf("unknown rpc type %d", rpcType) } // Dispatch the RPC select { case n.consumeCh <- rpc: case <-n.shutdownCh: return ErrTransportShutdown } // Wait for response select { case resp := <-respCh: // Send the error first respErr := "" if resp.Error != nil { respErr = resp.Error.Error() } if err := enc.Encode(respErr); err != nil { return err } // Send the response if err := enc.Encode(resp.Response); err != nil { return err } case <-n.shutdownCh: return ErrTransportShutdown } return nil }
func (l *ListLink) Decode(p []byte, ctx *Context) error { var dec *codec.Decoder = codec.NewDecoderBytes(p, ctx.mh) err := dec.Decode(l) return err }
func (cm ChairMessage) DecodeFrom(dec *codec.Decoder) error { return dec.Decode(&cm) }
// CodecDecodeSelf for ugorji.go codec package @todo write test ... not sure if ok func (n *NullFloat64) CodecDecodeSelf(d *codec.Decoder) { if err := d.Decode(&n.Float64); err != nil { log.Error("dbr.NullFloat64.CodecEncodeSelf", "err", err, "n", n) } }
// CodecDecodeSelf for ugorji.go codec package @todo write test ... not sure if ok func (n *NullBool) CodecDecodeSelf(d *codec.Decoder) { if err := d.Decode(&n.Bool); err != nil { log.Error("dbr.NullBool.CodecEncodeSelf", "err", err, "n", n) } }
// CodecDecodeSelf for ugorji.go codec package @todo write test ... not sure if ok func (n *NullTime) CodecDecodeSelf(d *codec.Decoder) { if err := d.Decode(&n.Time); err != nil { PkgLog.Debug("dbr.NullTime.CodecDecodeSelf", "err", err, "n", n) } }
// CodecDecodeSelf for ugorji.go codec package @todo write test ... not sure if ok func (n *NullString) CodecDecodeSelf(d *codec.Decoder) { if err := d.Decode(&n.String); err != nil { PkgLog.Debug("dbr.NullString.CodecDecodeSelf", "err", err, "n", n) } // think about empty string and Valid value ... }