Add NOP packet.

This commit is contained in:
2019-10-13 20:54:10 +01:00
parent 03af9b7f2e
commit 7fd5b16b1d
3 changed files with 56 additions and 6 deletions

View File

@@ -1,7 +1,8 @@
namespace DiscImageChef.Devices.Remote
{
public enum DicPacketType : byte
public enum DicPacketType : sbyte
{
Nop = -1,
Hello = 1,
CommandListDevices = 2,
ResponseListDevices = 3

View File

@@ -14,8 +14,8 @@ namespace DiscImageChef.Devices.Remote
{
public class Remote : IDisposable
{
private readonly Socket _socket;
private readonly string _host;
private readonly Socket _socket;
public Remote(string host)
{
@@ -54,19 +54,38 @@ namespace DiscImageChef.Devices.Remote
throw new ArgumentException();
}
byte[] buf;
if (hdr.packetType != DicPacketType.Hello)
{
if (hdr.packetType != DicPacketType.Nop)
{
DicConsole.ErrorWriteLine("Expected Hello Packet, got packet type {0}...", hdr.packetType);
throw new ArgumentException();
}
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
throw new IOException();
}
var nop = Marshal.ByteArrayToStructureLittleEndian<DicPacketNop>(buf);
DicConsole.ErrorWriteLine($"{nop.reason}");
throw new ArgumentException();
}
if (hdr.version != Consts.PacketVersion)
{
DicConsole.ErrorWriteLine("Unrecognized packet version...");
throw new ArgumentException();
}
var buf = new byte[hdr.len];
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
@@ -173,12 +192,29 @@ namespace DiscImageChef.Devices.Remote
}
if (hdr.packetType != DicPacketType.ResponseListDevices)
{
if (hdr.packetType != DicPacketType.Nop)
{
DicConsole.ErrorWriteLine("Expected List Devices Response Packet, got packet type {0}...",
hdr.packetType);
return new DeviceInfo[0];
}
buf = new byte[hdr.len];
len = _socket.Receive(buf, buf.Length, SocketFlags.None);
if (len < buf.Length)
{
DicConsole.ErrorWriteLine("Could not read from the network...");
return new DeviceInfo[0];
}
var nop = Marshal.ByteArrayToStructureLittleEndian<DicPacketNop>(buf);
DicConsole.ErrorWriteLine($"{nop.reason}");
return new DeviceInfo[0];
}
if (hdr.version != Consts.PacketVersion)
{
DicConsole.ErrorWriteLine("Unrecognized packet version...");

View File

@@ -53,4 +53,17 @@ namespace DiscImageChef.Devices.Remote
public readonly DicPacketHeader hdr;
public readonly ushort devices;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct DicPacketNop
{
public DicPacketHeader hdr;
public byte reasonCode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public readonly byte[] spare;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string reason;
}
}