@@ -16,12 +16,12 @@ namespace Darp.Ble.HciHost.Gatt;
1616
1717internal sealed partial class HciHostGattClientPeer : GattClientPeer , IDisposable
1818{
19- private const ushort MaxMtu = 517 ;
2019 private const ushort GattMaxAttributeValueSize = 512 ;
21-
2220 private readonly IDisposable _hostSubscription ;
2321 private readonly IDisposable _assemblerSubscription ;
2422
23+ private bool _disposing ;
24+
2525 public ushort ConnectionHandle { get ; }
2626 public new HciHostBlePeripheral Peripheral { get ; }
2727 public Hci . Host . HciHost Host => Peripheral . Device . HciDevice . Host ;
@@ -57,6 +57,7 @@ private void HandleDisconnectionEvent(HciDisconnectionCompleteEvent hciEvent)
5757 hciEvent . Reason
5858 ) ;
5959 _disconnectedBehavior . OnNext ( value : true ) ;
60+ Dispose ( ) ;
6061 }
6162
6263 [ MessageSink ]
@@ -74,6 +75,7 @@ private void HandlePhyUpdateComplete(HciLePhyUpdateCompleteEvent hciEvent)
7475 [ MessageSink ]
7576 private async void HandleTypeRequest ( AttReadByTypeReq < ushort > request )
7677 {
78+ ObjectDisposedException . ThrowIf ( _disposing , this ) ;
7779 using Activity ? activity = Logging . StartHandleAttRequestActivity ( request , this ) ;
7880 BleUuid attributeType = BleUuid . FromUInt16 ( request . AttributeType ) ;
7981 int availablePduSpace = AttMtu - 2 ;
@@ -125,6 +127,7 @@ GattDatabaseEntry attribute in Peripheral.GattDatabase.Where(x =>
125127 [ MessageSink ]
126128 private async void HandleGroupTypeRequest ( AttReadByGroupTypeReq < ushort > request )
127129 {
130+ ObjectDisposedException . ThrowIf ( _disposing , this ) ;
128131 using Activity ? activity = Logging . StartHandleAttRequestActivity ( request , this ) ;
129132 if ( request . AttributeGroupType is not ( 0x2800 or 0x2801 ) )
130133 {
@@ -178,6 +181,7 @@ private async void HandleGroupTypeRequest(AttReadByGroupTypeReq<ushort> request)
178181 [ MessageSink ]
179182 private async void HandleGroupTypeRequest ( AttFindByTypeValueReq request )
180183 {
184+ ObjectDisposedException . ThrowIf ( _disposing , this ) ;
181185 using Activity ? activity = Logging . StartHandleAttRequestActivity ( request , this ) ;
182186
183187 BleUuid requestedAttributeType = BleUuid . FromUInt16 ( request . AttributeType ) ;
@@ -225,6 +229,7 @@ GattDatabaseEntry attribute in Peripheral.GattDatabase.Where(x =>
225229 [ MessageSink ]
226230 private async void HandleReadRequest ( AttReadReq request )
227231 {
232+ ObjectDisposedException . ThrowIf ( _disposing , this ) ;
228233 using Activity ? activity = Logging . StartHandleAttRequestActivity ( request , this ) ;
229234
230235 if ( ! Peripheral . GattDatabase . TryGetAttribute ( request . AttributeHandle , out IGattAttribute ? attribute ) )
@@ -272,6 +277,7 @@ private async void HandleReadRequest(AttReadReq request)
272277 [ MessageSink ]
273278 private void HandleFindInformationRequest ( AttFindInformationReq request )
274279 {
280+ ObjectDisposedException . ThrowIf ( _disposing , this ) ;
275281 using Activity ? activity = Logging . StartHandleAttRequestActivity ( request , this ) ;
276282
277283 if ( request . StartingHandle is 0 || request . EndingHandle < request . StartingHandle )
@@ -331,6 +337,7 @@ GattDatabaseEntry attribute in Peripheral.GattDatabase.Where(x =>
331337 [ MessageSink ]
332338 private async void HandleAttWriteRequest ( AttWriteReq request )
333339 {
340+ ObjectDisposedException . ThrowIf ( _disposing , this ) ;
334341 using Activity ? activity = Logging . StartHandleAttRequestActivity ( request , this ) ;
335342
336343 if ( ! Peripheral . GattDatabase . TryGetAttribute ( request . AttributeHandle , out IGattAttribute ? attribute ) )
@@ -401,6 +408,10 @@ private async void HandleAttWriteRequest(AttWriteReq request)
401408
402409 public void Dispose ( )
403410 {
411+ if ( _disposing )
412+ return ;
413+ _disposing = true ;
414+ _disconnectedBehavior . OnCompleted ( ) ;
404415 _disconnectedBehavior . Dispose ( ) ;
405416 _hostSubscription . Dispose ( ) ;
406417 _assemblerSubscription . Dispose ( ) ;
0 commit comments