/******************************************************************************
* 概述:     事件处理
* 函数名:    dealMonitorEvent
* 返回值:    error
* 参数列表:  参数名          参数类型      取值范围     描述
*            isReq          bool                     是否为req事件
*
*******************************************************************************/
func (this *ZmqSocket) dealMonitorEvent(s *zmq4.Socket, isReq bool) error {
	log4.Debug("start dealMonitorEvent...")
	for {
		a, b, c, err := s.RecvEvent(0)
		//if runtime.GOOS == "windows" && len(b) == 0 && c == 0 {
		//	log4.Debug("monitor eagan windows")
		//	return nil
		//}
		if err != nil {
			errno1 := zmq4.AsErrno(err)
			switch errno1 {
			case zmq4.Errno(syscall.EAGAIN):
				log4.Debug("monitor eagan ")
				return nil
			case zmq4.Errno(syscall.EINTR):
				log4.Debug("monitor EINTR")
				continue
			default:
				log4.Debug("zmq req Get err %v, %d!", errno1, errno1)
			}
		}
		if a == 0 {
			//			log4.Debug("monitor return")
			return nil
		}
		switch a {
		case zmq4.EVENT_CONNECTED:
			log4.Info("sub or req monitor event CONNECTED, url:%s %s", this.mreqUrl, this.mreqUrl)
			this.mchooseMutex.Lock()
			defer this.mchooseMutex.Unlock()
			if isReq {
				this.mreqOK = true
			} else {
				this.msubOK = true
			}
			if this.mstateChFlag && this.mreqOK && this.msubOK {
				this.mstateChFlag = false
				select {
				case this.mstateCh <- true:
				default:
				}
			}
			//			this.mok++
		case zmq4.EVENT_DISCONNECTED:
			log4.Error("sub or req monitor event DISCONNECTED, url:%s %s", this.mreqUrl, this.mreqUrl)
			this.mchooseMutex.Lock()
			defer this.mchooseMutex.Unlock()
			if isReq {
				this.mreqOK = false
			} else {
				this.msubOK = false
			}
			//			this.mok--
			if this.mChoose {
				this.mChoose = false
				topics := this.mzmq.MdataCache.GetSdsTopic()
				for i := 0; i < len(*topics); i++ {
					if err := this.msubSocket.SetUnsubscribe((*topics)[i]); err != nil {
						log4.Error("SetUnSubscribe(%s) falied, %s", (*topics)[i], err.Error())
					}
				}
				this.mzmq.Mevent.UpdateWorkSocket()
			}
		case zmq4.EVENT_CLOSED:
			//			this.mchooseMutex.Lock()
			//			this.mok
			//			if this.mChoose{
			//				this.mChoose = false
			//				this.mevent.UpdateWorkSocket()
			//			log4.Error("sdssdk zmqreq monitor event CLOSED", b, c)
			//			}
			//			this.mchooseMutex.UnLock()
		default:
			log4.Debug("zmqreq monitor unknow event err", a, b, c, err)
		}
	}
	return nil

}