コード例 #1
0
ファイル: dispatcher.go プロジェクト: skycoin/skycoin
	n, e = encoder.DeserializeRawToValue(msg, v)
	return
}

// Packgs a Message into []byte containing length, id and data
var encodeMessage = func(msg Message) []byte {
	t := reflect.ValueOf(msg).Elem().Type()
	msgId, succ := MessageIdMap[t]
	if !succ {
		txt := "Attempted to serialize message struct not in MessageIdMap: %v"
		log.Panicf(txt, msg)
	}
	bMsg := encoder.Serialize(msg)

	// message length
	bLen := encoder.SerializeAtomic(uint32(len(bMsg) + len(msgId)))
	m := make([]byte, 0)
	m = append(m, bLen...)     // length prefix
	m = append(m, msgId[:]...) // message id
	m = append(m, bMsg...)     // message bytes
	return m
}

// Sends []byte over a net.Conn
var sendByteMessage = func(conn net.Conn, msg []byte,
	timeout time.Duration) error {
	deadline := time.Time{}
	if timeout != 0 {
		deadline = time.Now().Add(timeout)
	}
	if err := conn.SetWriteDeadline(deadline); err != nil {
コード例 #2
0
ファイル: pool.go プロジェクト: skycoin/skycoin
		conns[i] = c.Conn
		i++
	}
	return conns
}

// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.

// Sends []byte over a net.Conn
// Why does this set deadline every packet
var sendByteMessage = func(conn net.Conn, channel uint16, msg []byte,
	timeout time.Duration) error {

	//message length and channel id
	bLen := encoder.SerializeAtomic(uint32(len(msg)))
	chanByte := encoder.SerializeAtomic(uint16(channel))

	//log.Printf("len= %v blen= %v chans= %v \n", len(msg), len(bLen), len(chanByte))
	d := make([]byte, 0, len(msg)+6)
	d = append(d, bLen...)     //length prefix
	d = append(d, chanByte...) //channel id
	d = append(d, msg...)      //message data

	//log.Printf("len2= %v \n ", len(d))

	deadline := time.Time{}
	if timeout != 0 {
		deadline = time.Now().Add(timeout)
	}
	if err := conn.SetWriteDeadline(deadline); err != nil {