Advanced ET Unity of online game development framework 07- correction Guest Sign asynchronous BUG

Copyright notice:

  • This article Original starting in the following website:
  1. Blog Park "excellent dream maker culture" in space: https: //www.cnblogs.com/raymondking123
  2. Excellent dream maker culture official blog: https: //91make.top
  3. Excellent game dream maker culture lecture: https: //91make.ke.qq.com
  4. "Excellent dream maker culture" of micro-channel public number: umaketop
  • You are free to reprint, but must include the full copyright notice!

Origin

  • OnStartClick method on my blog post of async asynchronous method is used, but must perform in its calling thread because Microsoft does not guarantee the creation of async method, we can not guarantee program is single-threaded
  • ET entire framework is based on the single-threaded multi-process design ideas (in order to avoid loss of performance and logic errors caused by multiple threads), so in order to avoid multiple threads trap async result, ET custom set of multithreaded system , which are beginning to return type ET, for example:
    • Task → ETTask
    • void → ETVoid
    • Task → ETTask
    • ET authors said such a sentence: the return value of the async method never use native .NET Task, in all ETTask
  • Supporting, we need to modify the existing code as follows:
public class MyStartUIComponent : Component
{
    GameObject goStartButton;

    public void OnAwake()
    {
        var rc = this.Parent.GameObject.GetComponent<ReferenceCollector>();
        goStartButton = rc.Get<GameObject>("StartButton");
        goStartButton.GetComponent<Button>().onClick.AddListener(OnStartClick);
    }

    private void OnStartClick()
    {
        // Coroutine()方法模拟将OnStartClickAsync()作为协程执行的效果(确保在单线程中执行)
        OnStartClickAsync().Coroutine();
    }

    private async ETVoid OnStartClickAsync()
    {
        // 处理游客登录点击
        try
        {
            // 1: 创建一个realmSession,并利用此session去call一个rpc消息(模型层session表示具体网络参数和连接,热更层负责通信逻辑,它调用模型层)
            ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
            Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);

            // 2:若登录失败,会收到异常;若成功,则会得到认证信息(模型层没有C2R_Login的类定义,所以必须配套由热更层的session去call)
            var r2cLogin = await realmSession.Call(new C2R_Login()
            {
                Account = Guid.NewGuid().ToString(),
                Password = "111111",
            }) as R2C_Login;

            // 3:再利用此认证信息中的网关服务器地址,去创建gateSession
            session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(r2cLogin.Address);
            Session gateSession = ComponentFactory.Create<Session, ETModel.Session>(session);

            // 4:用gateSession去登录gate服(传入登录key)
            var g2cLoginGate = await gateSession.Call(new C2G_LoginGate()
            {
                Key = r2cLogin.Key
            }) as G2C_LoginGate;

            // 5:登录成功后,用返回的playerId,创建Player对象
            var p = ETModel.ComponentFactory.CreateWithId<Player>(g2cLoginGate.PlayerId);
            ETModel.Game.Scene.GetComponent<PlayerComponent>().MyPlayer = p;

            // 6:发送登录完成事件
            Game.EventSystem.Run(MyEventType.MyLoginFinish);
        }
        catch (Exception e)
        {
            Log.Error(e);
        }
    }
}

Guess you like

Origin www.cnblogs.com/raymondking123/p/11369723.html