Assert() private method

private Assert ( bool condition ) : void
condition bool
return void
Beispiel #1
0
        public override object Clone(object json_obj)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            return(new Dictionary <string, object>(d));
        }
Beispiel #2
0
        public override bool HasField(object json_obj, string field_name)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            return(d.ContainsKey(field_name));
        }
Beispiel #3
0
        public override void RemoveStringField(object json_obj, string field_name)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            d.Remove(field_name);
        }
Beispiel #4
0
        public override Int64 GetIntegerField(object json_obj, string field_name)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            return(Convert.ToInt64(d [field_name]));
        }
Beispiel #5
0
        public override string Serialize(object json_obj)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            return(Json.Serialize(d));
        }
Beispiel #6
0
        public override string GetStringField(object json_obj, string field_name)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            return(d[field_name] as string);
        }
        /// <summary>
        /// The sender must fill in the mcast_msg.
        /// The "channel_id" field is mandatory.
        /// And mcas_msg must have join and leave flags set.
        /// </summary>
        public bool SendToChannel(FunMulticastMessage mcast_msg)
        {
            DebugUtils.Assert(encoding_ == FunEncoding.kProtobuf);
            DebugUtils.Assert(mcast_msg != null);
            DebugUtils.Assert(!mcast_msg.join);
            DebugUtils.Assert(!mcast_msg.leave);

            string channel_id = mcast_msg.channel;

            DebugUtils.Assert(channel_id != "");

            lock (channel_lock_)
            {
                if (!Connected)
                {
                    DebugUtils.Log("Not connected. If you are trying to leave a channel in which you were, "
                                   + "connect first while preserving the session id you used for join.");
                    return(false);
                }
                if (!channels_.ContainsKey(channel_id))
                {
                    DebugUtils.Log("You are not in the channel: {0}", channel_id);
                    return(false);
                }
            }

            FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast);

            network_.SendMessage(kMulticastMsgType, fun_msg);
            return(true);
        }
 /// <summary>
 /// The sender must fill in the mcast_msg.
 /// The "channel_id" field is mandatory.
 /// And mcas_msg must have join and leave flags set.
 /// </summary>
 public bool SendToChannel(object json_msg)
 {
     DebugUtils.Assert(encoding_ == FunEncoding.kJson);
     // TODO(dkmoon): Verifies the passed json_msg has required fields.
     network_.SendMessage(kMulticastMsgType, json_msg);
     return(true);
 }
 private void DownloadFileCompleteCb(object sender, System.ComponentModel.AsyncCompletedEventArgs ar)
 {
     try
     {
         if (ar.Error != null)
         {
             DebugUtils.Log("Exception Error: {0}", ar.Error);
             OnResultCallback(AnnounceResult.kExceptionError);
             DebugUtils.Assert(false);
         }
         else
         {
             image_list_.RemoveAt(0);
             if (image_list_.Count > 0)
             {
                 KeyValuePair <string, string> item = image_list_[0];
                 web_client_.DownloadFileAsync(new Uri(item.Key), item.Value);
                 DebugUtils.Log("Download url: {0}", item.Key);
             }
             else
             {
                 DebugUtils.Log("Download file completed.");
                 OnResultCallback(AnnounceResult.kSuccess);
             }
         }
     }
     catch (Exception e)
     {
         DebugUtils.Log("Failure in DownloadFileCompleteCb: {0}", e.ToString());
         OnResultCallback(AnnounceResult.kExceptionError);
     }
 }
Beispiel #10
0
        public override void SetIntegerField(object json_obj, string field_name, Int64 value)
        {
            Dictionary <string, object> d = json_obj as Dictionary <string, object>;

            DebugUtils.Assert(d != null);
            d [field_name] = value;
        }
        public FunapiMulticastClient(FunapiNetwork network, FunEncoding encoding)
        {
            DebugUtils.Assert(network != null);
            network_  = network;
            encoding_ = encoding;

            network_.RegisterHandlerWithProtocol(kMulticastMsgType, TransportProtocol.kTcp, OnReceived);
        }
Beispiel #12
0
        public override bool Handshake(string in_header, ref string out_header)
        {
            DebugUtils.Assert(state == State.kHandshaking);

            enc_key_ = Convert.ToUInt32(in_header);
            dec_key_ = enc_key_;

            SetState(State.kEstablished);

            return(true);
        }
