當前位置: 首頁>>代碼示例>>Golang>>正文


Golang protocol.Message類代碼示例

本文整理匯總了Golang中push-server/protocol.Message的典型用法代碼示例。如果您正苦於以下問題:Golang Message類的具體用法?Golang Message怎麽用?Golang Message使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Message類的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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

示例4: MakeCacheMessage

func (this *CacheComponent2) MakeCacheMessage(msg *protocol.Message) (*protocol.Message, error) {
	data := make([]byte, 8+msg.DataLen)
	t := time.Now().UnixNano()
	binary.BigEndian.PutUint64(data[:8], uint64(t))
	msg.DataLen += 8
	msg.MsgType = protocol.MT_PUSH_WITH_RESPONSE
	copy(data[8:], msg.Data)
	msg.Data = data
	return msg, nil
}
開發者ID:huayuxian,項目名稱:im-server-go,代碼行數:10,代碼來源:cache_component2.go

示例5: 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

示例6: 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

示例7: 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

示例8: Save

func (this *CacheComponent) Save(msg protocol.Message) error {
	this.CacheLock.Lock()
	defer this.CacheLock.Unlock()
	if this.Cache == nil {
		this.Cache = make(map[string]CacheDev, 0)
	}

	if _, ok := this.Cache[msg.DestUuid]; !ok {
		val := CacheDev{
			MDev: make(map[uint32]*CacheList, 0),
		}
		this.Cache[msg.DestUuid] = val
	}

	if _, ok := this.Cache[msg.DestUuid].MDev[msg.DeviceId]; !ok {
		val := &CacheList{
			List:  list.New(),
			Total: 0,
		}
		this.Cache[msg.DestUuid].MDev[msg.DeviceId] = val
	}

	this.Cache[msg.DestUuid].MDev[msg.DeviceId].Total++
	id := this.Cache[msg.DestUuid].MDev[msg.DeviceId].Total
	msg.DataLen = 4 + msg.DataLen
	buf := make([]byte, msg.DataLen)
	binary.BigEndian.PutUint32(buf[0:4], id)
	if len(msg.Data) > 0 {
		copy(buf[4:msg.DataLen], msg.Data)
	}
	msg.Data = buf

	msg.MsgType = protocol.MT_PUSH_WITH_RESPONSE
	msg.PushType = protocol.PT_PUSHSERVER
	msg.Number &= HEAD_RESET

	e := this.Cache[msg.DestUuid].MDev[msg.DeviceId].List.PushBack(CacheEntry{msg, id})
	if e == nil {
		err := errors.New("CacheComponent.Save() Error: can not save msg to list")
		fmt.Println(err.Error())
		return err
	}

	return nil
}
開發者ID:huayuxian,項目名稱:im-server-go,代碼行數:45,代碼來源:cache_component.go

示例9: 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

示例10: 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

示例11: 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

示例12: 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

示例13: 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

示例14: handleKeepAlive

func (this *DispatcherComponent) handleKeepAlive(msg *protocol.Message) error {
	if msg == nil {
		err := errors.New("DispatcherComponent.handleKeepAlive() Error: invalid argument")
		fmt.Println(err.Error())
		return err
	}
	buffPool := buffpool.GetBuffPool()
	err := buffPool.UpdateClientStatus(msg.Number, msg.UserUuid, msg.DeviceId, msg.Token, true)
	if err != nil {
		err = errors.New("DispatcherComponent.handleKeepAlive() Error: can not update client's connections status" + err.Error())
		fmt.Println(err.Error())
		return err
	}
	msg.PushType = protocol.PT_PUSHSERVER
	msg.UserUuid, msg.DestUuid = msg.DestUuid, msg.UserUuid
	GetDispatcherOutComponent().EPushBack(msg, false)
	return nil
}
開發者ID:huayuxian,項目名稱:im-server-go,代碼行數:18,代碼來源:dispatcher_component.go


注:本文中的push-server/protocol.Message類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。