Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/SoapstoneLib/SoapstoneLib/KnownServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ namespace SoapstoneLib
public sealed class KnownServer
{
/// <summary>
/// Standard server info for DSMapStudio.
/// Standard server info for DSMapStudio and DSMapStudio fork Smithbox.
/// </summary>
public static readonly KnownServer DSMapStudio = new KnownServer(22720, "DSMapStudio");
public static readonly KnownServer DSMapStudio = new KnownServer(22720, "DSMapStudio", "Smithbox");

/// <summary>
/// Standard server info for Smithbox.
/// Alternate name for DSMapstudio/Smithbox server info.
/// </summary>
public static readonly KnownServer Smithbox = new KnownServer(22721, "Smithbox");
public static readonly KnownServer Smithbox = DSMapStudio;

/// <summary>
/// Expected local process name of this server. This usually matches the exe name.
/// An expected local process name of this server. This usually matches the exe name.
/// </summary>
public string ProcessName { get; }
public string ProcessName => ProcessNames.FirstOrDefault();

/// <summary>
/// All expected local process names of this server. This usually matches the exe name.
/// </summary>
public IReadOnlyList<string> ProcessNames { get; }

/// <summary>
/// Standard port for this server to run at. A different one may be selected if it's busy.
Expand All @@ -40,18 +45,18 @@ public sealed class KnownServer
/// Otherwise, the port is used directly, if non-zero. The port is also preferred
/// if there are multiple ports associated with the given process name.
/// </summary>
public KnownServer(ushort portHint, string processName)
public KnownServer(ushort portHint, params string[] processNames)
{
if (portHint == 0 && processName == null)
if (portHint == 0 && processNames.Length == 0)
{
throw new ArgumentException($"One of process or port must be provided in KnownServer");
}
PortHint = portHint;
ProcessName = processName;
ProcessNames = processNames.ToList().AsReadOnly();
}

/// <inheritdoc />
public override string ToString() => $"KnownServer[PortHint={PortHint},ProcessName={ProcessName}]";
public override string ToString() => $"KnownServer[PortHint={PortHint},ProcessNames={string.Join(",", ProcessNames)}]";

/// <summary>
/// Try to find a running server using heuristic info.
Expand All @@ -67,10 +72,14 @@ internal bool FindServer(out int realPort)
MIB_TCP6ROW_OWNER_PID[] rows6 = GetAllTcpConnections<MIB_TCP6ROW_OWNER_PID>();

// If process is given, always try to use it, and fail if not present
if (ProcessName != null)
if (ProcessNames.Count > 0)
{
Process[] processes = Process.GetProcessesByName(ProcessName);
if (processes.Length == 0)
List<Process> processes = new();
foreach (string name in ProcessNames)
{
processes.AddRange(Process.GetProcessesByName(name));
}
if (processes.Count == 0)
{
return false;
}
Expand Down
Loading