コード例 #1
0
        public IEnumerator CreateCharacterController(Coroutine coroutine,
                                                     ulong characterId,
                                                     AsyncReturnValue <CT> controller,
                                                     object[] args)
        {
            controller.Value = null;
            DataItem dataItem;

            if (mDictionary.TryGetValue(characterId, out dataItem))
            {
                Logger.Error("Item already exist.");
                controller.Value = dataItem.Controller;
                yield break;
            }

            dataItem = new DataItem();

            dataItem.Controller       = new CT();
            dataItem.Controller.State = CharacterState.Created;
            dataItem.Controller.InitByBase(characterId, args);
            dataItem.SimpleData   = dataItem.Controller.GetSimpleData();
            dataItem.LastSaveTime = DateTime.Now;

            mDictionary.Add(characterId, dataItem);

            var co = CoroutineFactory.NewSubroutine(SaveData, coroutine, characterId, dataItem, true);

            if (co.MoveNext())
            {
                yield return(co);
            }

            controller.Value = dataItem.Controller;
        }
コード例 #2
0
        public static AsyncReturnValue <T> Create(T t)
        {
            AsyncReturnValue <T> value;

            if (!sStack.TryPop(out value))
            {
                value = new AsyncReturnValue <T>();
            }
            value.Value = t;
            return(value);
        }
コード例 #3
0
        public IEnumerator GetOrCreateCharacterController(Coroutine coroutine,
                                                          ulong characterId,
                                                          object[] args,
                                                          bool createIfNotExist,
                                                          AsyncReturnValue <CT> result)
        {
            if (result == null)
            {
                Logger.Error("AsyncReturnValue can not be null");
                yield break;
            }
            result.Value = null;
            if (DB == null)
            {
                Logger.Error("CharacterManager must initialize before use.");
                yield break;
            }

            DataItem dataItem;

            if (mDictionary.TryGetValue(characterId, out dataItem))
            {
                if (dataItem.Controller != null)
                {
                    result.Value = dataItem.Controller;
                    yield break;
                }
                mDictionary.Remove(characterId);
            }

            dataItem = new DataItem();
            var controller = new CT();

            controller.State = CharacterState.Created;

            var cat = mCategory;

            var data = DB.Get <DT>(coroutine, cat, characterId);

            yield return(data);

            DataItem temp;

            if (mDictionary.TryGetValue(characterId, out temp))
            {
                if (dataItem.Controller != null)
                {
                    result.Value = dataItem.Controller;
                    yield break;
                }
                mDictionary.Remove(characterId);
            }

            // can not get data from db
            if (data.Data == null)
            {
                if (createIfNotExist)
                {
                    try
                    {
                        controller.InitByBase(characterId, args);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex);
                    }

                    result.Value        = controller;
                    dataItem.Controller = controller;
                    dataItem.SimpleData = controller.GetSimpleData();
                    mDictionary.Add(characterId, dataItem);

                    // when we create new character, we should save it immediately.
                    var co = CoroutineFactory.NewSubroutine(SaveData, coroutine, characterId, dataItem, true);
                    if (co.MoveNext())
                    {
                        yield return(co);
                    }

                    yield break;
                }

                Logger.Error("can not load character {0} 's data from db.", characterId);
                yield break;
            }

            try
            {
                controller.InitByDb(characterId, data.Data);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                yield break;
            }

            result.Value          = controller;
            dataItem.Controller   = controller;
            dataItem.SimpleData   = controller.GetSimpleData();
            dataItem.LastSaveTime = DateTime.Now;
            mDictionary.Add(characterId, dataItem);
        }
コード例 #4
0
        public IEnumerator GetSimpeData(Coroutine coroutine, ulong characterId, AsyncReturnValue <SDT> returnValue)
        {
            returnValue.Value = default(SDT);
            if (DB == null)
            {
                Logger.Error("CharacterManager must initialize before use.");
                yield break;
            }

            DataItem dataItem;

            if (mDictionary.TryGetValue(characterId, out dataItem))
            {
                if (dataItem.Controller != null)
                {
                    if (dataItem.Controller.DbDirty)
                    {
                        var simpeData = dataItem.Controller.GetSimpleData();
                        dataItem.SimpleData = simpeData;
                        returnValue.Value   = simpeData;
                        yield break;
                    }
                }

                returnValue.Value = dataItem.SimpleData;
                yield break;
            }

            var cat  = mCategory;
            var data = DB.Get <SDT>(coroutine, cat, "__s_:" + characterId);

            yield return(data);

            // check again.
            if (mDictionary.TryGetValue(characterId, out dataItem))
            {
                if (dataItem.Controller != null)
                {
                    if (dataItem.Controller.DbDirty)
                    {
                        var simpeData = dataItem.Controller.GetSimpleData();
                        dataItem.SimpleData = simpeData;
                        returnValue.Value   = simpeData;
                        yield break;
                    }
                }

                returnValue.Value = dataItem.SimpleData;
                yield break;
            }

            if (data.Data != null)
            {
                dataItem              = new DataItem();
                dataItem.SimpleData   = data.Data;
                dataItem.LastSaveTime = DateTime.Now;
                mDictionary.Add(characterId, dataItem);
                returnValue.Value = data.Data;
                yield break;
            }

            returnValue.Value = default(SDT);
        }
コード例 #5
0
        public object DoRequest(Coroutine co, string url, Dictionary <string, string> postDictionary, AsyncReturnValue <string> result)
        {
            Task.Run(async() =>
            {
                HttpWebRequest request = null;
                WebResponse response   = null;
                try
                {
                    request           = (HttpWebRequest)WebRequest.Create(url);
                    request.Proxy     = null;
                    request.KeepAlive = false;

                    request.Method      = "POST";
                    var boundary        = "--abcdef";
                    request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                    request.Headers.Add("cache-control", "no-cache");
                    var sendStream = await request.GetRequestStreamAsync();
                    foreach (var pair in postDictionary)
                    {
                        AddContent(sendStream, pair.Key, pair.Value, boundary);
                    }
                    var sp   = string.Format("--{0}--\r\n", boundary);
                    var data = Encoding.UTF8.GetBytes(sp);
                    sendStream.Write(data, 0, data.Length);
                    sendStream.Close();
                    response   = await request.GetResponseAsync();
                    var stream = response.GetResponseStream();

                    if (stream != null)
                    {
                        using (var reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            result.Value = reader.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.ToString());
                }
                finally
                {
                    try
                    {
                        if (response != null)
                        {
                            response.Close();
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.ToString());
                    }

                    mAgentBase.mWaitingEvents.Add(new ContinueEvent(co));
                }
            });

            return(null);
        }