mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 12:14:16 +01:00
feat: remove socket cleanup tasks
This commit is contained in:
parent
7b286c427c
commit
2cef75bbbe
2 changed files with 0 additions and 101 deletions
|
|
@ -8,8 +8,6 @@ using System.Threading.Tasks;
|
||||||
using Dalamud.Logging.Internal;
|
using Dalamud.Logging.Internal;
|
||||||
using Dalamud.Utility;
|
using Dalamud.Utility;
|
||||||
|
|
||||||
using TerraFX.Interop.Windows;
|
|
||||||
|
|
||||||
namespace Dalamud.Networking.Rpc.Transport;
|
namespace Dalamud.Networking.Rpc.Transport;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -94,13 +92,6 @@ internal class UnixRpcTransport : IRpcTransport
|
||||||
}
|
}
|
||||||
|
|
||||||
this.acceptLoopTask = Task.Factory.StartNew(this.AcceptLoopAsync, TaskCreationOptions.LongRunning);
|
this.acceptLoopTask = Task.Factory.StartNew(this.AcceptLoopAsync, TaskCreationOptions.LongRunning);
|
||||||
|
|
||||||
// note: needs to be run _after_ we're alive so that we don't delete our own socket.
|
|
||||||
// TODO: This should *probably* be handed by the launcher instead.
|
|
||||||
if (this.cleanupSocketDirectory != null)
|
|
||||||
{
|
|
||||||
Task.Run(async () => await UnixSocketUtil.CleanStaleSockets(this.cleanupSocketDirectory));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Invoke an RPC request on a specific client expecting a result.</summary>
|
/// <summary>Invoke an RPC request on a specific client expecting a result.</summary>
|
||||||
|
|
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
using System.IO;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace Dalamud.Networking.Rpc;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A set of utilities to help manage Unix sockets.
|
|
||||||
/// </summary>
|
|
||||||
internal static class UnixSocketUtil
|
|
||||||
{
|
|
||||||
// Default probe timeout in milliseconds.
|
|
||||||
private const int DefaultProbeMs = 200;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Test whether a Unix socket is alive/listening.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path to test.</param>
|
|
||||||
/// <param name="timeoutMs">How long to wait for a connection success.</param>
|
|
||||||
/// <returns>A task result representing if a socket is alive or not.</returns>
|
|
||||||
public static async Task<bool> IsSocketAlive(string path, int timeoutMs = DefaultProbeMs)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(path)) return false;
|
|
||||||
var endpoint = new UnixDomainSocketEndPoint(path);
|
|
||||||
using var client = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
|
|
||||||
|
|
||||||
var connectTask = client.ConnectAsync(endpoint);
|
|
||||||
var completed = await Task.WhenAny(connectTask, Task.Delay(timeoutMs)).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (completed == connectTask)
|
|
||||||
{
|
|
||||||
// Connected or failed very quickly. If the task is successful, the socket is alive.
|
|
||||||
if (connectTask.IsCompletedSuccessfully)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client.Shutdown(SocketShutdown.Both);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// ignored
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Find and remove stale Dalamud RPC sockets.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="directory">The directory to scan for stale sockets.</param>
|
|
||||||
/// <param name="probeTimeoutMs">The timeout to wait for a connection attempt to succeed.</param>
|
|
||||||
/// <returns>A task that executes when sockets are purged.</returns>
|
|
||||||
public static async Task CleanStaleSockets(string directory, int probeTimeoutMs = DefaultProbeMs)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(directory) || !Directory.Exists(directory)) return;
|
|
||||||
|
|
||||||
foreach (var file in Directory.EnumerateFiles(directory, "DalamudRPC.*.sock", SearchOption.TopDirectoryOnly))
|
|
||||||
{
|
|
||||||
// we don't need to check ourselves.
|
|
||||||
if (file.Contains(Environment.ProcessId.ToString())) continue;
|
|
||||||
|
|
||||||
bool shouldDelete;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
shouldDelete = !await IsSocketAlive(file, probeTimeoutMs);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
shouldDelete = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shouldDelete)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Delete(file);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log.Error(ex, "Could not delete stale socket file: {File}", file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue