Exemple #1
0
        public static async void Login()
        {
            try
            {
                Game.Scene.AddComponent <MessageDispatherComponent, AppType>(AppType.Client);
                ClientConfig      clientConfig     = Game.Scene.AddComponent <ClientConfigComponent>().Config.GetComponent <ClientConfig>();
                NetOuterComponent networkComponent = Game.Scene.AddComponent <NetOuterComponent>();
                using (Session session = networkComponent.Create(clientConfig.Address))
                {
                    R2C_Login s2CLogin = await session.Call <C2R_Login, R2C_Login>(new C2R_Login { Account = "abcdef", Password = "******" });

                    networkComponent.Remove(session.Id);

                    // 连接Gate
                    using (Session gateSession = networkComponent.Create(s2CLogin.Address))
                    {
                        await gateSession.Call <C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
                    }

                    Log.Info("连接Gate验证成功!");
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }
        private void Start()
        {
            try
            {
                //异步操作回调到主线程
                SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
                Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);

                //添加ET组件到Game.Scene

                ///网络组件
                m_NetOuter = Game.Scene.AddComponent <NetOuterComponent>();

                //帧同步组件
                Game.Scene.AddComponent <ClientFrameComponent>();

                //消息识别码组件
                Game.Scene.AddComponent <OpcodeTypeComponent>();

                //消息分发组件
                Game.Scene.AddComponent <MessageDispatherComponent>();
            }
            catch (System.Exception e)
            {
                throw new GameFrameworkException("初始化ET网络组件时出现异常:" + e);
            }
        }
        private void OnGUI()
        {
            if (!Application.isPlaying)
            {
                GUILayout.Label("请启动游戏!");
                return;
            }


            AppType reloadType = AppType.None;

            this.isAll = GUILayout.Toggle(this.isAll, "All");
            if (this.isAll)
            {
                for (int i = 0; i < this.isCheck.Length; ++i)
                {
                    this.isCheck[i] = true;
                }
            }

            for (int i = 0; i < this.serverTypes.Count; ++i)
            {
                this.isCheck[i] = GUILayout.Toggle(this.isCheck[i], this.serverTypes[i].ToString());
                if (!this.isCheck[i])
                {
                    this.isAll = false;
                }
            }

            this.managerAddress = EditorGUILayout.TextField("Manager Address: ", this.managerAddress);

            if (GUILayout.Button("Reload"))
            {
                for (int i = 0; i < this.isCheck.Length; ++i)
                {
                    if (this.isCheck[i])
                    {
                        reloadType = reloadType | this.serverTypes[i];
                    }
                }
                NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();
                using (Session session = networkComponent.Create($"{this.managerAddress}"))
                {
                    try
                    {
                        session.Call <C2M_Reload, M2C_Reload>(new C2M_Reload {
                            AppType = reloadType
                        });
                    }
                    catch (RpcException e)
                    {
                        Log.Error(e.ToString());
                    }
                }

                Log.Info("Reload OK!");
            }
        }
Exemple #4
0
        public static void Awake(this BenchmarkComponent self, IPEndPoint ipEndPoint)
        {
            try
            {
                NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();
                self.Session = networkComponent.Create(ipEndPoint);

                self.TestLoginAsync();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
 public static void Awake(this WebSocketBenchmarkComponent self, string address)
 {
     try
     {
         NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();
         for (int i = 0; i < 1000; i++)
         {
             self.TestAsync(networkComponent, i, address).Coroutine();
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
 public static void Awake(this BenchmarkComponent self, IPEndPoint ipEndPoint)
 {
     try
     {
         NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();
         for (int i = 0; i < 1000; i++)
         {
             self.TestAsync(networkComponent, ipEndPoint, i);
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
Exemple #7
0
 public static void Awake(this BenchmarkComponent self, string address)
 {
     try
     {
         IPEndPoint        ipEndPoint       = NetworkHelper.ToIPEndPoint(address);
         NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();
         for (int i = 0; i < 1000; i++)
         {
             self.TestAsync(networkComponent, ipEndPoint, i).Coroutine();
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
        private static async void Awake(this BenchmakComponent component, string address)
        {
            try
            {
                NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();

                for (int i = 0; i < 100; i++)
                {
                    await Game.Scene.GetComponent <TimerComponent>().WaitAsync(10);

                    component.TestAsync(networkComponent, address, i);
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }
Exemple #9
0
 public static async void TestAsync(this BenchmarkComponent self, NetOuterComponent networkComponent, IPEndPoint ipEndPoint, int j)
 {
     try
     {
         using (Session session = networkComponent.Create(ipEndPoint))
         {
             int i = 0;
             while (i < 100000000)
             {
                 ++i;
                 await self.Send(session, j);
             }
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
 public static async ETVoid TestAsync(this WebSocketBenchmarkComponent self, NetOuterComponent networkComponent, int j, string address)
 {
     try
     {
         using (Session session = networkComponent.Create(address))
         {
             int i = 0;
             while (i < 100000000)
             {
                 ++i;
                 await self.Send(session, j);
             }
         }
     }
     catch (RpcException e)
     {
         Log.Error(e);
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
Exemple #11
0
 public static void Update(this NetOuterComponent self)
 {
     self.Update();
 }
Exemple #12
0
 public static void Awake(this NetOuterComponent self, IPEndPoint ipEndPoint)
 {
     self.Awake(NetworkProtocol.TCP, ipEndPoint);
     self.MessagePacker     = new ProtobufPacker();
     self.MessageDispatcher = new OuterMessageDispatcher();
 }
Exemple #13
0
        public static async void Awake(this BenchmarkComponent self, ClientConfig clientConfig)
        {
            try
            {
                self.timerComponent  = Game.Scene.GetComponent <TimerComponent>();
                self.networkProfiler = new ProfilerUtility.NetworkProfiler();

                if (!File.Exists(clientConfig.UserCollectionPath))
                {
                    Console.WriteLine($"Invalid user data path: {clientConfig.UserCollectionPath}");
                    return;
                }
                IPEndPoint        ipEndPoint       = ETModel.NetworkHelper.ToIPEndPoint(clientConfig.Address);
                NetOuterComponent networkComponent = Game.Scene.GetComponent <NetOuterComponent>();

                string json = await File.ReadAllTextAsync(clientConfig.UserCollectionPath);

                List <BsonDocument> users = BsonSerializer.Deserialize <List <BsonDocument> >(json);
                //users = users.Where(e => e["identity"].AsInt32 == (int)User.Identity.TestPlayer).ToList();
                users = users.Where(e => e["userId"].AsString.Length == 10).ToList();

                int limit = clientConfig.Count * clientConfig.RobotCount;
                for (int i = clientConfig.CurrentRobot, j = 0; i < limit; i += clientConfig.RobotCount, j++)
                {
                    //self.TestAsync(networkComponent, ipEndPoint, i);
                    if (i >= users.Count)
                    {
                        Console.WriteLine($"testing player is over count of collection");
                        break;
                    }
                    BsonDocument      user = users[i];
                    TestPlayerSetting testPlayerSetting = new TestPlayerSetting();
                    //testPlayerSetting.DeviceUniqueIdentifier = user["email"].AsString;
                    testPlayerSetting.DeviceUniqueIdentifier = user["userId"].AsString;
                    BenchmarkClientComponent.ClientSetting clientSetting = new BenchmarkClientComponent.ClientSetting
                    {
                        networkComponent  = networkComponent,
                        ipEndPoint        = ipEndPoint,
                        testPlayerSetting = testPlayerSetting,
                        robotMode         = (BenchmarkClientComponent.RobotMode)clientConfig.RobotMode,
                        roadSettingId     = clientConfig.RoadSettingId,
                    };
                    var client = ComponentFactory.Create <BenchmarkClientComponent, BenchmarkClientComponent.ClientSetting>(clientSetting);
                    client.index = j;
                    self.clients.Add(testPlayerSetting.DeviceUniqueIdentifier, client);
                    self.clientList.Add(client);
                }

                while (!self.IsDisposed)
                {
                    await self.timerComponent.WaitForSecondAsync(1);

                    if (self.watchTargetList.Count != 0)
                    {
                        foreach (var v in self.clientList.Where(e => self.watchTargetList.Contains(e.index)))
                        {
                            v.PrintMessage();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Exemple #14
0
        public async void OnVisitorLogin()
        {
            if (isLogining || this.IsDisposed)
            {
                return;
            }
            NetOuterComponent netOuterComponent = ETModel.Game.Scene.GetComponent <NetOuterComponent>();

            //设置登录中状态
            isLogining = true;
            SessionWrap sessionWrap = null;

            try
            {
                string macAddress = SystemInfo.deviceUniqueIdentifier;
                if (!Define.IsEditorMode)
                {
                    macAddress += System.DateTime.Now.ToString();
                }
                string password = "******";

                IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
                Session    session        = netOuterComponent.Create(connetEndPoint);
                sessionWrap = new SessionWrap(session);
                sessionWrap.session.GetComponent <SessionCallbackComponent>().DisposeCallback += s =>
                {
                    if (Game.Scene.GetComponent <UIComponent>()?.Get(UIType.UILogin) != null)
                    {
                        isLogining = false;
                    }
                };

                R2C_Login r2CLogin = (R2C_Login)await sessionWrap.Call(new C2R_Login()
                {
                    Account = macAddress, Password = password
                });

                sessionWrap.Dispose();

                if (this.IsDisposed)
                {
                    return;
                }

                if (r2CLogin.Error == ErrorCode.ERR_AccountOrPasswordError)
                {
                    return;
                }

                connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
                Session gateSession = netOuterComponent.Create(connetEndPoint);
                Game.Scene.AddComponent <SessionWrapComponent>().Session = new SessionWrap(gateSession);
                //SessionWeap添加连接断开组件,用于处理客户端连接断开
                Game.Scene.GetComponent <SessionWrapComponent>().Session.AddComponent <SessionOfflineComponent>();
                ETModel.Game.Scene.AddComponent <SessionComponent>().Session = gateSession;
                G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionWrapComponent.Instance.Session.Call(new C2G_LoginGate()
                {
                    Key = r2CLogin.Key
                });

                if (g2CLoginGate.Error == ErrorCode.ERR_ConnectGateKeyError)
                {
                    Game.Scene.GetComponent <SessionWrapComponent>().Session.Dispose();
                    return;
                }

                Log.Info("登陆gate成功!");

                //保存本地玩家
                User user = ETModel.ComponentFactory.CreateWithId <User, long>(g2CLoginGate.PlayerId, g2CLoginGate.UserId);
                ClientComponent.Instance.LocalPlayer = user;

                Game.Scene.GetComponent <UIComponent>().Create(UIType.UILobby);
                Game.Scene.GetComponent <UIComponent>().Remove(UIType.UILogin);
            }
            catch (Exception e)
            {
                sessionWrap?.Dispose();
                Log.Error(e);
            }
            finally
            {
                //断开验证服务器的连接
                netOuterComponent.Remove(sessionWrap.session.Id);
                //设置登录处理完成状态
                isLogining = false;
            }
        }
Exemple #15
0
 public static void Awake(this NetOuterComponent self, string host, int port)
 {
     self.Awake(NetworkProtocol.TCP, host, port);
     self.MessagePacker     = new MongoPacker();
     self.MessageDispatcher = new OuterMessageDispatcher();
 }
Exemple #16
0
 public static void Awake(this NetOuterComponent self)
 {
     self.Awake(NetworkProtocol.TCP);
     self.MessagePacker     = new MongoPacker();
     self.MessageDispatcher = new OuterMessageDispatcher();
 }
        /// <summary>
        /// 登录按钮事件
        /// </summary>
        public async void OnLogin()
        {
            if (isLogining || this.IsDisposed)
            {
                return;
            }
            NetOuterComponent netOuterComponent = Game.Scene.ModelScene.GetComponent <NetOuterComponent>();

            //设置登录中状态
            isLogining = true;
            Session sessionWrap = null;

            try
            {
                //创建登录服务器连接
                IPEndPoint      connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
                ETModel.Session session        = netOuterComponent.Create(connetEndPoint);
                sessionWrap = ComponentFactory.Create <Session, ETModel.Session>(session);
                sessionWrap.session.GetComponent <SessionCallbackComponent>().DisposeCallback += s =>
                {
                    if (Game.Scene.GetComponent <UIComponent>()?.Get(UIType.LandlordsLogin) != null)
                    {
                        prompt.text = "连接失败";
                        isLogining  = false;
                    }
                };

                //发送登录请求
                prompt.text = "正在登录中....";
                R2C_Login_Ack r2C_Login_Ack = await sessionWrap.Call(new C2R_Login_Req()
                {
                    Account = account.text, Password = password.text
                }) as R2C_Login_Ack;

                prompt.text = "";

                if (this.IsDisposed)
                {
                    return;
                }

                if (r2C_Login_Ack.Error == ErrorCode.ERR_LoginError)
                {
                    prompt.text   = "登录失败,账号或密码错误";
                    password.text = "";
                    return;
                }

                //创建Gate服务器连接
                connetEndPoint = NetworkHelper.ToIPEndPoint(r2C_Login_Ack.Address);
                ETModel.Session gateSession = netOuterComponent.Create(connetEndPoint);
                Game.Scene.AddComponent <SessionComponent>().Session = ComponentFactory.Create <Session, ETModel.Session>(gateSession);
                //SessionWeap添加连接断开组件,用于处理客户端连接断开
                Game.Scene.GetComponent <SessionComponent>().Session.AddComponent <SessionOfflineComponent>();
                Game.Scene.ModelScene.AddComponent <ETModel.SessionComponent>().Session = gateSession;

                //登录Gate服务器
                G2C_LoginGate_Ack g2C_LoginGate_Ack = await SessionComponent.Instance.Session.Call(new C2G_LoginGate_Req()
                {
                    Key = r2C_Login_Ack.Key
                }) as G2C_LoginGate_Ack;

                if (g2C_LoginGate_Ack.Error == ErrorCode.ERR_ConnectGateKeyError)
                {
                    prompt.text   = "连接网关服务器超时";
                    password.text = "";
                    Game.Scene.GetComponent <SessionComponent>().Session.Dispose();
                    return;
                }

                Log.Info("登录成功");

                //保存本地玩家
                User user = ETModel.ComponentFactory.CreateWithId <User, long>(g2C_LoginGate_Ack.PlayerID, g2C_LoginGate_Ack.UserID);
                ClientComponent.Instance.LocalPlayer = user;

                //跳转到大厅界面
                Game.Scene.GetComponent <UIComponent>().Create(UIType.LandlordsLobby);
                Game.Scene.GetComponent <UIComponent>().Remove(UIType.LandlordsLogin);
            }
            catch (Exception e)
            {
                prompt.text = "登录异常";
                Log.Error(e.ToStr());
            }
            finally
            {
                //断开验证服务器的连接
                sessionWrap.Dispose();
                //设置登录处理完成状态
                isLogining = false;
            }
        }