66 lines
1.7 KiB
C#

/*********************************************************************************************
* 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 (Exception 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);
}
}
}
}