Beispiel #1
0
func (m *Msg) Pack() []byte {
	buf := make([]byte, 0, 100)
	var n [8]byte

	if m.Op<Tmin || m.Op>=Tend {
		dbg.Fatal("unknown msg type %d", m.Op)
	}
	buf = append(buf, byte(m.Op))
	buf = nchan.PutString(buf, m.Rid)
	if m.Op==Tget || m.Op==Tput {
		binary.LittleEndian.PutUint64(n[0:], uint64(m.Off))
		buf = append(buf, n[:8]...)
	}
	if m.Op == Tget {
		binary.LittleEndian.PutUint64(n[0:], uint64(m.Count))
		buf = append(buf, n[:8]...)
	}
	if m.Op==Tput || m.Op==Tmkdir || m.Op==Twstat {
		buf = append(buf, m.D.Pack()...)
	}
	if m.Op == Tmove {
		buf = nchan.PutString(buf, m.To)
	}
	if m.Op==Tfind || m.Op==Tget || m.Op==Tput || m.Op==Tfindget {
		buf = nchan.PutString(buf, m.Pred)
	}
	if m.Op==Tfind || m.Op==Tfindget {
		buf = nchan.PutString(buf, m.Spref)
		buf = nchan.PutString(buf, m.Dpref)
		binary.LittleEndian.PutUint64(n[0:], uint64(m.Depth))
		buf = append(buf, n[:8]...)
	}
	return buf
}
Beispiel #2
0
/*
	Pack the dir into a []byte that could be used later with ParseDir to
	unpack the same dir.
*/
func (e Dir) Pack() []byte {
	buf := make([]byte, 0, 100)
	var hdr [4]byte
	buf = append(buf, hdr[:]...)
	for k, v := range e {
		buf = nchan.PutString(buf, k)
		buf = nchan.PutString(buf, v)
	}
	n := uint32(len(buf) - len(hdr))
	binary.LittleEndian.PutUint32(buf[0:], n)
	return buf
}