示例#1
0
文件: mq_client.go 项目: allmad/madq
func (c *Client) writeLoop() {
	c.wg.Add(1)
	defer func() {
		c.wg.Done()
		c.Close()
	}()

	var (
		err    error
		putErr *rpc.PutError
		ctx    *rpc.Reply
		flag   []byte
		w      = bufio.NewWriter(c.conn)
		args   = make([]rpc.Item, 1)
	)

	for !c.state.IsClosed() {
		select {
		case err = <-c.errChan:
			if logex.Equal(err, io.EOF) {
				return
			}
			if err != nil {
				logex.Error(err)
			}
			args[0] = rpc.NewError(err)
			flag = rpc.FlagReply
		case putErr = <-c.putErrChan:
			if logex.Equal(putErr.Err, io.EOF) {
				return
			}
			if putErr.Err != nil {
				logex.Error(putErr)
			}
			args[0] = rpc.NewStruct(putErr)
			flag = rpc.FlagReply
		case ctx = <-c.incoming:
			args[0] = rpc.NewStruct(ctx)
			flag = rpc.FlagMsgPush
		case <-c.parentStopChan:
			return
		case <-c.stopChan:
			return
		}

		err = rpc.Write(w, flag, args)
		if err == nil {
			err = logex.Trace(w.Flush())
		}
		if err != nil {
			logex.Error(err)
			return
		}
	}
}
示例#2
0
文件: api.go 项目: allmad/madq
func (a *Ins) Put(topicName string, msgs []*message.Ins) (int, error) {
	var err rpc.PutError
	a.doReq(rpc.MPut, []rpc.Item{
		rpc.NewString(topicName),
		rpc.NewMsgs(msgs),
	}, rpc.NewStruct(&err))
	return err.N, err.Err
}
示例#3
0
文件: api.go 项目: allmad/madq
func (a *Ins) readLoop() {
	a.wg.Add(1)
	defer func() {
		a.wg.Done()
		a.Close()
	}()

	var (
		pt   byte
		err  error
		req  *Request
		item *list.Element
	)

	r := bufio.NewReader(a.conn)
	msgStruct := rpc.NewStruct(nil)

	for !a.state.IsClosed() {
		pt, err = r.ReadByte()
		if err != nil {
			if !logex.Equal(err, io.EOF) {
				logex.Error(err)
			}
			return
		}
		if pt == rpc.FlagMsgPush[0] {
			var reply rpc.Reply
			if err = msgStruct.Set(&reply).PSet(r); err != nil {
				logex.Error(err)
				continue
			}
			a.replyChan <- &reply
			continue
		}
		if pt != rpc.FlagReply[0] {
			logex.Error("unexpect packetType:", pt)
			return
		}
		a.Lock()
		item = a.reqQueue.Front()
		a.reqQueue.Remove(item)
		req = item.Value.(*Request)
		a.Unlock()
		if err = req.replyObj.PSet(r); err != nil {
			logex.Error(err)
			return
		}
		req.Reply <- struct{}{}
	}
	return
}