This commit is contained in:
80
PROMS/Volian.Pipe.Library/PipeServer.cs
Normal file
80
PROMS/Volian.Pipe.Library/PipeServer.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user