示例#1
0
        public override Task OutcomingMessage(NList message)
        {
            byte[]        data          = ProtoUtils.Serialize(message);
            NetDataWriter netDataWriter = new NetDataWriter();

            netDataWriter.PutBytesWithLength(data);
            Connection.Send(netDataWriter, DeliveryMethod.ReliableOrdered);

            return(Task.CompletedTask);
        }
示例#2
0
 public override async Task OutcomingMessage(NList send_msg)
 {
     try
     {
         if (_Context.Channel.IsActive)
         {
             var         bytes  = ProtoUtils.Serialize(send_msg);
             IByteBuffer buffer = Unpooled.WrappedBuffer(bytes);
             await _Context.WriteAndFlushAsync(buffer);
         }
     }
     catch (Exception ex)
     {
         _Logger.LogError(
             $"OutcomingMessage异常:\n" +
             $"{ex.Message}\n" +
             $"{ex.StackTrace}\n" +
             $"{send_msg}");
     }
 }
示例#3
0
        protected override void Encode(IChannelHandlerContext context, T message, List <object> output)
        {
            Contract.Requires(context != null);
            Contract.Requires(message != null);
            Contract.Requires(output != null);

            IByteBuffer buffer = null;

            try
            {
                buffer = Unpooled.WrappedBuffer(ProtoUtils.Serialize(message));
                output.Add(buffer);
                buffer = null;
            }
            catch (Exception exception)
            {
                throw new CodecException(exception);
            }
            finally
            {
                buffer?.Release();
            }
        }
示例#4
0
文件: Cache.cs 项目: lulzzz/OpenH5
        private async Task BatchCache(object arg)
        {
            if (BatchCahceList.Count <= 0)
            {
                return;
            }

            try
            {
                int            db    = (int)(Identity % CacheUtils.EntityDBs);
                IRedisDatabase redis = _CacheClient.GetDb(db);

                ITransaction trans = redis.Database.CreateTransaction();

                foreach (NList batch in BatchCahceList)
                {
                    CacheOption option    = (CacheOption)batch.Get <int>(0);
                    Nuid        entity_id = batch.Get <Nuid>(1);

                    switch (option)
                    {
                    case CacheOption.SetEntity:
                    {
                        string entity_type = batch.Get <string>(2);
                        string key         = CacheUtils.BuildEntities(entity_id);
                        Task   task        = trans.HashSetAsync(key, entity_id.Unique.ToString(), entity_type);
                    }
                    break;

                    case CacheOption.DelEntity:
                    {
                        string       entity_type   = batch.Get <string>(2);
                        EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type);
                        if (entity_prefab == null)
                        {
                            continue;
                        }

                        foreach (TablePrefab table_prefab in entity_prefab.tables.Values)
                        {
                            string table_key  = CacheUtils.BuildTable(entity_id, table_prefab.name);
                            Task   table_task = trans.KeyDeleteAsync(table_key);
                        }

                        string field_key  = CacheUtils.BuildFields(entity_id);
                        Task   field_task = trans.KeyDeleteAsync(field_key);

                        string entity_key  = CacheUtils.BuildEntities(entity_id);
                        Task   entity_task = trans.HashDeleteAsync(entity_key, entity_id.Unique.ToString());
                    }
                    break;

                    case CacheOption.SetField:
                    {
                        string field_name  = batch.Get <string>(2);
                        byte[] field_value = batch.Get <byte[]>(3);

                        string key  = CacheUtils.BuildFields(entity_id);
                        Task   task = trans.HashSetAsync(key, field_name, field_value);
                    }
                    break;

                    case CacheOption.SetRow:
                    {
                        string table_name = batch.Get <string>(2);
                        long   row        = batch.Get <long>(3);
                        NList  row_value  = batch.Get <NList>(4);

                        string key  = CacheUtils.BuildTable(entity_id, table_name);
                        Task   task = trans.HashSetAsync(key, row, ProtoUtils.Serialize(row_value));
                    }
                    break;

                    case CacheOption.DelRow:
                    {
                        string table_name = batch.Get <string>(2);
                        long   row        = batch.Get <long>(3);

                        string key  = CacheUtils.BuildTable(entity_id, table_name);
                        Task   task = trans.HashDeleteAsync(key, row);
                    }
                    break;

                    case CacheOption.ClearTable:
                    {
                        string table_name = batch.Get <string>(2);

                        string key  = CacheUtils.BuildTable(entity_id, table_name);
                        Task   task = trans.KeyDeleteAsync(key);
                    }
                    break;

                    default:
                        break;
                    }
                }

                bool result = await trans.ExecuteAsync();

                if (result)
                {
                    BatchCahceList.Clear();
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, string.Format("{0} BatchCache ExecuteAsync Failed", Identity));
            }
        }
示例#5
0
        public async Task SetField <T>(Nuid id, string field_name, T field_value)
        {
            if (field_value == null)
            {
                return;
            }

            Entity entity = EntityManager.Get(id);

            if (entity != null)
            {
                Field field = entity.GetField(field_name);
                if (field == null)
                {
                    return;
                }

                NList result;
                if (!field.TrySet(field_value, out result))
                {
                    return;
                }

                BatchCahceList.Add(NList.New().Add((int)CacheOption.SetField).Add(id).Add(field_name).Add(ProtoUtils.Serialize(field_value)));

                await CallbackField(id, field_name, FieldEvent.Change, result);
            }
            else
            {
                if (id.Origin == Identity)
                {
                    T old_value = await GetCacheField <T>(id, field_name);

                    if (await SetCacheField(id, field_name, field_value))
                    {
                        NList result = NList.New();
                        result.Add(old_value);
                        result.Add(field_value);
                        await CallbackField(id, field_name, FieldEvent.Change, result);
                    }
                }
                else
                {
                    INode node = GrainFactory.GetGrain <INode>(id.Origin);
                    await node.SetField(id, field_name, field_value);
                }
            }
        }