@@ -139,29 +139,19 @@ public async Task ConnectAsync(IButtplugClientConnector connector, CancellationT
139139 Convert . ToInt32 ( Math . Round ( si . MaxPingTime / 2.0 , 0 ) ) ) ;
140140 }
141141
142- if ( si . MessageVersion < ButtplugConsts . CurrentSpecVersion )
142+ if ( si . ProtocolVersionMajor < ButtplugConsts . ProtocolVersionMajor )
143143 {
144144 await DisconnectAsync ( ) . ConfigureAwait ( false ) ;
145145 throw new ButtplugHandshakeException (
146- $ "Buttplug Server's schema version ({ si . MessageVersion } ) is less than the client's ({ ButtplugConsts . CurrentSpecVersion } ). A newer server is required.",
146+ $ "Buttplug Server's schema version ({ si . ProtocolVersionMajor } ) is less than the client's ({ ButtplugConsts . ProtocolVersionMajor } ). A newer server is required.",
147147 res . Id ) ;
148148 }
149149
150150 // Get full device list and populate internal list
151151 var resp = await _handler . SendMessageAsync ( new RequestDeviceList ( ) ) . ConfigureAwait ( false ) ;
152152 if ( resp is DeviceList list )
153153 {
154- foreach ( var d in list . Devices )
155- {
156- if ( _devices . ContainsKey ( d . DeviceIndex ) )
157- {
158- continue ;
159- }
160-
161- var device = new ButtplugClientDevice ( _handler , d ) ;
162- _devices [ d . DeviceIndex ] = device ;
163- DeviceAdded ? . Invoke ( this , new DeviceAddedEventArgs ( device ) ) ;
164- }
154+ HandleDeviceList ( list ) ;
165155 }
166156 else
167157 {
@@ -174,9 +164,7 @@ public async Task ConnectAsync(IButtplugClientConnector connector, CancellationT
174164 throw new ButtplugHandshakeException (
175165 "Received unknown response to DeviceList handshake query" ) ;
176166 }
177-
178167 break ;
179-
180168 case Error e :
181169 await DisconnectAsync ( ) . ConfigureAwait ( false ) ;
182170 throw ButtplugException . FromError ( e ) ;
@@ -247,6 +235,30 @@ private void ConnectorErrorHandler(object sender, ButtplugExceptionEventArgs exc
247235 ErrorReceived ? . Invoke ( this , exception ) ;
248236 }
249237
238+ private void HandleDeviceList ( DeviceList listMsg )
239+ {
240+ foreach ( var pair in listMsg . Devices )
241+ {
242+ if ( ! _devices . ContainsKey ( pair . Key ) )
243+ {
244+ var dev = new ButtplugClientDevice ( _handler , pair . Value ) ;
245+ _devices . TryAdd ( pair . Key , dev ) ;
246+ DeviceAdded ? . Invoke ( this , new DeviceAddedEventArgs ( dev ) ) ;
247+ }
248+ }
249+
250+ foreach ( var index in _devices . Keys )
251+ {
252+ if ( ! listMsg . Devices . ContainsKey ( index ) )
253+ {
254+ if ( _devices . TryRemove ( index , out var oldDev ) )
255+ {
256+ DeviceRemoved ? . Invoke ( this , new DeviceRemovedEventArgs ( oldDev ) ) ;
257+ }
258+ }
259+ }
260+ }
261+
250262 /// <summary>
251263 /// Message Received event handler. Either tries to match incoming messages as replies to
252264 /// messages we've sent, or fires an event related to an incoming event, like device
@@ -260,28 +272,8 @@ private async void MessageReceivedHandler(object sender, MessageReceivedEventArg
260272
261273 switch ( msg )
262274 {
263- case DeviceAdded d :
264- var dev = new ButtplugClientDevice ( _handler , d ) ;
265- _devices . AddOrUpdate ( d . DeviceIndex , dev , ( u , device ) => dev ) ;
266- DeviceAdded ? . Invoke ( this , new DeviceAddedEventArgs ( dev ) ) ;
267- break ;
268-
269- case DeviceRemoved d :
270- if ( ! _devices . ContainsKey ( d . DeviceIndex ) )
271- {
272- ErrorReceived ? . Invoke ( this ,
273- new ButtplugExceptionEventArgs (
274- new ButtplugDeviceException (
275- "Got device removed message for unknown device." ,
276- msg . Id ) ) ) ;
277- return ;
278- }
279-
280- if ( _devices . TryRemove ( d . DeviceIndex , out var oldDev ) )
281- {
282- DeviceRemoved ? . Invoke ( this , new DeviceRemovedEventArgs ( oldDev ) ) ;
283- }
284-
275+ case DeviceList d :
276+ HandleDeviceList ( d ) ;
285277 break ;
286278
287279 case ScanningFinished _:
0 commit comments