public TestQuest(JSONNode node) : base(node, QuestType.testQuest)
 {
     if (node.ContainsKey(_jsonRewardItem)) // может и не быть, делаем проверку.
     {
         QuestRewardItems = new Dictionary <int, int>();
         foreach (JSONNode item in node[_jsonRewardItem].AsArray)
         {
             QuestRewardItems[item[_jsonItemId].AsInt] = item[_jsonItemCount].AsInt;
         }
     }
 }
        /// <summary>
        /// Check the message.
        /// </summary>
        private bool checkMsg(JSONNode msg, JSONNode proto)
        {
            ICollection <string> protoKeys = ((JSONClass)proto).Keys();

            foreach (string key in protoKeys)
            {
                JSONNode value = proto[key];
                object   proto_option;
                if (value.TryGetValue("option", out proto_option))
                {
                    switch (proto_option.ToString())
                    {
                    case "required":
                        if (!msg.ContainsKey(key))
                        {
                            return(false);
                        }
                        else
                        {
                        }
                        break;

                    case "optional":
                        object value_type;

                        JSONNode messages = proto["__messages"];

                        value_type = value["type"];

                        if (msg.ContainsKey(key))
                        {
                            Object value_proto;

                            if (messages.TryGetValue(value_type.ToString(), out value_proto) || protos.TryGetValue("message " + value_type.ToString(), out value_proto))
                            {
                                checkMsg(msg[key], (JSONNode)value_proto);
                            }
                        }
                        break;

                    case "repeated":
                        object msg_name;
                        object msg_type;
                        if (value.TryGetValue("type", out value_type) && msg.TryGetValue(key, out msg_name))
                        {
                            if (((JSONNode)proto["__messages"]).TryGetValue(value_type.ToString(), out msg_type) || protos.TryGetValue("message " + value_type.ToString(), out msg_type))
                            {
                                List <object> o = (List <object>)msg_name;
                                foreach (object item in o)
                                {
                                    if (!checkMsg((JSONNode)item, (JSONNode)msg_type))
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        private void processHandshakeData(JSONNode msg)
        {
            Debug.Log(msg.GetType().ToString() + "THE MESSAGE:" + msg.ToString());
            //Handshake error
            if (!msg.ContainsKey("code") || !msg.ContainsKey("sys") || Convert.ToInt32(msg["code"]) != 200)
            {
                throw new Exception("Handshake error! Please check your handshake config.");
            }

            //Set compress data
            JSONNode sys = msg["sys"];

            JSONNode dict = new JSONNode();

            if (sys.ContainsKey("dict"))
            {
                dict = sys["dict"];
            }

            JSONClass protos       = new JSONClass();
            JSONClass serverProtos = new JSONClass();
            JSONClass clientProtos = new JSONClass();

            if (sys.ContainsKey("protos"))
            {
                protos.Add(sys["protos"]);
                serverProtos.Add(protos["server"]);
                clientProtos.Add(protos["client"]);

                //protos = (JSONClass)sys["protos"];
                //serverProtos = (JSONClass)protos["server"];
                //clientProtos = (JSONClass)protos["client"];
            }

            messageProtocol = new MessageProtocol(dict, serverProtos, clientProtos);

            //messageProtocol = new MessageProtocol(msg["sys"]["dict"],msg["protos"]["server"],msg["protos"]["client"]);

            //Init heartbeat service
            int interval = 0;

            if (msg["sys"].ContainsKey("heartbeat"))
            {
                interval = Convert.ToInt32(msg["sys"]["heartbeat"]);
            }
            heartBeatService = new HeartBeatService(interval, this);

            if (interval > 0)
            {
                heartBeatService.start();
            }


            //send ack and change protocol state
            handshake.ack();
            this.state = ProtocolState.working;


            //Invoke handshake callback
            JSONClass user = new JSONClass();

            if (msg.ContainsKey("user"))
            {
                user.Add(msg["user"]);
            }

            handshake.invokeCallback(user);
        }
Exemplo n.º 4
0
        public byte[] encode(string route, uint id, JSONNode msg)
        {
            int routeLength = byteLength(route);

            if (routeLength > MSG_Route_Limit)
            {
                throw new Exception("Route is too long!");
            }

            //Encode head
            //The maximus length of head is 1 byte flag + 4 bytes message id + route string length + 1byte
            byte[] head   = new byte[routeLength + 6];
            int    offset = 1;
            byte   flag   = 0;

            if (id > 0)
            {
                byte[] bytes = Protobuf.Encoder.encodeUInt32(id);

                writeBytes(bytes, offset, head);
                flag   |= ((byte)MessageType.MSG_REQUEST) << 1;
                offset += bytes.Length;
            }
            else
            {
                flag |= ((byte)MessageType.MSG_NOTIFY) << 1;
            }

            //Compress head
            if (dict.ContainsKey(route))
            {
                ushort cmpRoute = dict[route];
                writeShort(offset, cmpRoute, head);
                flag   |= MSG_Route_Mask;
                offset += 2;
            }
            else
            {
                //Write route length
                head[offset++] = (byte)routeLength;

                //Write route
                writeBytes(Encoding.UTF8.GetBytes(route), offset, head);
                offset += routeLength;
            }

            head [0] = flag;

            //Encode body
            byte[] body;
            if (encodeProtos.ContainsKey(route))
            {
                body = protobuf.encode(route, msg);
            }
            else
            {
                body = Encoding.UTF8.GetBytes(msg.ToString());
            }

            //Construct the result
            byte[] result = new byte[offset + body.Length];
            for (int i = 0; i < offset; i++)
            {
                result[i] = head[i];
            }

            for (int i = 0; i < body.Length; i++)
            {
                result[offset + i] = body[i];
            }

            //Add id to route map
            if (id > 0)
            {
                reqMap.Add(id, route);
            }

            return(result);
        }
Exemplo n.º 5
0
        public Message decode(byte[] buffer)
        {
            //Decode head
            //Get flag
            byte flag = buffer [0];
            //Set offset to 1, for the 1st byte will always be the flag
            int offset = 1;

            //Get type from flag;
            MessageType type = (MessageType)((flag >> 1) & MSG_Type_Mask);
            uint        id   = 0;
            string      route;

            if (type == MessageType.MSG_RESPONSE)
            {
                int length;
                id = (uint)Protobuf.Decoder.decodeUInt32(offset, buffer, out length);
                if (id <= 0 || !reqMap.ContainsKey(id))
                {
                    return(null);
                }
                else
                {
                    route = reqMap[id];
                    reqMap.Remove(id);
                }

                offset += length;
            }
            else if (type == MessageType.MSG_PUSH)
            {
                //Get route
                if ((flag & 0x01) == 1)
                {
                    ushort routeId = readShort(offset, buffer);
                    route = abbrs[routeId];

                    offset += 2;
                }
                else
                {
                    byte length = buffer[offset];
                    offset += 1;

                    route   = Encoding.UTF8.GetString(buffer, offset, length);
                    offset += length;
                }
            }
            else
            {
                return(null);
            }

            //Decode body
            byte[] body = new byte[buffer.Length - offset];
            for (int i = 0; i < body.Length; i++)
            {
                body[i] = buffer[i + offset];
            }

            JSONNode msg;

            if (decodeProtos.ContainsKey(route))
            {
                msg = protobuf.decode(route, body);
            }
            else
            {
                msg = JSON.Parse(Encoding.UTF8.GetString(body));
            }

            //Construct the message
            return(new Message(type, id, route, msg));
        }