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


Golang Message.Encode方法代码示例

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


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

示例1: Dispatch2

func (this *CacheComponent2) Dispatch2(msg *protocol.Message, flag bool) {
	cached := uint16(1 << 15)
	if (msg.Number & cached) != cached {
		return
	}
	msg.Number &= HEAD_RESET

	msg, err := this.MakeCacheMessage(msg)
	if err != nil {
		fmt.Println("CacheCompoennt Dispatch2 Error:" + err.Error())
		return
	}
	data1, err := msg.Encode()
	if err != nil {
		fmt.Println("CacheCompoennt Dispatch2 Error:", err)
		return
	}
	data2 := make([]byte, len(data1)+8)
	copy(data2[:8], msg.Data[0:8])
	copy(data2[8:], data1)

	snode_component.GetNodeComponent().Forward(stree_define.ADD_CACHE_ENTRY, msg.DestUuid, msg.DeviceId, data2)
	//if this message is a contact sync message, send it to root first.
	if (msg.Number == protocol.N_ACCOUNT_SYS || msg.DeviceId == 0) && flag {
		snode_component.GetNodeComponent().Forward(stree_define.BROADCAST_TO_CLIENT, msg.DestUuid, msg.DeviceId, data1)
	} else {
		GetDispatcherOutComponent().NPushBack(msg, flag)
	}
	return
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:30,代码来源:cache_component2.go

示例2: sendToClient2

func (this *DispatcherOutComponent) sendToClient2(msg *protocol.Message, forward bool, buffPool *buffpool.BuffPool) error {
	if msg == nil || buffPool == nil {
		err := errors.New("DispatcherOutComponent.sendToClient2() Error: invalid argument")
		fmt.Println(err.Error())
		return err
	}

	clientConnArr := make([]*model.Client, 0)
	if msg.Token > 0 {
		clientConn := buffPool.GetClient(msg.Number, msg.DestUuid, msg.DeviceId, msg.Token)
		if clientConn != nil && clientConn.Conn != nil {
			clientConnArr = append(clientConnArr, clientConn)
		}
		if (clientConnArr == nil || len(clientConnArr) <= 0) && forward && GetRunWithSnode().Flag {
			buf, err := msg.Encode()
			if err != nil {
				fmt.Println("DispatcherOutComponent.sendToClient2() Error: " + err.Error())
				fmt.Println("\n", time.Now().String(), "Not find user, not forward due to encode failed.", "\n")
				return err
			}
			snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf)
			return nil
		}
	} else if msg.Token == 0 && msg.DeviceId > 0 {
		clientConnArr = buffPool.QueryForDeviceId(msg.Number, msg.DestUuid, msg.DeviceId)
		if (clientConnArr == nil || len(clientConnArr) <= 0) && forward && GetRunWithSnode().Flag {
			buf, err := msg.Encode()
			if err != nil {
				fmt.Println("DispatcherOutComponent.sendToClient2() Error:" + err.Error())
				fmt.Println("\n", time.Now().String(), "Not find user, not forward due to encode failed.", "\n")
				return err
			}
			snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf)
			return nil
		}
	} else if msg.Token == 0 && msg.DeviceId == 0 {
		clientConnArr = buffPool.QueryForUuid(msg.Number, msg.DestUuid)
	}

	err := errors.New("DispatcherOutComponent.sendToClient2() Error: the client's connection not pass authorization yet")
	for _, client := range clientConnArr {
		if client == nil || client.Conn == nil {
			continue
		}
		if !this.checkConnActivity(client.Time, client, buffPool, msg.Number, msg.DestUuid, msg.DeviceId, msg.Token) {
			err := errors.New("DispatcherOutComponent.sendToClient2() Error: client's connection is not alive")
			fmt.Println(err.Error())
			continue
		}
		err2 := this.sendMessage(msg, client.Conn)
		if err2 != nil {
			err2 = errors.New("DispatcherOutComponent.sendToClient2() Error: can not send message to client's connection")
			fmt.Println(err.Error())
			continue
		}
	}
	return nil
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:58,代码来源:dispatcher_out_component.go

