Skip to content

Commit

Permalink
replace SingleInstanceApplication by Mutex and PipeClient/PipeServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Dec 9, 2021
1 parent f6f4943 commit b71bfb8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 128 deletions.
25 changes: 16 additions & 9 deletions ControllerCommon/PipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class PipeClient
public readonly NamedPipeClient<PipeMessage> client;
private readonly ILogger logger;

public event ConnectedEventHandler Connected;
public delegate void ConnectedEventHandler(Object sender);

public event DisconnectedEventHandler Disconnected;
public delegate void DisconnectedEventHandler(Object sender);

Expand All @@ -23,10 +26,8 @@ public class PipeClient

public bool connected;

public PipeClient(string pipeName, ILogger logger)
public PipeClient(string pipeName)
{
this.logger = logger;

m_queue = new ConcurrentQueue<PipeMessage>();

// monitors processes and settings
Expand All @@ -41,9 +42,14 @@ public PipeClient(string pipeName, ILogger logger)
client.Error += OnError;
}

public PipeClient(string pipeName, ILogger logger) : this(pipeName)
{
this.logger = logger;
}

private void OnClientDisconnected(NamedPipeConnection<PipeMessage, PipeMessage> connection)
{
logger.LogInformation("Client {0} disconnected", connection.Id);
logger?.LogInformation("Client {0} disconnected", connection.Id);
Disconnected?.Invoke(this);

connected = false;
Expand All @@ -55,7 +61,7 @@ public void Start()
return;

client.Start();
logger.LogInformation($"Pipe Client has started");
logger?.LogInformation($"Pipe Client has started");
}

public void Stop()
Expand All @@ -64,26 +70,27 @@ public void Stop()
return;

client.Stop();
logger.LogInformation($"Pipe Client has stopped");
logger?.LogInformation($"Pipe Client has stopped");
}

private void OnServerMessage(NamedPipeConnection<PipeMessage, PipeMessage> connection, PipeMessage message)
{
logger.LogDebug("Client {0} opcode: {1} says: {2}", connection.Id, message.code, string.Join(" ", message.ToString()));
logger?.LogDebug("Client {0} opcode: {1} says: {2}", connection.Id, message.code, string.Join(" ", message.ToString()));
ServerMessage?.Invoke(this, message);

switch (message.code)
{
case PipeCode.SERVER_PING:
connected = true;
logger.LogInformation("Client {0} is now connected!", connection.Id);
Connected?.Invoke(this);
logger?.LogInformation("Client {0} is now connected!", connection.Id);
break;
}
}

private void OnError(Exception exception)
{
logger.LogError("PipClient failed. {0}", exception.Message);
logger?.LogError("PipClient failed. {0}", exception.Message);
}

public void SendMessage(PipeMessage message)
Expand Down
20 changes: 20 additions & 0 deletions ControllerCommon/PipeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,25 @@ public PipeClientHidder()
code = PipeCode.CLIENT_HIDDER;
}
}

[Serializable]
public class PipeConsoleArgs : PipeMessage
{
public string[] args;

public PipeConsoleArgs()
{
code = PipeCode.CLIENT_CONSOLE;
}
}

[Serializable]
public class PipeServerShutdown : PipeMessage
{
public PipeServerShutdown()
{
code = PipeCode.SERVER_SHUTDOWN;
}
}
#endregion
}
27 changes: 18 additions & 9 deletions ControllerCommon/PipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public enum PipeCode

CLIENT_SCREEN = 11, // Sent to server to update screen details
// args: width, height

CLIENT_CONSOLE = 12, // Sent from client to client to pass parameters
// args: string[] parameters

SERVER_SHUTDOWN = 13, // Sent to client to halt process
// args: ...
}

public class PipeServer
Expand All @@ -57,10 +63,8 @@ public class PipeServer

public bool connected;

public PipeServer(string pipeName, ILogger logger)
public PipeServer(string pipeName)
{
this.logger = logger;

m_queue = new ConcurrentQueue<PipeMessage>();

// monitors processes and settings
Expand All @@ -79,13 +83,18 @@ public PipeServer(string pipeName, ILogger logger)
server.Error += OnError;
}

public PipeServer(string pipeName, ILogger logger) : this(pipeName)
{
this.logger = logger;
}

public void Start()
{
if (server == null)
return;

server.Start();
logger.LogInformation($"Pipe Server has started");
logger?.LogInformation($"Pipe Server has started");
}

public void Stop()
Expand All @@ -94,12 +103,12 @@ public void Stop()
return;

server = null;
logger.LogInformation($"Pipe Server has stopped");
logger?.LogInformation($"Pipe Server has stopped");
}

private void OnClientConnected(NamedPipeConnection<PipeMessage, PipeMessage> connection)
{
logger.LogInformation("Client {0} is now connected!", connection.Id);
logger?.LogInformation("Client {0} is now connected!", connection.Id);
Connected?.Invoke(this);

connected = true;
Expand All @@ -110,21 +119,21 @@ private void OnClientConnected(NamedPipeConnection<PipeMessage, PipeMessage> con

private void OnClientDisconnected(NamedPipeConnection<PipeMessage, PipeMessage> connection)
{
logger.LogInformation("Client {0} disconnected", connection.Id);
logger?.LogInformation("Client {0} disconnected", connection.Id);
Disconnected?.Invoke(this);

connected = false;
}

private void OnClientMessage(NamedPipeConnection<PipeMessage, PipeMessage> connection, PipeMessage message)
{
logger.LogDebug("Client {0} opcode: {1} says: {2}", connection.Id, message.code, string.Join(" ", message.ToString()));
logger?.LogDebug("Client {0} opcode: {1} says: {2}", connection.Id, message.code, string.Join(" ", message.ToString()));
ClientMessage?.Invoke(this, message);
}

private void OnError(Exception exception)
{
logger.LogError("PipeServer failed. {0}", exception.Message);
logger?.LogError("PipeServer failed. {0}", exception.Message);
}

public void SendMessage(PipeMessage message)
Expand Down
4 changes: 0 additions & 4 deletions ControllerHelper/ControllerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ public partial class ControllerHelper : Form

private readonly ILogger logger;

public ControllerHelper()
{
}

public ControllerHelper(ILogger logger)
{
InitializeComponent();
Expand Down
Loading

0 comments on commit b71bfb8

Please sign in to comment.