This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bumped to 0.5.0
- Loading branch information
Showing
8 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters