yasio / yasio_unity

The unity c# wrapper of yasio
MIT License
19 stars 3 forks source link

遇到个问题,当网络回调带主线程的方法,比如Time.frame这样的函数,会出现报错,然后编辑器暂停,下次其他网络层就会连接不上 #4

Closed yingnierxiao closed 2 months ago

yingnierxiao commented 2 months ago

这个时候就要关闭unity,重新打开网络才会正常,这个有什么办法调试吗? 正常情况下面这条会输出,出现报错的情况,下次启动,下面这条就不会输出了 920[93 INFO NET] [yasio][1725607650920][index: 1] open server succeed, socket.fd=50032 listening at 127.0.0.1:9999... 921[93 INFO NET] [yasio][1725607650922][index: 0] connecting server 127.0.0.1(127.0.0.1):9999... 922[93 INFO NET] [yasio][1725607650923][index: 1] the connection https://github.com/yasio/yasio_unity/pull/3 <127.0.0.1:9999> --> <127.0.0.1:3470> is established. 924[93 INFO NET] [yasio][1725607650925][index: 0] the connection https://github.com/yasio/yasio_unity/issues/4 <127.0.0.1:3470> --> <127.0.0.1:9999> is established.( 这条就不会输出)

halx99 commented 2 months ago

有没有具体的复现 demo ?

yingnierxiao commented 2 months ago

yasio_unity的的工程 unsafe public void OnConnectSuccess(int channel = 0) { if (channel == AppProtocol.CLIENT_CHANNEL) { Debug.LogFormat("Connect success, channel={0}", channel);

        //把下面代码开启模拟一次报错,然后暂停编辑器,去掉下面的代码,就能复现,重新运行一次,登陆的时候就没法发包了
        Debug.Log(Time.frameCount); 
        // 模拟登录协议
        var loginReq = new AppProtocol.LoginReq();
        loginReq.uid = 1219;
        Span<byte> ud = loginReq.encode();

        nsm.SendSerializedMsg(AppProtocol.CMD_LOGIN_REQ, ud, AppProtocol.CLIENT_CHANNEL);

        loginReq.Dispose();
    }
    else if(channel == AppProtocol.SERVER_CHANNEL)
    {
        Debug.LogFormat("A client income, channel={0}", channel);
    }
}

2024 09 06_郑捷 0a31a33520c9b213c552beb0089724a8

halx99 commented 2 months ago

异常会导致调度线程停止工作

halx99 commented 2 months ago

调试了下,发生c#异常后,yasio 网络线程被意外终止了,猜测 .net 框架 TermianteThread 终止了当前线程,这种情况基本没法处理 image

halx99 commented 2 months ago

https://github.com/yasio/yasio/commit/b9c0868c236b4ec04803a0fa200a44676a98746b

主仓库最新提交确保 stop 后 可重启网络线程

halx99 commented 2 months ago

最新版本 v4.3.1 亲测,发生异常后,停止预览,重新启动预览即可,无需重启unity编辑器,感谢反馈

yingnierxiao commented 2 months ago

😄