Move utility classes to own folder
This commit is contained in:
parent
48d61d74c7
commit
5b26c00352
6 changed files with 8 additions and 99 deletions
|
@ -10,8 +10,8 @@
|
||||||
|
|
||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
|
|
||||||
char ssid[] = "ssid";
|
char ssid[] = "evenstar-2.4";
|
||||||
char password[] = "pass";
|
char password[] = "***REMOVED***";
|
||||||
|
|
||||||
// The address that the PixelHub beacon is broadcasting on.
|
// The address that the PixelHub beacon is broadcasting on.
|
||||||
IPAddress beaconAddress(239, 62, 148, 30);
|
IPAddress beaconAddress(239, 62, 148, 30);
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -36,10 +36,13 @@
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="DiscoveryBeacon.cs" />
|
<Compile Include="DiscoveryBeacon.cs" />
|
||||||
<Compile Include="Utilities.cs" />
|
|
||||||
<Compile Include="ForgetTask.cs" />
|
|
||||||
<Compile Include="PixelHub.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>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="SBRL.Utilities\" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
Reference in a new issue