Ejemplo n.º 1
0
        public static int NewService(string serviceClass, string serviceName, byte[] param)
        {
            Type   type = Type.GetType(serviceClass);
            object obj  = Activator.CreateInstance(type);

            ServiceContext service = obj as ServiceContext;

            ServiceSlots.GetInstance().Add(service);

            Message initMsg = new Message();

            initMsg.Source      = 0;
            initMsg.Destination = service.GetId();
            initMsg.Method      = "Init";
            initMsg.RPCSession  = 0;
            initMsg.Data        = param;
            initMsg.Type        = MessageType.ServiceRequest;

            service.Push(initMsg);

            if (serviceName != "")
            {
                ServiceSlots.GetInstance().Name(service.GetId(), serviceName);
            }

            LoggerHelper.Info(service.GetId(), string.Format("{0} launched", serviceName));

            return(service.GetId());
        }
Ejemplo n.º 2
0
        public void Loop()
        {
            long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            int  count     = m_timerNodeQueue.Count;

            for (int i = 0; i < count; i++)
            {
                SSTimerNode timerNode = null;
                if (m_timerNodeQueue.TryDequeue(out timerNode))
                {
                    if (timestamp >= timerNode.TimeoutTimestamp)
                    {
                        Message msg = new Message();
                        msg.Source      = 0;
                        msg.Destination = timerNode.Opaque;
                        msg.Method      = "";
                        msg.Data        = null;
                        msg.RPCSession  = timerNode.Session;
                        msg.Type        = MessageType.Timer;

                        ServiceContext service = ServiceSlots.GetInstance().Get(timerNode.Opaque);
                        service.Push(msg);
                    }
                    else
                    {
                        m_timerNodeQueue.Enqueue(timerNode);
                    }
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        public static void Info(int source, string msg)
        {
            string        logger        = "logger";
            LoggerService loggerService = (LoggerService)ServiceSlots.GetInstance().Get(logger);

            Message message = new Message();

            message.Method      = "OnLog";
            message.Data        = Encoding.ASCII.GetBytes(msg);
            message.Destination = loggerService.GetId();
            message.Source      = source;
            message.Type        = MessageType.ServiceRequest;
            loggerService.Push(message);
        }
Ejemplo n.º 4
0
        private void OnReadPacketComplete(int opaque, long sessionId, byte[] buffer, int packetSize)
        {
            SocketData data = new SocketData();

            data.connection = sessionId;
            data.buffer     = Convert.ToBase64String(buffer);

            Message msg = new Message();

            msg.Source      = 0;
            msg.Destination = opaque;
            msg.Method      = "SocketData";
            msg.Data        = data.encode();
            msg.RPCSession  = 0;
            msg.Type        = MessageType.Socket;

            ServiceContext service = ServiceSlots.GetInstance().Get(opaque);

            service.Push(msg);
        }
Ejemplo n.º 5
0
        private void OnConnectedComplete(int opaque, long sessionId, string ip, int port)
        {
            ClusterClientSocketConnected connected = new ClusterClientSocketConnected();

            connected.connection = sessionId;
            connected.ip         = ip;
            connected.port       = port;

            Message msg = new Message();

            msg.Source      = 0;
            msg.Destination = opaque;
            msg.Method      = "SocketConnected";
            msg.Data        = connected.encode();
            msg.RPCSession  = 0;
            msg.Type        = MessageType.Socket;

            ServiceContext service = ServiceSlots.GetInstance().Get(opaque);

            service.Push(msg);
        }
Ejemplo n.º 6
0
        private void OnAcceptComplete(int opaque, long sessionId, string ip, int port)
        {
            SocketAccept accept = new SocketAccept();

            accept.connection = sessionId;
            accept.ip         = ip;
            accept.port       = port;

            Message msg = new Message();

            msg.Source      = 0;
            msg.Destination = opaque;
            msg.Method      = "SocketAccept";
            msg.Data        = accept.encode();
            msg.RPCSession  = 0;
            msg.Type        = MessageType.Socket;

            ServiceContext service = ServiceSlots.GetInstance().Get(opaque);

            service.Push(msg);
        }
Ejemplo n.º 7
0
        private void OnSessionError(int opaque, long sessionId, string remoteEndPoint, int errorCode, string errorText)
        {
            SocketError sprotoSocketError = new SocketError();

            sprotoSocketError.errorCode      = errorCode;
            sprotoSocketError.errorText      = errorText;
            sprotoSocketError.connection     = sessionId;
            sprotoSocketError.remoteEndPoint = remoteEndPoint;

            Message msg = new Message();

            msg.Source      = 0;
            msg.Destination = opaque;
            msg.Method      = "SocketError";
            msg.Data        = sprotoSocketError.encode();
            msg.RPCSession  = 0;
            msg.Type        = MessageType.Socket;

            ServiceContext service = ServiceSlots.GetInstance().Get(opaque);

            service.Push(msg);
        }
Ejemplo n.º 8
0
        private void Boot(BootServices customBoot)
        {
            // create global instance first
            m_globalMQ     = GlobalMQ.GetInstance();
            m_serviceSlots = ServiceSlots.GetInstance();
            m_netpackQueue = NetworkPacketQueue.GetInstance();
            m_timer        = SSTimer.GetInstance();

            NetProtocol.GetInstance();

            // create logger service second
            Logger_Init loggerInit = new Logger_Init();

            if (m_bootConfig.ContainsKey("Logger"))
            {
                if (Directory.Exists(m_bootConfig["Logger"].ToString()))
                {
                    loggerInit.logger_path = Path.GetFullPath(m_bootConfig["Logger"].ToString());
                }
                else
                {
                    DirectoryInfo di = Directory.CreateDirectory(m_bootConfig["Logger"].ToString());
                    if (di.Exists)
                    {
                        loggerInit.logger_path = Path.GetFullPath(m_bootConfig["Logger"].ToString());
                    }
                    else
                    {
                        loggerInit.logger_path = "../";
                    }
                }
            }
            else
            {
                loggerInit.logger_path = "../";
            }
            SparkServerUtility.NewService("SparkServer.Framework.Service.Logger.LoggerService", "logger", loggerInit.encode());

            m_tcpObjectContainer = new TCPObjectContainer();
            if (m_bootConfig.ContainsKey("ClusterConfig"))
            {
                InitCluster();
            }

            if (m_bootConfig.ContainsKey("Gateway"))
            {
                InitGateway();
            }

            customBoot();

            LoggerHelper.Info(0, "Start SparkServer Server...");

            for (int i = 0; i < m_workerNum; i++)
            {
                Thread thread = new Thread(new ThreadStart(ThreadWorker));
                thread.Start();
            }

            Thread timerThread = new Thread(new ThreadStart(ThreadTimer));

            timerThread.Start();
        }