Beispiel #1
0
// writeMsg write msg to conn.
func (c *SeqChannel) writeMsg(key string, m *myrpc.Message) (err error) {
	var (
		oldMsg, msg, sendMsg []byte
	)
	// push message
	for e := c.conn.Front(); e != nil; e = e.Next() {
		conn, _ := e.Value.(*Connection)
		// if version empty then use old protocol
		if conn.Version == "" {
			if oldMsg == nil {
				if oldMsg, err = m.OldBytes(); err != nil {
					return
				}
			}
			sendMsg = oldMsg
		} else {
			if msg == nil {
				if msg, err = m.Bytes(); err != nil {
					return
				}
			}
			sendMsg = msg
		}
		// TODO use goroutine
		conn.Write(key, sendMsg)
	}
	return
}
Beispiel #2
0
// PushMsg implements the Channel PushMsg method.
func (c *SeqChannel) PushMsg(key string, m *myrpc.Message, expire uint) error {
	var (
		oldMsg, msg, sendMsg []byte
		err                  error
	)
	client := myrpc.MessageRPC.Get()
	if client == nil {
		return ErrMessageRPC
	}
	c.mutex.Lock()
	// private message need persistence
	// if message expired no need persistence, only send online message
	// rewrite message id
	m.MsgId = c.timeID.ID()
	if m.GroupId != myrpc.PublicGroupId && expire > 0 {
		args := &myrpc.MessageSavePrivateArgs{Key: key, Msg: m.Msg, MsgId: m.MsgId, Expire: expire}
		ret := 0
		if err = client.Call(myrpc.MessageServiceSavePrivate, args, &ret); err != nil {
			c.mutex.Unlock()
			glog.Errorf("%s(\"%s\", \"%v\", &ret) error(%v)", myrpc.MessageServiceSavePrivate, key, args, err)
			return err
		}
	}
	// push message
	for e := c.conn.Front(); e != nil; e = e.Next() {
		conn, _ := e.Value.(*Connection)
		// if version empty then use old protocol
		if conn.Version == "" {
			if oldMsg == nil {
				oldMsg, err = m.OldBytes()
				if err != nil {
					c.mutex.Unlock()
					return err
				}
			}
			sendMsg = oldMsg
		} else {
			if msg == nil {
				msg, err = m.Bytes()
				if err != nil {
					c.mutex.Unlock()
					return err
				}
			}
			sendMsg = msg
		}
		conn.Write(key, sendMsg)
	}
	c.mutex.Unlock()
	return nil
}
Beispiel #3
0
// PushMsg implements the Channel PushMsg method.
func (c *SeqChannel) PushMsg(key string, m *myrpc.Message, expire uint) (err error) {
	client := myrpc.MessageRPC.Get()
	if client == nil {
		return ErrMessageRPC
	}
	c.mutex.Lock()
	// private message need persistence
	// if message expired no need persistence, only send online message
	// rewrite message id
	//m.MsgId = c.timeID.ID()
	m.MsgId = id.Get()
	if m.GroupId != myrpc.PublicGroupId && expire > 0 {
		args := &myrpc.MessageSavePrivateArgs{Key: key, Msg: m.Msg, MsgId: m.MsgId, Expire: expire}
		ret := 0
		if err = client.Call(myrpc.MessageServiceSavePrivate, args, &ret); err != nil {
			c.mutex.Unlock()
			log.Error("%s(\"%s\", \"%v\", &ret) error(%v)", myrpc.MessageServiceSavePrivate, key, args, err)
			return
		}
	}
	// push message
	if err = c.writeMsg(key, m); err != nil {
		c.mutex.Unlock()
		log.Error("c.WriteMsg(\"%s\", m) error(%v)", key, err)
		return
	}
	c.mutex.Unlock()
	return
}