This commit is contained in:
Rich 2016-04-05 16:07:52 +00:00
parent e83a173039
commit 9a9da3fc1d
3 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Volian.Pipe.Library")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Volian.Pipe.Library")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("08f1c830-b1ee-4978-905e-103b02cc4254")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// 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("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,66 @@
/*********************************************************************************************
* Copyright 2016 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
*********************************************************************************************/
using System;
using System.Collections.Generic;
using System.IO.Pipes;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
namespace Volian.Pipe.Library
{
public class PipeClient
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private int _TimeOut = 1000;
public int TimeOut
{
get { return _TimeOut; }
set { _TimeOut = value; }
}
public PipeClient(string name)
{
_Name = name;
}
public void Send(string msg)
{
try
{
NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", Name, 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(msg);
pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
}
catch (TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
}
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 (Exception oEX)
{
Console.WriteLine(oEX.Message);
}
}
}
}

View File

@ -0,0 +1,80 @@
/*********************************************************************************************
* Copyright 2016 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
*********************************************************************************************/
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
namespace Volian.Pipe.Library
{
// Delegate for passing received message back to caller
public delegate void DelegateMessage(string Reply);
public class PipeServer
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public PipeServer(string name)
{
_Name = name;
}
public event DelegateMessage PipeMessage;
string _pipeName;
public void Listen()
{
try
{
// Create the new async pipe
NamedPipeServerStream pipeServer = new NamedPipeServerStream(Name, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
// Wait for a connection
pipeServer.BeginWaitForConnection(WaitForConnectionCallBack, pipeServer);
}
catch (Exception oEX)
{
Console.WriteLine(oEX.Message);
}
}
private void WaitForConnectionCallBack(IAsyncResult iar)
{
try
{
Console.WriteLine("========WaitForConnectionCallBack========\r\n");
// Get the pipe
NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
// End waiting for the connection
pipeServer.EndWaitForConnection(iar);
byte[] buffer = new byte[4096];
// Read the incoming message
int bytecount = 4096;
StringBuilder sb = new StringBuilder();
while (bytecount == 4096)
{
bytecount = pipeServer.Read(buffer, 0, 4096);
// Convert byte buffer to string
string stringData = Encoding.UTF8.GetString(buffer, 0, bytecount);
sb.Append(stringData);
}
// Pass message back to calling form
PipeMessage.Invoke(sb.ToString());
// Kill original sever and create new wait server
pipeServer.Close();
pipeServer = null;
//pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
//// Recursively wait for the connection again and again....
//pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
Listen();
}
catch
{
return;
}
}
}
}