Skip to content

Commit d5f866c

Browse files
Remove device ID from XboxDevice and re-handle arrival message
1 parent 1d8a3d2 commit d5f866c

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

Program/PacketParsing/Backends/PcapBackend.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private static unsafe void OnPacketArrival(object sender, PacketCapture packet)
137137
ulong deviceId = header.DeviceId;
138138
if (!devices.TryGetValue(deviceId, out var device))
139139
{
140-
device = new XboxDevice(deviceId);
140+
device = new XboxDevice();
141141
devices.Add(deviceId, device);
142142
Console.WriteLine($"Device with ID {deviceId:X12} was connected");
143143
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace RB4InstrumentMapper.Parsing
4+
{
5+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
6+
internal readonly struct DeviceArrival
7+
{
8+
public readonly ulong SerialNumber;
9+
public readonly ushort VendorId;
10+
public readonly ushort ProductId;
11+
private readonly ulong ignored1;
12+
private readonly ulong ignored2;
13+
}
14+
}

Program/PacketParsing/XboxClient.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ internal unsafe XboxResult HandleMessage(CommandHeader header, ReadOnlySpan<byte
6868
{
6969
// Commands to ignore
7070
case CommandId.Acknowledgement:
71-
case CommandId.Arrival:
7271
case CommandId.Authentication:
7372
case CommandId.SerialNumber:
7473
break;
7574

75+
case CommandId.Arrival:
76+
return HandleArrival(commandData);
77+
7678
case CommandId.Status:
7779
return HandleStatus(commandData);
7880

@@ -162,6 +164,18 @@ private unsafe XboxResult ProcessPacketChunk(CommandHeader header, ref ReadOnlyS
162164
return XboxResult.Pending;
163165
}
164166

167+
/// <summary>
168+
/// Handles the arrival message of the device.
169+
/// </summary>
170+
private unsafe XboxResult HandleArrival(ReadOnlySpan<byte> data)
171+
{
172+
if (data.Length < sizeof(DeviceArrival) || MemoryMarshal.TryRead(data, out DeviceArrival arrival))
173+
return XboxResult.InvalidMessage;
174+
175+
Console.WriteLine($"New client connected with ID {arrival.SerialNumber:X12}");
176+
return XboxResult.Success;
177+
}
178+
165179
/// <summary>
166180
/// Handles the arrival message of the device.
167181
/// </summary>

Program/PacketParsing/XboxDevice.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,11 @@ public class XboxDevice : IDisposable
2424
{
2525
public static MappingMode MapperMode;
2626

27-
public ulong DeviceId { get; }
28-
2927
/// <summary>
3028
/// The clients currently on the device.
3129
/// </summary>
3230
private readonly Dictionary<int, XboxClient> clients = new Dictionary<int, XboxClient>();
3331

34-
public XboxDevice(ulong deviceId)
35-
{
36-
DeviceId = deviceId;
37-
}
38-
3932
~XboxDevice()
4033
{
4134
Dispose(false);
@@ -71,7 +64,6 @@ public unsafe XboxResult HandlePacket(ReadOnlySpan<byte> data)
7164
{
7265
client = new XboxClient();
7366
clients.Add(header.Client, client);
74-
Console.WriteLine($"Client {header.Client} connected on device with ID {DeviceId:X12}");
7567
}
7668
var clientResult = client.HandleMessage(header, commandData);
7769
switch (clientResult)

Program/RB4InstrumentMapper.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Compile Include="PacketParsing\Mappers\VigemMapper.cs" />
123123
<Compile Include="PacketParsing\Mappers\VjoyMapper.cs" />
124124
<Compile Include="PacketParsing\Packets\CommandHeader.cs" />
125+
<Compile Include="PacketParsing\Packets\DeviceArrival.cs" />
125126
<Compile Include="PacketParsing\Packets\DeviceStatus.cs" />
126127
<Compile Include="PacketParsing\Packets\DrumInput.cs" />
127128
<Compile Include="PacketParsing\Packets\GamepadInput.cs" />

0 commit comments

Comments
 (0)