Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to solve the program freezing when calling bootstrap.BindAsync(localAddress).Result in Unity? #610

Open
Siwen opened this issue Aug 4, 2023 · 2 comments

Comments

@Siwen
Copy link

Siwen commented Aug 4, 2023

How to solve the program freezing when calling bootstrap.BindAsync(localAddress).Result in Unity?

Code:

     private void init () {
        _executorPool = executorPool;
        _executorPool.CreateMessageExecutor();
        _eventLoopGroup = eventLoopGroup;
        _scheduleThread = new EventLoopScheduleThread();
        bootstrap = new Bootstrap();
        bootstrap.Group(_eventLoopGroup);
        bootstrap.ChannelFactory(() => new SocketDatagramChannel(AddressFamily.InterNetwork));
        bootstrap.Handler(new ActionChannelInitializer<SocketDatagramChannel>(channel =>
        {
            var pipeline = channel.Pipeline;
            pipeline.AddLast(new ClientChannelHandler(_channelManager,channelConfig));
        }));
     }

    private static IChannel bindLocal(Bootstrap bootstrap,EndPoint localAddress = null)
    {
        if (localAddress == null)
        {
            localAddress = new IPEndPoint(IPAddress.Any, 0);
        }

        // ------> program freezing <------
        return bootstrap.BindAsync(localAddress).Result;
    }
@rekcah1986
Copy link

Same for me.

@Buryyy
Copy link

Buryyy commented Sep 8, 2024

The freezing issue in your code occurs because you are calling .Result on a task returned by bootstrap.BindAsync(), which blocks the calling thread until the task completes. In Unity, blocking the main thread is problematic because Unity's game loop (and most UI-related tasks) runs on the main thread, which leads to freezing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants