Move utility classes to own folder

This commit is contained in:
Starbeamrainbowlabs 2016-10-16 15:22:05 +01:00
parent 48d61d74c7
commit 5b26c00352
6 changed files with 8 additions and 99 deletions

View File

@ -10,8 +10,8 @@
#include "Utilities.h"
char ssid[] = "ssid";
char password[] = "pass";
char ssid[] = "evenstar-2.4";
char password[] = "***REMOVED***";
// The address that the PixelHub beacon is broadcasting on.
IPAddress beaconAddress(239, 62, 148, 30);

View File

@ -1,94 +0,0 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Net;
using SBRL.Utilities;
using System.IO;
namespace PixelHub
{
/// <summary>
/// Provides the server component of the Hull Pixelbot server that can be used to control any number of pixel bots at once.
/// </summary>
public class PixelHub
{
TextWriter logger;
IPAddress bindAddress = IPAddress.Any;
int port;
TcpListener server;
/// <summary>
/// Whether the Hull Pixelbot serve is currently active and listening.
/// </summary>
public bool Active { get; private set; } = false;
public IPAddress BindAddress
{
get {
return bindAddress;
}
set {
if (Active == true)
throw new InvalidOperationException("Error: The bind address can't be changed once the server has started listening!");
bindAddress = value;
}
}
public int Port
{
get {
return port;
}
set {
if (Active == true)
throw new InvalidOperationException("Error: The port can't be changed once the server has started listening!");
port = value;
}
}
public IPEndPoint Endpoint
{
get {
return new IPEndPoint(BindAddress, Port);
}
}
public PixelHub(int inPort, TextWriter inLogger)
{
Port = inPort;
logger = inLogger;
}
public PixelHub(int inPort) : this(inPort, Console.Out)
{
}
public async Task Listen()
{
server = new TcpListener(Endpoint);
logger.WriteLine("TCP Listener set up on {0}.", Endpoint);
Active = true;
while(true)
{
TcpClient nextClient = await server.AcceptTcpClientAsync();
AsyncTools.ForgetTask(Handle(nextClient));
}
// TODO: Add an shutdown CancellationToken thingy here
//Active = false;
}
private async Task Handle(TcpClient client)
{
logger.WriteLine("Accepted connection from {0}.", client.Client.RemoteEndPoint);
using (StreamReader incoming = new StreamReader(client.GetStream()))
using (StreamWriter outgoing = new StreamWriter(client.GetStream()) { AutoFlush = true })
{
string nextLine;
while((nextLine = await incoming.ReadLineAsync()) != null)
{
Console.WriteLine("Got message from client: '{0}'", nextLine.Trim());
}
}
Console.WriteLine("Lost connection from {0}.", client.Client.RemoteEndPoint);
}
}
}

View File

@ -36,10 +36,13 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DiscoveryBeacon.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="ForgetTask.cs" />
<Compile Include="PixelHub.cs" />
<Compile Include="PrefixedWriter.cs" />
<Compile Include="SBRL.Utilities\ForgetTask.cs" />
<Compile Include="SBRL.Utilities\Utilities.cs" />
<Compile Include="SBRL.Utilities\PrefixedWriter.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Folder Include="SBRL.Utilities\" />
</ItemGroup>
</Project>