A quick and dirty Windows Service to start and stop network services on-demand.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

78 lines
1.4 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KnockKnock.Lib
{
internal class ConnectionPool
{
public delegate void PoolEvent(ConnectionPool sender);
public event PoolEvent Closed;
List<Connection> connections;
public ConnectionPool()
{
connections = new List<Connection>();
}
public void Open()
{
foreach (Connection c in this.connections)
{
c.Open();
}
}
public void AddConnection(Connection c)
{
this.connections.Add(c);
c.Received += Connection_Receive;
c.Closed += Connection_Closed;
}
public void RemoveConnection(Connection c)
{
this.connections.Remove(c);
c.Received -= Connection_Receive;
c.Closed -= Connection_Closed;
}
public void Close()
{
foreach (Connection c in this.connections)
{
c.Close();
}
}
protected void Connection_Receive(Connection sender, byte[] data)
{
System.Diagnostics.Debug.WriteLine("Receive " + data.Length + " bytes");
foreach (Connection c in this.connections)
{
if (c != sender)
{
System.Diagnostics.Debug.WriteLine("Send " + data.Length + " bytes");
c.Send(data);
}
}
}
protected void Connection_Closed(Connection sender)
{
System.Diagnostics.Debug.WriteLine("Closed");
foreach (Connection c in this.connections)
{
if (c != sender)
{
c.Close();
}
}
Closed(this);
}
}
}