コード例 #1
0
        ///// <summary>
        ///// 新连接未成功
        ///// </summary>
        ///// <param name="clientIp"></param>
        ///// <param name="clientPort"></param>
        //public void NewSessionConnectionUnfinished(string clientIp, int clientPort)
        //{
        //    this.logManager.Message.WriteLine("{0} NewSession, Connection Unfinished: Client: {1}:{2}", DateTime.Now, clientIp, clientPort);
        //}

        /// <summary>
        /// 命令执行
        /// </summary>
        /// <param name="session"></param>
        /// <param name="serverName"></param>
        /// <param name="commandName"></param>
        /// <param name="cacheType"></param>
        /// <param name="invokeSeconds"></param>
        /// <param name="seconds"></param>
        /// <param name="result"></param>
        public void Command(ServerSession session, string serverName, string commandName, bool result, ServerCacheType cacheType, double invokeSeconds, double seconds)
        {
            this.logManager.Message.WriteLine("{0} SessionId: {1}, {2}.{3}:{4}, Cache:{5}, InvokeSeconds:{6},Seconds:{7}",
                                              DateTime.Now, session.Id, serverName, commandName, result ? "Success" : "Failure", cacheType, invokeSeconds, seconds);
        }
コード例 #2
0
ファイル: ServerSession.cs プロジェクト: aooshi/adf.cs
        /// <summary>
        /// invoke
        /// </summary>
        /// <param name="commandInfo"></param>
        /// <param name="parameters"></param>
        /// <param name="valueBuffer"></param>
        /// <param name="cacheType"></param>
        /// <returns></returns>
        private CsResult Invoke(CommandInfo commandInfo, object[] parameters, out byte[] valueBuffer, out ServerCacheType cacheType)
        {
            /*
             * this method ,try catch, safe cache error
             */

            //variable init
            string cacheKey     = null;
            string cacheVersion = null;
            object invokeValue  = null;
            var    result       = CsResult.Fail;
            //bool hasContent = false;

            //
            ServerConfigItem configItem = null;
            bool             isCache    = false;

            if (ServerCache.Enable)
            {
                configItem = ServerConfig.GetItem(commandInfo.Server, commandInfo.Name);
                isCache    = configItem != null && configItem.CacheExpires > 0;
            }

            //init out
            valueBuffer = null;
            cacheType   = ServerCacheType.NoCache;

            //cache get
            if (isCache)
            {
                //mode = version
                if (configItem.CacheVersion != null)
                {
                    cacheVersion = ServerCache.Cache.Get(configItem.CacheVersion);
                }
                //get cache
                cacheKey = ServerCache.BuildKey(configItem.CacheKey, cacheVersion, parameters);

                try
                {
                    valueBuffer = ServerCache.Cache.Get <byte[]>(cacheKey);
                    result      = CsResult.Success;
                }
                catch (Exception e)
                {
                    this.serverLogger.CommandCacheException(commandInfo.Server, commandInfo.Name, e);
                }

                cacheType = valueBuffer != null && result == CsResult.Success ? ServerCacheType.Cached : ServerCacheType.NoCache;
            }

            //invoke
            if (valueBuffer == null)
            {
                cacheType = ServerCacheType.NoCache;
                result    = CsResult.Success;

                //no ObjectCache
                if (invokeValue == null)
                {
                    invokeValue = commandInfo.Method.Invoke(commandInfo.Instance, parameters);
                }

                //是否有内容
                //hasContent = invokeValue != null;

                //buffer = bodylength+body, parameter index + parameter length + parameter

                using (var m = new MemoryStream(CsSocket.BODY_BUFFER_SIZE))
                {
                    //body
                    using (var m2 = new MemoryStream(CsSocket.BODY_BUFFER_SIZE))
                    {
                        if (invokeValue == null)
                        {
                            //length
                            m.Write(BitConverter.GetBytes((int)-1), 0, CsSocket.LENGTH_BUFFER_SIZE);
                        }
                        else
                        {
                            CsSerializer.Serialize(m2, invokeValue);
                            //length
                            m.Write(BitConverter.GetBytes((int)m2.Length), 0, CsSocket.LENGTH_BUFFER_SIZE);
                            //body
                            m2.WriteTo(m);
                        }
                    }
                    //parameters
                    // count
                    m.Write(BitConverter.GetBytes((int)commandInfo.OutsIndexLength), 0, CsSocket.LENGTH_BUFFER_SIZE);
                    using (var m2 = new MemoryStream(CsSocket.BODY_BUFFER_SIZE))
                    {
                        for (int i = 0, index = 0; i < commandInfo.OutsIndexLength; i++)
                        {
                            m2.SetLength(0);
                            m2.Position = 0;
                            //
                            index = (int)commandInfo.OutsIndex[i];
                            //index
                            m.Write(BitConverter.GetBytes(index), 0, CsSocket.LENGTH_BUFFER_SIZE);
                            //
                            if (parameters[index] == null)
                            {
                                //length
                                m.Write(BitConverter.GetBytes((int)-1), 0, CsSocket.LENGTH_BUFFER_SIZE);
                            }
                            else
                            {
                                CsSerializer.Serialize(m2, parameters[index]);
                                //length
                                m.Write(BitConverter.GetBytes((int)m2.Length), 0, CsSocket.LENGTH_BUFFER_SIZE);
                                //body
                                m2.WriteTo(m);
                            }
                        }
                    }
                    //
                    valueBuffer = m.ToArray();
                }
            }

            //cache set
            //if (result == CsResult.Success && cacheType == CacheType.NoCache && hasContent && commandInfo.CacheExpires > 0)
            if (isCache && result == CsResult.Success && cacheType == ServerCacheType.NoCache)
            {
                try
                {
                    //set cache
                    ServerCache.Cache.Set(cacheKey, valueBuffer, configItem.CacheExpires);
                    cacheType = ServerCacheType.NewCache;
                }
                catch (Exception e)
                {
                    this.serverLogger.CommandCacheException(commandInfo.Server, commandInfo.Name, e);
                }
            }

            //delete cache
            if (ServerCache.Enable && isCache && configItem.CacheDeletes != null)
            {
                this.DeleteCache(commandInfo, configItem, parameters);
            }

            return(result);
        }