示例3: handleMTPublish

func (this *DispatcherComponent) handleMTPublish(msg *protocol.Message, flag bool, buffPool *buffpool.BuffPool) error {
	if msg.DeviceId == 0 && flag && GetRunWithSnode().Flag {
		buf, err := msg.Encode()
		if err != nil {
			err = errors.New("DispatcherComponnet.handleMTPublish() Error: " + err.Error())
			fmt.Println(err.Error())
			return err
		}
		snode_component.GetNodeComponent().Forward(stree_define.BROADCAST_TO_CLIENT, msg.DestUuid, msg.DeviceId, buf)
		return nil
	} else {
		GetDispatcherOutComponent().NPushBack(msg, flag)
	}
	return nil
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:15,代码来源:dispatcher_component.go

示例4: sendResponse

func sendResponse(conn *net.Conn, message *protocol.Message) error {
	if conn == nil || message == nil {
		err := errors.New("RegisterComponent sendResponse Error : the argument is invalid")
		return err
	}
	buffer, err := message.Encode()
	if err != nil {
		err = fmt.Errorf("RegisterComponent sendResposne Error: %v", err)
		return err
	}
	index := 0
	for index < len(buffer) {
		n, err := (*conn).Write(buffer[index:])
		index += n
		if index < len(buffer) && err != nil {
			return err
		}
	}
	return nil
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:20,代码来源:register_component.go

示例5: sendMessage

func (this *DispatcherOutComponent) sendMessage(msg *protocol.Message, conn *net.Conn) error {
	if msg == nil || conn == nil {
		err := errors.New("DispatcherOutComponent sendMessage() Error: invalid argument")
		fmt.Println(err.Error())
		return err
	}

	buf, err := msg.Encode()
	if err != nil {
		err = errors.New("DispatcherOutComponent sendMessage() Error: invalid argument " + err.Error())
		fmt.Println(err.Error())
		return err
	}

	n, err := (*conn).Write(buf)
	if err != nil {
		fmt.Println("\n", time.Now().String(), " Write buf to connection failed, the connection will be closed, n = ", n, err, "\n")
		(*conn).Close()
	}
	return nil
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:21,代码来源:dispatcher_out_component.go

示例6: sendToService2

func (this *DispatcherOutComponent) sendToService2(msg *protocol.Message, forward bool, buffPool *buffpool.BuffPool) (*net.Conn, error) {
	if msg == nil || buffPool == nil {
		err := errors.New("DispatcherOutComponent.sendToService() Error: invalid argument")
		fmt.Println(err.Error())
		return nil, err
	}
	var conn *net.Conn
	if msg.DestUuid == P_UID {
		conn = buffPool.GetService(msg.Number)
	} else {
		conn = buffPool.QueryService(msg.Number, msg.DestUuid)
		if conn == nil && forward && GetRunWithSnode().Flag {
			buf, err := msg.Encode()
			if err != nil {
				fmt.Println(err)
				return nil, err
			}
			snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf)
			return nil, nil
		}
	}

	if conn == nil {
		err := errors.New("DispatcherOutComponent.sendToService() Error:  can not find dest server")
		fmt.Println(err.Error())
		return nil, err
	}

	err := this.sendMessage(msg, conn)
	if err != nil {
		err = errors.New("DispatcherOutComponent.sendToService() Error: can not send message to server's connection")
		fmt.Println(err.Error())
		return conn, err
	}
	if msg.MsgType == protocol.MT_PUBLISH {
		fmt.Println("\n", time.Now().String(), "MT_PUBLISH: sendToService2 Directly OK", "\n")
	}
	return conn, nil
}
开发者ID:huayuxian,项目名称:im-server-go,代码行数:39,代码来源:dispatcher_out_component.go


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