Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

Commit

Permalink
* Switched to named pipes for IPC
Browse files Browse the repository at this point in the history
* Bumped to 0.5.0
  • Loading branch information
RobThree committed Oct 15, 2013
1 parent 7d22c77 commit 50023a8
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 10 deletions.
19 changes: 19 additions & 0 deletions Fop2DD/Core/DDCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Fop2DD.Core.Common;
using Fop2DD.Core.Connection;
using Fop2DD.Core.Hotkeys;
using Fop2DD.Core.IPC;
using Fop2DD.Core.Logging;
using Fop2DD.Core.Systray;
using System;
Expand All @@ -20,6 +21,7 @@ public class DDCore : IDisposable

private ToolStripMenuItem _fop2webinterface;
private ToolStripMenuItem _fop2userportal;
private DDPipeServer _pipeserver;

private IDDLogger logger = DDLogManager.GetLogger(typeof(DDCore));

Expand All @@ -43,6 +45,23 @@ public DDCore()
_hotkeymanager.DialRequest += event_DialRequest;

_connectionmanager.RegisterListener(_notifyicon);

_pipeserver = new DDPipeServer();
_pipeserver.MessageReceived += (s, e) => {
this.DialFromCommandlineArgs(e.Data.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
};
_pipeserver.Listen(GetIPCPipeName());
}

public static void SendIPCPessage(string message)
{
DDPipeClient _client = new DDPipeClient();
_client.Send(message, GetIPCPipeName());
}

private static string GetIPCPipeName()
{
return Application.ProductName + "." + Environment.UserName;
}

public void DialFromCommandlineArgs(string[] args)
Expand Down
45 changes: 45 additions & 0 deletions Fop2DD/Core/IPC/DDPipeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.IO.Pipes;
using System.Text;

namespace Fop2DD.Core.IPC
{
class DDPipeClient
{
public void Send(string value, string pipeName, int timeout = 1000)
{
try
{
NamedPipeClientStream pipestream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous);
// The connect function will indefinitely wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipestream.Connect(timeout);
byte[] _buffer = Encoding.UTF8.GetBytes(value);
pipestream.BeginWrite(_buffer, 0, _buffer.Length, new AsyncCallback(AsyncSend), pipestream);
}
catch
{

}
}

private void AsyncSend(IAsyncResult iar)
{
try
{
// Get the pipe
NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

// End the write
pipeStream.EndWrite(iar);
pipeStream.Flush();
pipeStream.Close();
pipeStream.Dispose();
}
catch
{

}
}
}
}
14 changes: 14 additions & 0 deletions Fop2DD/Core/IPC/DDPipeMessageReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Fop2DD.Core.IPC
{
public class DDPipeMessageReceivedEventArgs : EventArgs
{
public string Data { get; set; }

public DDPipeMessageReceivedEventArgs(string data)
{
this.Data = data;
}
}
}
70 changes: 70 additions & 0 deletions Fop2DD/Core/IPC/DDPipeServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.IO.Pipes;
using System.Text;

namespace Fop2DD.Core.IPC
{
public class DDPipeServer
{
public event MessageReceivedHandler MessageReceived;
public delegate void MessageReceivedHandler(object sender, DDPipeMessageReceivedEventArgs e);

private string _pipename;
private const int BUFFERSIZE = 4096;

public void Listen(string PipeName)
{
try
{
_pipename = PipeName;
// Create the new async pipe
NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, 0);

// Wait for a connection
pipeServer.BeginWaitForConnection
(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
}
catch
{

}
}

private void WaitForConnectionCallBack(IAsyncResult iar)
{
try
{
// Get the pipe
NamedPipeServerStream pipeserver = (NamedPipeServerStream)iar.AsyncState;
// End waiting for the connection
pipeserver.EndWaitForConnection(iar);

byte[] buffer = new byte[BUFFERSIZE];

// Read the incoming message
var bytesread = pipeserver.Read(buffer, 0, buffer.Length);

// Convert byte buffer to string
string data = Encoding.UTF8.GetString(buffer, 0, bytesread);

// Pass message back to calling form
if (!string.IsNullOrEmpty(data) && (this.MessageReceived != null))
MessageReceived(this, new DDPipeMessageReceivedEventArgs(data));

// Kill original sever and create new wait server
pipeserver.Close();
pipeserver.Dispose();
pipeserver = new NamedPipeServerStream(_pipename, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, BUFFERSIZE);

// Recursively wait for the connection again and again....
pipeserver.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeserver);
}
catch
{
return;
}
}
}


}
3 changes: 3 additions & 0 deletions Fop2DD/Fop2DD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<Compile Include="Core\Common\ShellCommand.cs" />
<Compile Include="Core\Common\Validators.cs" />
<Compile Include="Core\Connection\DDFop2Endpoint.cs" />
<Compile Include="Core\IPC\DDPipeClient.cs" />
<Compile Include="Core\IPC\DDPipeServer.cs" />
<Compile Include="Core\IPC\DDPipeMessageReceivedEventArgs.cs" />
<Compile Include="Core\Logging\ExtensionMethods.cs" />
<Compile Include="Core\Logging\IDDlogger.cs" />
<Compile Include="Core\Logging\DDLogEntry.cs" />
Expand Down
8 changes: 3 additions & 5 deletions Fop2DD/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fop2DD.Core;
using Fop2DD.Core.IPC;
using Fop2DD.Core.Logging;
using Newtonsoft.Json;
using System;
Expand Down Expand Up @@ -51,11 +52,8 @@ static void Main(string[] args)
}
else
{
//There's already an instance running

//TODO: We need to get the running instance to run _core.DialFromCommandlineArgs(args) on it
//OR
//TODO: We need to notify the running instance via IPC (tcp? named pipes? other...?) to run _core.DialFromCommandlineArgs(args) FOR us...
//There's already an instance running; pass along our commandline args
DDCore.SendIPCPessage(string.Join("|", args));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Fop2DD/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.4.3.0")]
[assembly: AssemblyFileVersion("0.4.3.0")]
[assembly: AssemblyVersion("0.5.0.0")]
[assembly: AssemblyFileVersion("0.5.0.0")]
6 changes: 3 additions & 3 deletions Fop2DD/setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
[Setup]
AppPublisher=KeenSystems
AppPublisherURL=http://www.keensystems.eu
AppVersion=0.4.3.0
AppVersion=0.5.0.0
AppName=Fop2DD
AppVerName=Fop2DD 0.4.3
AppVerName=Fop2DD 0.5.0
AppCopyright=Copyright (C) 2013 KeenSystems
VersionInfoVersion=0.4.3.0
VersionInfoVersion=0.5.0.0
DefaultDirName={pf}\KeenSystems\Fop2DD
DefaultGroupName=KeenSystems\Fop2DD
UninstallDisplayIcon={app}\Fop2DD.exe
Expand Down

0 comments on commit 50023a8

Please sign in to comment.