예제 #1
0
 public Task ReceiveAsync(IContext context)
 {
     switch (context.Message)
     {
     case RequestPackage package:
         //ResponsePackage repPackage = new ResponsePackage();
         //repPackage.InitPackage(package);
         //IByteBuffer data = GameEnvironment.ActionDispatcher.TryEncodePackage(repPackage);
         //var session = SessionManager.Get(repPackage.SessionId);
         //session.PostSend(data);
         RequestData reqPackage = new RequestData();
         reqPackage.ActorName = ActorManagement.GetActorName <LoginActor>();
         var loginPid = ActorManagement.GetOrLoadPid <LoginActor>();
         loginPid.Tell(reqPackage);
         break;
     }
     return(Actor.Done);
 }
예제 #2
0
        static void Main(string[] args)
        {
            //ActorConfig config = new ActorConfig();
            //config.LocationIPAddress = "127.0.0.1";
            //config.LocationPort = 8001;
            //config.LocationActorName = "LOCATIONACTORNAME";
            //config.ActorIPAddress = "127.0.0.1";
            //config.ActorPort = 9001;
            ////RedisConfig config = new RedisConfig();
            ////config.PreRedisKey = "PLUSDATA";
            ////config.RedisHost = "127.0.0.1:6379";
            ////config.RedisDB = 0;
            //string txt = Newtonsoft.Json.JsonConvert.SerializeObject(config);
            //System.IO.File.WriteAllText(string.Format("config/{0}.txt", config.GetType().Name), txt);
            //try
            //{
            //    RedisHelper.Init();

            //    RedisHelper.HashSet<GameUser>("GAMEUSER", "10038", new GameUser() { UserId = 10038, NickName = "test" });
            //    var user = RedisHelper.HashGet<GameUser>("GAMEUSER", "10038");

            //    Stopwatch watch = new Stopwatch();
            //    watch.Start();
            //    for (int i = 0; i < 100000; i++)
            //    {
            //        RedisHelper.StringSet(i.ToString(), i.ToString());
            //        RedisHelper.StringGet(i.ToString());
            //    }
            //    watch.Stop();
            //    Console.WriteLine(watch.ElapsedMilliseconds);
            //}
            //catch (Exception ex)
            //{

            //}

            ServerHost host = new ServerHost();

            try
            {
                Serialization.RegisterFileDescriptor(Messages.ProtosReflection.Descriptor);
                Remote.Start(GameEnvironment.ActorConfig.ActorIPAddress, GameEnvironment.ActorConfig.ActorPort);

                #region Location Server

                var serverLocationPid = Actor.SpawnNamed(
                    Actor.FromProducer(() => new LocationActor()),
                    GameEnvironment.ActorConfig.LocationActorName);

                #endregion

                var locationPid = new PID(string.Format("{0}:{1}",
                                                        GameEnvironment.ActorConfig.LocationIPAddress,
                                                        GameEnvironment.ActorConfig.LocationPort),
                                          GameEnvironment.ActorConfig.LocationActorName);

                ActorManagement.SetLocationPid(locationPid);

                #region Data Server

                var register = Actor.SpawnNamed(
                    Actor.FromProducer(() => new RegisterActor()),
                    ActorManagement.GetActorName <RegisterActor>());

                ActorManagement.RegisterPid <RegisterActor>(register);

                #endregion

                #region Login Server

                var loginPid = Actor.SpawnNamed(
                    Actor.FromProducer(() => new LoginActor()),
                    ActorManagement.GetActorName <LoginActor>());

                ActorManagement.RegisterPid <LoginActor>(loginPid);

                #endregion

                host.RunServerAsync().Wait();

                TraceLog.WriteInfo("服务器{0},{1}启动成功", GameEnvironment.SocketConfig.IPAddress, GameEnvironment.SocketConfig.Port);

                Task.Run(() =>
                {
                    while (true)
                    {
                        Task.Delay(1000).Wait();
                        Console.WriteLine("Session count:{0},{1}:{2}", SessionManager.GetCount(), ActorManagement.GetPidCount(), ActorManagement.GetPidTypeNames());
                        Console.WriteLine("线程个数:{0}", ThreadIds.Count);
                    }
                });

                Console.Read();
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("服务器{0},{1}启动失败,{2},{3}", GameEnvironment.SocketConfig.IPAddress, GameEnvironment.SocketConfig.Port, ex.Message, ex.StackTrace);
            }
            finally
            {
                host.CloseServerAsync().Wait();
            }
        }