コード例 #1
0
        public static void LogAndStatistics(int result, Net.Protocol p, bool IsRequestSaved)
        {
            var actionName = p.GetType().FullName;

            if (IsRequestSaved == false)
            {
                actionName = actionName + ":Response";
            }

            if (result != 0)
            {
                var logLevel = (null != p.Service.Zeze)
                    ? p.Service.Zeze.Config.ProcessReturnErrorLogLevel
                    : NLog.LogLevel.Info;

                logger.Log(logLevel,
                           "Task {0} Return={1}@{2}:{3} UserState={4}",
                           actionName,
                           result,
                           Zeze.Net.Protocol.GetModuleId(result),
                           Zeze.Net.Protocol.GetProtocolId(result),
                           p.UserState);
            }
#if ENABLE_STATISTICS
            ProcedureStatistics.Instance.GetOrAdd(actionName).GetOrAdd(result).IncrementAndGet();
#endif
        }
コード例 #2
0
 public static System.Threading.Tasks.Task Run(
     Procedure procdure,
     Net.Protocol from = null,
     Action <Net.Protocol, int> actionWhenError = null)
 {
     return(System.Threading.Tasks.Task.Run(() => Call(procdure, from, actionWhenError)));
 }
コード例 #3
0
        public static int Call(
            Procedure procdure,
            Net.Protocol from = null,
            Action <Net.Protocol, int> actionWhenError = null)
        {
            bool?isRequestSaved = from?.IsRequest;

            try
            {
                // 日志在Call里面记录。因为要支持嵌套。
                // 统计在Call里面实现。
                int result = procdure.Call();
                if (result != 0 && null != isRequestSaved && isRequestSaved.Value)
                {
                    actionWhenError?.Invoke(from, result);
                }
                return(result);
            }
            catch (Exception ex)
            {
                // Procedure.Call处理了所有错误。应该不会到这里。除非内部错误。
                if (null != isRequestSaved && isRequestSaved.Value)
                {
                    actionWhenError?.Invoke(from, Procedure.Excption);
                }
                logger.Error(ex, procdure.ActionName);
                return(Procedure.Excption);
            }
        }
コード例 #4
0
    private void Start()
    {
        // Fix the target framerate for standalone platforms
        Application.targetFrameRate = 60;

        // Get spawn transforms so that we can access them from another thread
        spawnTrans = spawnTransforms.Select(x => new Trans(x.position, x.rotation, x.name)).ToList();

        // Fetch the ball controller from the scene
        ballController = GameObject.Find(Constants.Ball).GetComponent <BallController>();

        // Get port and protocol from config file
        try
        {
            Configuration serverConfig = Configuration.LoadFromFile("ServerConfig.cfg");
            port = serverConfig["Config"]["Port"].IntValue;
            string protoString = serverConfig["Config"]["Protocol"].StringValue.ToUpper();
            if (protoString.Equals("UDP"))
            {
                protocol = Net.Protocol.Udp;
            }
            else if (protoString.Equals("TCP"))
            {
                protocol = Net.Protocol.Tcp;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        catch
        {
            Debug.LogWarning("Failed to load Configuration file!");
            Debug.LogWarning("Using the default values: [" + port + "] (TCP)");
            protocol = Net.Protocol.Tcp;
        }

        // Create the server
        server = Net.ServerFactory.Create(protocol);

        // Start the server
        server.OnRecv += OnMsgRecv;
        server.Start(Constants.PORT);
    }
コード例 #5
0
    private void Start()
    {
        // Fix the target framerate
        Application.targetFrameRate = 90;

        // Cache text labels
        recvTextField = GameObject.Find("RecvTxt").GetComponent <Text>();
        onlineTxt     = GameObject.Find("OnlineTxt").GetComponent <Text>();

        // Get ip, port and protocol from config file
        try
        {
            Configuration clientConfig = Configuration.LoadFromFile("ClientConfig.cfg");
            ip   = clientConfig["Config"]["IP"].StringValue;
            port = clientConfig["Config"]["Port"].IntValue;
            string protoString = clientConfig["Config"]["Protocol"].StringValue.ToUpper();
            if (protoString.Equals("UDP"))
            {
                protocol = Net.Protocol.Udp;
            }
            else if (protoString.Equals("TCP"))
            {
                protocol = Net.Protocol.Tcp;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        catch
        {
            Debug.LogWarning("Failed to load Configuration file!");
            Debug.LogWarning("Using the default values: [" + ip + ":" + port + "] (TCP)");
            protocol = Net.Protocol.Tcp;
        }

        // Create the client
        client = Net.ClientFactory.Create(protocol);

        // Start the client
        client.OnRecv  += OnMsgRecv;
        client.OnError += OnError;
        client.Start(ip, port);
    }
コード例 #6
0
        public static int Call(Func <int> func, Net.Protocol p,
                               Action <Net.Protocol, int> actionWhenError = null)
        {
            bool IsRequestSaved = p.IsRequest; // 记住这个,以后可能会被改变。

            try
            {
                int result = func();
                if (result != 0 && IsRequestSaved)
                {
                    actionWhenError?.Invoke(p, result);
                }
                LogAndStatistics(result, p, IsRequestSaved);
                return(result);
            }
            catch (Exception ex)
            {
                while (ex is AggregateException)
                {
                    ex = ex.InnerException;
                }

                var errorCode = ex is TaskCanceledException
                    ? Procedure.CancelExcption
                    : Procedure.Excption;

                if (IsRequestSaved)
                {
                    actionWhenError?.Invoke(p, errorCode);
                }

                LogAndStatistics(errorCode, p, IsRequestSaved);
                // 使用 InnerException
                logger.Error(ex, "Task {0} Exception UserState={1}",
                             p.GetType().FullName, p.UserState);
                return(errorCode);
            }
        }