Skip to content

Remove standalone server dependency on Unity#71

Merged
Extremelyd1 merged 2 commits into
mainfrom
remove-standalone-dependency
May 30, 2026
Merged

Remove standalone server dependency on Unity#71
Extremelyd1 merged 2 commits into
mainfrom
remove-standalone-dependency

Conversation

@Extremelyd1
Copy link
Copy Markdown
Owner

Fixes #70.

Server-side code depended on Unity due to the packet handlers using a type from Unity, because client-side dispatches handlers to Unity's main thread.

This PR fixes the issue by introducing an interface for dispatchers, where a server-side implementation dispatches immediately, while the client-side implementation dispatches by running it on main thread of Unity. Due to classes implementating an interface, the Unity types are no longer loaded when creating the packet handler registry instances on the server-side.

@Liparakis
Copy link
Copy Markdown
Contributor

One small thing i would change -- i would convert the interfaces to Singleton and save resources on the Dispatcher allocations.

/// <summary>
/// Implementation of packet handler dispatcher to immediately invoke the handler directly for the server-side.
/// </summary>
internal class ServerPacketHandlerRegistryDispatcher : IPacketHandlerRegistryDispatcher {
    public static readonly ServerPacketHandlerRegistryDispatcher Instance = new();
    private ServerPacketHandlerRegistryDispatcher() { }
    public void Dispatch(Action action) => action.Invoke();
}

/// <summary>
/// Implementation of packet handler dispatcher to invoke the handler on Unity's main thread.
/// </summary>
internal class ClientPacketHandlerRegistryDispatcher : IPacketHandlerRegistryDispatcher {
    public static readonly ClientPacketHandlerRegistryDispatcher Instance = new();
    private ClientPacketHandlerRegistryDispatcher() { }
    public void Dispatch(Action action) => ThreadUtil.RunActionOnMainThread(action);
}

//PacketManager.cs
private readonly PacketHandlerRegistry<ClientExamplePacketId, ClientPacketHandler> _clientExampleRegistry = new(
        "Example", ClientPacketHandlerRegistryDispatcher.Instance
 );
private readonly PacketHandlerRegistry<ServerExamplePacketId, ServerPacketHandler> _serverExampleRegistry = new(
        "Example", ServerPacketHandlerRegistryDispatcher.Instance
);

@Extremelyd1 Extremelyd1 merged commit 41761c7 into main May 30, 2026
6 checks passed
@Extremelyd1 Extremelyd1 deleted the remove-standalone-dependency branch May 30, 2026 07:53
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

Successfully merging this pull request may close these issues.

SSMPServer cannot accepts connections due to dependency on Unity

2 participants