Beispiel #1
0
    private void HandleMsg3(LoginRspMsg msg)
    {
        var type = msg.GetType();

        Debug.LogFormat("M3 - {0}", msg.Player);
        ShowAsText(string.Format("handle msg => {0} - {1}|{2}", type.FullName, msg.Player.Name, msg.Player.Status));
    }
Beispiel #2
0
    private void HandleMsg2(TestMsg2 msg, IReliableDataLink fromLink)
    {
        var type = msg.GetType();

        Debug.LogFormat("M2 - {0}", msg.Age);
        ShowAsText(string.Format("handle msg => {0} - {1}", type.FullName, msg.Age));
        MessageProcessor.UnRegisterHandler <TestMsg2>(HandleMsg2);
    }
Beispiel #3
0
    private async Task RequstHttp()
    {
        Debug.LogFormat("work task start {0}", Thread.CurrentThread.ManagedThreadId);
        await Task.Delay(1000);

        HttpClient http = new HttpClient();
        var        txt  = await http.GetStringAsync("http://www.baidu.com");

        UnityEngine.Debug.Log(txt);
        Debug.LogFormat("work task stop {0}", Thread.CurrentThread.ManagedThreadId);
    }
Beispiel #4
0
    public void OnClickDeserialize()
    {
        var loginMsg = new LoginReqMsg {
            Account  = "account",
            Password = "******",
            Extra    = "哈哈哈",
        };
        var reqMsgBytes     = MessagePackSerializer.Serialize(loginMsg);
        var reqMsgreqMsgObj = MessagePackSerializer.Deserialize <LoginReqMsg>(reqMsgBytes);

        var registerMsg = new RegisterReqMsg {
            Phone    = "1350000",
            Authcode = "验证码",
        };

        var fs  = new FileStream(Path.Combine(Application.dataPath, "Resources", "Test2.bytes"), FileMode.Open);
        var _as = new byte[fs.Length];

        fs.Read(_as, 0, _as.Length);
        fs.Close();
        fs.Dispose();

        var a = new CancellationTokenSource();

        Debug.LogFormat("unity main {0}", Thread.CurrentThread.ManagedThreadId);


        var mainThreadTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();

        Task.Run(() => {
            Debug.LogFormat("Main Task Thread [{0}]", Thread.CurrentThread.ManagedThreadId);
            var msgpackTask = new Task(async() => {
                Debug.LogFormat("MsgpackTask Task Thread [{0}]", Thread.CurrentThread.ManagedThreadId);
                await Task.Delay(100);
                var registerMsgBytes = MessagePackSerializer.Serialize(registerMsg);
                var deserializeMsg   = MessagePackSerializer.Deserialize <RegisterReqMsg>(registerMsgBytes);
                text.text            = string.Format("Account : {0}-{1}-{2}", reqMsgreqMsgObj.Account
                                                     , reqMsgreqMsgObj.Password, reqMsgreqMsgObj.Extra);
                text2.text = string.Format("Account : {0}-{1}", deserializeMsg.Phone, deserializeMsg.Authcode);
            });
            msgpackTask.Start(mainThreadTaskScheduler);

            var webTask = new Task(async() => {
                Debug.LogFormat("before await Thread [{0}]", Thread.CurrentThread.ManagedThreadId);
                await Wait();
                Debug.LogFormat("after await Thread [{0}]", Thread.CurrentThread.ManagedThreadId);
            });
            webTask.Start(mainThreadTaskScheduler);
        }, _cancellationTokenSource.Token);
    }
Beispiel #5
0
    private async Task Wait()
    {
        Debug.LogFormat("async task A {0}", Thread.CurrentThread.ManagedThreadId);
        /// await RequstHttp();
        await Task.Run(RequstHttp);

        Debug.LogFormat("async task B {0}", Thread.CurrentThread.ManagedThreadId);
        await Task.Delay(1 * 1000);

        Debug.LogFormat("async task C {0}", Thread.CurrentThread.ManagedThreadId);
        await Task.Delay(1 * 1000);

        Debug.LogFormat("async task D {0}", Thread.CurrentThread.ManagedThreadId);

        //  task 调用方式
        //  测试表明:
        //  1、对于使用new创建的task,await关键字不能让task被调度,需要显式调用Start(),调度器默认使用 TaskScheduler.Current
        //  2、Task.RunTask、Task.Factory.StartNew创建的task,调度器默认使用 TaskScheduler.Default
        //  3、默认情况下,TaskScheduler.Current = TaskScheduler.Default
        //  4、异步方法的调用,同new创建的task一样,并且也遵循 2、3
        //  5、异步task中使用 await 一个 task之后,当前调度会变成这个task的调度器
    }