Beispiel #13
0
        public override Int64 Decrypt(ArraySegment <byte> src, ArraySegment <byte> dst, string in_header)
        {
            DebugUtils.Assert(state == State.kEstablished);

            if (in_header.Length > 0)
            {
                DebugUtils.LogWarning("Wrong encryptor header.");
                return(-1);
            }

            return(Encrypt(src, dst, false));
        }
        private void OnReceived(string msg_type, object body)
        {
            DebugUtils.Assert(msg_type == kMulticastMsgType);

            string channel_id = "";

            if (encoding_ == FunEncoding.kJson)
            {
                DebugUtils.Assert(body is Dictionary <string, object>);
                Dictionary <string, object> msg = body as Dictionary <string, object>;

                channel_id = msg[kChannelId] as string;

                lock (channel_lock_)
                {
                    if (!channels_.ContainsKey(channel_id))
                    {
                        DebugUtils.Log("You are not in the channel: {0}", channel_id);
                        return;
                    }

                    ChannelReceiveHandler h = channels_[channel_id];
                    h(channel_id, body);
                }
            }
            else
            {
                DebugUtils.Assert(body is FunMessage);
                FunMessage msg = body as FunMessage;

                object obj = network_.GetMessage(msg, MessageType.multicast);
                DebugUtils.Assert(obj != null);

                FunMulticastMessage mcast_msg = obj as FunMulticastMessage;
                channel_id = mcast_msg.channel;

                lock (channel_lock_)
                {
                    if (!channels_.ContainsKey(channel_id))
                    {
                        DebugUtils.Log("You are not in the channel: {0}", channel_id);
                        return;
                    }

                    ChannelReceiveHandler h = channels_[channel_id];
                    h(channel_id, mcast_msg);
                }
            }
        }
Beispiel #15
0
        public override Int64 Encrypt(ArraySegment <byte> src, ArraySegment <byte> dst, ref string out_header)
        {
            DebugUtils.Assert(state == State.kEstablished);

            if (dst.Count < src.Count)
            {
                return(-1);
            }

            if (!src.Equals(dst))
            {
                dst = new ArraySegment <byte>(src.Array, 0, src.Count);
            }

            return(src.Count);
        }
Beispiel #16
0
        private static Int64 Encrypt(ArraySegment <byte> src, ArraySegment <byte> dst, ref UInt32 key)
        {
            if (dst.Count < src.Count)
            {
                return(-1);
            }

            // update key
            key = 8253729 * key + 2396403;

            int    shift_len = (int)(key & 0x0F);
            UInt32 key32     = CircularLeftShift(key, shift_len);

            // Encrypted in kBlockSize
            for (int i = 0; i < (src.Count / kBlockSize); ++i)
            {
                UInt32 s = BitConverter.ToUInt32(src.Array, src.Offset + i * kBlockSize);
                byte[] d = BitConverter.GetBytes(s ^ key32);
                DebugUtils.Assert(d.Length == kBlockSize);

                for (int j = 0; j < d.Length; ++j)
                {
                    dst.Array[dst.Offset + i * kBlockSize + j] = d[j];
                }
            }

            byte key8 = 0;

            byte[] k = BitConverter.GetBytes(key);
            if (BitConverter.IsLittleEndian)
            {
                key8 = CircularLeftShift(k[0], shift_len);
            }
            else
            {
                key8 = CircularLeftShift(k[3], shift_len);
            }

            // The remaining values are encrypted in units of 1byte
            for (int i = 0; i < (src.Count % kBlockSize); ++i)
            {
                int idx = src.Count - 1 - i;
                dst.Array[dst.Offset + idx] = (byte)(src.Array[src.Offset + idx] ^ key8);
            }

            return(src.Count);
        }
        private void LoadCachedList()
        {
            cached_list_.Clear();

            string path = target_path_ + kCachedFileName;

            if (!File.Exists(path))
            {
                return;
            }

            StreamReader stream = File.OpenText(path);
            string       data   = stream.ReadToEnd();

            stream.Close();

            if (data.Length <= 0)
            {
                DebugUtils.Log("Failed to get a cached file list.");
                DebugUtils.Assert(false);
                return;
            }

            Dictionary <string, object> json = Json.Deserialize(data) as Dictionary <string, object>;
            List <object> list = json["list"] as List <object>;

            foreach (Dictionary <string, object> node in list)
            {
                DownloadFileInfo info = new DownloadFileInfo();
                info.path = node["path"] as string;
                info.size = Convert.ToUInt32(node["size"]);
                info.hash = node["hash"] as string;
                if (node.ContainsKey("front"))
                {
                    info.hash_front = node["front"] as string;
                }
                else
                {
                    info.hash_front = "";
                }

                cached_list_.Add(info);
            }
        }
