Example #1
0
        public void SendProtocol()
        {
            Dictionary <string, string> args = new Dictionary <string, string>();

            foreach (KVString kv in arguments)
            {
                args.Add(kv.key, kv.val);
            }
            HoxisProtocol proto = new HoxisProtocol
            {
                type   = protocolType,
                handle = FF.ObjectToJson(new ReqHandle {
                    req = method, ts = SF.GetTimeStamp(TimeUnit.Millisecond)
                }),
                err      = error,
                receiver = new HoxisProtocolReceiver {
                    type = receiverType, uid = receiverUID
                },
                sender = new HoxisProtocolSender {
                    uid = senderUID, aid = senderAgentID, loopback = loopback
                },
                action = new HoxisProtocolAction(method, new HoxisProtocolArgs(args)),
                desc   = description
            };

            HoxisDirector.Ins.ProtocolPost(proto);
        }
Example #2
0
 public void LogTitle(string copyright, string project, string version)
 {
     Log("=============== Hoxis Server ===============");
     Log("-- Copyright: " + copyright);
     Log("-- Project: " + project);
     Log("-- Version: " + version);
     Log("-- IPv4: " + SF.GetLocalIP());
     Log("-- Platform: " + SF.GetOSVersion());
     Log("-- Time: " + SF.GetDateTime().ToString());
     Log("=================== logs ===================");
 }
Example #3
0
        /// <summary>
        /// Rapidly send a request protocol
        /// </summary>
        /// <param name="method"></param>
        /// <param name="kvs"></param>
        public void Request(string method, params KVString[] kvs)
        {
            HoxisProtocol proto = new HoxisProtocol
            {
                type   = ProtocolType.Request,
                handle = FF.ObjectToJson(new ReqHandle {
                    req = method, ts = SF.GetTimeStamp(TimeUnit.Millisecond)
                }),
                err      = "",
                receiver = HoxisProtocolReceiver.undef,
                sender   = HoxisProtocolSender.undef,
                action   = new HoxisProtocolAction(method, kvs),
                desc     = ""
            };

            ProtocolPost(proto);
            // todo wait for response
        }
Example #4
0
        public void LogTag(string content, string tag = "", string speaker = "", bool console = false)
        {
            string s;

            if (speaker == "")
            {
                s = FF.StringFormat("[{0}]{1}:  {2}", SF.GetDateTime(), tag, content);
            }
            else
            {
                s = FF.StringFormat("[{0}]{1}:  {2}--->{3}", SF.GetDateTime(), tag, speaker, content);
            }
            _writer.WriteLine(s);
            if (console)
            {
                Console.WriteLine(s);
            }
        }
        /// <summary>
        /// Check the request name and time stamp
        /// </summary>
        /// <param name="proto"></param>
        /// <param name="ret"></param>
        public void CheckRequest(HoxisProtocol proto, out Ret ret)
        {
            ReqHandle handle = FF.JsonToObject <ReqHandle>(proto.handle, out ret);

            if (ret.code != 0)
            {
                return;
            }
            // Check if request name matches method name
            if (handle.req != proto.action.method)
            {
                ret = new Ret(LogLevel.Info, 1, "request name doesn't match method name"); return;
            }
            // Check if expired
            long ts   = handle.ts;
            long intv = Math.Abs(SF.GetTimeStamp(TimeUnit.Millisecond) - ts);

            if (intv > requestTTL)
            {
                ret = new Ret(LogLevel.Info, 1, "request is expired"); return;
            }
            ret = Ret.ok;
        }
 /// <summary>
 /// New log name of an user
 /// </summary>
 /// <param name="uid"></param>
 /// <returns></returns>
 public static string NewUserLogName(long uid)
 {
     return(FF.StringAppend(uid.ToString(), "@", SF.GetTimeStamp().ToString(), ".log"));
 }
        /// <summary>
        /// Init the configuration, such as the ip, port, socket and arguments
        /// </summary>
        /// <param name="configPath"></param>
        public void InitializeConfig(out Ret ret, string configPath = "")
        {
            // Init config
            string path;

            if (configPath != "")
            {
                path = configPath;
            }
            else
            {
                path = FF.StringAppend(basicPath, @"..\..\DacLib\Hoxis\Configs\hoxis_server.toml");
            }
            config = new TomlConfiguration(path, out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server", true); return;
            }
            _logger.LogInfo("read configuration success", "Server");

            // Assign ip, port and init the sokcet
            localIP = SF.GetLocalIP(out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            port = config.GetInt("server", "port", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("ip is {0}, port is {1}", localIP, port.ToString()), "Server");

            // Init connection reception
            maxConn = config.GetInt("server", "max_conn", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _connReception = new CriticalPreformPool <HoxisConnection>(maxConn);
            _logger.LogInfo(FF.StringFormat("max connections is {0}", maxConn), "Server");

            // Init affair queue
            affairQueueCapacity = config.GetInt("server", "affair_queue_capacity", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("affair queue capacity is {0}", affairQueueCapacity), "Server");
            affairQueueProcessQuantity = config.GetShort("server", "affair_queue_process_quantity", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("affair queue process quantity is {0}", affairQueueProcessQuantity), "Server");
            _affairQueue               = new FiniteProcessQueue <KV <int, object> >(affairQueueCapacity, affairQueueProcessQuantity);
            _affairQueue.onProcess    += ProcessAffair;
            affairQueueProcessInterval = config.GetInt("server", "affair_queue_process_interval", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("affair queue process interval is {0}ms", affairQueueProcessInterval), "Server");

            // Init heartbeat update
            heartbeatUpdateInterval = config.GetInt("server", "heartbeat_update_interval", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("heartbeat update interval is {0}ms", heartbeatUpdateInterval), "Server");

            // Init connection
            HoxisConnection.readBufferSize = config.GetInt("conn", "read_buffer_size", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("read buffer size of connection is {0}", HoxisConnection.readBufferSize), "Server");

            // Init cluster
            _clusters            = new Dictionary <string, HoxisCluster>();
            HoxisCluster.maxUser = config.GetInt("cluster", "max_user", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("max users of cluster is {0}", HoxisCluster.maxUser), "Server");

            // Init team
            HoxisTeam.maxUser = config.GetInt("team", "max_user", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("max users of team is {0}", HoxisTeam.maxUser), "Server");

            // Init user
            HoxisUser.requestTTL = config.GetLong("user", "request_ttl", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("request time to live is {0}ms", HoxisUser.requestTTL), "Server");
            HoxisUser.heartbeatTimeout = config.GetInt("user", "heartbeat_timeout", out ret);
            if (ret.code != 0)
            {
                _logger.LogFatal(ret.desc, "Server"); return;
            }
            _logger.LogInfo(FF.StringFormat("heartbeat timeout is {0}ms", HoxisUser.heartbeatTimeout), "Server");

            _logger.LogInfo("init success", "Server", true);
        }