当前位置: 首页>>代码示例>>Golang>>正文


Golang Message.Decode方法代码示例

本文整理汇总了Golang中push-server/protocol.Message.Decode方法的典型用法代码示例。如果您正苦于以下问题:Golang Message.Decode方法的具体用法?Golang Message.Decode怎么用?Golang Message.Decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在push-server/protocol.Message的用法示例。


在下文中一共展示了Message.Decode方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ForwardPush

func (this *NodeComponent) ForwardPush(msg *structure.Packet) {
	m := protocol.Message{}
	err := m.Decode(msg.Data)
	if err != nil {
		fmt.Println("NodeComponent Error:MSG Decode Failed", err)
		return
	}
	this.ForwardMsgs <- msg
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:9,代码来源:node_component.go

示例2: ReceiveMsg

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)
	}
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:55,代码来源:node_component.go

示例3: ReceiveFromDaughter

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)
	}
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:52,代码来源:handle_daughter.go

示例4: NoCacheForward

func (this *NodeComponent) NoCacheForward(Uuid string, Data []byte) {
	Packet := GetPacket(define.FORWARD, Uuid, Data)

	buff, err := Packet.Encode()
	if err != nil {
		fmt.Println("NodeComponent Error:Encode No Cache Forward Packet")
		return
	}

	n, err := this.Write(buff)
	if n == 0 || err != nil {
		return
	}

	msg := protocol.Message{}
	err = msg.Decode(Packet.Data)
	if err != nil {
		fmt.Println("NodeComponent Error: MSG Decode Failed, ", err)
		return
	}
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:21,代码来源:node_component.go

示例5: SendToDaughter

func (this *DaughterHandler) SendToDaughter() {
	for {
		//	fmt.Println("\n", time.Now().String(), " SendToDaughter Directly", "\n")
		//        packet := <-this.toDaughter
		//从toDaughter中接收数据
		packet := this.ToDaughterPop()
		//返回一个*net.Conn
		conn := GetSTree().UserMap.Get(packet.Uuid)
		if conn == nil && packet.Number == define.ALLOC_SERVER {
			fmt.Println("\n", "Geta Min LoadConn ", "\n")
			conn = GetSTree().GetMinLoadConn()
		}
		if conn == nil {
			err := errors.New("component: STree.SendToDaughter() failed, can not find user in user's list")
			fmt.Println(err.Error())
			fmt.Println("\n", "destID = ", packet.Uuid, "\n")
			continue
		}
		buf, err := packet.Encode()
		if err != nil {
			err = errors.New("component: STree.SendToDaughter() failed, can not get buf from packet, " + err.Error())
			fmt.Println(err.Error())
			continue
		}
		//向conn中写入Packet的buf
		_, err = (*conn).Write(buf)
		if err != nil {
			fmt.Println("\n", time.Now().String(), " in SendToDaughter, Got an error ", err, "\n")
			fmt.Println(err.Error())
			continue
		}
		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(), " SendToDaughter Directly OK, packet.Uuid: ", packet.Uuid, " msg.UserId: ", msg.UserId, " msg.DestId: ", msg.DestId, "\n")
	}
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:40,代码来源:handle_daughter.go


注:本文中的push-server/protocol.Message.Decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。