Ejemplo n.º 1
0
func ReadBytes(r io.Reader) []byte {
	var size uint16
	ext.AssertE(binary.Read(r, DEFAULT_BYTE_ORDER, &size))
	data := make([]byte, size)
	ext.AssertE(binary.Read(r, DEFAULT_BYTE_ORDER, data))
	ext.LogDebug("READ\t%d", size)
	return data
}
Ejemplo n.º 2
0
func (r *nodeReceiver) Run() {
	defer r.Stopped()
	defer r.Conn.Close()

	if RELEASE {
		ext.AssertE(r.Conn.SetReadDeadline(time.Now().Add(10 * time.Second)))
	}
	infoData := ReadBytes(r.Conn)

	var info nodeInfo
	GobDecode(infoData, &info)

	ext.Assert(info.Cookie == MyInfo().Cookie)
	ext.Assert(info.ID != MyInfo().ID)

	r.nodeInfo = info
	AddNode(r.ID, r.nodeInfo)
	ext.LogDebug("ESTABLISHED\tLOCAL\t%s\tREMOTE\t%s\tINFO\t%v", r.LocalAddr().String(), r.RemoteAddr().String(), r.nodeInfo)

	for !r.StopRequested() {
		if RELEASE {
			ext.AssertE(r.Conn.SetReadDeadline(time.Now().Add(time.Minute)))
		}
		data := ReadBytes(r.Conn)
		if r.Decompress != nil {
			data = r.Decompress(data)
		}

		buf := bytes.NewBuffer(data)

		dec := gob.NewDecoder(buf)

		var source, target PID
		var msgSize int
		ext.AssertE(dec.Decode(source))
		ext.AssertE(dec.Decode(target))
		ext.AssertE(dec.Decode(msgSize))

		msg := r.Decode(buf.Bytes())
		ext.Assert(msg != nil)

		Cast(source, target, msg)
	}
}
Ejemplo n.º 3
0
func WriteBytes(w io.Writer, data []byte) {
	ext.AssertE(binary.Write(w, DEFAULT_BYTE_ORDER, uint16(len(data))))
	ext.AssertE(binary.Write(w, DEFAULT_BYTE_ORDER, data))
	ext.LogDebug("WRITTEN\t%d", len(data))
}