Exemplo n.º 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)
	}
}
Exemplo n.º 2
0
func (this *DaughterHandler) ReceiveFromDaughter(conn *net.Conn) {
	for {
		//PACKET_HEAD_LEN = 19
		headBuf := make([]byte, define.PACKET_HEAD_LEN)
		//MAX_BUF = 2048
		data := make([]byte, define.MAX_BUF)
		//读取头部信息
		_, err := (*conn).Read(headBuf)
		if err != nil && err != io.EOF {
			fmt.Println(err.Error())
			continue
		} else if err == io.EOF {
			fmt.Println("\n", time.Now().String(), " In DaughterHander.ReceiverFromDaughter, connection ", (*conn).RemoteAddr(), " is invalid", "\n")
			(*conn).Close()
			return
		}
		head := structure.Head{}
		err = head.Decode(headBuf)
		if err != nil {
			err = errors.New("component: STree.Receive failed, can not decode the head buf, " + err.Error())
			fmt.Println(err.Error())
			continue
		}
		if head.DataLen > 0 {
			if int(head.DataLen) > len(data) {
				data = make([]byte, head.DataLen)
			}
			//从conn中读取Data[]部分
			_, err = (*conn).Read(data[:head.DataLen])
			if err != nil && err != io.EOF {
				fmt.Println("Got a error ", err)
				continue
			} else if err == io.EOF {
				fmt.Println("\n", time.Now().String(), " In DaughterHander.ReceiverFromDaughter, connection ", (*conn).RemoteAddr(), " is invalid", "\n")
				(*conn).Close()
				return
			}
		}

		packet := &structure.Packet{head, data[:head.DataLen]}
		if packet.Number == define.FORWARD {
			msg := push_protocol.Message{}
			err = msg.Decode(packet.Data)
			if err != nil {
				fmt.Println("MSG Decode Failed, ", err)
				return
			}
			fmt.Println("\n", time.Now().String(), " ReceiveFromDaughter, packet.Uuid: ", packet.Uuid, " msg.UserId: ", msg.UserId, " msg.DestId: ", msg.DestId, "\n")
		}
		this.Dispatch(packet, conn)
	}
}
Exemplo n.º 3
0
func (this *ParentHandler) ReceiveFromParent() {
	for {
		//MAX_BUF = 2048
		data := make([]byte, define.MAX_BUF)
		//PACKET_HEAD_LEN = 19
		headBuf := make([]byte, define.PACKET_HEAD_LEN)

		if this.masterConn == nil {
			time.Sleep(30 * time.Second)
			continue
		}
		//从10.0.0.11:11002上的tcp连接中读取head信息并填充到headbuf
		_, err := (*this.masterConn).Read(headBuf)
		if err != nil && err != io.EOF {
			continue
		} else if err == io.EOF {
			return
		}
		head := structure.Head{}
		err = head.Decode(headBuf)
		if err != nil {
			err = errors.New("component: ReceiveFromParent failed, can not decode the head buf, " + err.Error())
			fmt.Println(err.Error())
			continue
		}
		if head.DataLen > 0 {
			if int(head.DataLen) > len(data) {
				data = make([]byte, head.DataLen)
			}
			//从10.0.0.11:11002上的tcp连接中读取Data部分的数据
			_, err = (*this.masterConn).Read(data[:head.DataLen])
			if err != nil && err != io.EOF {
				continue
			} else if err == io.EOF {
				return
			}
		}

		packet := structure.Packet{head, data[:head.DataLen]}

		this.dispatch(&packet)
	}
}