Esempio n. 1
0
func main() {
	mh := codec.MsgpackHandle{}
	mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
	buf := make([]byte, 10240)

	b := B{"hello", 3}
	msg_b := struct{ B B }{b}
	fmt.Printf("Send %#v\n", msg_b)

	enc := codec.NewEncoderBytes(&buf, &mh)
	err := enc.Encode(msg_b)
	if err != nil {
		log.Fatalf("Encode: %s", err)
	}

	var rcv interface{}
	dec := codec.NewDecoderBytes(buf, &mh)
	err = dec.Decode(&rcv)
	if err != nil {
		log.Fatalf("Decode: %s", err)
	}
	fmt.Printf("Raw Recv: %#v\n", rcv)

	var msg MSG
	dec = codec.NewDecoderBytes(buf, &mh)
	err = dec.Decode(&msg)
	if err != nil {
		log.Fatalf("Decode: %s", err)
	}
	fmt.Printf("Msg Recv: %#v\n", msg)
	fmt.Printf("Msg.A Recv: %#v\n", msg.A)
	fmt.Printf("Msg.B Recv: %#v\n", msg.B)
}
Esempio n. 2
0
// Puts a new value into the database. Handles encoding using msgPack. Putting nil deletes
// an existing key-value pair
func (db *DB) Put(key string, value interface{}) error {
	glog.V(2).Infoln("put", key, value)
	if value != nil {
		var data []byte
		e := codec.NewEncoderBytes(&data, &mh)
		err := e.Encode(value)
		if err != nil {
			return fmt.Errorf("database.Put encoding for key %s: %s", key, err.Error())
		}
		err = db.ldb.Put([]byte(key), data, nil)
		if err != nil {
			return fmt.Errorf("database.Put key %s: %s", key, err.Error())
		}
	} else {
		db.ldb.Delete([]byte(key), nil)
	}
	return nil
}
Esempio n. 3
0
// Send sends a message across the channel to a receiver on the
// other side of the transport.
func (c *channel) Send(message interface{}) error {
	if c.direction == inbound {
		return ErrWrongDirection
	}
	mCopy, mErr := c.copyMessage(message)
	if mErr != nil {
		return mErr
	}

	var buf []byte
	encoder := codec.NewEncoderBytes(&buf, c.session.handler)
	encodeErr := encoder.Encode(mCopy)
	if encodeErr != nil {
		return encodeErr
	}

	// TODO check length of buf
	_, writeErr := c.stream.Write(buf)
	if writeErr != nil {
		return writeErr
	}
	return nil
}