コード例 #1
0
ファイル: PanelTitle.cs プロジェクト: leekcake/LastOutsider
    public async Task CallbackTouch()
    {
        if (TouchProgress)
        {
            return;
        }
        TouchProgress = true;

        var account = Consts.GetAccountFromFile();
        var socket  = await NetworkManager.Instance.GetGameSocketAsync(loginIfMissing : false);

        if (account == null)
        {
            var authToken = new byte[128];
            var rnd       = new RNGCryptoServiceProvider();
            rnd.GetBytes(authToken);

            account = await ConnectCanvas.Instance.CreateConnectInformation <Account>("계정 생성", (listener) =>
            {
                socket.GenerateAccountAsync(authToken, listener);
            }, null).WaitAsync();

            if (account == null)
            {
                return;
            }
            await AsyncUtil.WriteBytesAsync(Consts.ACCOUNT_FILE, MessagePackSerializer.Serialize(account));

            beforeLogin.SetActive(false);
            afterLogin.SetActive(true);
        }
        else
        {
            await ConnectCanvas.Instance.CreateConnectInformation("로그인", (listener) =>
            {
                if (account == null)
                {
                    throw new Exception("계정이 없음");
                }
                socket.LoginAccountAsync(account.Id, account.AuthToken, listener);
            }, null)
            .StartAfter(ConnectCanvas.Instance.CreateConnectInformation("시작 데이터 가져오는중", (listener) =>
            {
                socket.FetchDataAsync(listener);
            }, new FinishListener <FetchData>((data) =>
            {
                DataManager.Instance.ReadFetchData(data);
            }, (message) => {  }), autoStart: false
                                                                        )).WaitAsync();

            SceneManager.LoadScene("MainScene");
        }
        TouchProgress = false;
    }
コード例 #2
0
    /// <summary>
    /// 저장되어 있는 게임 소켓을 반환합니다
    /// </summary>
    /// <param name="createIfMissing">참일시 소켓이 없는경우 서버와 연결합니다</param>
    /// <returns></returns>
    public async Task <GameSocket> GetGameSocketAsync(bool createIfMissing = true, bool loginIfMissing = true)
    {
        while (SocketInCreate)
        {
            await Task.Delay(1);
        }

        if (gameSocket == null && createIfMissing)
        {
            SocketInCreate         = true;
            tcpClient              = new TcpClient();
            gameSocket             = new GameSocket();
            gameSocket.printHelper = new Printer();
            var handshakeCI = ConnectCanvas.Instance.CreateConnectInformation("서버에 연결", async(listener) =>
            {
                try
                {
                    await tcpClient.ConnectAsync(serverIP, serverPort);
                    gameSocket.AttachNetworkStream(tcpClient.GetStream());
                    listener.OnFinish();
                }
                catch (Exception ex)
                {
                    listener.OnError(ex.Message);
                }
            }, null, -1)
                              .StartAfter(ConnectCanvas.Instance.CreateConnectInformation("연결을 암호화", (receiver) =>
            {
                gameSocket.HandshakeAsync(receiver);
                if (!loginIfMissing)
                {
                    SocketInCreate = false;
                }
            }, null, autoStart: false));

            if (loginIfMissing)
            {
                var account = Consts.GetAccountFromFile();
                await handshakeCI.StartAfter(ConnectCanvas.Instance.CreateConnectInformation("로그인", (listener) =>
                {
                    if (account == null)
                    {
                        throw new Exception("계정이 없음");
                    }
                    gameSocket.LoginAccountAsync(account.Id, account.AuthToken, listener);
                }, null, autoStart: false))
                .StartAfter(ConnectCanvas.Instance.CreateConnectInformation("데이터를 다시 가져오는중", (listener) =>
                {
                    gameSocket.FetchDataAsync(listener);
                }, new FinishListener <FetchData>((data) =>
                {
                    DataManager.Instance.ReadFetchData(data);
                    SocketInCreate = false;
                }, (message) => { })
                                                                            )).WaitAsync();
            }
            else
            {
                await handshakeCI.WaitAsync();
            }
        }
        return(gameSocket);
    }