Пример #1
0
func (c *Client) Hello() {
	//dataPackage := pool.Get(DATA_PACKAGE).(*protocol.DataPackage)
	dataPackage := protocol.GetDataPackage()
	dataPackage.Head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE)
	dataPackage.Head.Command = protocol.CLIENT_CMD_HEARTBEAT
	buf := protocol.GetBuffer()
	binary.Write(buf, binary.BigEndian, dataPackage.Head)
	c.Conn.SetDeadline(time.Now().Add(NET_WRITE_DATELINE))
	if _, err := c.Conn.Write(buf.Bytes()); err != nil {
		syslog.Warn(err)
	}
	protocol.PutBuffer(buf)
	protocol.PutDataPackage(dataPackage)
}
Пример #2
0
func (this *Service) HandleMessage(message *nsq.Message) error {
	dp := protocol.GetDataPackage()
	if err := dp.Unpackage(message.Body); err != nil {
		syslog.Warn(err, string(message.Body))
		protocol.PutDataPackage(dp)
		return nil
	}
	this.RWMutex.RLock()
	client, ok := this.ClientMap[dp.Tail.SID]
	this.RWMutex.RUnlock()

	if !ok || client.Stat == CLIENT_STATE_OFFLINE || dp.Tail.SID != client.Sid {
		protocol.PutDataPackage(dp)
		return nil
	}
	if client.RespCh == nil {
		protocol.PutDataPackage(dp)
		return nil
	}
	dp.Data = client
	client.RespCh <- dp
	atomic.AddInt32(&client.MsgCount, 1)
	return nil
}
Пример #3
0
func (this *ClientGroup) Response() {
	var dp *protocol.DataPackage = nil
	var client *Client = nil
	for this.Exit == false {
		select {
		case dp = <-this.RespCh:
			client = dp.Data.(*Client)
			if err := client.Response(dp); err != nil {
				syslog.Warn(err)
			}
			atomic.AddInt32(&client.MsgCount, -1)
			protocol.PutDataPackage(dp)
		}
	}
}
Пример #4
0
func (h *DBHandler) HandleMessage(message *nsq.Message) error {
	var resp []byte
	var err error
	defer func() {
		if err != nil {
			syslog.Warn(err)
		}
	}()
	dataPackage := protocol.GetDataPackage()
	defer protocol.PutDataPackage(dataPackage)

	if err := dataPackage.Unpackage(message.Body); err != nil {
		return nil
	}
	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	syslog.Debug(*head, string(jsonStr), *tail)

	switch head.Command {
	case protocol.CLIENT_CMD_USER_REGISTER:
		resp = h.Register(head, jsonStr, tail)
	case protocol.CLIENT_CMD_USER_LOGIN:
		resp = h.Login(head, jsonStr, tail)
	default:
		syslog.Debug("invalid cmd:", head.Command, *tail)
		return nil
	}

	respBuf := protocol.GetBuffer()
	defer protocol.PutBuffer(respBuf)
	head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE + len(resp))
	binary.Write(respBuf, binary.BigEndian, head)
	binary.Write(respBuf, binary.BigEndian, resp)
	binary.Write(respBuf, binary.BigEndian, tail)
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	syslog.Debug2("db_server --> conn_server publish:", topic, head, string(resp), tail)
	return nil
}
Пример #5
0
func (c *Client) Request() bool {
	var reqBuf [2048]byte
	curTime := time.Now().Unix()
	c.Conn.SetDeadline(time.Now().Add(NET_READ_DATELINE))
	if _, err := io.ReadFull(c.Conn, reqBuf[:2]); err != nil {
		if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
			// 太长时间没有交互退出
			if c.Stat == CLIENT_STATE_INIT && c.LastTime+CONNECT_NO_LOGIN_TIMEOUT < curTime {
				syslog.Info(c.Uid, c.Sid, "no login timeout 30 s, lastTime:", c.LastTime, " nowTime:", curTime)
				return false
			} else if c.LastTime+CONNECT_NO_REQUEST_TIMEOUT < curTime {
				syslog.Info(c.Uid, c.Sid, "timeout 180 s, lastTime:", c.LastTime, " nowTime:", curTime)
				return false
			}
			return true
		}
		syslog.Info(c.Uid, c.Sid, "recv error:", err.Error())
		return false
	} else {
		headLen := binary.BigEndian.Uint16(reqBuf[0:2])
		if headLen > protocol.DATA_PACKAGE_MAX_SIZE {
			syslog.Info(c.Uid, c.Sid, "head len :", headLen, " too large", reqBuf[0], reqBuf[1])
			return true
		} else if headLen < protocol.DATA_PACKAGE_HEAD_SIZE {
			syslog.Info(c.Uid, c.Sid, "head len :", headLen, " too small", reqBuf[0], reqBuf[1])
			return true
		}
		c.Conn.SetDeadline(time.Now().Add(NET_READ_MAX_DATELINE))
		if _, err := io.ReadFull(c.Conn, reqBuf[2:headLen]); err != nil {
			syslog.Info(c.Uid, c.Sid, "recv error:", err)
			return false
		}
		dataPackage := protocol.GetDataPackage()
		defer protocol.PutDataPackage(dataPackage)
		if err := dataPackage.Unpackage(reqBuf[:headLen]); err != nil {
			syslog.Info(c.Uid, c.Sid, "DecPkgBody failed :", err)
			return false
		}
		head := dataPackage.Head
		syslog.Debug("client requst:", head)
		if head.Command != protocol.CLIENT_CMD_HEARTBEAT && head.Command != protocol.CLIENT_CMD_USER_LOGIN &&
			head.Command != protocol.CLIENT_CMD_USER_REGISTER && c.Stat != CLIENT_STATE_ONLINE {
			syslog.Info("no login no operate!!!", head)
			return false
		} else {
			c.LastTime = time.Now().Unix()
			dataPackage.Tail.IP = g_IP
			dataPackage.Tail.Port = g_Port
			dataPackage.Tail.SID = c.Sid
			dataPackage.Tail.UID = c.Uid
			dataPackage.Data = c
			if head.Command != protocol.CLIENT_CMD_HEARTBEAT {
				//派发消息到其它服务器处理
				err = c.Produce(dataPackage)
			} else {
				//回复心跳
				err = c.Response(dataPackage)
			}
			if err != nil {
				syslog.Info(err)
				return false
			}
		}
	}
	return true
}