Skip to content

Commit 2fb48f1

Browse files
ButtplugWSClient::SendMessage() always increments the msgID
This means consumers of the library do not need to manually set the message IDs at all now. Fixes #318
1 parent 30a8acd commit 2fb48f1

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

Buttplug.Client.Test/ButtPlugClientTests.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async void TestConnection()
4242
Assert.True(res != null);
4343
Assert.True(res is Core.Messages.Test);
4444
Assert.True(((Core.Messages.Test)res).TestString == "Test string");
45-
Assert.True(((Core.Messages.Test)res).Id == msgId);
45+
Assert.True(((Core.Messages.Test)res).Id > msgId);
4646

4747
// Check ping is working
4848
Thread.Sleep(400);
@@ -52,9 +52,15 @@ public async void TestConnection()
5252
Assert.True(res != null);
5353
Assert.True(res is Core.Messages.Test);
5454
Assert.True(((Core.Messages.Test)res).TestString == "Test string");
55-
Assert.True(((Core.Messages.Test)res).Id == msgId);
55+
Assert.True(((Core.Messages.Test)res).Id > msgId);
5656

57-
Assert.True(client.nextMsgId > 4);
57+
res = await client.SendMsg(new Core.Messages.Test("Test string"));
58+
Assert.True(res != null);
59+
Assert.True(res is Core.Messages.Test);
60+
Assert.True(((Core.Messages.Test)res).TestString == "Test string");
61+
Assert.True(((Core.Messages.Test)res).Id > msgId);
62+
63+
Assert.True(client.nextMsgId > 5);
5864

5965
await client.RequestDeviceList();
6066

@@ -77,7 +83,7 @@ public async void TestSSLConnection()
7783
Assert.True(res != null);
7884
Assert.True(res is Core.Messages.Test);
7985
Assert.True(((Core.Messages.Test)res).TestString == "Test string");
80-
Assert.True(((Core.Messages.Test)res).Id == msgId);
86+
Assert.True(((Core.Messages.Test)res).Id > msgId);
8187

8288
// Check ping is working
8389
Thread.Sleep(400);
@@ -87,7 +93,7 @@ public async void TestSSLConnection()
8793
Assert.True(res != null);
8894
Assert.True(res is Core.Messages.Test);
8995
Assert.True(((Core.Messages.Test)res).TestString == "Test string");
90-
Assert.True(((Core.Messages.Test)res).Id == msgId);
96+
Assert.True(((Core.Messages.Test)res).Id > msgId);
9197

9298
Assert.True(client.nextMsgId > 4);
9399

Buttplug.Client/ButtplugWSClient.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
using Buttplug.Core;
88
using Buttplug.Core.Messages;
99
using JetBrains.Annotations;
10-
using static Buttplug.Client.DeviceEventArgs;
11-
using WebSocket4Net;
1210
using SuperSocket.ClientEngine;
11+
using WebSocket4Net;
12+
using static Buttplug.Client.DeviceEventArgs;
1313

1414
namespace Buttplug.Client
1515
{
@@ -305,7 +305,7 @@ private async void onPingTimer(object state)
305305
{
306306
try
307307
{
308-
var msg = await SendMessage(new Ping(nextMsgId));
308+
var msg = await SendMessage(new Ping());
309309
if (msg is Error)
310310
{
311311
_owningDispatcher.Send(_ =>
@@ -326,7 +326,7 @@ private async void onPingTimer(object state)
326326

327327
public async Task RequestDeviceList()
328328
{
329-
var resp = await SendMessage(new RequestDeviceList(nextMsgId));
329+
var resp = await SendMessage(new RequestDeviceList());
330330
if (!(resp is DeviceList) || (resp as DeviceList).Devices == null)
331331
{
332332
if (resp is Error)
@@ -365,17 +365,17 @@ public ButtplugClientDevice[] getDevices()
365365

366366
public async Task<bool> StartScanning()
367367
{
368-
return await SendMessageExpectOk(new StartScanning(nextMsgId));
368+
return await SendMessageExpectOk(new StartScanning());
369369
}
370370

371371
public async Task<bool> StopScanning()
372372
{
373-
return await SendMessageExpectOk(new StopScanning(nextMsgId));
373+
return await SendMessageExpectOk(new StopScanning());
374374
}
375375

376376
public async Task<bool> RequestLog(string aLogLevel)
377377
{
378-
return await this.SendMessageExpectOk(new RequestLog(aLogLevel, nextMsgId));
378+
return await this.SendMessageExpectOk(new RequestLog(aLogLevel));
379379
}
380380

381381
public async Task<ButtplugMessage> SendDeviceMessage(ButtplugClientDevice aDevice, ButtplugDeviceMessage aDeviceMsg)
@@ -403,6 +403,9 @@ protected async Task<bool> SendMessageExpectOk(ButtplugMessage aMsg)
403403

404404
protected async Task<ButtplugMessage> SendMessage(ButtplugMessage aMsg)
405405
{
406+
// The client always increments the IDs on outgoing messages
407+
aMsg.Id = nextMsgId;
408+
406409
var promise = new TaskCompletionSource<ButtplugMessage>();
407410
_waitingMsgs.TryAdd(aMsg.Id, promise);
408411

0 commit comments

Comments
 (0)