Compare commits
No commits in common. "fa9335602eb849b6a70dace4ef41cec4a7b92f03" and "41f55c3ef2f16122b72b1634e4a99ce8b3d480d6" have entirely different histories.
fa9335602e
...
41f55c3ef2
5 changed files with 9 additions and 59 deletions
|
@ -54,7 +54,7 @@ namespace PixelHub.Server
|
||||||
/// Sends the given command to the remote PixelBot.
|
/// Sends the given command to the remote PixelBot.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="command">The command to send.</param>
|
/// <param name="command">The command to send.</param>
|
||||||
public async Task Send(PixelMessage command)
|
public async Task Send(PixelCommand command)
|
||||||
{
|
{
|
||||||
byte[] message = command.AsCompiledCommand();
|
byte[] message = command.AsCompiledCommand();
|
||||||
await client.GetStream().WriteAsync(message, 0, message.Length);
|
await client.GetStream().WriteAsync(message, 0, message.Length);
|
||||||
|
|
|
@ -34,7 +34,7 @@ namespace PixelHub
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies a single command that is to be sent to a PixelBot.
|
/// Specifies a single command that is to be sent to a PixelBot.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class PixelMessage
|
public abstract class PixelCommand
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A random number generator. Useful for generating default message ids.
|
/// A random number generator. Useful for generating default message ids.
|
||||||
|
@ -73,20 +73,7 @@ namespace PixelHub
|
||||||
public byte[] Header
|
public byte[] Header
|
||||||
{
|
{
|
||||||
get {
|
get {
|
||||||
byte[] result = new byte[HeaderSize];
|
|
||||||
// Fetch all the components needed to build the message header and convert them to byte arrays
|
|
||||||
byte[] rawProtocolVersion = BitConverter.GetBytes(ProtocolVersion);
|
|
||||||
byte[] rawMessageType = BitConverter.GetBytes((ushort)MessageType);
|
|
||||||
byte[] rawMessageId = BitConverter.GetBytes((ushort)MessageId);
|
|
||||||
byte[] rawMessageLength = BitConverter.GetBytes(Payload.Length);
|
|
||||||
|
|
||||||
// Copy the pieces into the correct places
|
|
||||||
Buffer.BlockCopy(rawProtocolVersion, 0, result, 0, rawProtocolVersion.Length);
|
|
||||||
Buffer.BlockCopy(rawMessageType, 0, result, 2, rawProtocolVersion.Length);
|
|
||||||
Buffer.BlockCopy(rawMessageId, 0, result, 4, rawProtocolVersion.Length);
|
|
||||||
Buffer.BlockCopy(rawMessageLength, 0, result, 8, rawProtocolVersion.Length);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,23 +82,16 @@ namespace PixelHub
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract byte[] Payload { get; protected set; }
|
public abstract byte[] Payload { get; protected set; }
|
||||||
|
|
||||||
public PixelMessage()
|
public PixelCommand()
|
||||||
{
|
{
|
||||||
byte[] rawMessageId = new byte[sizeof(uint)];
|
byte[] rawMessageId = new byte[sizeof(uint)];
|
||||||
rand.NextBytes(rawMessageId);
|
rand.NextBytes(rawMessageId);
|
||||||
MessageId = BitConverter.ToUInt32(rawMessageId, 0);
|
MessageId = BitConverter.ToUInt32(rawMessageId, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns this message in it's raw format that's sent and received over the wire.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>This message, as a byte array.</returns>
|
|
||||||
public byte[] AsCompiledCommand()
|
public byte[] AsCompiledCommand()
|
||||||
{
|
{
|
||||||
byte[] message = new byte[HeaderSize + Payload.Length];
|
byte[] message = new byte[HeaderSize + Payload.Length];
|
||||||
Buffer.BlockCopy(Header, 0, message, 0, HeaderSize);
|
|
||||||
Buffer.BlockCopy(Payload, 0, message, HeaderSize, Payload.Length);
|
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace PixelHub.Server.PixelMessages
|
namespace PixelHub.Server.PixelCommands
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Hard-resets the PixelBot. Useful if something has gone wrong, or to reset it's internal state.
|
/// Hard-resets the PixelBot. Useful if something has gone wrong, or to reset it's internal state.
|
||||||
/// Note that this will cause the PixelBot to drop it's connection!
|
/// Note that this will cause the PixelBot to drop it's connection!
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ResetMessage : PixelMessage
|
public class ResetCommand : PixelCommand
|
||||||
{
|
{
|
||||||
public override byte[] Payload {
|
public override byte[] Payload {
|
||||||
get {
|
get {
|
||||||
|
@ -19,7 +19,7 @@ namespace PixelHub.Server.PixelMessages
|
||||||
|
|
||||||
public new MessageTypes MessageType { get; private set; } = MessageTypes.Reset;
|
public new MessageTypes MessageType { get; private set; } = MessageTypes.Reset;
|
||||||
|
|
||||||
public ResetMessage()
|
public ResetCommand()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
|
@ -40,15 +40,14 @@
|
||||||
<Compile Include="SBRL.Utilities\ForgetTask.cs" />
|
<Compile Include="SBRL.Utilities\ForgetTask.cs" />
|
||||||
<Compile Include="SBRL.Utilities\Utilities.cs" />
|
<Compile Include="SBRL.Utilities\Utilities.cs" />
|
||||||
<Compile Include="SBRL.Utilities\PrefixedWriter.cs" />
|
<Compile Include="SBRL.Utilities\PrefixedWriter.cs" />
|
||||||
|
<Compile Include="PixelCommand.cs" />
|
||||||
<Compile Include="PixelBot.cs" />
|
<Compile Include="PixelBot.cs" />
|
||||||
<Compile Include="NetTools.cs" />
|
<Compile Include="NetTools.cs" />
|
||||||
<Compile Include="PixelMessage.cs" />
|
<Compile Include="PixelCommands\ResetCommand.cs" />
|
||||||
<Compile Include="PixelMessages\HandshakeRequestMessage.cs" />
|
|
||||||
<Compile Include="PixelMessages\ResetMessage.cs" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="SBRL.Utilities\" />
|
<Folder Include="SBRL.Utilities\" />
|
||||||
<Folder Include="PixelMessages\" />
|
<Folder Include="PixelCommands\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace PixelHub.Server.PixelMessages
|
|
||||||
{
|
|
||||||
public class HandshakeRequestMessage : PixelMessage
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// The type of PixelBot the client thinks it is. Rather like a user-agent string actually.
|
|
||||||
/// </summary>
|
|
||||||
public string PixelBotType = string.Empty;
|
|
||||||
|
|
||||||
public override byte[] Payload {
|
|
||||||
get {
|
|
||||||
return Encoding.UTF8.GetBytes(PixelBotType);
|
|
||||||
}
|
|
||||||
protected set {
|
|
||||||
PixelBotType = Encoding.UTF8.GetString(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public new MessageTypes MessageType { get; private set; } = MessageTypes.HandshakeRequest;
|
|
||||||
|
|
||||||
public HandshakeRequestMessage()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue