Пример #1
0
    public new void ProcessReceivedMessage(byte[] bytes)
    {
        // parse to base message.
        var message = BaseMessage.Parser.ParseFrom(bytes);

        // get target message info.
        var           type          = ProtoDic.GetProtoTypeByProtoId(message.Id);
        MessageParser messageParser = ProtoDic.GetMessageParser(type.TypeHandle);

        // convert to target message object.
        object target = messageParser.ParseFrom(message.Data);

        List <Action <object> > list = null;

        if (_MessageCallbacks.TryGetValue(type, out list))
        {
            if (list == null || list.Count == 0)
            {
                Console.WriteLine("list == null || list.Count == 0");
                return;
            }

            for (int i = 0, length = list.Count; i < length; i++)
            {
                int index = i;
                //Loom.QueueOnMainThread(() =>
                //{
                list[index](target);
                //});
            }
        }
    }
Пример #2
0
        void ProcessReceivedMessage(byte[] bytes, TcpClient client)
        {
            // parse to base message.
            var message = BaseMessage.Parser.ParseFrom(bytes);

            // get target message info.
            var type = ProtoDic.GetProtoTypeByProtoId(message.Id);

            Console.WriteLine("received data type=" + type);
            MessageParser messageParser = ProtoDic.GetMessageParser(type.TypeHandle);

            // convert to target message object.
            object target = messageParser.ParseFrom(message.Data);

            List <Action <object, TcpClient> > list = null;

            if (_MessageCallbacks.TryGetValue(type, out list))
            {
                if (list == null || list.Count == 0)
                {
                    Console.WriteLine("list == null || list.Count == 0");
                    return;
                }

                for (int i = 0, length = list.Count; i < length; i++)
                {
                    list[i](target, client);
                }
            }
        }
        /// <summary>
        /// 派发协议
        /// </summary>
        /// <param name="protoId"></param>
        /// <param name="buff"></param>
        public void DispatchProto(int protoId, byte[] buff)
        {
            if (!ProtoDic.ContainProtoId(protoId))
            {
                Debug.LogError("未知协议号");
                return;
            }
            Type protoType = ProtoDic.GetProtoTypeByProtoId(protoId);

            try
            {
                MessageParser messageParser = ProtoDic.GetMessageParser(protoType.TypeHandle);
                object        toc           = messageParser.ParseFrom(buff);
                sEvents.Enqueue(new KeyValuePair <Type, object>(protoType, toc));
            }
            catch
            {
                Debug.Log("DispatchProto Error:" + protoType.ToString());
            }
        }
Пример #4
0
        void OnReceivedGameMsg(byte[] msg, IPEndPoint ip)
        {
            // parse to base message.
            var message = BaseMessage.Parser.ParseFrom(msg);

            // get target message info.
            var type = ProtoDic.GetProtoTypeByProtoId(message.Id);

            if (type == typeof(UDPGameStart))
            {
                MessageParser messageParser = ProtoDic.GetMessageParser(type.TypeHandle);
                // convert to target message object.
                UDPGameStart target = messageParser.ParseFrom(message.Data) as UDPGameStart;
                if (target != null)
                {
                    if (_GatePlayerInfos.ContainsKey(target.UserId))
                    {
                        var info = _GatePlayerInfos[target.UserId];
                        info._UserToken._GameIp         = ip;
                        _GatePlayerInfos[target.UserId] = info;
                    }

                    for (int i = 0, length = _GameRooms.Count; i < length; i++)
                    {
                        var room = _GameRooms[i];
                        for (int j = 0, max = room._UserIds.Length; j < max; j++)
                        {
                            if (room._UserIds[j] == target.UserId)
                            {
                                room.PlayerReady(target.UserId, ip);
                                return;
                            }
                        }
                    }
                }
                return;
            }

            int userId = 0;
            // find client
            var e = _GatePlayerInfos.GetEnumerator();

            while (e.MoveNext())
            {
                if (e.Current.Value._UserToken._GameIp.ToString() == ip.ToString())
                {
                    userId = e.Current.Value.UserId;
                }
            }
            if (userId != 0)
            {
                for (int i = 0, length = _GameRooms.Count; i < length; i++)
                {
                    var room = _GameRooms[i];
                    for (int j = 0, max = room._UserIds.Length; j < max; j++)
                    {
                        if (room._UserIds[j] == userId)
                        {
                            room.Receive(msg);
                            return;
                        }
                    }
                }
            }
        }