Beispiel #18
0
        public static Encryptor Create(string name)
        {
            if (name == Encryptor0.kName)
            {
                return(new Encryptor0());
            }
            else if (name == Encryptor1.kName)
            {
                return(new Encryptor1());
            }
            else if (name == Encryptor2.kName)
            {
                return(new Encryptor2());
            }

            DebugUtils.LogWarning("Unknown encryptor: {0}", name);
            DebugUtils.Assert(false);

            return(null);
        }
Beispiel #19
0
        public static Encryptor Create(EncryptionType type)
        {
            if (type == EncryptionType.kDummyEncryption)
            {
                return(new Encryptor0());
            }
            else if (type == EncryptionType.kIFunEngine1Encryption)
            {
                return(new Encryptor1());
            }
            else if (type == EncryptionType.kIFunEngine2Encryption)
            {
                return(new Encryptor2());
            }

            DebugUtils.LogWarning("Unknown encryptor: {0}", type);
            DebugUtils.Assert(false);

            return(null);
        }
Beispiel #20
0
        public override void Init(params object[] param)
        {
            Debug.Log("TwitterConnector Initialization.");
            DebugUtils.Assert(param[0] is string);
            DebugUtils.Assert(param[1] is string);

            oauth_handler_ = new OAuthHandler(param[0] as string, param[1] as string);

            string oauth_token        = EncryptedPlayerPrefs.GetString("oauth_token");
            string oauth_token_secret = EncryptedPlayerPrefs.GetString("oauth_token_secret");

            if (!string.IsNullOrEmpty(oauth_token) && !string.IsNullOrEmpty(oauth_token_secret))
            {
                oauth_handler_.AddParameter("oauth_token", oauth_token);
                oauth_handler_.AddParameter("oauth_token_secret", oauth_token_secret);

                my_info_.id   = EncryptedPlayerPrefs.GetString("user_id");
                my_info_.name = EncryptedPlayerPrefs.GetString("screen_name");

                Debug.Log("Already logged in.");
                OnEventNotify(SNResultCode.kLoggedIn);
            }
        }
