コード例 #1
0
func (this *NodeComponent) ReceiveMsg() {

	defer func() {
		if err := recover(); err != nil {
			fmt.Println("NodeComponent Error: " + err.(error).Error())
		}
	}()

	headbuff := make([]byte, define.PACKET_HEAD_LEN)
	for {

		var head structure.Head
		var Packet structure.Packet
		//如果无连接,尝试重新建立tcp连接
		if this.StreeConn == nil {
			this.ReConnect()
			continue
		}
		//从tcp连接中先读取头部信息
		n, err := this.StreeConn.Read(headbuff)
		//如果读取出错,则尝试重新建立tcp连接
		if n == 0 || n != define.PACKET_HEAD_LEN || err != nil {
			fmt.Println("NodeComponent Error:Read STree msg")
			this.ReConnect()
			continue
		}

		err = head.Decode(headbuff)
		if err != nil {
			fmt.Println("NodeComponent Error: Decode Stree msg")
			return
		}

		databuff := make([]byte, head.DataLen)
		//再从连接中读取剩余数据
		n, err = this.StreeConn.Read(databuff)
		if n == 0 || n != int(head.DataLen) || err != nil {
			fmt.Println("NodeComponent Error: Read Stree Data")
			this.ReConnect()
			continue
		}

		Packet.Head = head
		Packet.Data = databuff
		msg := protocol.Message{}
		//Packet.Data至少44位
		err = msg.Decode(Packet.Data)
		if err != nil {
			fmt.Println("NodeComponent Error:MSG Decode Failed", err)
			return
		}

		this.ReceiveMsgHandle(&Packet)
	}
}
コード例 #2
0
func (this *ParentHandler) handleAllocServer(packet *structure.Packet) {
	//ALLOC_SERVER_RESPONSE = 0x0a
	packet.Number = define.ALLOC_SERVER_RESPONSE
	packet.Data = make([]byte, 6)
	//PushServerPort:9005
	binary.BigEndian.PutUint16(packet.Data[4:6], this.PushServerPort)
	//返回一个个字段都为空值的STree
	stree := GetSTree()
	if pConn, ok := stree.UserMap.UserMap[packet.Uuid]; ok {
		ip := net.ParseIP(strings.Split((*pConn).RemoteAddr().String(), ":")[0]).To4()
		copy(packet.Data[0:4], ip)
	} else {
		conn := stree.GetMinLoadConn()
		ip := net.ParseIP(strings.Split((*conn).RemoteAddr().String(), ":")[0]).To4()
		copy(packet.Data[0:4], ip)
	}
	this.SendToParent(packet)
}
コード例 #3
0
ファイル: stree.go プロジェクト: huayuxian/im-server-go
func (this *STree) SendHeartBeat() {
	for {
		packet := structure.Packet{}
		packet.Number = define.HEART_BEAT
		packet.Uuid = define.P_UID
		packet.DataLen = 12
		packet.Data = make([]byte, packet.DataLen)

		this.SelfInfo.Lock.RLock()
		userNumber := this.SelfInfo.UserNumber
		maxConn := this.SelfInfo.MaxConnNumber
		currConn := this.SelfInfo.CurrentConnNumber
		this.SelfInfo.Lock.RUnlock()

		binary.BigEndian.PutUint32(packet.Data[0:4], uint32(userNumber))
		binary.BigEndian.PutUint32(packet.Data[4:8], uint32(maxConn))
		binary.BigEndian.PutUint32(packet.Data[8:12], uint32(currConn))
		GetParentHandler().SendToParent(&packet)
		time.Sleep(time.Duration(this.Seconds * 1000 * 1000 * 1000))
	}
}
コード例 #4
0
func (this *ClientHandler) sendAllocReq(conn *net.Conn) (*structure.Packet, error) {
	buf := make([]byte, 19)
	_, err := (*conn).Read(buf)
	if err != nil {
		err = errors.New("component: sendAllocReq() failed, can not read a log in message from conn")
		fmt.Println(err.Error())
		(*conn).Close()
		return nil, err
	}
	req := structure.Packet{}
	err = req.Decode(buf)
	if err != nil {
		err = errors.New("component: sendAllReq() failed, can not get message from received buf")
		fmt.Println(err.Error())
		(*conn).Close()
		return nil, err
	}
	req.Number = define.ALLOC_SERVER
	req.DataLen = 0
	req.Data = nil
	GetDaughterHandler().PushToDaughter(&req)
	return &req, nil
}