add ObexListener.GetContextAsync() (#447)

* add AcceptBluetoothClientAsync() to IBluetoothListener

* add ObexListener.GetContextAsync()
This commit is contained in:
Bonuspunkt
2025-03-09 14:44:49 +01:00
committed by GitHub
parent 02648eeac9
commit bb327006a0
10 changed files with 153 additions and 5 deletions
@@ -8,6 +8,8 @@
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.Sdp;
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace InTheHand.Net.Sockets
{
@@ -224,6 +226,14 @@ namespace InTheHand.Net.Sockets
return _bluetoothListener.AcceptBluetoothClient();
}
public Task<BluetoothClient> AcceptBluetoothClientAsync()
{
if (!Active)
throw new InvalidOperationException("Not listening. You must call the Start() method before calling this method.");
return _bluetoothListener.AcceptBluetoothClientAsync();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
@@ -5,7 +5,7 @@
<Company>In The Hand Ltd</Company>
<Authors>Peter Foot</Authors>
<Product>32feet.NET</Product>
<Version>4.1.44</Version>
<Version>4.2.0</Version>
<RepositoryUrl>https://github.com/inthehand/32feet</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -20,8 +20,8 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<Copyright>Copyright (c) 2004-2024 In The Hand Ltd</Copyright>
<Description>32feet.NET is an open-source project to make personal area networking technologies such as Bluetooth easily accessible from .NET code.</Description>
<FileVersion>4.1.44</FileVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<FileVersion>4.2.0</FileVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyName>InTheHand.Net.Bluetooth</AssemblyName>
<AssemblyTitle>InTheHand.Net.Bluetooth-$(TargetFramework)</AssemblyTitle>
<GenerateDocumentationFile Condition=" '$(Configuration)' == 'Release' ">true</GenerateDocumentationFile>
@@ -8,6 +8,7 @@
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.Sdp;
using System;
using System.Threading.Tasks;
namespace InTheHand.Net.Sockets
{
@@ -27,5 +28,6 @@ namespace InTheHand.Net.Sockets
ServiceRecord ServiceRecord { get; set; }
BluetoothClient AcceptBluetoothClient();
Task<BluetoothClient> AcceptBluetoothClientAsync();
}
}
@@ -8,6 +8,7 @@
using Android.Bluetooth;
using InTheHand.Net.Bluetooth;
using System;
using System.Threading.Tasks;
namespace InTheHand.Net.Sockets
{
@@ -54,5 +55,17 @@ namespace InTheHand.Net.Sockets
return null;
}
public async Task<BluetoothClient> AcceptBluetoothClientAsync()
{
var newSocket = await socket.AcceptAsync();
if (newSocket != null)
{
return new BluetoothClient(new AndroidBluetoothClient(newSocket));
}
return null;
}
}
}
@@ -10,6 +10,7 @@ using InTheHand.Net.Bluetooth.Sdp;
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace InTheHand.Net.Sockets
{
@@ -62,9 +63,19 @@ namespace InTheHand.Net.Sockets
return socket.Accept();
}
public Task<Socket> AcceptSocketAsync()
{
return Task.Factory.FromAsync(socket.BeginAccept, socket.EndAccept, null);
}
public BluetoothClient AcceptBluetoothClient()
{
return new BluetoothClient(new LinuxBluetoothClient((LinuxSocket)AcceptSocket()));
}
public async Task<BluetoothClient> AcceptBluetoothClientAsync()
{
return new BluetoothClient(new LinuxBluetoothClient((LinuxSocket) await AcceptSocketAsync()));
}
}
}
@@ -229,9 +229,28 @@ namespace InTheHand.Net.Sockets
}
}
public Task<Socket> AcceptSocketAsync()
{
if (NativeMethods.IsRunningOnMono())
{
var w32Socket = (Win32Socket)socket;
return Task.Factory.FromAsync(w32Socket.BeginAccept, w32Socket.EndAccept,null);
}
else
{
return Task.Factory.FromAsync(socket.BeginAccept, socket.EndAccept, null);
}
}
public BluetoothClient AcceptBluetoothClient()
{
return new BluetoothClient(new Win32BluetoothClient(AcceptSocket()));
}
public async Task<BluetoothClient> AcceptBluetoothClientAsync()
{
return new BluetoothClient(new Win32BluetoothClient(await AcceptSocketAsync()));
}
}
}
@@ -8,6 +8,8 @@
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.Sdp;
using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets;
@@ -83,5 +85,40 @@ namespace InTheHand.Net.Sockets
throw new InvalidOperationException();
}
}
public async Task<BluetoothClient> AcceptBluetoothClientAsync()
{
if(listener != null)
{
await FromWaitHandle(listenHandle);
pending = false;
return currentClient;
}
else
{
throw new InvalidOperationException();
}
}
private static Task<bool> FromWaitHandle(EventWaitHandle handle)
{
// Handle synchronous cases.
var alreadySignalled = handle.WaitOne(0);
if (alreadySignalled)
return Task.FromResult(true);
var tcs = new TaskCompletionSource<bool>();
ThreadPool.RegisterWaitForSingleObject(
waitObject: handle,
callBack: (state, @out) => tcs.TrySetResult(!@out),
state: null,
millisecondsTimeOutInterval: -1,
executeOnlyOnce: true
);
return tcs.Task;
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
<Sdk Name="DotNet.ReproducibleBuilds.Isolated" Version="1.2.4" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.0.4</Version>
<Version>4.1.0</Version>
<Company>In The Hand Ltd</Company>
<Product>32feet</Product>
<Authors>Peter Foot</Authors>
+1 -1
View File
@@ -41,7 +41,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="InTheHand.Net.Bluetooth" Version="4.1.44" />
<PackageReference Include="InTheHand.Net.Bluetooth" Version="4.2.0" />
<PackageReference Include="InTheHand.Net.IrDA" Version="4.0.4" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.25" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
+56
View File
@@ -15,6 +15,7 @@ using InTheHand.Net.Bluetooth;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
using InTheHand.Net.Bluetooth.Sdp;
using InTheHand.Net.Bluetooth.AttributeIds;
@@ -291,5 +292,60 @@ namespace InTheHand.Net
return null;
}
}
public async Task<ObexListenerContext> GetContextAsync()
{
if (!listening)
{
throw new InvalidOperationException("Listener not started");
}
try
{
Socket socket;
Stream stream = null;
switch (transport)
{
case ObexTransport.Bluetooth:
var bluetoothClient = await bListener.AcceptBluetoothClientAsync();
socket = (await bListener.AcceptBluetoothClientAsync()).Client;
if (socket == null)
{
stream = bluetoothClient.GetStream(); // platforms which don't use System.Net.Sockets can return a stream instead
}
break;
case ObexTransport.IrDA:
#if NO_IRDA
throw new NotSupportedException("No IrDA on this platform.");
#else
socket = (await iListener.AcceptIrDAClientAsync(null)).Client;
break;
#endif
default:
socket = (await tListener.AcceptTcpClientAsync()).Client;
break;
}
if (socket != null)
{
Debug.WriteLine($"Socket {socket.GetHashCode():X8}: Accepted", "ObexListener");
return new ObexListenerContext(socket);
}
else if (stream != null)
{
Debug.WriteLine($"Stream {stream.GetHashCode():X8}: Accepted", "ObexListener");
// call to stream implementation to go here
}
return null;
}
catch
{
return null;
}
}
}
}