func (this *CacheComponent2) MakeCacheMessage(msg *protocol.Message) (*protocol.Message, error) {
	data := make([]byte, 8+msg.DataLen)
	t := time.Now().UnixNano()
	binary.BigEndian.PutUint64(data[:8], uint64(t))
	msg.DataLen += 8
	msg.MsgType = protocol.MT_PUSH_WITH_RESPONSE
	copy(data[8:], msg.Data)
	msg.Data = data
	return msg, nil
}
示例#2
0
func (this *CacheComponent) Save(msg protocol.Message) error {
	this.CacheLock.Lock()
	defer this.CacheLock.Unlock()
	if this.Cache == nil {
		this.Cache = make(map[string]CacheDev, 0)
	}

	if _, ok := this.Cache[msg.DestUuid]; !ok {
		val := CacheDev{
			MDev: make(map[uint32]*CacheList, 0),
		}
		this.Cache[msg.DestUuid] = val
	}

	if _, ok := this.Cache[msg.DestUuid].MDev[msg.DeviceId]; !ok {
		val := &CacheList{
			List:  list.New(),
			Total: 0,
		}
		this.Cache[msg.DestUuid].MDev[msg.DeviceId] = val
	}

	this.Cache[msg.DestUuid].MDev[msg.DeviceId].Total++
	id := this.Cache[msg.DestUuid].MDev[msg.DeviceId].Total
	msg.DataLen = 4 + msg.DataLen
	buf := make([]byte, msg.DataLen)
	binary.BigEndian.PutUint32(buf[0:4], id)
	if len(msg.Data) > 0 {
		copy(buf[4:msg.DataLen], msg.Data)
	}
	msg.Data = buf

	msg.MsgType = protocol.MT_PUSH_WITH_RESPONSE
	msg.PushType = protocol.PT_PUSHSERVER
	msg.Number &= HEAD_RESET

	e := this.Cache[msg.DestUuid].MDev[msg.DeviceId].List.PushBack(CacheEntry{msg, id})
	if e == nil {
		err := errors.New("CacheComponent.Save() Error: can not save msg to list")
		fmt.Println(err.Error())
		return err
	}

	return nil
}