Beispiel #21
0
 public virtual bool Handshake(string in_header, ref string out_header)
 {
     DebugUtils.Assert(false);
     return(true);
 }
        // Callback function for downloaded file.
        private void DownloadFileCompleteCb(object sender, System.ComponentModel.AsyncCompletedEventArgs ar)
        {
            mutex_.WaitOne();

            bool failed = false;

            try
            {
                // It can be true when CancelAsync() called in Stop().
                if (ar.Cancelled)
                {
                    return;
                }

                if (ar.Error != null)
                {
                    DebugUtils.Log("Exception Error: {0}", ar.Error);
                    DebugUtils.Assert(false);
                    failed = true;
                }
                else
                {
                    var info = (DownloadFileInfo)ar.UserState;
                    if (info == null)
                    {
                        DebugUtils.Log("DownloadFileInfo object is null.");
                        failed = true;
                    }
                    else
                    {
                        string path = target_path_ + info.path;
                        File.Move(cur_download_path_, path);

                        ++cur_download_count_;
                        cur_download_size_ += info.size;
                        download_list_.Remove(info);
                        cached_list_.Add(info);

                        DownloadResourceFile();
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtils.Log("Failure in DownloadFileCompleteCb: {0}", e.ToString());
                failed = true;
            }
            finally
            {
                mutex_.ReleaseMutex();
            }

            if (failed)
            {
                web_client_.Dispose();
                File.Delete(cur_download_path_);

                if (retry_download_count_ < kMaxRetryCount)
                {
                    ++retry_download_count_;
                    FunapiManager.instance.StartCoroutine(AsyncDownloadFile());
                }
                else
                {
                    Stop();
                }
            }
        }
Beispiel #23
0
        public override Int64 Encrypt(ArraySegment <byte> src, ArraySegment <byte> dst, ref string out_header)
        {
            DebugUtils.Assert(state == State.kEstablished);

            return(Encrypt(src, dst, true));
        }
        private void DownloadDataCompleteCb(object sender, DownloadDataCompletedEventArgs ar)
        {
            try
            {
                if (ar.Error != null)
                {
                    DebugUtils.Log("Exception Error: {0}", ar.Error);
                    OnResultCallback(AnnounceResult.kExceptionError);
                    DebugUtils.Assert(false);
                }
                else
                {
                    // Parse json
                    string data = Encoding.UTF8.GetString(ar.Result);
                    Dictionary <string, object> json = Json.Deserialize(data) as Dictionary <string, object>;
                    if (json == null)
                    {
                        DebugUtils.Log("Deserialize json failed. json: {0}", data);
                        OnResultCallback(AnnounceResult.kInvalidJson);
                        return;
                    }

                    DebugUtils.Assert(json.ContainsKey("list"));
                    List <object> list = json["list"] as List <object>;
                    if (list == null || list.Count <= 0)
                    {
                        DebugUtils.Log("Invalid announcement list. list: {0}", list);
                        OnResultCallback(AnnounceResult.kListIsNullOrEmpty);
                        return;
                    }

                    announce_list_.Clear();

                    foreach (Dictionary <string, object> node in list)
                    {
                        announce_list_.Add(node);

                        // download image
                        if (node.ContainsKey(kImageUrlKey) && node.ContainsKey(kImageMd5Key))
                        {
                            CheckDownloadImage(node[kImageUrlKey] as string, node[kImageMd5Key] as string);
                        }
                    }

                    DebugUtils.Log("Announcement has been updated. total count: {0}", announce_list_.Count);

                    if (image_list_.Count > 0)
                    {
                        // Request a file.
                        KeyValuePair <string, string> item = image_list_[0];
                        web_client_.DownloadFileAsync(new Uri(item.Key), item.Value);
                        DebugUtils.Log("Download url: {0}", item.Key);
                    }
                    else
                    {
                        OnResultCallback(AnnounceResult.kSuccess);
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtils.Log("Failure in DownloadDataCompleteCb: {0}", e.ToString());
                OnResultCallback(AnnounceResult.kExceptionError);
            }
        }
        // Callback function for list of files
        private void DownloadDataCompleteCb(object sender, DownloadDataCompletedEventArgs ar)
        {
            mutex_.WaitOne();

            bool failed = false;

            try
            {
                if (ar.Error != null)
                {
                    DebugUtils.Log("Exception Error: {0}", ar.Error);
                    DebugUtils.Assert(false);
                    failed = true;
                }
                else
                {
                    // It can be true when CancelAsync() called in Stop().
                    if (ar.Cancelled)
                    {
                        return;
                    }

                    // Parse json
                    string data = Encoding.UTF8.GetString(ar.Result);
                    Dictionary <string, object> json = Json.Deserialize(data) as Dictionary <string, object>;

                    //DebugUtils.Log("Json data >> {0}", data);

                    // Redirect url
                    if (json.ContainsKey("url"))
                    {
                        string url = json["url"] as string;
                        if (url[url.Length - 1] != '/')
                        {
                            url += "/";
                        }

                        host_url_ = url;
                        DebugUtils.Log("Redirect download url: {0}", host_url_);
                    }

                    List <object> list = json["data"] as List <object>;
                    if (list.Count <= 0)
                    {
                        DebugUtils.Log("Invalid list data. List count is 0.");
                        DebugUtils.Assert(false);
                        failed = true;
                    }
                    else
                    {
                        download_list_.Clear();

                        foreach (Dictionary <string, object> node in list)
                        {
                            DownloadFileInfo info = new DownloadFileInfo();
                            info.path = node["path"] as string;
                            info.size = Convert.ToUInt32(node["size"]);
                            info.hash = node["md5"] as string;
                            if (node.ContainsKey("md5_front"))
                            {
                                info.hash_front = node["md5_front"] as string;
                            }
                            else
                            {
                                info.hash_front = "";
                            }

                            download_list_.Add(info);
                        }

                        // Checks files
                        manager_.AddEvent(() =>
                                          manager_.StartCoroutine(CheckFileList(download_list_)));
                    }
                }
            }
            catch (Exception e)
            {
                DebugUtils.Log("Failure in DownloadDataCompleteCb: {0}", e.ToString());
                failed = true;
            }
            finally
            {
                mutex_.ReleaseMutex();
            }

            if (failed)
            {
                Stop();
            }
        }