code optimization
This commit is contained in:
@@ -2,66 +2,65 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.IO
|
||||
namespace Pilz.IO;
|
||||
|
||||
/// <summary>
|
||||
/// stellt den Erben "Server" und "Client" 2 verschiedene
|
||||
/// Message-Events zur Verfügung, und ein Event-Raisendes Dispose
|
||||
/// </summary>
|
||||
public abstract class ManagedPipe : IDisposable
|
||||
{
|
||||
public delegate void EventHandlerWithOneArgument<T0>(T0 Sender);
|
||||
|
||||
/// <summary>
|
||||
/// stellt den Erben "Server" und "Client" 2 verschiedene
|
||||
/// Message-Events zur Verfügung, und ein Event-Raisendes Dispose
|
||||
/// Zur Ausgabe chat-verwaltungstechnischer Status-Informationen
|
||||
/// </summary>
|
||||
public abstract class ManagedPipe : IDisposable
|
||||
public event EventHandler<DataEventArgs> StatusMessage;
|
||||
/// <summary>Zur Ausgabe von Chat-Messages</summary>
|
||||
public event EventHandler<DataEventArgs> RetriveData;
|
||||
public event EventHandlerWithOneArgument<ManagedPipe> Disposed;
|
||||
|
||||
private bool _IsDisposed = false;
|
||||
|
||||
protected abstract void Dispose(bool disposing);
|
||||
public abstract void Send(byte[] bytes);
|
||||
public abstract Task SendAsnyc(byte[] bytes);
|
||||
|
||||
protected void OnStatusMessage(DataEventArgs e)
|
||||
{
|
||||
public delegate void EventHandlerWithOneArgument<T0>(T0 Sender);
|
||||
StatusMessage?.Invoke(this, e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Zur Ausgabe chat-verwaltungstechnischer Status-Informationen
|
||||
/// </summary>
|
||||
public event EventHandler<DataEventArgs> StatusMessage;
|
||||
/// <summary>Zur Ausgabe von Chat-Messages</summary>
|
||||
public event EventHandler<DataEventArgs> RetriveData;
|
||||
public event EventHandlerWithOneArgument<ManagedPipe> Disposed;
|
||||
protected void OnRetriveData(DataEventArgs e)
|
||||
{
|
||||
RetriveData?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private bool _IsDisposed = false;
|
||||
public void RemoveFrom<T>(ICollection<T> Coll) where T : ManagedPipe
|
||||
{
|
||||
Coll.Remove((T)this);
|
||||
}
|
||||
|
||||
protected abstract void Dispose(bool disposing);
|
||||
public abstract void Send(byte[] bytes);
|
||||
public abstract Task SendAsnyc(byte[] bytes);
|
||||
|
||||
protected void OnStatusMessage(DataEventArgs e)
|
||||
public bool IsDisposed
|
||||
{
|
||||
get
|
||||
{
|
||||
StatusMessage?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected void OnRetriveData(DataEventArgs e)
|
||||
{
|
||||
RetriveData?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public void RemoveFrom<T>(ICollection<T> Coll) where T : ManagedPipe
|
||||
{
|
||||
Coll.Remove((T)this);
|
||||
}
|
||||
|
||||
public bool IsDisposed
|
||||
{
|
||||
get
|
||||
{
|
||||
return _IsDisposed;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddTo<T>(ICollection<T> Coll) where T : ManagedPipe
|
||||
{
|
||||
Coll.Add((T)this);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_IsDisposed)
|
||||
return;
|
||||
_IsDisposed = true;
|
||||
Dispose(true); // rufe die erzwungenen Überschreibungen von Sub Dispose(Boolean)
|
||||
Disposed?.Invoke(this);
|
||||
GC.SuppressFinalize(this);
|
||||
return _IsDisposed;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddTo<T>(ICollection<T> Coll) where T : ManagedPipe
|
||||
{
|
||||
Coll.Add((T)this);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_IsDisposed)
|
||||
return;
|
||||
_IsDisposed = true;
|
||||
Dispose(true); // rufe die erzwungenen Überschreibungen von Sub Dispose(Boolean)
|
||||
Disposed?.Invoke(this);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -1,90 +1,89 @@
|
||||
using System;
|
||||
using global::System.IO.Pipes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using global::System.IO.Pipes;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.IO
|
||||
namespace Pilz.IO;
|
||||
|
||||
public class ManagedPipeClient : ManagedPipe
|
||||
{
|
||||
public class ManagedPipeClient : ManagedPipe
|
||||
private PipeStream pipeStream;
|
||||
private byte[] _Buf = new byte[1024];
|
||||
|
||||
public ManagedPipeClient(string pipeName) : this(pipeName, ".")
|
||||
{
|
||||
private PipeStream pipeStream;
|
||||
private byte[] _Buf = new byte[1024];
|
||||
}
|
||||
|
||||
public ManagedPipeClient(string pipeName) : this(pipeName, ".")
|
||||
public ManagedPipeClient(string pipeName, string serverName) : this(pipeName, serverName, -1)
|
||||
{
|
||||
}
|
||||
|
||||
public ManagedPipeClient(string pipeName, int connectionTimeout) : this(pipeName, ".", connectionTimeout)
|
||||
{
|
||||
}
|
||||
|
||||
public ManagedPipeClient(string pipeName, string serverName, int connectionTimeout)
|
||||
{
|
||||
var clnt = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
|
||||
clnt.Connect(connectionTimeout);
|
||||
if (!clnt.IsConnected)
|
||||
{
|
||||
throw new TimeoutException("Connection timeout!");
|
||||
}
|
||||
|
||||
public ManagedPipeClient(string pipeName, string serverName) : this(pipeName, serverName, -1)
|
||||
SetPipe(clnt);
|
||||
}
|
||||
|
||||
public ManagedPipeClient(PipeStream pipe)
|
||||
{
|
||||
SetPipe(pipe);
|
||||
}
|
||||
|
||||
private void SetPipe(PipeStream pipe)
|
||||
{
|
||||
pipeStream = pipe;
|
||||
pipeStream.BeginRead(_Buf, 0, _Buf.Length, EndRead, null);
|
||||
}
|
||||
|
||||
private void EndRead(IAsyncResult ar)
|
||||
{
|
||||
if (IsDisposed)
|
||||
return;
|
||||
int bytesCount = pipeStream.EndRead(ar);
|
||||
if (bytesCount == 0) // leere Datenübermittlung signalisiert Verbindungsabbruch
|
||||
{
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public ManagedPipeClient(string pipeName, int connectionTimeout) : this(pipeName, ".", connectionTimeout)
|
||||
var list = new List<byte>();
|
||||
for (int i = 0, loopTo = bytesCount - 1; i <= loopTo; i++)
|
||||
list.Add(_Buf[i]);
|
||||
while (bytesCount == _Buf.Length)
|
||||
{
|
||||
}
|
||||
|
||||
public ManagedPipeClient(string pipeName, string serverName, int connectionTimeout)
|
||||
{
|
||||
var clnt = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
|
||||
clnt.Connect(connectionTimeout);
|
||||
if (!clnt.IsConnected)
|
||||
{
|
||||
throw new TimeoutException("Connection timeout!");
|
||||
}
|
||||
|
||||
SetPipe(clnt);
|
||||
}
|
||||
|
||||
public ManagedPipeClient(PipeStream pipe)
|
||||
{
|
||||
SetPipe(pipe);
|
||||
}
|
||||
|
||||
private void SetPipe(PipeStream pipe)
|
||||
{
|
||||
pipeStream = pipe;
|
||||
pipeStream.BeginRead(_Buf, 0, _Buf.Length, EndRead, null);
|
||||
}
|
||||
|
||||
private void EndRead(IAsyncResult ar)
|
||||
{
|
||||
if (IsDisposed)
|
||||
return;
|
||||
int bytesCount = pipeStream.EndRead(ar);
|
||||
if (bytesCount == 0) // leere Datenübermittlung signalisiert Verbindungsabbruch
|
||||
{
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
var list = new List<byte>();
|
||||
for (int i = 0, loopTo = bytesCount - 1; i <= loopTo; i++)
|
||||
bytesCount = pipeStream.Read(_Buf, 0, _Buf.Length);
|
||||
for (int i = 0, loopTo1 = bytesCount - 1; i <= loopTo1; i++)
|
||||
list.Add(_Buf[i]);
|
||||
while (bytesCount == _Buf.Length)
|
||||
{
|
||||
bytesCount = pipeStream.Read(_Buf, 0, _Buf.Length);
|
||||
for (int i = 0, loopTo1 = bytesCount - 1; i <= loopTo1; i++)
|
||||
list.Add(_Buf[i]);
|
||||
}
|
||||
|
||||
var deargs = new DataEventArgs(list.ToArray());
|
||||
OnRetriveData(deargs);
|
||||
|
||||
pipeStream.BeginRead(_Buf, 0, _Buf.Length, EndRead, null);
|
||||
}
|
||||
|
||||
public override Task SendAsnyc(byte[] bytes)
|
||||
{
|
||||
return Task.Run(() => Send(bytes));
|
||||
}
|
||||
var deargs = new DataEventArgs(list.ToArray());
|
||||
OnRetriveData(deargs);
|
||||
|
||||
public override void Send(byte[] data)
|
||||
{
|
||||
pipeStream.Write(data, 0, data.Length);
|
||||
}
|
||||
pipeStream.BeginRead(_Buf, 0, _Buf.Length, EndRead, null);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
pipeStream.Dispose();
|
||||
}
|
||||
public override Task SendAsnyc(byte[] bytes)
|
||||
{
|
||||
return Task.Run(() => Send(bytes));
|
||||
}
|
||||
|
||||
public override void Send(byte[] data)
|
||||
{
|
||||
pipeStream.Write(data, 0, data.Length);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
pipeStream.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,108 +1,107 @@
|
||||
using System;
|
||||
using global::System.IO.Pipes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using global::System.IO.Pipes;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.IO
|
||||
namespace Pilz.IO;
|
||||
|
||||
public class ManagedPipeServer : ManagedPipe
|
||||
{
|
||||
public class ManagedPipeServer : ManagedPipe
|
||||
|
||||
// Pro Verbindung (Anfrage) wird ein Client-Objekt generiert, das den Datenaustausch dieser Verbindung abwickelt
|
||||
public List<ManagedPipeClient> Clients { get; private set; } = new List<ManagedPipeClient>();
|
||||
|
||||
private readonly string pipeName = "";
|
||||
private readonly int maxNumbersOfServerInstances;
|
||||
private int numberOfStartedServerInstances = 0;
|
||||
|
||||
public ManagedPipeServer(string pipeName) : this(pipeName, 1)
|
||||
{
|
||||
}
|
||||
|
||||
// Pro Verbindung (Anfrage) wird ein Client-Objekt generiert, das den Datenaustausch dieser Verbindung abwickelt
|
||||
public List<ManagedPipeClient> Clients { get; private set; } = new List<ManagedPipeClient>();
|
||||
public ManagedPipeServer(string pipeName, int maxNumbersOfServerInstances)
|
||||
{
|
||||
this.pipeName = pipeName;
|
||||
this.maxNumbersOfServerInstances = maxNumbersOfServerInstances;
|
||||
CreateWaitingStream();
|
||||
}
|
||||
|
||||
private readonly string pipeName = "";
|
||||
private readonly int maxNumbersOfServerInstances;
|
||||
private int numberOfStartedServerInstances = 0;
|
||||
|
||||
public ManagedPipeServer(string pipeName) : this(pipeName, 1)
|
||||
private void CreateWaitingStream()
|
||||
{
|
||||
if (numberOfStartedServerInstances < maxNumbersOfServerInstances)
|
||||
{
|
||||
}
|
||||
|
||||
public ManagedPipeServer(string pipeName, int maxNumbersOfServerInstances)
|
||||
{
|
||||
this.pipeName = pipeName;
|
||||
this.maxNumbersOfServerInstances = maxNumbersOfServerInstances;
|
||||
CreateWaitingStream();
|
||||
}
|
||||
|
||||
private void CreateWaitingStream()
|
||||
{
|
||||
if (numberOfStartedServerInstances < maxNumbersOfServerInstances)
|
||||
{
|
||||
var strm = new NamedPipeServerStream(pipeName, PipeDirection.InOut, maxNumbersOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
|
||||
numberOfStartedServerInstances += 1;
|
||||
strm.BeginWaitForConnection(EndAccept, strm);
|
||||
}
|
||||
}
|
||||
|
||||
private void EndAccept(IAsyncResult ar)
|
||||
{
|
||||
NamedPipeServerStream strm = (NamedPipeServerStream)ar.AsyncState;
|
||||
strm.EndWaitForConnection(ar);
|
||||
if (IsDisposed)
|
||||
{
|
||||
strm.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
var withBlock = new ManagedPipeClient(strm);
|
||||
withBlock.RetriveData += Client_RetriveData;
|
||||
withBlock.StatusMessage += Client_StatusMessage;
|
||||
withBlock.Disposed += Client_Disposed;
|
||||
withBlock.AddTo(Clients);
|
||||
}
|
||||
|
||||
CreateWaitingStream();
|
||||
}
|
||||
|
||||
/* TODO ERROR: Skipped RegionDirectiveTrivia */
|
||||
private void Client_Disposed(ManagedPipe Sender)
|
||||
{
|
||||
// den Client für die beendete Verbindung entfernen
|
||||
Sender.RemoveFrom(Clients);
|
||||
numberOfStartedServerInstances -= 1;
|
||||
CreateWaitingStream();
|
||||
}
|
||||
|
||||
private void Client_RetriveData(object sender, DataEventArgs e)
|
||||
{
|
||||
// einkommende ChatMessages anzeigen, und an alle versenden
|
||||
OnRetriveData(e);
|
||||
}
|
||||
|
||||
private void Client_StatusMessage(object sender, DataEventArgs e)
|
||||
{
|
||||
// einkommende StatusMessages durchreichen (zur Anzeige)
|
||||
OnStatusMessage(e);
|
||||
}
|
||||
|
||||
/* TODO ERROR: Skipped EndRegionDirectiveTrivia */
|
||||
public override Task SendAsnyc(byte[] bytes)
|
||||
{
|
||||
return Task.Run(() => Send(bytes));
|
||||
}
|
||||
|
||||
public override void Send(byte[] data)
|
||||
{
|
||||
foreach (ManagedPipeClient client in Clients) // an alle versenden
|
||||
client.Send(data);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (numberOfStartedServerInstances < maxNumbersOfServerInstances)
|
||||
{
|
||||
using (var clnt = new NamedPipeClientStream(pipeName))
|
||||
{
|
||||
// Herstellen einer Dummi-Verbindung, damit der ServerStream aus dem Wartezustand herauskommt.
|
||||
clnt.Connect();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = Clients.Count - 1; i >= 0; i -= 1)
|
||||
Clients[i].Dispose();
|
||||
var strm = new NamedPipeServerStream(pipeName, PipeDirection.InOut, maxNumbersOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
|
||||
numberOfStartedServerInstances += 1;
|
||||
strm.BeginWaitForConnection(EndAccept, strm);
|
||||
}
|
||||
}
|
||||
|
||||
private void EndAccept(IAsyncResult ar)
|
||||
{
|
||||
NamedPipeServerStream strm = (NamedPipeServerStream)ar.AsyncState;
|
||||
strm.EndWaitForConnection(ar);
|
||||
if (IsDisposed)
|
||||
{
|
||||
strm.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
var withBlock = new ManagedPipeClient(strm);
|
||||
withBlock.RetriveData += Client_RetriveData;
|
||||
withBlock.StatusMessage += Client_StatusMessage;
|
||||
withBlock.Disposed += Client_Disposed;
|
||||
withBlock.AddTo(Clients);
|
||||
}
|
||||
|
||||
CreateWaitingStream();
|
||||
}
|
||||
|
||||
/* TODO ERROR: Skipped RegionDirectiveTrivia */
|
||||
private void Client_Disposed(ManagedPipe Sender)
|
||||
{
|
||||
// den Client für die beendete Verbindung entfernen
|
||||
Sender.RemoveFrom(Clients);
|
||||
numberOfStartedServerInstances -= 1;
|
||||
CreateWaitingStream();
|
||||
}
|
||||
|
||||
private void Client_RetriveData(object sender, DataEventArgs e)
|
||||
{
|
||||
// einkommende ChatMessages anzeigen, und an alle versenden
|
||||
OnRetriveData(e);
|
||||
}
|
||||
|
||||
private void Client_StatusMessage(object sender, DataEventArgs e)
|
||||
{
|
||||
// einkommende StatusMessages durchreichen (zur Anzeige)
|
||||
OnStatusMessage(e);
|
||||
}
|
||||
|
||||
/* TODO ERROR: Skipped EndRegionDirectiveTrivia */
|
||||
public override Task SendAsnyc(byte[] bytes)
|
||||
{
|
||||
return Task.Run(() => Send(bytes));
|
||||
}
|
||||
|
||||
public override void Send(byte[] data)
|
||||
{
|
||||
foreach (ManagedPipeClient client in Clients) // an alle versenden
|
||||
client.Send(data);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (numberOfStartedServerInstances < maxNumbersOfServerInstances)
|
||||
{
|
||||
using (var clnt = new NamedPipeClientStream(pipeName))
|
||||
{
|
||||
// Herstellen einer Dummi-Verbindung, damit der ServerStream aus dem Wartezustand herauskommt.
|
||||
clnt.Connect();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = Clients.Count - 1; i >= 0; i -= 1)
|
||||
Clients[i].Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user