juney1110 / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

Feature Request: Add ability to provide SynchronizationContext in NetPeer.RegisterReceivedCallback #161

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This isn't a bug. Your framework is a pleasure to use.

I've run into an issue where I'm initialising the NetClient on the UI thread, 
but would like the callbacks to be executed in the ThreadPool, not the UI 
thread.

I could make the code change myself, but I'd prefer to avoid local changes to 
your code.

Would you mind adding the following function 
overload?NetPeer.RegisterReceivedCallback(SendOrPostCallback callback, 
SynchronizationContext synchronizationContext)

The implementation could look something like:

/// <summary>
/// Call this to register a callback for when a new message arrives
/// </summary>
public void RegisterReceivedCallback(SendOrPostCallback callback)
{
    RegisterReceivedCallback(callback, SynchronizationContext.Current);
}

/// <summary>
/// Call this to register a callback for when a new message arrives
/// </summary>
public void RegisterReceivedCallback(SendOrPostCallback callback, 
SynchronizationContext synchronizationContext)
{
    if (synchronizationContext == null)
        throw new NetException("Need a SynchronizationContext to register callback on correct thread!");
    if (m_receiveCallbacks == null)
        m_receiveCallbacks = new List<NetTuple<SynchronizationContext, SendOrPostCallback>>();
    m_receiveCallbacks.Add(new NetTuple<SynchronizationContext, SendOrPostCallback>(synchronizationContext, callback));
}

Original issue reported on code.google.com by crowe.da...@gmail.com on 21 Jan 2014 at 2:05

GoogleCodeExporter commented 9 years ago
Good idea; added in rev 349

Original comment by lidg...@gmail.com on 22 Jan 2014 at 2:03

GoogleCodeExporter commented 9 years ago
Awesome. Thanks!

Original comment by crowe.da...@gmail.com on 22 Jan 2014 at 10:29