From 319b06c35a7443fa55ec45a3d9064f09b566a3ae Mon Sep 17 00:00:00 2001 From: Chris Wood Date: Wed, 6 May 2020 19:46:28 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + README.md | 164 ++- RSSDP_LICENSE.md | 21 + src/Rssdp.NetCore/GlobalSuppressions.cs | Bin 0 -> 4436 bytes src/Rssdp.NetCore/Properties/AssemblyInfo.cs | 19 + src/Rssdp.NetCore/Rssdp.NetCore.csproj | 75 ++ src/Rssdp.NetCore/SsdpDevicePublisher.cs | 92 ++ src/Rssdp.NetCore/project.json | 26 + src/Rssdp.Shared/AssemblyInfoCommon.cs | 9 + src/Rssdp.Shared/CustomHttpHeaders.cs | 295 +++++ src/Rssdp.Shared/DeviceAvailableEventArgs.cs | 61 + src/Rssdp.Shared/DeviceEventArgs.cs | 49 + src/Rssdp.Shared/DeviceNetworkType.cs | 17 + .../DeviceNetworkTypeExtensions.cs | 37 + .../DeviceUnavailableEventArgs.cs | 60 + src/Rssdp.Shared/DiscoveredSsdpDevice.cs | 177 +++ .../DisposableManagedObjectBase.cs | 76 ++ src/Rssdp.Shared/ExceptionExtensions.cs | 47 + src/Rssdp.Shared/GlobalSuppressions.cs | Bin 0 -> 15112 bytes src/Rssdp.Shared/HttpParserBase.cs | 244 ++++ src/Rssdp.Shared/HttpRequestParser.cs | 91 ++ src/Rssdp.Shared/HttpResponseParser.cs | 93 ++ src/Rssdp.Shared/IEnumerableExtensions.cs | 28 + src/Rssdp.Shared/ISocketFactory.cs | 36 + src/Rssdp.Shared/ISsdpCommunicationsServer.cs | 81 ++ src/Rssdp.Shared/ISsdpDeviceLocator.cs | 146 +++ src/Rssdp.Shared/ISsdpDevicePublisher.cs | 37 + src/Rssdp.Shared/ISsdpLogger.cs | 38 + src/Rssdp.Shared/IUPnPDeviceValidator.cs | 27 + src/Rssdp.Shared/IUdpSocket.cs | 28 + src/Rssdp.Shared/NullLogger.cs | 63 + src/Rssdp.Shared/ReadOnlyEnumerable.cs | 46 + src/Rssdp.Shared/ReceivedUdpData.cs | 29 + src/Rssdp.Shared/RequestReceivedEventArgs.cs | 60 + src/Rssdp.Shared/ResponseReceivedEventArgs.cs | 60 + src/Rssdp.Shared/Rssdp.Shared.projitems | 58 + src/Rssdp.Shared/Rssdp.Shared.shproj | 13 + src/Rssdp.Shared/ServiceEventArgs.cs | 49 + src/Rssdp.Shared/SocketClosedException.cs | 43 + src/Rssdp.Shared/SsdpCommunicationsServer.cs | 451 +++++++ src/Rssdp.Shared/SsdpConstants.cs | 88 ++ src/Rssdp.Shared/SsdpDevice.cs | 985 +++++++++++++++ src/Rssdp.Shared/SsdpDeviceExtensions.cs | 37 + src/Rssdp.Shared/SsdpDeviceIcon.cs | 51 + src/Rssdp.Shared/SsdpDeviceLocatorBase.cs | 736 +++++++++++ src/Rssdp.Shared/SsdpDeviceProperties.cs | 206 ++++ src/Rssdp.Shared/SsdpDeviceProperty.cs | 36 + src/Rssdp.Shared/SsdpDevicePublisherBase.cs | 1078 +++++++++++++++++ src/Rssdp.Shared/SsdpEmbeddedDevice.cs | 69 ++ src/Rssdp.Shared/SsdpRootDevice.cs | 176 +++ src/Rssdp.Shared/SsdpService.cs | 284 +++++ src/Rssdp.Shared/SsdpStandardsMode.cs | 29 + src/Rssdp.Shared/TaskEx.cs | 54 + src/Rssdp.Shared/UPnP10DeviceValidator.cs | 223 ++++ src/Rssdp.Shared/UdpEndPoint.cs | 37 + src/Shared/AssemblyInfoCommon.cs | 15 + src/Shared/CodeAnalysisDictionary.xml | 31 + src/Shared/ExceptionExtensions.cs | 45 + src/Shared/SsdpDeviceLocator.cs | 63 + src/Shared/SsdpTraceLogger.cs | 62 + src/Shared/SystemNetSockets/SocketFactory.cs | 206 ++++ src/Shared/SystemNetSockets/UdpSocket.cs | 194 +++ src/Shared/WinRTSockets/SocketsFactory.cs | 69 ++ .../WinRTSockets/SsdpDevicePublisher.cs | 92 ++ src/Shared/WinRTSockets/UdpSocket.cs | 150 +++ src/Ukor.sln | 37 + src/Ukor/Configuration/Application.cs | 81 ++ src/Ukor/Configuration/ApplicationList.cs | 11 + src/Ukor/Configuration/ApplicationOptions.cs | 14 + src/Ukor/Configuration/CSharpDetails.cs | 13 + src/Ukor/Configuration/CommandLineDetails.cs | 19 + src/Ukor/Configuration/GeneralOptions.cs | 10 + src/Ukor/Configuration/ICSharpAction.cs | 9 + src/Ukor/Configuration/LocalServerOptions.cs | 8 + src/Ukor/Controllers/DiscoveryController.cs | 28 + src/Ukor/Controllers/InputController.cs | 15 + src/Ukor/Controllers/InstallController.cs | 16 + src/Ukor/Controllers/KeyDownController.cs | 16 + src/Ukor/Controllers/KeyPressController.cs | 28 + src/Ukor/Controllers/KeyUpController.cs | 16 + src/Ukor/Controllers/LaunchController.cs | 44 + src/Ukor/Controllers/QueryController.cs | 61 + src/Ukor/Controllers/SearchController.cs | 16 + src/Ukor/Logging/SsdpLogger.cs | 49 + src/Ukor/Program.cs | 23 + src/Ukor/Properties/launchSettings.json | 14 + src/Ukor/Services/ApplicationService.cs | 86 ++ src/Ukor/Services/IApplicationService.cs | 10 + src/Ukor/Services/IKeyPressService.cs | 10 + src/Ukor/Services/KeyPressService.cs | 53 + src/Ukor/Startup.cs | 220 ++++ .../StaticResponses/QueryActiveAppResponse.cs | 10 + .../QueryDeviceInfoResponse.cs | 80 ++ .../QueryMediaPlayerResponse.cs | 27 + src/Ukor/Ukor.csproj | 17 + src/Ukor/appsettings.Development.json | 9 + src/Ukor/appsettings.json | 27 + src/Ukor/wwwroot/Device.xml | 34 + src/Ukor/wwwroot/app.jpg | Bin 0 -> 10937 bytes src/Ukor/wwwroot/dial_SCPD.xml | 12 + src/Ukor/wwwroot/ecp_SCPD.xml | 12 + two-button-permutations.txt | 225 ++++ 102 files changed, 9323 insertions(+), 1 deletion(-) create mode 100644 RSSDP_LICENSE.md create mode 100644 src/Rssdp.NetCore/GlobalSuppressions.cs create mode 100644 src/Rssdp.NetCore/Properties/AssemblyInfo.cs create mode 100644 src/Rssdp.NetCore/Rssdp.NetCore.csproj create mode 100644 src/Rssdp.NetCore/SsdpDevicePublisher.cs create mode 100644 src/Rssdp.NetCore/project.json create mode 100644 src/Rssdp.Shared/AssemblyInfoCommon.cs create mode 100644 src/Rssdp.Shared/CustomHttpHeaders.cs create mode 100644 src/Rssdp.Shared/DeviceAvailableEventArgs.cs create mode 100644 src/Rssdp.Shared/DeviceEventArgs.cs create mode 100644 src/Rssdp.Shared/DeviceNetworkType.cs create mode 100644 src/Rssdp.Shared/DeviceNetworkTypeExtensions.cs create mode 100644 src/Rssdp.Shared/DeviceUnavailableEventArgs.cs create mode 100644 src/Rssdp.Shared/DiscoveredSsdpDevice.cs create mode 100644 src/Rssdp.Shared/DisposableManagedObjectBase.cs create mode 100644 src/Rssdp.Shared/ExceptionExtensions.cs create mode 100644 src/Rssdp.Shared/GlobalSuppressions.cs create mode 100644 src/Rssdp.Shared/HttpParserBase.cs create mode 100644 src/Rssdp.Shared/HttpRequestParser.cs create mode 100644 src/Rssdp.Shared/HttpResponseParser.cs create mode 100644 src/Rssdp.Shared/IEnumerableExtensions.cs create mode 100644 src/Rssdp.Shared/ISocketFactory.cs create mode 100644 src/Rssdp.Shared/ISsdpCommunicationsServer.cs create mode 100644 src/Rssdp.Shared/ISsdpDeviceLocator.cs create mode 100644 src/Rssdp.Shared/ISsdpDevicePublisher.cs create mode 100644 src/Rssdp.Shared/ISsdpLogger.cs create mode 100644 src/Rssdp.Shared/IUPnPDeviceValidator.cs create mode 100644 src/Rssdp.Shared/IUdpSocket.cs create mode 100644 src/Rssdp.Shared/NullLogger.cs create mode 100644 src/Rssdp.Shared/ReadOnlyEnumerable.cs create mode 100644 src/Rssdp.Shared/ReceivedUdpData.cs create mode 100644 src/Rssdp.Shared/RequestReceivedEventArgs.cs create mode 100644 src/Rssdp.Shared/ResponseReceivedEventArgs.cs create mode 100644 src/Rssdp.Shared/Rssdp.Shared.projitems create mode 100644 src/Rssdp.Shared/Rssdp.Shared.shproj create mode 100644 src/Rssdp.Shared/ServiceEventArgs.cs create mode 100644 src/Rssdp.Shared/SocketClosedException.cs create mode 100644 src/Rssdp.Shared/SsdpCommunicationsServer.cs create mode 100644 src/Rssdp.Shared/SsdpConstants.cs create mode 100644 src/Rssdp.Shared/SsdpDevice.cs create mode 100644 src/Rssdp.Shared/SsdpDeviceExtensions.cs create mode 100644 src/Rssdp.Shared/SsdpDeviceIcon.cs create mode 100644 src/Rssdp.Shared/SsdpDeviceLocatorBase.cs create mode 100644 src/Rssdp.Shared/SsdpDeviceProperties.cs create mode 100644 src/Rssdp.Shared/SsdpDeviceProperty.cs create mode 100644 src/Rssdp.Shared/SsdpDevicePublisherBase.cs create mode 100644 src/Rssdp.Shared/SsdpEmbeddedDevice.cs create mode 100644 src/Rssdp.Shared/SsdpRootDevice.cs create mode 100644 src/Rssdp.Shared/SsdpService.cs create mode 100644 src/Rssdp.Shared/SsdpStandardsMode.cs create mode 100644 src/Rssdp.Shared/TaskEx.cs create mode 100644 src/Rssdp.Shared/UPnP10DeviceValidator.cs create mode 100644 src/Rssdp.Shared/UdpEndPoint.cs create mode 100644 src/Shared/AssemblyInfoCommon.cs create mode 100644 src/Shared/CodeAnalysisDictionary.xml create mode 100644 src/Shared/ExceptionExtensions.cs create mode 100644 src/Shared/SsdpDeviceLocator.cs create mode 100644 src/Shared/SsdpTraceLogger.cs create mode 100644 src/Shared/SystemNetSockets/SocketFactory.cs create mode 100644 src/Shared/SystemNetSockets/UdpSocket.cs create mode 100644 src/Shared/WinRTSockets/SocketsFactory.cs create mode 100644 src/Shared/WinRTSockets/SsdpDevicePublisher.cs create mode 100644 src/Shared/WinRTSockets/UdpSocket.cs create mode 100644 src/Ukor.sln create mode 100644 src/Ukor/Configuration/Application.cs create mode 100644 src/Ukor/Configuration/ApplicationList.cs create mode 100644 src/Ukor/Configuration/ApplicationOptions.cs create mode 100644 src/Ukor/Configuration/CSharpDetails.cs create mode 100644 src/Ukor/Configuration/CommandLineDetails.cs create mode 100644 src/Ukor/Configuration/GeneralOptions.cs create mode 100644 src/Ukor/Configuration/ICSharpAction.cs create mode 100644 src/Ukor/Configuration/LocalServerOptions.cs create mode 100644 src/Ukor/Controllers/DiscoveryController.cs create mode 100644 src/Ukor/Controllers/InputController.cs create mode 100644 src/Ukor/Controllers/InstallController.cs create mode 100644 src/Ukor/Controllers/KeyDownController.cs create mode 100644 src/Ukor/Controllers/KeyPressController.cs create mode 100644 src/Ukor/Controllers/KeyUpController.cs create mode 100644 src/Ukor/Controllers/LaunchController.cs create mode 100644 src/Ukor/Controllers/QueryController.cs create mode 100644 src/Ukor/Controllers/SearchController.cs create mode 100644 src/Ukor/Logging/SsdpLogger.cs create mode 100644 src/Ukor/Program.cs create mode 100644 src/Ukor/Properties/launchSettings.json create mode 100644 src/Ukor/Services/ApplicationService.cs create mode 100644 src/Ukor/Services/IApplicationService.cs create mode 100644 src/Ukor/Services/IKeyPressService.cs create mode 100644 src/Ukor/Services/KeyPressService.cs create mode 100644 src/Ukor/Startup.cs create mode 100644 src/Ukor/StaticResponses/QueryActiveAppResponse.cs create mode 100644 src/Ukor/StaticResponses/QueryDeviceInfoResponse.cs create mode 100644 src/Ukor/StaticResponses/QueryMediaPlayerResponse.cs create mode 100644 src/Ukor/Ukor.csproj create mode 100644 src/Ukor/appsettings.Development.json create mode 100644 src/Ukor/appsettings.json create mode 100644 src/Ukor/wwwroot/Device.xml create mode 100644 src/Ukor/wwwroot/app.jpg create mode 100644 src/Ukor/wwwroot/dial_SCPD.xml create mode 100644 src/Ukor/wwwroot/ecp_SCPD.xml create mode 100644 two-button-permutations.txt diff --git a/.gitignore b/.gitignore index dfcfd56..45ca835 100644 --- a/.gitignore +++ b/.gitignore @@ -348,3 +348,5 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ + +src/lib \ No newline at end of file diff --git a/README.md b/README.md index ad5708a..c424155 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,164 @@ # Ukor -A fake Roku Streaming Stick device that can be used as gateway for enabling home automation +A fake Roku Streaming Stick device that can be used as gateway to enable home automation. + +## What is it? Why was it written? + +I have a Logitech Harmony remote at home and it's connected to a few different devices: Fire TV Cubes, Chromecasts, Rokus, amplifiers, light bulbs, etc. However, I ran into a use case where as part of an activity, I wanted to do something slightly unusual (change the behaviour of my DNS server). + +Unfortunately, Harmony doesn't allow you to do such things; you can work with a very specific range of devices, but not beyond those. + +The Harmony system can, however, speak to Roku devices and uses HTTP requests to do so. + +I decided to see whether I could build a *fake* Roku Streaming Stick+ - i.e. something that imitates the device's simple XML-based HTTP API. It would receive instructions from the Harmony Hub, but rather doing what was requested (e.g. launch a channel or fast-forward), it would do something completely different that could be defined through configuration - either via a command line task or a C# plugin. + +Ukor is so-named because it's Roku spelled backwards. I'm clearly not very imaginative! + +## What can it do? + +Ukor speaks "SSDP" - a Universal Plug And Play protocol - so it can be discovered on your home network by home automation systems such as Logitech's Harmony Hub. + +Once discovered, Ukor has been designed to launch applications in two ways: + +* using a sequence of Roku button presses; or +* by launching a channel. + +For example, you could get a Harmony activity to send `Fwd, Fwd` (i.e. the fast-forward button twice) and use this sequence to launch an executable. + +Alternatively, all your configured Ukor applications can be launched as if they were a channel. So much like you'd ordinarily tell a *real* Roku device to launch the Netflix channel, you could instead tell your fake Roku to launch your "Turn On Intruder Alarm" channel. + +The reason both methods are offered is that the Harmony Hub only allows you to launch channels in a "Watch TV" activity, which isn't always ideally suited for every Harmony activity. Just being able to do stuff on a simple button press avoids those limitations. However, the channel-based approach may be easier on other automation platforms. + +## What can't it do? + +Hopefully it's obvious by now: it's *not* a real Roku. So you can't install apps, watch videos or anything like that! + +It's simply a means-to-an-end of opening up more automation possibilities within the home. + +When setting up Ukor within your home automation system, you'll probably be asked to confirm that your Roku device is now showing stuff on your television. Obviously, just lie and say "yes" in this scenario. We both know it's never going to actually display anything! + +## What are the pre-requisites to run Ukor? + +To run Ukor, you'll need the [.NET Core 3.1 runtime](https://dotnet.microsoft.com/download/dotnet-core/3.1). + +To develop using Ukor, you'll need Visual Studio or Visual Studio Code. + +## How do I configure it? + +### Software Configuration + +All configuration happens via `appsettings.json` . + +Within the file, you'll see an array called `Applications`. This is an individual application: + +```json + { + "Id": 1000, + "Name": "Set DNS", + "Action": "CSharp", + "CSharpDetails": { + "AssemblyPath": "C:\\MyPlugin\\Chris.Ukor.dll", + "ClassName": "SetDns" + }, + "LaunchKeySequence": [ + "Left" + ] + } +``` + +Applications have an integer-based `Id` which are used as channel IDs in the fake Roku. Start numbering these at `1000`. You can specify a `Name` that will be used as a channel name as well as an action that is either `CSharp` or `CommandLine`. + +For `CSharp` actions, provide a `CSharpDetails` property as shown above. This contains the file path to a .NET Standard or .NET Core DLL containing your plugin and the name of the class to instantiate. All fields are required. Plugin classes must implement the `ICSharpAction` interface found within the Ukor project and must have a parameterless constructor. + +For `CommandLine` actions, specify a `CommandLineDetails` property: + +```json + { + "Id": 1002, + "Name": "Launch Notepad", + "Action": "CommandLine", + "CommandLineDetails": { + "Executable": "notepad.exe", + "Arguments": null, + "WorkingFolder": null, + "WaitForExit": false + }, + "LaunchKeySequence": [ + "Fwd" + ] + } +``` + +`Arguments` and `WorkingFolder` are both optional; `Executable` and `WaitForExit` are required fields. + +An application can be launched optionally with a `LaunchKeySequence`. This is an array of button presses made up of the following values: + +``` + Home + Rev + Fwd + Play + Select + Left + Right + Down + Up + Back + InstantReplay + Info + Backspace + Search + Enter +``` + +The length of the sequence is specified by `KeySequenceLength` at the root of `appsettings.json`. All `LaunchKeySequence` arrays *must* be the same length as `KeySequenceLength`. + +In the above configuration examples, the `KeySequenceLength` is 1 since I'll personally never need anywhere near the 15 options afforded to me by the range of buttons above. If you're a bit more ambitious than me, a two-button sequence will give you [225 potential options](two-button-permutations.txt) and the linked file will allow you to work through the permutations systematically. A Harmony Hub typically executes both button presses within the same second, so there is only a minimal overhead of a longer sequence. + +### Firewall configuration + +You'll need to ensure that the following ports are opened on the machine hosting Ukor: + +* UDP port 1900 - this is used to receive SSDP search requests - e.g. when a Harmony Hub looks for devices to add on the local network; +* TCP port 8060 - this is used for Ukor's HTTP API, just like a real Roku. + +## How do I run it? + +Just run: + +``` +dotnet Ukor.dll +``` + +You might want to run Ukor when the hosting machine starts up using something like [NSSM](https://nssm.cc). + +## Example ICSharpAction class + +This is the typical structure of a C#-based plugin class: + +```csharp +using System.Threading.Tasks; +using Ukor.Configuration; + +namespace Chris.Ukor +{ + public class SetDns : ICSharpAction + { + public async Task DoActionAsync(Application application) + { + // Do whatever async action you like! + // "application" is the application object from your appsettings.json + // that's been triggered by a button-press sequence or channel launch. + } + } +} +``` + + + +## Why a custom version of RSSDP? + +Ukor uses a customised version of Yortw's [RSSDP](https://github.com/Yortw/RSSDP) library so that the fake Roku device can be discovered on the local network. + +As Ukor emulates a *real* Roku device, I needed more control over the USN and Notification Type values emitted by RSSDP to ensure they were faithful to the values a real Roku device would emit. I also needed to respond to search requests beyond those supported out-of-the-box by RSSDP. + +The RSSDP-derived code in this repo is therefore a very much cut-down and tweaked version of the official implementation, but I'm incredibly grateful to Yortw for the effort that's been put into that official implementation. \ No newline at end of file diff --git a/RSSDP_LICENSE.md b/RSSDP_LICENSE.md new file mode 100644 index 0000000..6d3b54d --- /dev/null +++ b/RSSDP_LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Troy Willmot + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/Rssdp.NetCore/GlobalSuppressions.cs b/src/Rssdp.NetCore/GlobalSuppressions.cs new file mode 100644 index 0000000000000000000000000000000000000000..ed2f3af65f2cb51b0e2b943b41486a1d28c82f69 GIT binary patch literal 4436 zcmeH~TT5F}5QXQt(Eo5qUnBxjh4gaI{&Kp*LW+DRTMhEtpsRaHM=&YWdhW5hQ(z;<(+cNkv!C{GbY-787jgfb=@>7vFTeF+C$ly7 zk#{85u5O%~90s zAmFP;Z)9ShU6m=d0;7q20nHRt-N2~gLoqQ%uNbM3>G=^?jp)d;=hv7$lzYlPk8a`q z$~*DvUYG1WdpARdDwjs+?txnT3t0*_`6%Um;vDLonWoWi5uLn*td{>%fwM5ocS!AOI34>FBDIT>d4kx7~6!c9jb`pylbaH z&%G~RRMAvpR6E6&*JroV#G==Z$Vss#xK4|edG(6SDUQ12R-OHHZDL2+tQztPG%5TG z?+I^&+N)d>spF{Ii_)6q^uQ_S@M=_LeIu~0(_M$yya~L=ZrBK%dHogZid?raKN`h+ zUi}v5pC(r2p%y5-4;>2pD|1)xk*zFgRz0Rq zJF{r-!}!&oinNHVd-Cc-`pNb`_md`>qG?Z^`+s^$gL<&I+}#6(>WeDqG2hH?LL^;~ z%|6E|e-Ehf*S5W+`8Kc9Z`l@|UY z#d)sS5BEde)x@S6;GIvtJPLD+&sdtwvQkIgr+xC6rrz>oQk>-%?J=L3iWNh%k@s`u Mv9kE#p3}du-z6Xez5oCK literal 0 HcmV?d00001 diff --git a/src/Rssdp.NetCore/Properties/AssemblyInfo.cs b/src/Rssdp.NetCore/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9e42339 --- /dev/null +++ b/src/Rssdp.NetCore/Properties/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System; +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RSSDP.NetCore")] +[assembly: AssemblyDescription(".NET Core specific implementation for RSSDP library.")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +[assembly: CLSCompliant(false)] +[assembly: NeutralResourcesLanguageAttribute("en-US")] \ No newline at end of file diff --git a/src/Rssdp.NetCore/Rssdp.NetCore.csproj b/src/Rssdp.NetCore/Rssdp.NetCore.csproj new file mode 100644 index 0000000..f6f03f5 --- /dev/null +++ b/src/Rssdp.NetCore/Rssdp.NetCore.csproj @@ -0,0 +1,75 @@ + + + + + 14.0 + Debug + AnyCPU + {AB769ED2-0A3D-41B7-A702-0E5B08DA158E} + Library + Properties + Rssdp + Rssdp + en-US + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + + v5.0 + + + true + full + false + bin\Debug\ + TRACE;DEBUG;CODE_ANALYSIS;NETSTANDARD;NETSTANDARD1_3;CODE_ANALYSIS + prompt + 4 + bin\Debug\Rssdp.xml + true + ..\RssdpRuleset.ruleset + + + pdbonly + true + ..\lib\netstandard13\ + TRACE;CODE_ANALYSIS;CODE_ANALYSIS;NETSTANDARD;NETSTANDARD1_3;CODE_ANALYSIS; + prompt + 4 + ..\lib\netstandard13\Rssdp.xml + true + ..\RssdpRuleset.ruleset + + + + + + + + SsdpDeviceLocator.cs + + + SocketFactory.cs + + + UdpSocket.cs + + + + + + + + Properties\CodeAnalysisDictionary.xml + + + + + + \ No newline at end of file diff --git a/src/Rssdp.NetCore/SsdpDevicePublisher.cs b/src/Rssdp.NetCore/SsdpDevicePublisher.cs new file mode 100644 index 0000000..6ff4a4e --- /dev/null +++ b/src/Rssdp.NetCore/SsdpDevicePublisher.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + /// + /// Allows publishing devices both as notification and responses to search requests. + /// + /// + /// This is the 'server' part of the system. You add your devices to an instance of this class so clients can find them. + /// + public class SsdpDevicePublisher : SsdpDevicePublisherBase + { + + #region Constructors + + /// + /// Default constructor. + /// + /// + /// Uses the default implementation and network settings for Windows and the SSDP specification. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")] + public SsdpDevicePublisher() + : this(new SsdpCommunicationsServer(new SocketFactory(null))) + { + + } + + /// + /// Full constructor. + /// + /// + /// Allows the caller to specify their own implementation for full control over the networking, or for mocking/testing purposes.. + /// + public SsdpDevicePublisher(ISsdpCommunicationsServer communicationsServer) + : base(communicationsServer, GetOSName(), GetOSVersion()) + { + + } + + /// + /// Partial constructor. + /// + /// The local port to use for socket communications, specify 0 to have the system choose it's own. + /// + /// Uses the default implementation and network settings for Windows and the SSDP specification, but specifies the local port to use for socket communications. Specify 0 to indicate the system should choose it's own port. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")] + public SsdpDevicePublisher(int localPort) + : this(new SsdpCommunicationsServer(new SocketFactory(null), localPort)) + { + + } + + /// + /// Partial constructor. + /// + /// The local port to use for socket communications, specify 0 to have the system choose it's own. + /// The number of hops a multicast packet can make before it expires. Must be 1 or greater. + /// + /// Uses the default implementation and network settings for Windows and the SSDP specification, but specifies the local port to use and multicast time to live setting for socket communications. + /// Specify 0 for the argument to indicate the system should choose it's own port. + /// The is actually a number of 'hops' on the network and not a time based argument. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")] + public SsdpDevicePublisher(int localPort, int multicastTimeToLive) + : this(new SsdpCommunicationsServer(new SocketFactory(null), localPort, multicastTimeToLive)) + { + } + + #endregion + + #region Private Methods + + private static string GetOSName() + { + return System.Runtime.InteropServices.RuntimeInformation.OSDescription; + } + + private static string GetOSVersion() + { + return System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription; + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.NetCore/project.json b/src/Rssdp.NetCore/project.json new file mode 100644 index 0000000..ee92dda --- /dev/null +++ b/src/Rssdp.NetCore/project.json @@ -0,0 +1,26 @@ +{ + "supports": {}, + "dependencies": { + "System.Collections": "4.3.0", + "System.Diagnostics.Debug": "4.3.0", + "System.Diagnostics.Tools": "4.3.0", + "System.IO": "4.3.0", + "System.Linq": "4.3.0", + "System.Net.Http": "4.3.2", + "System.Net.Primitives": "4.3.0", + "System.Net.Sockets": "4.3.0", + "System.Resources.ResourceManager": "4.3.0", + "System.Runtime": "4.3.0", + "System.Runtime.Extensions": "4.3.0", + "System.Runtime.InteropServices.RuntimeInformation": "4.3.0", + "System.Text.Encoding": "4.3.0", + "System.Text.Encoding.Extensions": "4.3.0", + "System.Threading": "4.3.0", + "System.Threading.Tasks": "4.3.0", + "System.Threading.Timer": "4.3.0", + "System.Xml.ReaderWriter": "4.3.0" + }, + "frameworks": { + "netstandard1.3": {} + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/AssemblyInfoCommon.cs b/src/Rssdp.Shared/AssemblyInfoCommon.cs new file mode 100644 index 0000000..6acf093 --- /dev/null +++ b/src/Rssdp.Shared/AssemblyInfoCommon.cs @@ -0,0 +1,9 @@ +using System.Reflection; + +[assembly: AssemblyProduct("Really Simple Service Discovery Protocol")] +[assembly: AssemblyCompany("Created by Yort. https://github.com/Yortw/RSSDP")] +[assembly: AssemblyCopyright("Released under the MIT license; http://choosealicense.com/licenses/mit/; https://github.com/Yortw/RSSDP")] +[assembly: AssemblyTrademark("")] + +[assembly: AssemblyVersion("4.0.2.0")] +[assembly: AssemblyFileVersion("4.0.2.0")] \ No newline at end of file diff --git a/src/Rssdp.Shared/CustomHttpHeaders.cs b/src/Rssdp.Shared/CustomHttpHeaders.cs new file mode 100644 index 0000000..99a028e --- /dev/null +++ b/src/Rssdp.Shared/CustomHttpHeaders.cs @@ -0,0 +1,295 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Represents a custom HTTP header sent on device search response or notification messages. + /// + public sealed class CustomHttpHeader + { + + #region Fields + + private string _Name; + private string _Value; + + #endregion + + #region Constructors + + /// + /// Full constructor. + /// + /// The field name of the header. + /// The value of the header + /// + /// As per RFC 822 and 2616, the name must contain only printable ASCII characters (33-126) excluding colon (:). The value may contain any ASCII characters except carriage return or line feed. + /// + /// Thrown if the name is null. + /// Thrown if the name is an empty value, or contains an invalid character. Also thrown if the value contains a \r or \n character. + public CustomHttpHeader(string name, string value) + { + Name = name; + Value = value; + } + + #endregion + + #region Public Properties + + /// + /// Return the name of this header. + /// + public string Name + { + get { return _Name; } + private set + { + EnsureValidName(value); + _Name = value; + } + } + + /// + /// Returns the value of this header. + /// + public string Value + { + get { return _Value; } + private set + { + EnsureValidValue(value); + _Value = value; + } + } + + #endregion + + #region Overrides + + /// + /// Returns the header formatted for use in an HTTP message. + /// + /// A string representing this header in the format of 'name: value'. + public override string ToString() + { + return this.Name + ": " + this.Value; + } + + #endregion + + #region Private Methods + + private static void EnsureValidName(string name) + { + if (name == null) throw new ArgumentNullException(nameof(name), "Name cannot be null."); + if (name.Length == 0) throw new ArgumentException("Name cannot be blank.", nameof(name)); + + foreach (var c in name) + { + var b = (byte)c; + if (c == ':' || b < 33 || b > 126) throw new ArgumentException("Name contains illegal characters.", nameof(name)); + } + } + + private static void EnsureValidValue(string value) + { + if (String.IsNullOrEmpty(value)) return; + + if (value.Contains("\r") || value.Contains("\n")) throw new ArgumentException("Invalid value.", nameof(value)); + } + + #endregion + + } + + /// + /// Represents a collection of custom HTTP headers, keyed by name. + /// + public class CustomHttpHeadersCollection : IEnumerable + { + #region Fields + + private IDictionary _Headers; + + #endregion + + #region Constructors + + /// + /// Default constructor. + /// + public CustomHttpHeadersCollection() + { + _Headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + + /// + /// Full constructor. + /// + /// Specifies the initial capacity of the collection. + public CustomHttpHeadersCollection(int capacity) + { + _Headers = new Dictionary(capacity); + } + + #endregion + + #region Public Methpds + + /// + /// Adds a instance to the collection. + /// + /// The instance to add to the collection. + /// + /// + /// + /// Thrown if is null. + public void Add(CustomHttpHeader header) + { + if (header == null) throw new ArgumentNullException(nameof(header)); + + lock (_Headers) + { + _Headers.Add(header.Name, header); + } + } + + #region Remove Overloads + + /// + /// Removes the specified header instance from the collection. + /// + /// The instance to remove from the collection. + /// + /// Only removes the specified header if that instance was in the collection, if another header with the same name exists in the collection it is not removed. + /// + /// True if an item was removed from the collection, otherwise false (because it did not exist or was not the same instance). + /// + /// Thrown if the is null. + public bool Remove(CustomHttpHeader header) + { + if (header == null) throw new ArgumentNullException(nameof(header)); + + lock (_Headers) + { + if (_Headers.ContainsKey(header.Name) && _Headers[header.Name] == header) + return _Headers.Remove(header.Name); + } + + return false; + } + + /// + /// Removes the property with the specified key ( from the collection. + /// + /// The name of the instance to remove from the collection. + /// True if an item was removed from the collection, otherwise false (because no item exists in the collection with that key). + /// Thrown if the argument is null or empty string. + public bool Remove(string headerName) + { + if (String.IsNullOrEmpty(headerName)) throw new ArgumentException("headerName cannot be null or empty.", nameof(headerName)); + + lock (_Headers) + { + return _Headers.Remove(headerName); + } + } + + #endregion + + /// + /// Returns a boolean indicating whether or not the specified instance is in the collection. + /// + /// An instance to check the collection for. + /// True if the specified instance exists in the collection, otherwise false. + public bool Contains(CustomHttpHeader header) + { + if (header == null) throw new ArgumentNullException(nameof(header)); + + lock (_Headers) + { + if (_Headers.ContainsKey(header.Name)) + return _Headers[header.Name] == header; + } + + return false; + } + + /// + /// Returns a boolean indicating whether or not a instance with the specified full name value exists in the collection. + /// + /// A string containing the full name of the instance to check for. + /// True if an item with the specified full name exists in the collection, otherwise false. + public bool Contains(string headerName) + { + if (String.IsNullOrEmpty(headerName)) throw new ArgumentException("headerName cannot be null or empty.", nameof(headerName)); + + lock (_Headers) + { + return _Headers.ContainsKey(headerName); + } + } + + #endregion + + #region Public Properties + + /// + /// Returns the number of items in the collection. + /// + public int Count + { + get { return _Headers.Count; } + } + + /// + /// Returns the instance from the collection that has the specified value. + /// + /// The full name of the property to return. + /// A instance from the collection. + /// Thrown if no item exists in the collection with the specified value. + public CustomHttpHeader this[string name] + { + get + { + return _Headers[name]; + } + } + + #endregion + + #region IEnumerable Members + + /// + /// Returns an enumerator of instances in this collection. + /// + /// An enumerator of instances in this collection. + public IEnumerator GetEnumerator() + { + lock (_Headers) + { + return _Headers.Values.GetEnumerator(); + } + } + + /// + /// Returns an enumerator of instances in this collection. + /// + /// An enumerator of instances in this collection. + IEnumerator IEnumerable.GetEnumerator() + { + lock (_Headers) + { + return _Headers.Values.GetEnumerator(); + } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/DeviceAvailableEventArgs.cs b/src/Rssdp.Shared/DeviceAvailableEventArgs.cs new file mode 100644 index 0000000..39f07e1 --- /dev/null +++ b/src/Rssdp.Shared/DeviceAvailableEventArgs.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + /// + /// Event arguments for the event. + /// + public sealed class DeviceAvailableEventArgs : EventArgs + { + + #region Fields + + private readonly DiscoveredSsdpDevice _DiscoveredDevice; + private readonly bool _IsNewlyDiscovered; + + #endregion + + #region Constructors + + /// + /// Full constructor. + /// + /// A instance representing the available device. + /// A boolean value indicating whether or not this device came from the cache. See for more detail. + /// Thrown if the parameter is null. + public DeviceAvailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool isNewlyDiscovered) + { + if (discoveredDevice == null) throw new ArgumentNullException("discoveredDevice"); + + _DiscoveredDevice = discoveredDevice; + _IsNewlyDiscovered = isNewlyDiscovered; + } + + #endregion + + #region Public Properties + + /// + /// Returns true if the device was discovered due to an alive notification, or a search and was not already in the cache. Returns false if the item came from the cache but matched the current search request. + /// + public bool IsNewlyDiscovered + { + get { return _IsNewlyDiscovered; } + } + + /// + /// A reference to a instance containing the discovered details and allowing access to the full device description. + /// + public DiscoveredSsdpDevice DiscoveredDevice + { + get { return _DiscoveredDevice; } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/DeviceEventArgs.cs b/src/Rssdp.Shared/DeviceEventArgs.cs new file mode 100644 index 0000000..8db06f2 --- /dev/null +++ b/src/Rssdp.Shared/DeviceEventArgs.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Event arguments for the and events. + /// + public sealed class DeviceEventArgs : EventArgs + { + + #region Fields + + private readonly SsdpDevice _Device; + + #endregion + + #region Constructors + + /// + /// Constructs a new instance for the specified . + /// + /// The associated with the event this argument class is being used for. + /// Thrown if the argument is null. + public DeviceEventArgs(SsdpDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + + _Device = device; + } + + #endregion + + #region Public Properties + + /// + /// Returns the instance the event is being raised for. + /// + public SsdpDevice Device + { + get { return _Device; } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/DeviceNetworkType.cs b/src/Rssdp.Shared/DeviceNetworkType.cs new file mode 100644 index 0000000..f082774 --- /dev/null +++ b/src/Rssdp.Shared/DeviceNetworkType.cs @@ -0,0 +1,17 @@ +namespace Rssdp +{ + /// + /// What type of sockets will be created: ipv6 or ipv4 + /// + public enum DeviceNetworkType + { + /// + /// Equals to AddressFamily.InternetNetwork + /// + IPv4, + /// + /// Equals to AddressFamily.InternetNetworkV6 + /// + IPv6 + } +} diff --git a/src/Rssdp.Shared/DeviceNetworkTypeExtensions.cs b/src/Rssdp.Shared/DeviceNetworkTypeExtensions.cs new file mode 100644 index 0000000..4f8090c --- /dev/null +++ b/src/Rssdp.Shared/DeviceNetworkTypeExtensions.cs @@ -0,0 +1,37 @@ +using System; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + /// + /// Provides extensions to the enum. + /// + public static class DeviceNetworkTypeExtensions + { + /// + /// Get multicast ip address for ipv4 or ipv6 network by + /// + /// + /// + /// + public static string GetMulticastIPAddress(this DeviceNetworkType deviceNetworkType) + { + string multicastIpAddress; + + switch (deviceNetworkType) + { + case DeviceNetworkType.IPv4: + multicastIpAddress = SsdpConstants.MulticastLocalAdminAddress; + break; + + case DeviceNetworkType.IPv6: + multicastIpAddress = SsdpConstants.MulticastLinkLocalAddressV6; + break; + default: + throw new ArgumentOutOfRangeException(nameof(deviceNetworkType)); + } + + return multicastIpAddress; + } + } +} diff --git a/src/Rssdp.Shared/DeviceUnavailableEventArgs.cs b/src/Rssdp.Shared/DeviceUnavailableEventArgs.cs new file mode 100644 index 0000000..5b7c143 --- /dev/null +++ b/src/Rssdp.Shared/DeviceUnavailableEventArgs.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + /// + /// Event arguments for the event. + /// + public sealed class DeviceUnavailableEventArgs : EventArgs + { + + #region Fields + + private readonly DiscoveredSsdpDevice _DiscoveredDevice; + private readonly bool _Expired; + + #endregion + + #region Constructors + + /// + /// Full constructor. + /// + /// A instance representing the device that has become unavailable. + /// A boolean value indicating whether this device is unavailable because it expired, or because it explicitly sent a byebye notification.. See for more detail. + /// Thrown if the parameter is null. + public DeviceUnavailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool expired) + { + if (discoveredDevice == null) throw new ArgumentNullException("discoveredDevice"); + + _DiscoveredDevice = discoveredDevice; + _Expired = expired; + } + + #endregion + + #region Public Properties + + /// + /// Returns true if the device is considered unavailable because it's cached information expired before a new alive notification or search result was received. Returns false if the device is unavailable because it sent an explicit notification of it's unavailability. + /// + public bool Expired + { + get { return _Expired; } + } + + /// + /// A reference to a instance containing the discovery details of the removed device. + /// + public DiscoveredSsdpDevice DiscoveredDevice + { + get { return _DiscoveredDevice; } + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/DiscoveredSsdpDevice.cs b/src/Rssdp.Shared/DiscoveredSsdpDevice.cs new file mode 100644 index 0000000..4ac5405 --- /dev/null +++ b/src/Rssdp.Shared/DiscoveredSsdpDevice.cs @@ -0,0 +1,177 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Net.Http.Headers; + +namespace Rssdp +{ + /// + /// Represents a discovered device, containing basic information about the device and the location of it's full device description document. Also provides convenience methods for retrieving the device description document. + /// + /// + /// + public sealed class DiscoveredSsdpDevice + { + + #region Fields + + private SsdpRootDevice _Device; + private DateTimeOffset _AsAt; + + private static HttpClient s_DefaultHttpClient; + + #endregion + + #region Public Properties + + /// + /// Sets or returns the type of notification, being either a uuid, device type, service type or upnp:rootdevice. + /// + public string NotificationType { get; set; } + + /// + /// Sets or returns the universal service name (USN) of the device. + /// + public string Usn { get; set; } + + /// + /// Sets or returns a URL pointing to the device description document for this device. + /// + public Uri DescriptionLocation { get; set; } + + /// + /// Sets or returns the length of time this information is valid for (from the time). + /// + public TimeSpan CacheLifetime { get; set; } + + /// + /// Sets or returns the date and time this information was received. + /// + public DateTimeOffset AsAt + { + get { return _AsAt; } + set + { + if (_AsAt != value) + { + _AsAt = value; + _Device = null; + } + } + } + + /// + /// Returns the headers from the SSDP device response message + /// + public HttpHeaders ResponseHeaders { get; set; } + + #endregion + + #region Public Methods + + /// + /// Returns true if this device information has expired, based on the current date/time, and the & properties. + /// + /// + public bool IsExpired() + { + return this.CacheLifetime == TimeSpan.Zero || this.AsAt.Add(this.CacheLifetime) <= DateTimeOffset.Now; + } + + /// + /// Retrieves the device description document specified by the property. + /// + /// + /// This method may choose to cache (or return cached) information if called multiple times within the period. + /// + /// This method using an HttpClient instance to retrieve the device description document, and as such any exception that can be thrown by HttpClient may be rethrown by this method. + /// On the UWP platform this is likely to be a instance and the hresult can be checked to determine the exact nature of the error. On other platforms it is likely to be a System.Net.WebException or System.Net.Http.HttpRequestException. + /// Check the documentation for HttpClient on the platform(s) you're using. + /// An instance describing the full device details. + public async Task GetDeviceInfo() + { + var device = _Device; + if (device == null || this.IsExpired()) + return await GetDeviceInfo(GetDefaultClient()); + else + return device; + } + + /// + /// Retrieves the device description document specified by the property using the provided instance. + /// + /// + /// This method may choose to cache (or return cached) information if called multiple times within the period. + /// This method performs no error handling, if an exception occurs downloading or parsing the document it will be thrown to the calling code. Ensure you setup correct error handling for these scenarios. + /// + /// A to use when downloading the document data. + /// This method using an HttpClient instance to retrieve the device description document, and as such any exception that can be thrown by HttpClient may be rethrown by this method. + /// On the UWP platform this is likely to be a instance and the hresult can be checked to determine the exact nature of the error. On other platforms it is likely to be a System.Net.WebException or System.Net.Http.HttpRequestException. + /// Check the documentation for HttpClient on the platform(s) you're using. + /// An instance describing the full device details. + public async Task GetDeviceInfo(HttpClient downloadHttpClient) + { + if (_Device == null || this.IsExpired()) + { + var rawDescriptionDocument = await downloadHttpClient.GetAsync(this.DescriptionLocation); + rawDescriptionDocument.EnsureSuccessStatusCode(); + + // Not using ReadAsStringAsync() here as some devices return the content type as utf-8 not UTF-8, + // which causes an (unneccesary) exception. + var data = await rawDescriptionDocument.Content.ReadAsByteArrayAsync(); + _Device = new SsdpRootDevice(this.DescriptionLocation, this.CacheLifetime, System.Text.UTF8Encoding.UTF8.GetString(data, 0, data.Length)); + } + + return _Device; + } + + #endregion + + #region Overrides + + /// + /// Returns the device's value. + /// + /// A string containing the device's universal service name. + public override string ToString() + { + return this.Usn; + } + + #endregion + + #region Private Methods + + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Can't call dispose on the handler since we pass it to the HttpClient, which outlives the scope of this method.")] + private static HttpClient GetDefaultClient() + { + if (s_DefaultHttpClient == null) + { + var handler = new System.Net.Http.HttpClientHandler(); + try + { + if (handler.SupportsAutomaticDecompression) + handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip; + + s_DefaultHttpClient = new HttpClient(handler); + } + catch + { + if (handler != null) + handler.Dispose(); + + throw; + } + } + + return s_DefaultHttpClient; + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/DisposableManagedObjectBase.cs b/src/Rssdp.Shared/DisposableManagedObjectBase.cs new file mode 100644 index 0000000..87f2aa7 --- /dev/null +++ b/src/Rssdp.Shared/DisposableManagedObjectBase.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Correclty implements the interface and pattern for an object containing only managed resources, and adds a few common niceities not on the interface such as an property. + /// + public abstract class DisposableManagedObjectBase : IDisposable + { + + #region Public Methods + + /// + /// Override this method and dispose any objects you own the lifetime of if disposing is true; + /// + /// True if managed objects should be disposed, if false, only unmanaged resources should be released. + protected abstract void Dispose(bool disposing); + + /// + /// Throws and if the property is true. + /// + /// + /// Thrown if the property is true. + /// + protected virtual void ThrowIfDisposed() + { + if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName); + } + + #endregion + + #region Public Properties + + /// + /// Sets or returns a boolean indicating whether or not this instance has been disposed. + /// + /// + public bool IsDisposed + { + get; + private set; + } + + #endregion + + #region IDisposable Members + + /// + /// Disposes this object instance and all internally managed resources. + /// + /// + /// Sets the property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behaviour of derived classes. + /// + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification="We do exactly as asked, but CA doesn't seem to like us also setting the IsDisposed property. Too bad, it's a good idea and shouldn't cause an exception or anything likely to interfer with the dispose process.")] + public void Dispose() + { + try + { + IsDisposed = true; + + Dispose(true); + } + finally + { + GC.SuppressFinalize(this); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/ExceptionExtensions.cs b/src/Rssdp.Shared/ExceptionExtensions.cs new file mode 100644 index 0000000..8be3146 --- /dev/null +++ b/src/Rssdp.Shared/ExceptionExtensions.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + /// + /// Provides extension methods to and derived objects. + /// + public static class ExceptionExtensions + { + /// + /// Returns true of the specified exception is one that indicates some form of memory corruption, out of memory state or other fatal exception that should *never* be handled by user code. + /// + /// The exception to check. + /// + /// Doesn't check for System.StackOverflowExceptions as if the stack really is full calling this method might check, therefore calling code must explicitly handle that exception type itself. + /// Specifically checks for the following exception types; + /// + /// + /// System.AccessViolationException + /// System.OutOfMemoryException + /// System.InvalidProgramException + /// + /// + /// + /// True if the specified exception is considered critical and should be re-thrown and not otherwise handled by user code. + public static bool IsCritical(this Exception exception) + { +#if NETSTANDARD + // Unrecoverable exceptions should not be getting caught and will be dealt with on a broad level by a high-level catch-all handler + // https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/breaking-change-rules.md#exceptions + return (exception is System.OutOfMemoryException) + || (exception is System.InvalidProgramException); +#elif WINRT || PORTABLE + return (exception is System.OutOfMemoryException); +#else + return (exception is System.AccessViolationException) + || (exception is System.OutOfMemoryException) + || (exception is System.InvalidProgramException); + +#endif + } + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/GlobalSuppressions.cs b/src/Rssdp.Shared/GlobalSuppressions.cs new file mode 100644 index 0000000000000000000000000000000000000000..ea19a00c36a540cd617aa7b6fad4ee8fd2897f21 GIT binary patch literal 15112 zcmeHOS#R4$5T54({SSot;sB0Pr$(Cu=!2aDFc8~UQo_)pu@Gz?d(F7(y8dT8GSZGnvOc8Inly#~^ih0O4qV_b~iaeD2`g_-GR`3X=Ejy^^u=r_b_ z3*51Pfa|4cVLRvGmGh0zPhr!Q*-s(iT2uLe)&WMhC0n7h)Nqz`2BYK;^27xFTu!x^ zUd?fNj<$GK`I=x2${wXpxtnmm#XI6xsm``1Si5T^l(IBN-)DfD_$OpBU?YuUytg>d zOLe<|%Vp`^!TyiUER?tyfA2ACTTZab81E-oOR?R;8V9l`f5@-056F&yBgzF~;O@w! zF;=_89Heh3hj`aD+{~R4e}r<5pUO3NsB*j|*R!}Cy1Mk~KvLfs3olM%IESePL|R+_ zo_1f7Qr=T}Q$M>0*)5abP3`nD*`*b)tGQIZ8&-UM`qtubJ2b9^#-*s$xf)AyBb{F3 zmT6pz%k8JE*RxgY*?R3H%V{@Bt)8)ru(+*Wa=%A%t2{+5?V9TE_h74Q99#9A%`ZGc z{9+HWu(LG67sPk+z75f_dV2(0E zE|M|I?Ls;et;Qs&3fB>UH~j_dIBDRDq-$SsW)b@zEl=r-ElIK z%&lr}@+Lc|X%bpLCs4zW!9T38aA{JV%(4Ptb==QV&t1D*ORs|1b47LfVf}c{wrY*_ zchIDYhWhmb?H#T0p~V+M*B(P#gwPh-t0Br@_S7!(olZ<#-d58wldqLphoJowd=SFc zVBR}1bzN%tUm06vd%59z_T;&xj9bd&Y+KI-tzD&O7vo1if_iNLE1P!3Kk#tbl9c*= zyCpv1?vian^kqm>PSKp`Hnf#F@DN(b7ODMgwvLJJHN{Fw7NP$Z7Hh|V;|Wt8T8d0*a60>JMmoK=LFPG5N)GJ z{}FL0t;}PD;|Mz9N`7xxY@_a4<=dmozNZXgQ`;yrO`}ZxUf$O=P<7g_3*E+VTBzq< z!1`hp7_}Y0!eAu{-yg#c?v2pcUM22a>Ypd Kw^5H@&V2@{a$Uy& literal 0 HcmV?d00001 diff --git a/src/Rssdp.Shared/HttpParserBase.cs b/src/Rssdp.Shared/HttpParserBase.cs new file mode 100644 index 0000000..7934419 --- /dev/null +++ b/src/Rssdp.Shared/HttpParserBase.cs @@ -0,0 +1,244 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; + +namespace Rssdp.Infrastructure +{ + /// + /// A base class for the and classes. Not intended for direct use. + /// + /// + public abstract class HttpParserBase where T : new() + { + + #region Fields + + private static readonly string[] LineTerminators = new string[] { "\r\n", "\n" }; + private static readonly char[] SeparatorCharacters = new char[] { ',', ';' }; + + #endregion + + #region Public Methods + + /// + /// Parses the provided into either a or object. + /// + /// A string containing the HTTP message to parse. + /// Either a or object containing the parsed data. + public abstract T Parse(string data); + + /// + /// Parses a string containing either an HTTP request or response into a or object. + /// + /// A or object representing the parsed message. + /// A reference to the collection for the object. + /// A string containing the data to be parsed. + /// An object containing the content of the parsed message. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification="Honestly, it's fine. MemoryStream doesn't mind.")] + protected virtual HttpContent Parse(T message, System.Net.Http.Headers.HttpHeaders headers, string data) + { + if (data == null) throw new ArgumentNullException("data"); + if (data.Length == 0) throw new ArgumentException("data cannot be an empty string.", "data"); + if (!LineTerminators.Any(data.Contains)) throw new ArgumentException("data is not a valid request, it does not contain any CRLF/LF terminators.", "data"); + + HttpContent retVal = null; + try + { + var contentStream = new System.IO.MemoryStream(); + try + { + retVal = new StreamContent(contentStream); + + var lines = data.Split(LineTerminators, StringSplitOptions.None); + + //First line is the 'request' line containing http protocol details like method, uri, http version etc. + ParseStatusLine(lines[0], message); + + int lineIndex = ParseHeaders(headers, retVal.Headers, lines); + + if (lineIndex < lines.Length - 1) + { + //Read rest of any remaining data as content. + if (lineIndex < lines.Length - 1) + { + //This is inefficient in multiple ways, but not sure of a good way of correcting. Revisit. + var body = System.Text.UTF8Encoding.UTF8.GetBytes(String.Join(null, lines, lineIndex, lines.Length - lineIndex)); + contentStream.Write(body, 0, body.Length); + contentStream.Seek(0, System.IO.SeekOrigin.Begin); + } + } + } + catch + { + if (contentStream != null) + contentStream.Dispose(); + + throw; + } + } + catch + { + if (retVal != null) + retVal.Dispose(); + + throw; + } + + return retVal; + } + + /// + /// Used to parse the first line of an HTTP request or response and assign the values to the appropriate properties on the . + /// + /// The first line of the HTTP message to be parsed. + /// Either a or to assign the parsed values to. + protected abstract void ParseStatusLine(string data, T message); + + /// + /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false). + /// + /// A string containing the name of the header to return the type of. + protected abstract bool IsContentHeader(string headerName); + + /// + /// Parses the HTTP version text from an HTTP request or response status line and returns a object representing the parsed values. + /// + /// A string containing the HTTP version, from the message status line. + /// A object containing the parsed version data. + protected static Version ParseHttpVersion(string versionData) + { + if (versionData == null) throw new ArgumentNullException("versionData"); + + var versionSeparatorIndex = versionData.IndexOf('/'); + if (versionSeparatorIndex <= 0 || versionSeparatorIndex == versionData.Length) throw new ArgumentException("request header line is invalid. Http Version not supplied or incorrect format.", "versionData"); + + return Version.Parse(versionData.Substring(versionSeparatorIndex + 1)); + } + + #endregion + + #region Private Methods + + /// + /// Parses a line from an HTTP request or response message containing a header name and value pair. + /// + /// A string containing the data to be parsed. + /// A reference to a collection to which the parsed header will be added. + /// A reference to a collection for the message content, to which the parsed header will be added. + private void ParseHeader(string line, System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders) + { + //Header format is + //name: value + var headerKeySeparatorIndex = line.IndexOf(":", StringComparison.OrdinalIgnoreCase); + var headerName = line.Substring(0, headerKeySeparatorIndex).Trim(); + var headerValue = line.Substring(headerKeySeparatorIndex + 1).Trim(); + + //Not sure how to determine where request headers and and content headers begin, + //at least not without a known set of headers (general headers first the content headers) + //which seems like a bad way of doing it. So we'll assume if it's a known content header put it there + //else use request headers. + + var values = ParseValues(headerValue); + var headersToAddTo = IsContentHeader(headerName) ? contentHeaders : headers; + + if (values.Count > 1) + headersToAddTo.TryAddWithoutValidation(headerName, values); + else + headersToAddTo.TryAddWithoutValidation(headerName, values.First()); + } + + private int ParseHeaders(System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders, string[] lines) + { + //Blank line separates headers from content, so read headers until we find blank line. + int lineIndex = 1; + string line = null, nextLine = null; + while (lineIndex + 1 < lines.Length && !String.IsNullOrEmpty((line = lines[lineIndex++]))) + { + //If the following line starts with space or tab (or any whitespace), it is really part of this header but split for human readability. + //Combine these lines into a single comma separated style header for easier parsing. + while (lineIndex < lines.Length && !String.IsNullOrEmpty((nextLine = lines[lineIndex]))) + { + if (nextLine.Length > 0 && Char.IsWhiteSpace(nextLine[0])) + { + line += "," + nextLine.TrimStart(); + lineIndex++; + } + else + break; + } + + ParseHeader(line, headers, contentHeaders); + } + return lineIndex; + } + + private static IList ParseValues(string headerValue) + { + // This really should be better and match the HTTP 1.1 spec, + // but this should actually be good enough for SSDP implementations + // I think. + var values = new List(); + + if (headerValue == "\"\"") + { + values.Add(String.Empty); + return values; + } + + var indexOfSeparator = headerValue.IndexOfAny(SeparatorCharacters); + if (indexOfSeparator <= 0) + values.Add(headerValue); + else + { + var segments = headerValue.Split(SeparatorCharacters); + if (headerValue.Contains("\"")) + { + for (int segmentIndex = 0; segmentIndex < segments.Length; segmentIndex++) + { + var segment = segments[segmentIndex]; + if (segment.Trim().StartsWith("\"", StringComparison.OrdinalIgnoreCase)) + segment = CombineQuotedSegments(segments, ref segmentIndex, segment); + + values.Add(segment); + } + } + else + values.AddRange(segments); + } + + return values; + } + + private static string CombineQuotedSegments(string[] segments, ref int segmentIndex, string segment) + { + var trimmedSegment = segment.Trim(); + for (int index = segmentIndex; index < segments.Length; index++) + { + if (trimmedSegment == "\"\"" || + ( + trimmedSegment.EndsWith("\"", StringComparison.OrdinalIgnoreCase) + && !trimmedSegment.EndsWith("\"\"", StringComparison.OrdinalIgnoreCase) + && !trimmedSegment.EndsWith("\\\"", StringComparison.OrdinalIgnoreCase)) + ) + { + segmentIndex = index; + return trimmedSegment.Substring(1, trimmedSegment.Length - 2); + } + + if (index + 1 < segments.Length) + trimmedSegment += "," + segments[index + 1].TrimEnd(); + } + + segmentIndex = segments.Length; + if (trimmedSegment.StartsWith("\"", StringComparison.OrdinalIgnoreCase) && trimmedSegment.EndsWith("\"", StringComparison.OrdinalIgnoreCase)) + return trimmedSegment.Substring(1, trimmedSegment.Length - 2); + else + return trimmedSegment; + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/HttpRequestParser.cs b/src/Rssdp.Shared/HttpRequestParser.cs new file mode 100644 index 0000000..0923f29 --- /dev/null +++ b/src/Rssdp.Shared/HttpRequestParser.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Parses a string into a or throws an exception. + /// + public sealed class HttpRequestParser : HttpParserBase + { + + #region Fields & Constants + + private readonly string[] ContentHeaderNames = new string[] + { + "Allow", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Location", "Content-MD5", "Content-Range", "Content-Type", "Expires", "Last-Modified" + }; + + #endregion + + #region Public Methods + + /// + /// Parses the specified data into a instance. + /// + /// A string containing the data to parse. + /// A instance containing the parsed data. + public override System.Net.Http.HttpRequestMessage Parse(string data) + { + System.Net.Http.HttpRequestMessage retVal = null; + + try + { + retVal = new System.Net.Http.HttpRequestMessage(); + + retVal.Content = Parse(retVal, retVal.Headers, data); + + return retVal; + } + finally + { + if (retVal != null) + retVal.Dispose(); + } + } + + #endregion + + #region Overrides + + /// + /// Used to parse the first line of an HTTP request or response and assign the values to the appropriate properties on the . + /// + /// The first line of the HTTP message to be parsed. + /// Either a or to assign the parsed values to. + protected override void ParseStatusLine(string data, HttpRequestMessage message) + { + if (data == null) throw new ArgumentNullException("data"); + if (message == null) throw new ArgumentNullException("message"); + + var parts = data.Split(' '); + if (parts.Length < 3) throw new ArgumentException("Status line is invalid. Insufficient status parts.", "data"); + + message.Method = new HttpMethod(parts[0].Trim()); + Uri requestUri; + if (Uri.TryCreate(parts[1].Trim(), UriKind.RelativeOrAbsolute, out requestUri)) + message.RequestUri = requestUri; + else + System.Diagnostics.Debug.WriteLine(parts[1]); + + message.Version = ParseHttpVersion(parts[2].Trim()); + } + + /// + /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false). + /// + /// A string containing the name of the header to return the type of. + protected override bool IsContentHeader(string headerName) + { + return ContentHeaderNames.Contains(headerName, StringComparer.OrdinalIgnoreCase); + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/HttpResponseParser.cs b/src/Rssdp.Shared/HttpResponseParser.cs new file mode 100644 index 0000000..ba85a16 --- /dev/null +++ b/src/Rssdp.Shared/HttpResponseParser.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Parses a string into a or throws an exception. + /// + public sealed class HttpResponseParser : HttpParserBase + { + + #region Fields & Constants + + private static readonly string[] ContentHeaderNames = new string[] + { + "Allow", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Location", "Content-MD5", "Content-Range", "Content-Type", "Expires", "Last-Modified" + }; + + #endregion + + #region Public Methods + + /// + /// Parses the specified data into a instance. + /// + /// A string containing the data to parse. + /// A instance containing the parsed data. + public override HttpResponseMessage Parse(string data) + { + System.Net.Http.HttpResponseMessage retVal = null; + try + { + retVal = new System.Net.Http.HttpResponseMessage(); + + retVal.Content = Parse(retVal, retVal.Headers, data); + + return retVal; + } + catch + { + if (retVal != null) + retVal.Dispose(); + + throw; + } + } + + #endregion + + #region Overrides Methods + + /// + /// Returns a boolean indicating whether the specified HTTP header name represents a content header (true), or a message header (false). + /// + /// A string containing the name of the header to return the type of. + /// A boolean, true if th specified header relates to HTTP content, otherwise false. + protected override bool IsContentHeader(string headerName) + { + return ContentHeaderNames.Contains(headerName, StringComparer.OrdinalIgnoreCase); + } + + /// + /// Used to parse the first line of an HTTP request or response and assign the values to the appropriate properties on the . + /// + /// The first line of the HTTP message to be parsed. + /// Either a or to assign the parsed values to. + protected override void ParseStatusLine(string data, HttpResponseMessage message) + { + if (data == null) throw new ArgumentNullException("data"); + if (message == null) throw new ArgumentNullException("message"); + + var parts = data.Split(' '); + if (parts.Length < 3) throw new ArgumentException("data status line is invalid. Insufficient status parts.", "data"); + + message.Version = ParseHttpVersion(parts[0].Trim()); + + int statusCode = -1; + if (!Int32.TryParse(parts[1].Trim(), out statusCode)) + throw new ArgumentException("data status line is invalid. Status code is not a valid integer.", "data"); + + message.StatusCode = (HttpStatusCode)statusCode; + message.ReasonPhrase = parts[2].Trim(); + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/IEnumerableExtensions.cs b/src/Rssdp.Shared/IEnumerableExtensions.cs new file mode 100644 index 0000000..f720739 --- /dev/null +++ b/src/Rssdp.Shared/IEnumerableExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp.Infrastructure +{ + internal static class IEnumerableExtensions + { + public static IEnumerable SelectManyRecursive(this IEnumerable source, Func> selector) + { + if (source == null) throw new ArgumentNullException("source"); + if (selector == null) throw new ArgumentNullException("selector"); + + return !source.Any() ? source : + source.Concat( + source + .SelectMany(i => selector(i).EmptyIfNull()) + .SelectManyRecursive(selector) + ); + } + + public static IEnumerable EmptyIfNull(this IEnumerable source) + { + return source ?? Enumerable.Empty(); + } + } +} diff --git a/src/Rssdp.Shared/ISocketFactory.cs b/src/Rssdp.Shared/ISocketFactory.cs new file mode 100644 index 0000000..a0540bd --- /dev/null +++ b/src/Rssdp.Shared/ISocketFactory.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Implemented by components that can create a platform specific UDP socket implementation, and wrap it in the cross platform interface. + /// + public interface ISocketFactory + { + + /// + /// Creates a new unicast socket using the specified local port number. + /// + /// The local port to bind to. + /// A implementation. + IUdpSocket CreateUdpSocket(int localPort); + + /// + /// Creates a new multicast socket using the specified multicast IP address, multicast time to live and local port. + /// + /// The multicast time to live value. Actually a maximum number of network hops for UDP packets. + /// The local port to bind to. + /// A implementation. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "ip", Justification="IP is a well known and understood abbreviation and the full name is excessive.")] + IUdpSocket CreateUdpMulticastSocket(int multicastTimeToLive, int localPort); + + /// + /// What type of sockets will be created: ipv6 or ipv4 + /// + DeviceNetworkType DeviceNetworkType { get; } + } +} diff --git a/src/Rssdp.Shared/ISsdpCommunicationsServer.cs b/src/Rssdp.Shared/ISsdpCommunicationsServer.cs new file mode 100644 index 0000000..d48411a --- /dev/null +++ b/src/Rssdp.Shared/ISsdpCommunicationsServer.cs @@ -0,0 +1,81 @@ +using System; + +namespace Rssdp.Infrastructure +{ + /// + /// Interface for a component that manages network communication (sending and receiving HTTPU messages) for the SSDP protocol. + /// + public interface ISsdpCommunicationsServer : IDisposable + { + + #region Events + + /// + /// Raised when a HTTPU request message is received by a socket (unicast or multicast). + /// + event EventHandler RequestReceived; + + /// + /// Raised when an HTTPU response message is received by a socket (unicast or multicast). + /// + event EventHandler ResponseReceived; + + #endregion + + #region Methods + + /// + /// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications. + /// + void BeginListeningForBroadcasts(); + + /// + /// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications. + /// + void StopListeningForBroadcasts(); + + /// + /// Stops listening for search responses on the local, unicast socket. + /// + void StopListeningForResponses(); + + /// + /// Sends a message to a particular address (uni or multicast) and port. + /// + /// A byte array containing the data to send. + /// A representing the destination address for the data. Can be either a multicast or unicast destination. + void SendMessage(byte[] messageData, UdpEndPoint destination); + + #endregion + + #region Properties + + /// + /// Gets or sets a boolean value indicating whether or not this instance is shared amongst multiple and/or instances. + /// + /// + /// If true, disposing an instance of a or a will not dispose this comms server instance. The calling code is responsible for managing the lifetime of the server. + /// + bool IsShared { get; set; } + + /// + /// Determines whether IPv4 or IPv5 sockets are used by this communications server. + /// + DeviceNetworkType DeviceNetworkType { get; } + + /// + /// The number of times the Udp message is sent. Any value less than 2 will result in one message being sent. SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. + /// + /// + int UdpSendCount { get; set; } + + /// + /// The delay between repeating messages (as specified in UdpSendCount). + /// + /// + TimeSpan UdpSendDelay { get; set; } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/ISsdpDeviceLocator.cs b/src/Rssdp.Shared/ISsdpDeviceLocator.cs new file mode 100644 index 0000000..4b7d107 --- /dev/null +++ b/src/Rssdp.Shared/ISsdpDeviceLocator.cs @@ -0,0 +1,146 @@ +using System; + +namespace Rssdp.Infrastructure +{ + /// + /// Interface for components that discover the existence of SSDP devices. + /// + /// + /// Discovering devices includes explicit search requests as well as listening for broadcast status notifications. + /// + /// + /// + /// + public interface ISsdpDeviceLocator + { + + #region Events + + /// + /// Event raised when a device becomes available or is found by a search request. + /// + /// + /// + /// + /// + event EventHandler DeviceAvailable; + + /// + /// Event raised when a device explicitly notifies of shutdown or a device expires from the cache. + /// + /// + /// + /// + /// + event EventHandler DeviceUnavailable; + + #endregion + + #region Properties + + /// + /// Sets or returns a string containing the filter for notifications. Notifications not matching the filter will not raise the or events. + /// + /// + /// Device alive/byebye notifications whose NT header does not match this filter value will still be captured and cached internally, but will not raise events about device availability. Usually used with either a device type of uuid NT header value. + /// Example filters follow; + /// upnp:rootdevice + /// urn:schemas-upnp-org:device:WANDevice:1 + /// "uuid:9F15356CC-95FA-572E-0E99-85B456BD3012" + /// + /// + /// + /// + /// + string NotificationFilter + { + get; + set; + } + + /// + /// Returns a boolean indicating whether or not a search is currently active. + /// + bool IsSearching { get; } + + #endregion + + #region Methods + + #region SearchAsync Overloads + + /// + /// Aynchronously performs a search for all devices using the default search timeout, and returns an awaitable task that can be used to retrieve the results. + /// + /// A task whose result is an of instances, representing all found devices. + System.Threading.Tasks.Task> SearchAsync(); + + /// + /// Performs a search for the specified search target (criteria) and default search timeout. + /// + /// The criteria for the search. Value can be; + /// + /// Root devicesupnp:rootdevice + /// Specific device by UUIDuuid:<device uuid> + /// Device typeFully qualified device type starting with urn: i.e urn:schemas-upnp-org:Basic:1 + /// + /// + /// A task whose result is an of instances, representing all found devices. + System.Threading.Tasks.Task> SearchAsync(string searchTarget); + + /// + /// Performs a search for the specified search target (criteria) and search timeout. + /// + /// The criteria for the search. Value can be; + /// + /// Root devicesupnp:rootdevice + /// Specific device by UUIDuuid:<device uuid> + /// Device typeA device namespace and type in format of urn:<device namespace>:device:<device type>:<device version> i.e urn:schemas-upnp-org:device:Basic:1 + /// Service typeA service namespace and type in format of urn:<service namespace>:service:<servicetype>:<service version> i.e urn:my-namespace:service:MyCustomService:1 + /// + /// + /// The amount of time to wait for network responses to the search request. Longer values will likely return more devices, but increase search time. A value between 1 and 5 is recommended by the UPnP 1.1 specification. Specify TimeSpan.Zero to return only devices already in the cache. + /// + /// By design RSSDP does not support 'publishing services' as it is intended for use with non-standard UPnP devices that don't publish UPnP style services. However, it is still possible to use RSSDP to search for devices implemetning these services if you know the service type. + /// + /// A task whose result is an of instances, representing all found devices. + System.Threading.Tasks.Task> SearchAsync(string searchTarget, TimeSpan searchWaitTime); + + /// + /// Performs a search for all devices using the specified search timeout. + /// + /// The amount of time to wait for network responses to the search request. Longer values will likely return more devices, but increase search time. A value between 1 and 5 is recommended by the UPnP 1.1 specification. Specify TimeSpan.Zero to return only devices already in the cache. + /// A task whose result is an of instances, representing all found devices. + System.Threading.Tasks.Task> SearchAsync(TimeSpan searchWaitTime); + + #endregion + + /// + /// Starts listening for broadcast notifications of service availability. + /// + /// + /// When called the system will listen for 'alive' and 'byebye' notifications. This can speed up searching, as well as provide dynamic notification of new devices appearing on the network, and previously discovered devices disappearing. + /// + /// + /// + /// + /// + void StartListeningForNotifications(); + + /// + /// Stops listening for broadcast notifications of service availability. + /// + /// + /// Does nothing if this instance is not already listening for notifications. + /// + /// Throw if the property is true. + /// + /// + /// + /// + void StopListeningForNotifications(); + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/ISsdpDevicePublisher.cs b/src/Rssdp.Shared/ISsdpDevicePublisher.cs new file mode 100644 index 0000000..dd1c200 --- /dev/null +++ b/src/Rssdp.Shared/ISsdpDevicePublisher.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Interface for components that publish the existence of SSDP devices. + /// + /// + /// Publishing a device includes sending notifications (alive and byebye) as well as responding to search requests when appropriate. + /// + /// + /// + public interface ISsdpDevicePublisher + { + /// + /// Adds a device (and it's children) to the list of devices being published by this server, making them discoverable to SSDP clients. + /// + /// The instance to add. + /// An awaitable . + void AddDevice(SsdpRootDevice device); + + /// + /// Removes a device (and it's children) from the list of devices being published by this server, making them undiscoverable. + /// + /// The instance to add. + /// An awaitable . + void RemoveDevice(SsdpRootDevice device); + + /// + /// Returns a read only list of devices being published by this instance. + /// + /// + System.Collections.Generic.IEnumerable Devices { get; } + + } +} diff --git a/src/Rssdp.Shared/ISsdpLogger.cs b/src/Rssdp.Shared/ISsdpLogger.cs new file mode 100644 index 0000000..b75dcb6 --- /dev/null +++ b/src/Rssdp.Shared/ISsdpLogger.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Interface for a simple logging component used by RSSDP to record internal activity. + /// + public interface ISsdpLogger + { + + /// + /// Records a regular log message. + /// + /// The text to be logged. + void LogInfo(string message); + + /// + /// Records a frequent or large log message usually only required when trying to trace a problem. + /// + /// The text to be logged. + void LogVerbose(string message); + + /// + /// Records an important message, but one that may not neccesarily be an error. + /// + /// The text to be logged. + void LogWarning(string message); + + /// + /// Records a message that represents an error. + /// + /// The text to be logged. + void LogError(string message); + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/IUPnPDeviceValidator.cs b/src/Rssdp.Shared/IUPnPDeviceValidator.cs new file mode 100644 index 0000000..39b8074 --- /dev/null +++ b/src/Rssdp.Shared/IUPnPDeviceValidator.cs @@ -0,0 +1,27 @@ +using System; + +namespace Rssdp.Infrastructure +{ + /// + /// Interface for components that check an object's properties meet the UPnP specification for a particular version. + /// + public interface IUpnpDeviceValidator + { + /// + /// Returns an enumerable set of strings, each one being a description of an invalid property on the specified root device. + /// + /// The to validate. + System.Collections.Generic.IEnumerable GetValidationErrors(SsdpRootDevice device); + + /// + /// Returns an enumerable set of strings, each one being a description of an invalid property on the specified device. + /// + /// The to validate. + System.Collections.Generic.IEnumerable GetValidationErrors(SsdpDevice device); + + /// + /// Validates the specified device and throws an if there are any validation errors. + /// + void ThrowIfDeviceInvalid(SsdpDevice device); + } +} diff --git a/src/Rssdp.Shared/IUdpSocket.cs b/src/Rssdp.Shared/IUdpSocket.cs new file mode 100644 index 0000000..139f43b --- /dev/null +++ b/src/Rssdp.Shared/IUdpSocket.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Provides a common interface across platforms for UDP sockets used by this SSDP implementation. + /// + public interface IUdpSocket : IDisposable + { + /// + /// Waits for and returns the next UDP message sent to this socket (uni or multicast). + /// + /// + System.Threading.Tasks.Task ReceiveAsync(); + + /// + /// Sends a UDP message to a particular end point (uni or multicast). + /// + /// The data to send. + /// The providing the address and port to send to. + void SendTo(byte[] messageData, UdpEndPoint endPoint); + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/NullLogger.cs b/src/Rssdp.Shared/NullLogger.cs new file mode 100644 index 0000000..acbbf34 --- /dev/null +++ b/src/Rssdp.Shared/NullLogger.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Provides a implementation that does nothing, effectively disabling logging. Use the property to obtain an instance as the constructor is private. + /// + /// + /// This logger is inherently thread-safe and the value can be shared among multiple components. + /// + public class NullLogger : ISsdpLogger + { + + private static ISsdpLogger s_Instance; + + private NullLogger() + { + } + + /// + /// Provides a single instance of . + /// + public static ISsdpLogger Instance + { + get { return s_Instance ?? (s_Instance = new NullLogger()); } + } + + /// + /// Does nothing. + /// + /// Unused as this implementation does not log. + public void LogError(string message) + { + } + + /// + /// Does nothing. + /// + /// Unused as this implementation does not log. + public void LogInfo(string message) + { + } + + /// + /// Does nothing. + /// + /// Unused as this implementation does not log. + public void LogVerbose(string message) + { + } + + /// + /// Does nothing. + /// + /// Unused as this implementation does not log. + public void LogWarning(string message) + { + } + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/ReadOnlyEnumerable.cs b/src/Rssdp.Shared/ReadOnlyEnumerable.cs new file mode 100644 index 0000000..1a69f88 --- /dev/null +++ b/src/Rssdp.Shared/ReadOnlyEnumerable.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + internal sealed class ReadOnlyEnumerable : System.Collections.Generic.IEnumerable + { + + #region Fields + + private IEnumerable _Items; + + #endregion + + #region Constructors + + public ReadOnlyEnumerable(IEnumerable items) + { + if (items == null) throw new ArgumentNullException("items"); + + _Items = items; + } + + #endregion + + #region IEnumerable Members + + public IEnumerator GetEnumerator() + { + return _Items.GetEnumerator(); + } + + #endregion + + #region IEnumerable Members + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return _Items.GetEnumerator(); + } + + #endregion + } +} diff --git a/src/Rssdp.Shared/ReceivedUdpData.cs b/src/Rssdp.Shared/ReceivedUdpData.cs new file mode 100644 index 0000000..d1c2ca3 --- /dev/null +++ b/src/Rssdp.Shared/ReceivedUdpData.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Used by the sockets wrapper to hold raw data received from a UDP socket. + /// + public sealed class ReceivedUdpData + { + /// + /// The buffer to place received data into. + /// + public byte[] Buffer { get; set; } + + /// + /// The number of bytes received. + /// + public int ReceivedBytes { get; set; } + + /// + /// The the data was received from. + /// + public UdpEndPoint ReceivedFrom { get; set; } + } +} diff --git a/src/Rssdp.Shared/RequestReceivedEventArgs.cs b/src/Rssdp.Shared/RequestReceivedEventArgs.cs new file mode 100644 index 0000000..a78f1b9 --- /dev/null +++ b/src/Rssdp.Shared/RequestReceivedEventArgs.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Provides arguments for the event. + /// + public sealed class RequestReceivedEventArgs : EventArgs + { + + #region Fields + + private readonly HttpRequestMessage _Message; + private readonly UdpEndPoint _ReceivedFrom; + + #endregion + + #region Constructors + + /// + /// Full constructor. + /// + /// The that was received. + /// A representing the sender's address (sometimes used for replies). + public RequestReceivedEventArgs(HttpRequestMessage message, UdpEndPoint receivedFrom) + { + _Message = message; + _ReceivedFrom = receivedFrom; + } + + #endregion + + #region Public Properties + + /// + /// The that was received. + /// + public HttpRequestMessage Message + { + get { return _Message; } + } + + /// + /// The the request came from. + /// + public UdpEndPoint ReceivedFrom + { + get { return _ReceivedFrom; } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/ResponseReceivedEventArgs.cs b/src/Rssdp.Shared/ResponseReceivedEventArgs.cs new file mode 100644 index 0000000..f883308 --- /dev/null +++ b/src/Rssdp.Shared/ResponseReceivedEventArgs.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Provides arguments for the event. + /// + public sealed class ResponseReceivedEventArgs : EventArgs + { + + #region Fields + + private readonly HttpResponseMessage _Message; + private readonly UdpEndPoint _ReceivedFrom; + + #endregion + + #region Constructors + + /// + /// Full constructor. + /// + /// The that was received. + /// A representing the sender's address (sometimes used for replies). + public ResponseReceivedEventArgs(HttpResponseMessage message, UdpEndPoint receivedFrom) + { + _Message = message; + _ReceivedFrom = receivedFrom; + } + + #endregion + + #region Public Properties + + /// + /// The that was received. + /// + public HttpResponseMessage Message + { + get { return _Message; } + } + + /// + /// The the response came from. + /// + public UdpEndPoint ReceivedFrom + { + get { return _ReceivedFrom; } + } + + #endregion + + } +} diff --git a/src/Rssdp.Shared/Rssdp.Shared.projitems b/src/Rssdp.Shared/Rssdp.Shared.projitems new file mode 100644 index 0000000..54973d2 --- /dev/null +++ b/src/Rssdp.Shared/Rssdp.Shared.projitems @@ -0,0 +1,58 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + cc2b3fbe-239e-4967-b566-1a7e4d9eaf5d + + + Rssdp.Shared + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Rssdp.Shared/Rssdp.Shared.shproj b/src/Rssdp.Shared/Rssdp.Shared.shproj new file mode 100644 index 0000000..e377af1 --- /dev/null +++ b/src/Rssdp.Shared/Rssdp.Shared.shproj @@ -0,0 +1,13 @@ + + + + cc2b3fbe-239e-4967-b566-1a7e4d9eaf5d + 14.0 + + + + + + + + diff --git a/src/Rssdp.Shared/ServiceEventArgs.cs b/src/Rssdp.Shared/ServiceEventArgs.cs new file mode 100644 index 0000000..1f00351 --- /dev/null +++ b/src/Rssdp.Shared/ServiceEventArgs.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Event arguments for the and events. + /// + public sealed class ServiceEventArgs : EventArgs + { + + #region Fields + + private readonly SsdpService _Service; + + #endregion + + #region Constructors + + /// + /// Constructs a new instance for the specified . + /// + /// The associated with the event this argument class is being used for. + /// Thrown if the argument is null. + public ServiceEventArgs(SsdpService service) + { + if (service == null) throw new ArgumentNullException(nameof(service)); + + _Service = service; + } + + #endregion + + #region Public Properties + + /// + /// Returns the instance the event is being raised for. + /// + public SsdpService Service + { + get { return _Service; } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SocketClosedException.cs b/src/Rssdp.Shared/SocketClosedException.cs new file mode 100644 index 0000000..0915dbc --- /dev/null +++ b/src/Rssdp.Shared/SocketClosedException.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// To be thrown when a socket is unexpectedly closed, or accessed in a closed state. + /// +#if SUPPORTS_SERIALISATION + [Serializable] +#endif + public class SocketClosedException : Exception + { + /// + /// Default constructor. + /// + public SocketClosedException() : this("The socket is closed.") { } + /// + /// Partial constructor. + /// + /// The error message associated with the error. + public SocketClosedException(string message) : base(message) { } + /// + /// Full constructor. + /// + /// The error message associated with the error. + /// Any inner exception that is wrapped by this exception. + public SocketClosedException(string message, Exception inner) : base(message, inner) { } + +#if SUPPORTS_SERIALISATION + /// + /// Deserialisation constructor. + /// + /// + /// + protected SocketClosedException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) + { + } +#endif + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpCommunicationsServer.cs b/src/Rssdp.Shared/SsdpCommunicationsServer.cs new file mode 100644 index 0000000..a5190a2 --- /dev/null +++ b/src/Rssdp.Shared/SsdpCommunicationsServer.cs @@ -0,0 +1,451 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Provides the platform independent logic for publishing device existence and responding to search requests. + /// + public sealed class SsdpCommunicationsServer : DisposableManagedObjectBase, ISsdpCommunicationsServer + { + + #region Fields + + /* + We could technically use one socket listening on port 1900 for everything. + This should get both multicast (notifications) and unicast (search response) messages, however + this often doesn't work under Windows because the MS SSDP service is running. If that service + is running then it will steal the unicast messages and we will never see search responses. + Since stopping the service would be a bad idea (might not be allowed security wise and might + break other apps running on the system) the only other work around is to use two sockets. + + We use one socket to listen for/receive notifications and search requests (_BroadcastListenSocket). + We use a second socket, bound to a different local port, to send search requests and listen for + responses (_SendSocket). The responses are sent to the local port this socket is bound to, + which isn't port 1900 so the MS service doesn't steal them. While the caller can specify a local + port to use, we will default to 0 which allows the underlying system to auto-assign a free port. + + */ + + private object _BroadcastListenSocketSynchroniser = new object(); + private IUdpSocket _BroadcastListenSocket; + + private object _SendSocketSynchroniser = new object(); + private IUdpSocket _SendSocket; + + private HttpRequestParser _RequestParser; + private HttpResponseParser _ResponseParser; + + private ISocketFactory _SocketFactory; + + private int _LocalPort; + private int _MulticastTtl; + + private bool _IsShared; + + #endregion + + #region Events + + /// + /// Raised when a HTTPU request message is received by a socket (unicast or multicast). + /// + public event EventHandler RequestReceived; + + /// + /// Raised when an HTTPU response message is received by a socket (unicast or multicast). + /// + public event EventHandler ResponseReceived; + + #endregion + + #region Public Properties + + /// + /// The number of times the Udp message is sent. Any value less than 2 will result in one message being sent. SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. + /// + /// + public int UdpSendCount { get; set; } = SsdpConstants.DefaultUdpResendCount; + + /// + /// The delay between repeating messages (as specified in UdpSendCount). + /// + /// + public TimeSpan UdpSendDelay { get; set; } = SsdpConstants.DefaultUdpResendDelay; + + #endregion + + #region Constructors + + /// + /// Minimum constructor. + /// + /// An implementation of the interface that can be used to make new unicast and multicast sockets. Cannot be null. + /// The argument is null. + public SsdpCommunicationsServer(ISocketFactory socketFactory) + : this(socketFactory, 0, SsdpConstants.SsdpDefaultMulticastTimeToLive) + { + } + + /// + /// Partial constructor. + /// + /// An implementation of the interface that can be used to make new unicast and multicast sockets. Cannot be null. + /// The specific local port to use for all sockets created by this instance. Specify zero to indicate the system should choose a free port itself. + /// The argument is null. + public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort) + : this(socketFactory, localPort, SsdpConstants.SsdpDefaultMulticastTimeToLive) + { + } + + /// + /// Full constructor. + /// + /// An implementation of the interface that can be used to make new unicast and multicast sockets. Cannot be null. + /// The specific local port to use for all sockets created by this instance. Specify zero to indicate the system should choose a free port itself. + /// The multicast time to live value for multicast sockets. Technically this is a number of router hops, not a 'Time'. Must be greater than zero. + /// The argument is null. + /// The argument is less than or equal to zero. + public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort, int multicastTimeToLive) + { + if (socketFactory == null) throw new ArgumentNullException("socketFactory"); + if (multicastTimeToLive <= 0) throw new ArgumentOutOfRangeException("multicastTimeToLive", "multicastTimeToLive must be greater than zero."); + + _BroadcastListenSocketSynchroniser = new object(); + _SendSocketSynchroniser = new object(); + + _LocalPort = localPort; + _SocketFactory = socketFactory; + + _RequestParser = new HttpRequestParser(); + _ResponseParser = new HttpResponseParser(); + + _MulticastTtl = multicastTimeToLive; + } + + #endregion + + #region Public Methods + + /// + /// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications. + /// + /// Thrown if the property is true (because has been called previously). + public void BeginListeningForBroadcasts() + { + ThrowIfDisposed(); + + if (_BroadcastListenSocket == null) + { + lock (_BroadcastListenSocketSynchroniser) + { + if (_BroadcastListenSocket == null) + _BroadcastListenSocket = ListenForBroadcastsAsync(); + } + } + } + + /// + /// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications. + /// + /// Thrown if the property is true (because has been called previously). + public void StopListeningForBroadcasts() + { + ThrowIfDisposed(); + + lock (_BroadcastListenSocketSynchroniser) + { + if (_BroadcastListenSocket != null) + { + _BroadcastListenSocket.Dispose(); + _BroadcastListenSocket = null; + } + } + } + + /// + /// Sends a message to a particular address (uni or multicast) and port. + /// + /// A byte array containing the data to send. + /// A representing the destination address for the data. Can be either a multicast or unicast destination. + /// Thrown if the argument is null. + /// Thrown if the property is true (because has been called previously). + public void SendMessage(byte[] messageData, UdpEndPoint destination) + { + if (messageData == null) throw new ArgumentNullException("messageData"); + + ThrowIfDisposed(); + + EnsureSendSocketCreated(); + + // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. + Repeat(UdpSendCount, UdpSendDelay, () => + { + SendMessageIfSocketNotDisposed(messageData, destination); + }); + } + + /// + /// Stops listening for search responses on the local, unicast socket. + /// + /// Thrown if the property is true (because has been called previously). + public void StopListeningForResponses() + { + ThrowIfDisposed(); + + lock (_SendSocketSynchroniser) + { + var socket = _SendSocket; + _SendSocket = null; + if (socket != null) + socket.Dispose(); + } + } + + #endregion + + #region Public Properties + + /// + /// Gets or sets a boolean value indicating whether or not this instance is shared amongst multiple and/or instances. + /// + /// + /// If true, disposing an instance of a or a will not dispose this comms server instance. The calling code is responsible for managing the lifetime of the server. + /// + public bool IsShared + { + get { return _IsShared; } + set { _IsShared = value; } + } + + /// + /// What type of sockets will be created: ipv6 or ipv4 + /// + public DeviceNetworkType DeviceNetworkType { get { return _SocketFactory.DeviceNetworkType; } } + + #endregion + + #region Overrides + + /// + /// Stops listening for requests, disposes this instance and all internal resources. + /// + /// + protected override void Dispose(bool disposing) + { + if (disposing) + { + lock (_BroadcastListenSocketSynchroniser) + { + if (_BroadcastListenSocket != null) + _BroadcastListenSocket.Dispose(); + } + + lock (_SendSocketSynchroniser) + { + if (_SendSocket != null) + _SendSocket.Dispose(); + } + } + } + + #endregion + + #region Private Methods + + private void SendMessageIfSocketNotDisposed(byte[] messageData, UdpEndPoint destination) + { + var socket = _SendSocket; + if (socket != null) + { + _SendSocket.SendTo(messageData, destination); + } + else + { + ThrowIfDisposed(); + } + } + + private static void Repeat(int repetitions, TimeSpan delay, Action work) + { + if (repetitions < 2) + repetitions = 1; + + for (int cnt = 0; cnt < repetitions; cnt++) + { + work(); + + if (delay != TimeSpan.Zero) + TaskEx.Delay(delay).Wait(); + } + } + + private IUdpSocket ListenForBroadcastsAsync() + { + var socket = _SocketFactory.CreateUdpMulticastSocket(_MulticastTtl, SsdpConstants.MulticastPort); + + ListenToSocket(socket); + + return socket; + } + + private IUdpSocket CreateSocketAndListenForResponsesAsync() + { + _SendSocket = _SocketFactory.CreateUdpSocket(_LocalPort); + + ListenToSocket(_SendSocket); + + return _SendSocket; + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capturing task to local variable removes compiler warning, task is not otherwise required.")] + private async void ListenToSocket(IUdpSocket socket) + { + // Tasks are captured to local variables even if we don't use them just to avoid compiler warnings. + try + { + await TaskEx.Run(async () => + { + + var cancelled = false; + while (!cancelled) + { + try + { + var result = await socket.ReceiveAsync().ConfigureAwait(false); + + if (result.ReceivedBytes > 0) + { + // Strange cannot convert compiler error here if I don't explicitly + // assign or cast to Action first. Assignment is easier to read, + // so went with that. + Action processWork = () => ProcessMessage(System.Text.UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.ReceivedFrom); + var processTask = TaskEx.Run(processWork); + } + } + catch (SocketClosedException) + { + if (this.IsDisposed) return; //No error or reconnect if we're shutdown. + + await ReconnectBroadcastListeningSocket(); + cancelled = true; + break; + } + catch (Exception) + { + cancelled = true; + } + } + }); + } + catch + { + if (this.IsDisposed) return; + + await ReconnectBroadcastListeningSocket(); + } + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] + private async Task ReconnectBroadcastListeningSocket() + { + var success = false; + while (!success) + { + try + { + var oldSocket = _BroadcastListenSocket; + + _BroadcastListenSocket = null; + BeginListeningForBroadcasts(); + + try + { + oldSocket?.Dispose(); + } + catch { } + + success = true; + break; + } + catch + { + await TaskEx.Delay(30000); + } + } + } + + private void EnsureSendSocketCreated() + { + if (_SendSocket == null) + { + lock (_SendSocketSynchroniser) + { + if (_SendSocket == null) + _SendSocket = CreateSocketAndListenForResponsesAsync(); + } + } + } + + private void ProcessMessage(string data, UdpEndPoint endPoint) + { + //Responses start with the HTTP version, prefixed with HTTP/ while + //requests start with a method which can vary and might be one we haven't + //seen/don't know. We'll check if this message is a request or a response + //by checking for the static HTTP/ prefix on the start of the message. + if (data.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase)) + { + HttpResponseMessage responseMessage = null; + try + { + responseMessage = _ResponseParser.Parse(data); + } + catch (ArgumentException) { } // Ignore invalid packets. + catch (FormatException) { } // Ignore invalid packets. + + if (responseMessage != null) + OnResponseReceived(responseMessage, endPoint); + } + else + { + HttpRequestMessage requestMessage = null; + try + { + requestMessage = _RequestParser.Parse(data); + } + catch (ArgumentException) { } // Ignore invalid packets. + catch (FormatException) { } // Ignore invalid packets. + + if (requestMessage != null) + OnRequestReceived(requestMessage, endPoint); + } + } + + private void OnRequestReceived(HttpRequestMessage data, UdpEndPoint endPoint) + { + //SSDP specification says only * is currently used but other uri's might + //be implemented in the future and should be ignored unless understood. + //Section 4.2 - http://tools.ietf.org/html/draft-cai-ssdp-v1-03#page-11 + if (data.RequestUri.ToString() != "*") return; + + var handlers = this.RequestReceived; + if (handlers != null) + handlers(this, new RequestReceivedEventArgs(data, endPoint)); + } + + private void OnResponseReceived(HttpResponseMessage data, UdpEndPoint endPoint) + { + var handlers = this.ResponseReceived; + if (handlers != null) + handlers(this, new ResponseReceivedEventArgs(data, endPoint)); + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpConstants.cs b/src/Rssdp.Shared/SsdpConstants.cs new file mode 100644 index 0000000..0d1d133 --- /dev/null +++ b/src/Rssdp.Shared/SsdpConstants.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Provides constants for common values related to the SSDP protocols. + /// + public static class SsdpConstants + { + /// + /// Multicast IPV6 Address used for SSDP multicast messages. Value is FF02::C. + /// + public const string MulticastLinkLocalAddressV6 = "FF02::C"; //(IPv6 link-local) + /// + /// Multicast IP Address used for SSDP multicast messages. Value is 239.255.255.250. + /// + public const string MulticastLocalAdminAddress = "239.255.255.250"; + /// + /// The UDP port used for SSDP multicast messages. Values is 1900. + /// + public const int MulticastPort = 1900; + /// + /// The default multicase TTL for SSDP multicast messages. Value is 4. + /// + public const int SsdpDefaultMulticastTimeToLive = 4; + + internal const string MSearchMethod = "M-SEARCH"; + + internal const string SsdpDiscoverMessage = "ssdp:discover"; + internal const string SsdpDiscoverAllSTHeader = "ssdp:all"; + + internal const string SsdpDeviceDescriptionXmlNamespace = "urn:schemas-upnp-org:device-1-0"; + + /// + /// Default buffer size for receiving SSDP broadcasts. Value is 8192 (bytes). + /// + public const int DefaultUdpSocketBufferSize = 8192; + /// + /// The maximum possible buffer size for a UDP message. Value is 65507 (bytes). + /// + public const int MaxUdpSocketBufferSize = 65507; // Max possible UDP packet size on IPv4 without using 'jumbograms'. + + /// + /// Namespace/prefix for UPnP device types. Values is schemas-upnp-org. + /// + public const string UpnpDeviceTypeNamespace = "schemas-upnp-org"; + /// + /// UPnP Root Device type. Value is upnp:rootdevice. + /// + public const string UpnpDeviceTypeRootDevice = "upnp:rootdevice"; + /// + /// The value is used by Windows Explorer for device searches instead of the UPNPDeviceTypeRootDevice constant. + /// Not sure why (different spec, bug, alternate protocol etc). Used to enable Windows Explorer support. + /// + public const string PnpDeviceTypeRootDevice = "pnp:rootdevice"; + /// + /// UPnP Basic Device type. Value is Basic. + /// + public const string UpnpDeviceTypeBasicDevice = "Basic"; + + internal const string SsdpKeepAliveNotification = "ssdp:alive"; + internal const string SsdpByeByeNotification = "ssdp:byebye"; + + /// + /// The default number of times to resend each UDP packet. + /// + /// + /// SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP. + /// This constant has a value of 3. + /// + public const int DefaultUdpResendCount = 3; + + private static readonly TimeSpan _DefaultUdpResendDelay = TimeSpan.FromMilliseconds(100); + + /// + /// The default time to delay between re-sends of UDP packets. + /// + /// + /// This property returns a value of 100 milliseconds. + /// + /// + public static TimeSpan DefaultUdpResendDelay { get { return _DefaultUdpResendDelay; } } + } +} diff --git a/src/Rssdp.Shared/SsdpDevice.cs b/src/Rssdp.Shared/SsdpDevice.cs new file mode 100644 index 0000000..1f53906 --- /dev/null +++ b/src/Rssdp.Shared/SsdpDevice.cs @@ -0,0 +1,985 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + /// + /// Base class representing the common details of a (root or embedded) device, either to be published or that has been located. + /// + /// + /// Do not derive new types directly from this class. New device classes should derive from either or . + /// + /// + /// + public abstract class SsdpDevice + { + + #region Fields + + private string _Udn; + private string _DeviceType; + private string _DeviceTypeNamespace; + private int _DeviceVersion; + private SsdpDevicePropertiesCollection _CustomProperties; + private CustomHttpHeadersCollection _CustomResponseHeaders; + + private IList _Devices; + private IList _Services; + + #endregion + + #region Events + + /// + /// Raised when a new child device is added. + /// + /// + /// + public event EventHandler DeviceAdded; + + /// + /// Raised when a child device is removed. + /// + /// + /// + public event EventHandler DeviceRemoved; + + /// + /// Raised when a new service is added. + /// + /// + /// + public event EventHandler ServiceAdded; + + /// + /// Raised when a service is removed. + /// + /// + /// + public event EventHandler ServiceRemoved; + + #endregion + + #region Constructors + + /// + /// Derived type constructor, allows constructing a device with no parent. Should only be used from derived types that are or inherit from . + /// + protected SsdpDevice() + { + _DeviceTypeNamespace = SsdpConstants.UpnpDeviceTypeNamespace; + _DeviceType = SsdpConstants.UpnpDeviceTypeBasicDevice; + _DeviceVersion = 1; + + this.Icons = new List(); + _Devices = new List(); + this.Devices = new ReadOnlyEnumerable(_Devices); + _CustomResponseHeaders = new CustomHttpHeadersCollection(); + _CustomProperties = new SsdpDevicePropertiesCollection(); + _Services = new List(); + this.Services = new ReadOnlyEnumerable(_Services); + } + + /// + /// Deserialisation constructor. + /// + /// Uses the provided XML string and parent device properties to set the properties of the object. The XML provided must be a valid UPnP device description document. + /// A UPnP device description XML document. + /// Thrown if the argument is null. + /// Thrown if the argument is empty. + protected SsdpDevice(string deviceDescriptionXml) + : this() + { + if (deviceDescriptionXml == null) throw new ArgumentNullException("deviceDescriptionXml"); + if (deviceDescriptionXml.Length == 0) throw new ArgumentException("deviceDescriptionXml cannot be an empty string.", "deviceDescriptionXml"); + + using (var ms = new System.IO.MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(deviceDescriptionXml))) + { + var reader = XmlReader.Create(ms); + + LoadDeviceProperties(reader, this); + } + } + + #endregion + + #region Public Properties + + #region UPnP Device Description Properties + + /// + /// Sets or returns the core device type (not including namespace, version etc.). Required. + /// + /// Defaults to the UPnP basic device type. + /// + /// + /// + public string DeviceType + { + get + { + return _DeviceType; + } + set + { + _DeviceType = value; + } + } + + /// + /// Sets or returns the namespace for the of this device. Optional, but defaults to UPnP schema so should be changed if is not a UPnP device type. + /// + /// Defaults to the UPnP standard namespace. + /// + /// + /// + public string DeviceTypeNamespace + { + get + { + return _DeviceTypeNamespace; + } + set + { + _DeviceTypeNamespace = value; + } + } + + /// + /// Sets or returns the version of the device type. Optional, defaults to 1. + /// + /// Defaults to a value of 1. + /// + /// + /// + public int DeviceVersion + { + get + { + return _DeviceVersion; + } + set + { + _DeviceVersion = value; + } + } + + /// + /// Returns the full device type string. + /// + /// + /// The format used is urn::device:: + /// + public string FullDeviceType + { + get + { + return String.Format("urn:{0}:device:{1}:{2}", + this.DeviceTypeNamespace ?? String.Empty, + this.DeviceType ?? String.Empty, + this.DeviceVersion); + } + } + + /// + /// Sets or returns the universally unique identifier for this device (without the uuid: prefix). Required. + /// + /// + /// Must be the same over time for a specific device instance (i.e. must survive reboots). + /// For UPnP 1.0 this can be any unique string. For UPnP 1.1 this should be a 128 bit number formatted in a specific way, preferably generated using the time and MAC based algorithm. See section 1.1.4 of http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf for details. + /// Technically this library implements UPnP 1.0, so any value is allowed, but we advise using UPnP 1.1 compatible values for good behaviour and forward compatibility with future versions. + /// + public string Uuid { get; set; } + + /// + /// Returns (or sets*) a unique device name for this device. Optional, not recommended to be explicitly set. + /// + /// + /// * In general you should not explicitly set this property. If it is not set (or set to null/empty string) the property will return a UDN value that is correct as per the UPnP specification, based on the other device properties. + /// The setter is provided to allow for devices that do not correctly follow the specification (when we discover them), rather than to intentionally deviate from the specification. + /// If a value is explicitly set, it is used verbatim, and so any prefix (such as uuid:) must be provided in the value. + /// + public string Udn + { + get + { + if (String.IsNullOrEmpty(_Udn) && !String.IsNullOrEmpty(this.Uuid)) + return "uuid:" + this.Uuid; + else + return _Udn; + } + set + { + _Udn = value; + } + } + + /// + /// Gets or sets a custom USN. + /// + /// + /// The USN. + /// + public string Usn { get; set; } + + /// + /// Gets or sets a custom notification type. + /// + /// + /// The custom notification type. + /// + public string NotificationType { get; set; } + + /// + /// Sets or returns a friendly/display name for this device on the network. Something the user can identify the device/instance by, i.e Lounge Main Light. Required. + /// + /// A short description for the end user. + public string FriendlyName { get; set; } + + /// + /// Sets or returns the name of the manufacturer of this device. Required. + /// + public string Manufacturer { get; set; } + + /// + /// Sets or returns a URL to the manufacturers web site. Optional. + /// + public Uri ManufacturerUrl { get; set; } + + /// + /// Sets or returns a description of this device model. Recommended. + /// + /// A long description for the end user. + public string ModelDescription { get; set; } + + /// + /// Sets or returns the name of this model. Required. + /// + public string ModelName { get; set; } + + /// + /// Sets or returns the number of this model. Recommended. + /// + public string ModelNumber { get; set; } + + /// + /// Sets or returns a URL to a web page with details of this device model. Optional. + /// + /// + /// Optional. May be relative to base URL. + /// + public Uri ModelUrl { get; set; } + + /// + /// Sets or returns the serial number for this device. Recommended. + /// + public string SerialNumber { get; set; } + + /// + /// Sets or returns the universal product code of the device, if any. Optional. + /// + /// + /// If not blank, must be exactly 12 numeric digits. + /// + public string Upc { get; set; } + + /// + /// Sets or returns the URL to a web page that can be used to configure/manager/use the device. Recommended. + /// + /// + /// May be relative to base URL. + /// + public Uri PresentationUrl { get; set; } + + #endregion + + /// + /// Returns a list of icons (images) that can be used to display this device. Optional, but recommended you provide at least one at 48x48 pixels. + /// + public IList Icons + { + get; + private set; + } + + /// + /// Returns a read-only enumerable set of objects representing children of this device. Child devices are optional. + /// + /// + /// + public IEnumerable Devices + { + get; + private set; + } + + /// + /// Returns a dictionary of objects keyed by . Each value represents a custom property in the device description document. + /// + public SsdpDevicePropertiesCollection CustomProperties + { + get + { + return _CustomProperties; + } + } + + /// + /// Provides a list of additional information to provide about this device in search response and notification messages. + /// + /// + /// The headers included here are included in the (HTTP headers) for search response and alive notifications sent in relation to this device. + /// Only values specified directly on this instance will be included, headers from ancestors are not automatically included. + /// + public CustomHttpHeadersCollection CustomResponseHeaders + { + get + { + return _CustomResponseHeaders; + } + } + + /// + /// Returns a read-only enumerable set of objects representing services associated with this device. + /// + /// + /// + public IEnumerable Services + { + get; + private set; + } + + #endregion + + #region Public Methods + + #region Child Device Methods + + /// + /// Adds a child device to the collection. + /// + /// The instance to add. + /// + /// If the device is already a member of the collection, this method does nothing. + /// Also sets the property of the added device and all descendant devices to the relevant instance. + /// + /// Thrown if the argument is null. + /// Thrown if the is already associated with a different instance than used in this tree. Can occur if you try to add the same device instance to more than one tree. Also thrown if you try to add a device to itself. + /// + public void AddDevice(SsdpEmbeddedDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + if (device.RootDevice != null && device.RootDevice != this.ToRootDevice()) throw new InvalidOperationException("This device is already associated with a different root device (has been added as a child in another branch)."); + if (device == this) throw new InvalidOperationException("Can't add device to itself."); + + bool wasAdded = false; + lock (_Devices) + { + if (!ChildDeviceExists(device)) + { + device.RootDevice = this.ToRootDevice(); + _Devices.Add(device); + wasAdded = true; + } + } + + if (wasAdded) + OnDeviceAdded(device); + } + + /// + /// Removes a child device from the collection. + /// + /// The instance to remove. + /// + /// If the device is not a member of the collection, this method does nothing. + /// Also sets the property to null for the removed device and all descendant devices. + /// + /// Thrown if the argument is null. + /// + public void RemoveDevice(SsdpEmbeddedDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + + bool wasRemoved = false; + lock (_Devices) + { + wasRemoved = _Devices.Remove(device); + } + + if (wasRemoved) + { + device.RootDevice = null; + OnDeviceRemoved(device); + } + } + + /// + /// Raises the event. + /// + /// The instance added to the collection. + /// + /// + protected virtual void OnDeviceAdded(SsdpEmbeddedDevice device) + { + var handlers = this.DeviceAdded; + if (handlers != null) + handlers(this, new DeviceEventArgs(device)); + } + + /// + /// Raises the event. + /// + /// The instance removed from the collection. + /// + /// + protected virtual void OnDeviceRemoved(SsdpEmbeddedDevice device) + { + var handlers = this.DeviceRemoved; + if (handlers != null) + handlers(this, new DeviceEventArgs(device)); + } + + #endregion + + #region Service Methods + + /// + /// Adds a service to the collection. + /// + /// The instance to add. + /// + /// If the service is already a member of the collection, this method does nothing. + /// Services should be added to the device before it is added to a publisher. + /// + /// Thrown if the argument is null. + /// + public void AddService(SsdpService service) + { + if (service == null) throw new ArgumentNullException(nameof(service)); + + bool wasAdded = false; + lock (_Services) + { + if (!ChildServiceExists(service)) + { + _Services.Add(service); + wasAdded = true; + } + } + + if (wasAdded) + OnServiceAdded(service); + } + + /// + /// Removes a service from the collection. + /// + /// The instance to remove. + /// + /// If the service is not a member of the collection, this method does nothing. + /// + /// Thrown if the argument is null. + /// + public void RemoveService(SsdpService service) + { + if (service == null) throw new ArgumentNullException(nameof(service)); + + bool wasRemoved = false; + lock (_Services) + { + wasRemoved = _Services.Remove(service); + } + + if (wasRemoved) + OnServiceRemoved(service); + } + + /// + /// Raises the event. + /// + /// The instance added to the collection. + /// + /// + protected virtual void OnServiceAdded(SsdpService service) + { + var handlers = this.ServiceAdded; + if (handlers != null) + handlers(this, new ServiceEventArgs(service)); + } + + /// + /// Raises the event. + /// + /// The instance removed from the collection. + /// + /// + protected virtual void OnServiceRemoved(SsdpService service) + { + var handlers = this.ServiceRemoved; + if (handlers != null) + handlers(this, new ServiceEventArgs(service)); + } + + #endregion + + /// + /// Writes this device to the specified as a device node and it's content. + /// + /// The to output to. + /// The to write out. + /// Thrown if the or argument is null. + protected virtual void WriteDeviceDescriptionXml(XmlWriter writer, SsdpDevice device) + { + if (writer == null) throw new ArgumentNullException("writer"); + if (device == null) throw new ArgumentNullException("device"); + + writer.WriteStartElement("device"); + + if (!String.IsNullOrEmpty(device.FullDeviceType)) + WriteNodeIfNotEmpty(writer, "deviceType", device.FullDeviceType); + + WriteNodeIfNotEmpty(writer, "friendlyName", device.FriendlyName); + WriteNodeIfNotEmpty(writer, "manufacturer", device.Manufacturer); + WriteNodeIfNotEmpty(writer, "manufacturerURL", device.ManufacturerUrl); + WriteNodeIfNotEmpty(writer, "modelDescription", device.ModelDescription); + WriteNodeIfNotEmpty(writer, "modelName", device.ModelName); + WriteNodeIfNotEmpty(writer, "modelNumber", device.ModelNumber); + WriteNodeIfNotEmpty(writer, "modelURL", device.ModelUrl); + WriteNodeIfNotEmpty(writer, "presentationURL", device.PresentationUrl); + WriteNodeIfNotEmpty(writer, "serialNumber", device.SerialNumber); + WriteNodeIfNotEmpty(writer, "UDN", device.Udn); + WriteNodeIfNotEmpty(writer, "UPC", device.Upc); + + WriteCustomProperties(writer, device); + WriteIcons(writer, device); + WriteChildDevices(writer, device); + WriteServices(writer, device); + + writer.WriteEndElement(); + } + + /// + /// Converts a string to a , or returns null if the string provided is null. + /// + /// The string value to convert. + /// A . + protected static Uri StringToUri(string value) + { + if (!String.IsNullOrEmpty(value)) + return new Uri(value, UriKind.RelativeOrAbsolute); + + return null; + } + + #endregion + + #region Private Methods + + #region Serialisation Methods + + private static void WriteCustomProperties(XmlWriter writer, SsdpDevice device) + { + foreach (var prop in device.CustomProperties) + { + writer.WriteElementString(prop.Namespace, prop.Name, SsdpConstants.SsdpDeviceDescriptionXmlNamespace, prop.Value); + } + } + + private static void WriteIcons(XmlWriter writer, SsdpDevice device) + { + if (device.Icons.Any()) + { + writer.WriteStartElement("iconList"); + + foreach (var icon in device.Icons) + { + writer.WriteStartElement("icon"); + + writer.WriteElementString("mimetype", icon.MimeType); + writer.WriteElementString("width", icon.Width.ToString()); + writer.WriteElementString("height", icon.Height.ToString()); + writer.WriteElementString("depth", icon.ColorDepth.ToString()); + writer.WriteElementString("url", icon.Url.ToString()); + + writer.WriteEndElement(); + } + + writer.WriteEndElement(); + } + } + + private void WriteChildDevices(XmlWriter writer, SsdpDevice parentDevice) + { + if (parentDevice.Devices.Any()) + { + writer.WriteStartElement("deviceList"); + + foreach (var device in parentDevice.Devices) + { + WriteDeviceDescriptionXml(writer, device); + } + + writer.WriteEndElement(); + } + } + + private static void WriteNodeIfNotEmpty(XmlWriter writer, string nodeName, string value) + { + if (!String.IsNullOrEmpty(value)) + writer.WriteElementString(nodeName, value); + } + + private static void WriteNodeIfNotEmpty(XmlWriter writer, string nodeName, Uri value) + { + if (value != null) + writer.WriteElementString(nodeName, value.ToString()); + } + + private static void WriteServices(XmlWriter writer, SsdpDevice device) + { + if (device.Services.Any()) + { + writer.WriteStartElement("serviceList"); + foreach (var service in device.Services) + { + WriteService(writer, service); + } + writer.WriteEndElement(); + } + } + + private static void WriteService(XmlWriter writer, SsdpService service) + { + writer.WriteStartElement("service"); + + WriteNodeIfNotEmpty(writer, "serviceType", service.FullServiceType); + WriteNodeIfNotEmpty(writer, "serviceId", service.ServiceId); + WriteNodeIfNotEmpty(writer, "SCPDURL", service.ScpdUrl); + WriteNodeIfNotEmpty(writer, "controlURL", service.ControlUrl); + WriteNodeIfNotEmpty(writer, "eventSubURL", service.EventSubUrl); + + writer.WriteEndElement(); + } + + #endregion + + #region Deserialisation Methods + + private void LoadDeviceProperties(XmlReader reader, SsdpDevice device) + { + ReadUntilDeviceNode(reader); + + while (!reader.EOF) + { + if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "device") + { + reader.Read(); + break; + } + + if (!SetPropertyFromReader(reader, device)) + reader.Read(); + } + } + + private static void ReadUntilDeviceNode(XmlReader reader) + { + while (!reader.EOF && (reader.LocalName != "device" || reader.NodeType != XmlNodeType.Element)) + { + reader.Read(); + } + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Yes, there is a large switch statement, not it's not really complex and doesn't really need to be rewritten at this point.")] + private bool SetPropertyFromReader(XmlReader reader, SsdpDevice device) + { + switch (reader.LocalName) + { + case "friendlyName": + device.FriendlyName = reader.ReadElementContentAsString(); + break; + + case "manufacturer": + device.Manufacturer = reader.ReadElementContentAsString(); + break; + + case "manufacturerURL": + device.ManufacturerUrl = StringToUri(reader.ReadElementContentAsString()); + break; + + case "modelDescription": + device.ModelDescription = reader.ReadElementContentAsString(); + break; + + case "modelName": + device.ModelName = reader.ReadElementContentAsString(); + break; + + case "modelNumber": + device.ModelNumber = reader.ReadElementContentAsString(); + break; + + case "modelURL": + device.ModelUrl = StringToUri(reader.ReadElementContentAsString()); + break; + + case "presentationURL": + device.PresentationUrl = StringToUri(reader.ReadElementContentAsString()); + break; + + case "serialNumber": + device.SerialNumber = reader.ReadElementContentAsString(); + break; + + case "UDN": + device.Udn = reader.ReadElementContentAsString(); + SetUuidFromUdn(device); + break; + + case "UPC": + device.Upc = reader.ReadElementContentAsString(); + break; + + case "deviceType": + SetDeviceTypePropertiesFromFullDeviceType(device, reader.ReadElementContentAsString()); + break; + + case "iconList": + reader.Read(); + LoadIcons(reader, device); + break; + + case "deviceList": + reader.Read(); + LoadChildDevices(reader, device); + break; + + case "serviceList": + reader.Read(); + LoadServices(reader, device); + if (!reader.EOF && reader.NodeType == XmlNodeType.EndElement && reader.Name == "serviceList") + reader.Read(); + break; + + default: + if (reader.NodeType == XmlNodeType.Element && reader.Name != "device" && reader.Name != "icon") + { + AddCustomProperty(reader, device); + break; + } + else + return false; + } + return true; + } + + private static void LoadServices(XmlReader reader, SsdpDevice device) + { + while (!reader.EOF && reader.NodeType != XmlNodeType.Element) + { + reader.Read(); + } + + while (!reader.EOF) + { + while (!reader.EOF && reader.NodeType != XmlNodeType.Element && !(reader.NodeType == XmlNodeType.EndElement && reader.Name == "serviceList")) + { + reader.Read(); + } + + if (reader.LocalName == "service") + { + var service = new SsdpService(reader.ReadOuterXml()); + device.AddService(service); + } + else + break; + } + } + + private static void SetDeviceTypePropertiesFromFullDeviceType(SsdpDevice device, string value) + { + if (String.IsNullOrEmpty(value) || !value.Contains(":")) + device.DeviceType = value; + else + { + var parts = value.Split(':'); + if (parts.Length == 5) + { + int deviceVersion = 1; + if (Int32.TryParse(parts[4], out deviceVersion)) + { + device.DeviceTypeNamespace = parts[1]; + device.DeviceType = parts[3]; + device.DeviceVersion = deviceVersion; + } + else + device.DeviceType = value; + } + else + device.DeviceType = value; + } + } + + private static void SetUuidFromUdn(SsdpDevice device) + { + if (device.Udn != null && device.Udn.StartsWith("uuid:", StringComparison.OrdinalIgnoreCase)) + device.Uuid = device.Udn.Substring(5).Trim(); + else + device.Uuid = device.Udn; + } + + private static void LoadIcons(XmlReader reader, SsdpDevice device) + { + while (!reader.EOF && reader.NodeType != XmlNodeType.Element) + { + reader.Read(); + } + + while (!reader.EOF) + { + while (!reader.EOF && reader.NodeType != XmlNodeType.Element && !(reader.NodeType == XmlNodeType.EndElement && reader.Name == "iconList")) + { + reader.Read(); + } + + if (reader.LocalName == "icon") + { + var icon = new SsdpDeviceIcon(); + LoadIconProperties(reader, icon); + device.Icons.Add(icon); + } + else + break; + } + } + + private static void LoadIconProperties(XmlReader reader, SsdpDeviceIcon icon) + { + while (!reader.EOF && (reader.LocalName != "icon" || reader.NodeType != XmlNodeType.Element)) + { + reader.Read(); + } + + while (!reader.EOF) + { + if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "icon") + { + reader.Read(); + break; + } + + switch (reader.LocalName) + { + case "depth": + icon.ColorDepth = reader.ReadElementContentAsInt(); + break; + + case "height": + icon.Height = reader.ReadElementContentAsInt(); + break; + + case "width": + icon.Width = reader.ReadElementContentAsInt(); + break; + + case "mimetype": + icon.MimeType = reader.ReadElementContentAsString(); + break; + + case "url": + icon.Url = StringToUri(reader.ReadElementContentAsString()); + break; + + default: + reader.Read(); + break; + } + } + } + + private void LoadChildDevices(XmlReader reader, SsdpDevice device) + { + while (!reader.EOF && reader.NodeType != XmlNodeType.Element) + { + reader.Read(); + } + + while (!reader.EOF) + { + while (!reader.EOF && reader.NodeType != XmlNodeType.Element) + { + reader.Read(); + } + + if (reader.LocalName == "device") + { + var childDevice = new SsdpEmbeddedDevice(); + LoadDeviceProperties(reader, childDevice); + device.AddDevice(childDevice); + } + else + break; + } + } + + private static void AddCustomProperty(XmlReader reader, SsdpDevice device) + { + // If the property is an empty element, there is no value to read + // Advance the reader and return + if (reader.IsEmptyElement) + { + reader.Read(); + return; + } + + var newProp = new SsdpDeviceProperty() { Namespace = reader.Prefix, Name = reader.LocalName }; + int depth = reader.Depth; + reader.Read(); + while (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.Comment) + { + reader.Read(); + } + + if (reader.NodeType != XmlNodeType.CDATA && reader.NodeType != XmlNodeType.Text) + { + while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != newProp.Name || reader.Prefix != newProp.Namespace || reader.Depth != depth)) + { + reader.Read(); + } + if (!reader.EOF) + reader.Read(); + return; + } + + newProp.Value = reader.Value; + + // We don't support complex nested types or repeat/multi-value properties + if (!device.CustomProperties.Contains(newProp.FullName)) + device.CustomProperties.Add(newProp); + } + + #endregion + + private bool ChildDeviceExists(SsdpDevice device) + { + return (from d in _Devices where device.Uuid == d.Uuid select d).Any(); + } + + private bool ChildServiceExists(SsdpService service) + { + return (from s in _Services where service.Uuid == s.Uuid select s).Any(); + } + + #endregion + + } +} diff --git a/src/Rssdp.Shared/SsdpDeviceExtensions.cs b/src/Rssdp.Shared/SsdpDeviceExtensions.cs new file mode 100644 index 0000000..0ad710a --- /dev/null +++ b/src/Rssdp.Shared/SsdpDeviceExtensions.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Extensions for and derived types. + /// + public static class SsdpDeviceExtensions + { + + /// + /// Returns the root device associated with a device instance derived from . + /// + /// The device instance to find the for. + /// + /// The must be or inherit from or , otherwise an will occur. + /// May return null if the instance is an embedded device not yet associated with a instance yet. + /// If is an instance of (or derives from it), returns the same instance cast to . + /// + /// The instance associated with the device instance specified, or null otherwise. + /// Thrown if is null. + /// Thrown if is not an instance of or dervied from either or . + public static SsdpRootDevice ToRootDevice(this SsdpDevice device) + { + if (device == null) throw new System.ArgumentNullException("device"); + + var rootDevice = device as SsdpRootDevice; + if (rootDevice == null) + rootDevice = ((SsdpEmbeddedDevice)device).RootDevice; + + return rootDevice; + } + } +} diff --git a/src/Rssdp.Shared/SsdpDeviceIcon.cs b/src/Rssdp.Shared/SsdpDeviceIcon.cs new file mode 100644 index 0000000..4ffda58 --- /dev/null +++ b/src/Rssdp.Shared/SsdpDeviceIcon.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + /// + /// Represents an icon published by an . + /// + public sealed class SsdpDeviceIcon + { + /// + /// The mime type for the image data returned by the property. + /// + /// + /// Required. Icon's MIME type (cf. RFC 2045, 2046, and 2387). Single MIME image type. At least one icon should be of type “image/png” (Portable Network Graphics, see IETF RFC 2083). + /// + /// + public string MimeType { get; set; } + + /// + /// The URL that can be called with an HTTP GET command to retrieve the image data. + /// + /// + /// Required. May be relative to base URL. Specified by UPnP vendor. Single URL. + /// + /// + public Uri Url { get; set; } + + /// + /// The width of the image in pixels. + /// + /// Required, must be greater than zero. + public int Width { get; set; } + + /// + /// The height of the image in pixels. + /// + /// Required, must be greater than zero. + public int Height { get; set; } + + /// + /// The colour depth of the image. + /// + /// Required, must be greater than zero. + public int ColorDepth { get; set; } + + } +} diff --git a/src/Rssdp.Shared/SsdpDeviceLocatorBase.cs b/src/Rssdp.Shared/SsdpDeviceLocatorBase.cs new file mode 100644 index 0000000..4ecab21 --- /dev/null +++ b/src/Rssdp.Shared/SsdpDeviceLocatorBase.cs @@ -0,0 +1,736 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Allows you to search the network for a particular device, device types, or UPnP service types. Also listenings for broadcast notifications of device availability and raises events to indicate changes in status. + /// + public abstract class SsdpDeviceLocatorBase : DisposableManagedObjectBase, ISsdpDeviceLocator + { + + #region Fields & Constants + + private List _Devices; + private ISsdpCommunicationsServer _CommunicationsServer; + + private IList _SearchResults; + private object _SearchResultsSynchroniser; + + private System.Threading.Timer _ExpireCachedDevicesTimer; + + private const string HttpURequestMessageFormat = @"{0} * HTTP/1.1 +HOST: {1}:{2} +MAN: ""{3}"" +MX: {5} +ST: {4} + +"; + + private static readonly TimeSpan DefaultSearchWaitTime = TimeSpan.FromSeconds(4); + private static readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1); + + #endregion + + #region Constructors + + /// + /// Default constructor. + /// + /// The implementation to use for network communications. + protected SsdpDeviceLocatorBase(ISsdpCommunicationsServer communicationsServer) + { + if (communicationsServer == null) throw new ArgumentNullException("communicationsServer"); + + _CommunicationsServer = communicationsServer; + _CommunicationsServer.ResponseReceived += CommsServer_ResponseReceived; + + _SearchResultsSynchroniser = new object(); + _Devices = new List(); + } + + #endregion + + #region Events + + /// + /// Raised for when + /// + /// An 'alive' notification is received that a device, regardless of whether or not that device is not already in the cache or has previously raised this event. + /// For each item found during a device (cached or not), allowing clients to respond to found devices before the entire search is complete. + /// Only if the notification type matches the property. By default the filter is null, meaning all notifications raise events (regardless of ant + /// + /// This event may be raised from a background thread, if interacting with UI or other objects with specific thread affinity invoking to the relevant thread is required. + /// + /// + /// + /// + /// + public event EventHandler DeviceAvailable; + + /// + /// Raised when a notification is received that indicates a device has shutdown or otherwise become unavailable. + /// + /// + /// Devices *should* broadcast these types of notifications, but not all devices do and sometimes (in the event of power loss for example) it might not be possible for a device to do so. You should also implement error handling when trying to contact a device, even if RSSDP is reporting that device as available. + /// This event is only raised if the notification type matches the property. A null or empty string for the will be treated as no filter and raise the event for all notifications. + /// The property may contain either a fully complete instance, or one containing just a USN and NotificationType property. Full information is available if the device was previously discovered and cached, but only partial information if a byebye notification was received for a previously unseen or expired device. + /// This event may be raised from a background thread, if interacting with UI or other objects with specific thread affinity invoking to the relevant thread is required. + /// + /// + /// + /// + /// + public event EventHandler DeviceUnavailable; + + #endregion + + #region Public Methods + + #region Search Overloads + + /// + /// Performs a search for all devices using the default search timeout. + /// + /// A task whose result is an of instances, representing all found devices. + public Task> SearchAsync() + { + return SearchAsync(SsdpConstants.SsdpDiscoverAllSTHeader, DefaultSearchWaitTime); + } + + /// + /// Performs a search for the specified search target (criteria) and default search timeout. + /// + /// The criteria for the search. Value can be; + /// + /// Root devicesupnp:rootdevice + /// Specific device by UUIDuuid:<device uuid> + /// Device typeFully qualified device type starting with urn: i.e urn:schemas-upnp-org:Basic:1 + /// + /// + /// A task whose result is an of instances, representing all found devices. + public Task> SearchAsync(string searchTarget) + { + return SearchAsync(searchTarget, DefaultSearchWaitTime); + } + + /// + /// Performs a search for all devices using the specified search timeout. + /// + /// The amount of time to wait for network responses to the search request. Longer values will likely return more devices, but increase search time. A value between 1 and 5 seconds is recommended by the UPnP 1.1 specification, this method requires the value be greater 1 second if it is not zero. Specify TimeSpan.Zero to return only devices already in the cache. + /// A task whose result is an of instances, representing all found devices. + public Task> SearchAsync(TimeSpan searchWaitTime) + { + return SearchAsync(SsdpConstants.SsdpDiscoverAllSTHeader, searchWaitTime); + } + + /// + /// Performs a search for the specified search target (criteria) and search timeout. + /// + /// The criteria for the search. Value can be; + /// + /// Root devicesupnp:rootdevice + /// Specific device by UUIDuuid:<device uuid> + /// Device typeA device namespace and type in format of urn:<device namespace>:device:<device type>:<device version> i.e urn:schemas-upnp-org:device:Basic:1 + /// Service typeA service namespace and type in format of urn:<service namespace>:service:<servicetype>:<service version> i.e urn:my-namespace:service:MyCustomService:1 + /// + /// + /// The amount of time to wait for network responses to the search request. Longer values will likely return more devices, but increase search time. A value between 1 and 5 seconds is recommended by the UPnP 1.1 specification, this method requires the value be greater 1 second if it is not zero. Specify TimeSpan.Zero to return only devices already in the cache. + /// + /// By design RSSDP does not support 'publishing services' as it is intended for use with non-standard UPnP devices that don't publish UPnP style services. However, it is still possible to use RSSDP to search for devices implemetning these services if you know the service type. + /// + /// A task whose result is an of instances, representing all found devices. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "expireTask", Justification = "Task is not actually required, but capturing to local variable suppresses compiler warning")] + public async Task> SearchAsync(string searchTarget, TimeSpan searchWaitTime) + { + if (searchTarget == null) throw new ArgumentNullException("searchTarget"); + if (searchTarget.Length == 0) throw new ArgumentException("searchTarget cannot be an empty string.", "searchTarget"); + if (searchWaitTime.TotalSeconds < 0) throw new ArgumentException("searchWaitTime must be a positive time."); + if (searchWaitTime.TotalSeconds > 0 && searchWaitTime.TotalSeconds <= 1) throw new ArgumentException("searchWaitTime must be zero (if you are not using the result and relying entirely in the events), or greater than one second."); + + ThrowIfDisposed(); + + if (_SearchResults != null) throw new InvalidOperationException("Search already in progress. Only one search at a time is allowed."); + _SearchResults = new List(); + + // If searchWaitTime == 0 then we are only going to report unexpired cached items, not actually do a search. + if (searchWaitTime > TimeSpan.Zero) + BroadcastDiscoverMessage(searchTarget, SearchTimeToMXValue(searchWaitTime)); + + lock (_SearchResultsSynchroniser) + { + foreach (var device in GetUnexpiredDevices().Where(NotificationTypeMatchesFilter)) + { + if (this.IsDisposed) break; + + DeviceFound(device, false); + } + } + + + if (searchWaitTime != TimeSpan.Zero && !this.IsDisposed) + await TaskEx.Delay(searchWaitTime).ConfigureAwait(false); + + IEnumerable retVal = null; + + try + { + lock (_SearchResultsSynchroniser) + { + retVal = _SearchResults; + _SearchResults = null; + } + + var expireTask = RemoveExpiredDevicesFromCacheAsync(); + } + finally + { + var server = _CommunicationsServer; + try + { + if (server != null) // In case we were disposed while searching. + server.StopListeningForResponses(); + } + catch (ObjectDisposedException) { } + } + + return retVal; + } + + #endregion + + /// + /// Starts listening for broadcast notifications of service availability. + /// + /// + /// When called the system will listen for 'alive' and 'byebye' notifications. This can speed up searching, as well as provide dynamic notification of new devices appearing on the network, and previously discovered devices disappearing. + /// + /// + /// + /// + /// Throw if the ty is true. + public void StartListeningForNotifications() + { + ThrowIfDisposed(); + + _CommunicationsServer.RequestReceived -= CommsServer_RequestReceived; + _CommunicationsServer.RequestReceived += CommsServer_RequestReceived; + _CommunicationsServer.BeginListeningForBroadcasts(); + } + + /// + /// Stops listening for broadcast notifications of service availability. + /// + /// + /// Does nothing if this instance is not already listening for notifications. + /// + /// + /// + /// + /// Throw if the property is true. + public void StopListeningForNotifications() + { + ThrowIfDisposed(); + + _CommunicationsServer.RequestReceived -= CommsServer_RequestReceived; + } + + /// + /// Raises the event. + /// + /// A representing the device that is now available. + /// True if the device was not currently in the cahce before this event was raised. + /// + protected virtual void OnDeviceAvailable(DiscoveredSsdpDevice device, bool isNewDevice) + { + if (this.IsDisposed) return; + + var handlers = this.DeviceAvailable; + if (handlers != null) + handlers(this, new DeviceAvailableEventArgs(device, isNewDevice)); + } + + /// + /// Raises the event. + /// + /// A representing the device that is no longer available. + /// True if the device expired from the cache without being renewed, otherwise false to indicate the device explicitly notified us it was being shutdown. + /// + protected virtual void OnDeviceUnavailable(DiscoveredSsdpDevice device, bool expired) + { + if (this.IsDisposed) return; + + var handlers = this.DeviceUnavailable; + if (handlers != null) + handlers(this, new DeviceUnavailableEventArgs(device, expired)); + } + + #endregion + + #region Public Properties + + /// + /// Returns a boolean indicating whether or not a search is currently in progress. + /// + /// + /// Only one search can be performed at a time, per instance. + /// + public bool IsSearching + { + get { return _SearchResults != null; } + } + + /// + /// Sets or returns a string containing the filter for notifications. Notifications not matching the filter will not raise the or events. + /// + /// + /// Device alive/byebye notifications whose NT header does not match this filter value will still be captured and cached internally, but will not raise events about device availability. Usually used with either a device type of uuid NT header value. + /// If the value is null or empty string then, all notifications are reported. + /// Example filters follow; + /// upnp:rootdevice + /// urn:schemas-upnp-org:device:WANDevice:1 + /// uuid:9F15356CC-95FA-572E-0E99-85B456BD3012 + /// + /// + /// + /// + /// + public string NotificationFilter + { + get; + set; + } + + #endregion + + #region Overrides + + /// + /// Disposes this object and all internal resources. Stops listening for all network messages. + /// + /// True if managed resources should be disposed, or false is only unmanaged resources should be cleaned up. + protected override void Dispose(bool disposing) + { + + if (disposing) + { + var timer = _ExpireCachedDevicesTimer; + if (timer != null) + timer.Dispose(); + + var commsServer = _CommunicationsServer; + _CommunicationsServer = null; + if (commsServer != null) + { + commsServer.ResponseReceived -= this.CommsServer_ResponseReceived; + commsServer.RequestReceived -= this.CommsServer_RequestReceived; + if (!commsServer.IsShared) + commsServer.Dispose(); + } + } + } + + #endregion + + #region Private Methods + + #region Discovery/Device Add + + private void AddOrUpdateDiscoveredDevice(DiscoveredSsdpDevice device) + { + bool isNewDevice = false; + lock (_Devices) + { + var existingDevice = FindExistingDeviceNotification(_Devices, device.NotificationType, device.Usn); + if (existingDevice == null) + { + _Devices.Add(device); + isNewDevice = true; + } + else + { + _Devices.Remove(existingDevice); + _Devices.Add(device); + } + } + + DeviceFound(device, isNewDevice); + } + + private void DeviceFound(DiscoveredSsdpDevice device, bool isNewDevice) + { + // Don't raise the event if we've already done it for a cached + // version of this device, and the cached version isn't + // "significantly" different, i.e location and cachelifetime + // haven't changed. + var raiseEvent = false; + + if (!NotificationTypeMatchesFilter(device)) return; + + lock (_SearchResultsSynchroniser) + { + if (_SearchResults != null) + { + var existingDevice = FindExistingDeviceNotification(_SearchResults, device.NotificationType, device.Usn); + if (existingDevice == null) + { + _SearchResults.Add(device); + raiseEvent = true; + } + else + { + if (existingDevice.DescriptionLocation != device.DescriptionLocation || existingDevice.CacheLifetime != device.CacheLifetime) + { + _SearchResults.Remove(existingDevice); + _SearchResults.Add(device); + raiseEvent = true; + } + } + } + else + raiseEvent = true; + } + + if (raiseEvent) + OnDeviceAvailable(device, isNewDevice); + } + + private bool NotificationTypeMatchesFilter(DiscoveredSsdpDevice device) + { + return String.IsNullOrEmpty(this.NotificationFilter) + || this.NotificationFilter == SsdpConstants.SsdpDiscoverAllSTHeader + || device.NotificationType == this.NotificationFilter; + } + + #endregion + + #region Network Message Processing + + private static byte[] BuildDiscoverMessage(string serviceType, TimeSpan mxValue, string multicastLocalAdminAddress) + { + return System.Text.UTF8Encoding.UTF8.GetBytes( + String.Format(HttpURequestMessageFormat, + SsdpConstants.MSearchMethod, + multicastLocalAdminAddress, + SsdpConstants.MulticastPort, + SsdpConstants.SsdpDiscoverMessage, + serviceType, + mxValue.TotalSeconds + ) + ); + } + + private void BroadcastDiscoverMessage(string serviceType, TimeSpan mxValue) + { + var multicastIpAddress = _CommunicationsServer.DeviceNetworkType.GetMulticastIPAddress(); + + var multicastMessage = BuildDiscoverMessage(serviceType, mxValue, multicastIpAddress); + + _CommunicationsServer.SendMessage(multicastMessage, new UdpEndPoint + { + IPAddress = multicastIpAddress, + Port = SsdpConstants.MulticastPort + }); + } + + private void ProcessSearchResponseMessage(HttpResponseMessage message) + { + if (!message.IsSuccessStatusCode) return; + + var location = GetFirstHeaderUriValue("Location", message); + if (location != null) + { + var device = new DiscoveredSsdpDevice() + { + DescriptionLocation = location, + Usn = GetFirstHeaderStringValue("USN", message), + NotificationType = GetFirstHeaderStringValue("ST", message), + CacheLifetime = CacheAgeFromHeader(message.Headers.CacheControl), + AsAt = DateTimeOffset.Now, + ResponseHeaders = message.Headers + }; + + AddOrUpdateDiscoveredDevice(device); + } + } + + private void ProcessNotificationMessage(HttpRequestMessage message) + { + if (String.Compare(message.Method.Method, "Notify", StringComparison.OrdinalIgnoreCase) != 0) return; + + var notificationType = GetFirstHeaderStringValue("NTS", message); + if (String.Compare(notificationType, SsdpConstants.SsdpKeepAliveNotification, StringComparison.OrdinalIgnoreCase) == 0) + ProcessAliveNotification(message); + else if (String.Compare(notificationType, SsdpConstants.SsdpByeByeNotification, StringComparison.OrdinalIgnoreCase) == 0) + ProcessByeByeNotification(message); + } + + private void ProcessAliveNotification(HttpRequestMessage message) + { + var location = GetFirstHeaderUriValue("Location", message); + if (location != null) + { + var device = new DiscoveredSsdpDevice() + { + DescriptionLocation = location, + Usn = GetFirstHeaderStringValue("USN", message), + NotificationType = GetFirstHeaderStringValue("NT", message), + CacheLifetime = CacheAgeFromHeader(message.Headers.CacheControl), + AsAt = DateTimeOffset.Now, + ResponseHeaders = message.Headers + }; + + AddOrUpdateDiscoveredDevice(device); + + ResetExpireCachedDevicesTimer(); + } + } + + private void ProcessByeByeNotification(HttpRequestMessage message) + { + var notficationType = GetFirstHeaderStringValue("NT", message); + if (!String.IsNullOrEmpty(notficationType)) + { + var usn = GetFirstHeaderStringValue("USN", message); + + if (!DeviceDied(usn, false)) + { + var deadDevice = new DiscoveredSsdpDevice() + { + AsAt = DateTime.UtcNow, + CacheLifetime = TimeSpan.Zero, + DescriptionLocation = null, + NotificationType = GetFirstHeaderStringValue("NT", message), + Usn = usn, + ResponseHeaders = message.Headers + }; + + if (NotificationTypeMatchesFilter(deadDevice)) + OnDeviceUnavailable(deadDevice, false); + } + + ResetExpireCachedDevicesTimer(); + } + } + + private void ResetExpireCachedDevicesTimer() + { + if (IsDisposed) return; + + if (_ExpireCachedDevicesTimer == null) + _ExpireCachedDevicesTimer = new Timer(this.ExpireCachedDevices, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); + + _ExpireCachedDevicesTimer.Change(60000, System.Threading.Timeout.Infinite); + } + + private void ExpireCachedDevices(object state) + { + RemoveExpiredDevicesFromCache(); + } + + #region Header/Message Processing Utilities + + private static string GetFirstHeaderStringValue(string headerName, HttpResponseMessage message) + { + string retVal = null; + IEnumerable values; + if (message.Headers.Contains(headerName)) + { + message.Headers.TryGetValues(headerName, out values); + if (values != null) + retVal = values.FirstOrDefault(); + } + + return retVal; + } + + private static string GetFirstHeaderStringValue(string headerName, HttpRequestMessage message) + { + string retVal = null; + IEnumerable values; + if (message.Headers.Contains(headerName)) + { + message.Headers.TryGetValues(headerName, out values); + if (values != null) + retVal = values.FirstOrDefault(); + } + + return retVal; + } + + private static Uri GetFirstHeaderUriValue(string headerName, HttpRequestMessage request) + { + string value = null; + IEnumerable values; + if (request.Headers.Contains(headerName)) + { + request.Headers.TryGetValues(headerName, out values); + if (values != null) + value = values.FirstOrDefault(); + } + + Uri retVal; + Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out retVal); + return retVal; + } + + private static Uri GetFirstHeaderUriValue(string headerName, HttpResponseMessage response) + { + string value = null; + IEnumerable values; + if (response.Headers.Contains(headerName)) + { + response.Headers.TryGetValues(headerName, out values); + if (values != null) + value = values.FirstOrDefault(); + } + + Uri retVal; + Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out retVal); + return retVal; + } + + private static TimeSpan CacheAgeFromHeader(System.Net.Http.Headers.CacheControlHeaderValue headerValue) + { + if (headerValue == null) return TimeSpan.Zero; + + return (TimeSpan)(headerValue.MaxAge ?? headerValue.SharedMaxAge ?? TimeSpan.Zero); + } + + #endregion + + #endregion + + #region Expiry and Device Removal + + private Task RemoveExpiredDevicesFromCacheAsync() + { + return TaskEx.Run(() => + { + RemoveExpiredDevicesFromCache(); + }); + } + + private void RemoveExpiredDevicesFromCache() + { + if (this.IsDisposed) return; + + IEnumerable expiredDevices = null; + lock (_Devices) + { + expiredDevices = (from device in _Devices where device.IsExpired() select device).ToArray(); + + foreach (var device in expiredDevices) + { + if (this.IsDisposed) return; + + _Devices.Remove(device); + } + } + + // Don't do this inside lock because DeviceDied raises an event + // which means public code may execute during lock and cause + // problems. + foreach (var expiredUsn in (from expiredDevice in expiredDevices select expiredDevice.Usn).Distinct()) + { + if (this.IsDisposed) return; + + DeviceDied(expiredUsn, true); + } + } + + private IEnumerable GetUnexpiredDevices() + { + lock (_Devices) + { + return (from device in _Devices where !device.IsExpired() select device).ToArray(); + } + } + + private bool DeviceDied(string deviceUsn, bool expired) + { + IEnumerable existingDevices = null; + lock (_Devices) + { + existingDevices = FindExistingDeviceNotifications(_Devices, deviceUsn); + foreach (var existingDevice in existingDevices) + { + if (this.IsDisposed) return true; + + _Devices.Remove(existingDevice); + } + } + + if (existingDevices != null && existingDevices.Any()) + { + lock (_SearchResultsSynchroniser) + { + if (_SearchResults != null) + { + var resultsToRemove = (from result in _SearchResults where result.Usn == deviceUsn select result).ToArray(); + foreach (var result in resultsToRemove) + { + if (this.IsDisposed) return true; + + _SearchResults.Remove(result); + } + } + } + + foreach (var removedDevice in existingDevices) + { + if (NotificationTypeMatchesFilter(removedDevice)) + OnDeviceUnavailable(removedDevice, expired); + } + + return true; + } + + return false; + } + + #endregion + + private static TimeSpan SearchTimeToMXValue(TimeSpan searchWaitTime) + { + if (searchWaitTime.TotalSeconds < 2 || searchWaitTime == TimeSpan.Zero) + return OneSecond; + else + return searchWaitTime.Subtract(OneSecond); + } + + private static DiscoveredSsdpDevice FindExistingDeviceNotification(IEnumerable devices, string notificationType, string usn) + { + return (from d in devices where d.NotificationType == notificationType && d.Usn == usn select d).FirstOrDefault(); + } + + private static IEnumerable FindExistingDeviceNotifications(IList devices, string usn) + { + return (from d in devices where d.Usn == usn select d).ToArray(); + } + + #endregion + + #region Event Handlers + + private void CommsServer_ResponseReceived(object sender, ResponseReceivedEventArgs e) + { + ProcessSearchResponseMessage(e.Message); + } + + private void CommsServer_RequestReceived(object sender, RequestReceivedEventArgs e) + { + ProcessNotificationMessage(e.Message); + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpDeviceProperties.cs b/src/Rssdp.Shared/SsdpDeviceProperties.cs new file mode 100644 index 0000000..850dfb0 --- /dev/null +++ b/src/Rssdp.Shared/SsdpDeviceProperties.cs @@ -0,0 +1,206 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Represents a collection of instances keyed by the property value. + /// + /// + /// Items added to this collection are keyed by their property value, at the time they were added. If the name changes after they were added to the collection, the key is not updated unless the item is manually removed and re-added to the collection. + /// + public class SsdpDevicePropertiesCollection : IEnumerable + { + + #region Fields + + private IDictionary _Properties; + + #endregion + + #region Constructors + + /// + /// Default constructor. + /// + public SsdpDevicePropertiesCollection() + { + _Properties = new Dictionary(); + } + + /// + /// Full constructor. + /// + /// Specifies the initial capacity of the collection. + public SsdpDevicePropertiesCollection(int capacity) + { + _Properties = new Dictionary(capacity); + } + + #endregion + + #region Public Methpds + + /// + /// Adds a instance to the collection. + /// + /// The property instance to add to the collection. + /// + /// + /// + /// Thrown if is null. + /// Thrown if the property of the argument is null or empty string, or if the collection already contains an item with the same key. + public void Add(SsdpDeviceProperty customDeviceProperty) + { + if (customDeviceProperty == null) throw new ArgumentNullException("customDeviceProperty"); + if (String.IsNullOrEmpty(customDeviceProperty.FullName)) throw new ArgumentException("customDeviceProperty.FullName cannot be null or empty."); + + lock (_Properties) + { + _Properties.Add(customDeviceProperty.FullName, customDeviceProperty); + } + } + + #region Remove Overloads + + /// + /// Removes the specified property instance from the collection. + /// + /// The instance to remove from the collection. + /// + /// Only remove the specified property if that instance was in the collection, if another property with the same full name exists in the collection it is not removed. + /// + /// True if an item was removed from the collection, otherwise false (because it did not exist or was not the same instance). + /// + /// Thrown if the is null. + /// Thrown if the property of the argument is null or empty string, or if the collection already contains an item with the same key. + public bool Remove(SsdpDeviceProperty customDeviceProperty) + { + if (customDeviceProperty == null) throw new ArgumentNullException("customDeviceProperty"); + if (String.IsNullOrEmpty(customDeviceProperty.FullName)) throw new ArgumentException("customDeviceProperty.FullName cannot be null or empty."); + + lock (_Properties) + { + if (_Properties.ContainsKey(customDeviceProperty.FullName) && _Properties[customDeviceProperty.FullName] == customDeviceProperty) + return _Properties.Remove(customDeviceProperty.FullName); + } + + return false; + } + + /// + /// Removes the property with the specified key ( from the collection. + /// + /// The full name of the instance to remove from the collection. + /// True if an item was removed from the collection, otherwise false (because no item exists in the collection with that key). + /// Thrown if the argument is null or empty string. + public bool Remove(string customDevicePropertyFullName) + { + if (String.IsNullOrEmpty(customDevicePropertyFullName)) throw new ArgumentException("customDevicePropertyFullName cannot be null or empty."); + + lock (_Properties) + { + return _Properties.Remove(customDevicePropertyFullName); + } + } + + #endregion + + /// + /// Returns a boolean indicating whether or not the specified instance is in the collection. + /// + /// An instance to check the collection for. + /// True if the specified instance exists in the collection, otherwise false. + public bool Contains(SsdpDeviceProperty customDeviceProperty) + { + if (customDeviceProperty == null) throw new ArgumentNullException("customDeviceProperty"); + if (String.IsNullOrEmpty(customDeviceProperty.FullName)) throw new ArgumentException("customDeviceProperty.FullName cannot be null or empty."); + + lock (_Properties) + { + if (_Properties.ContainsKey(customDeviceProperty.FullName)) + return _Properties[customDeviceProperty.FullName] == customDeviceProperty; + } + + return false; + } + + /// + /// Returns a boolean indicating whether or not a instance with the specified full name value exists in the collection. + /// + /// A string containing the full name of the instance to check for. + /// True if an item with the specified full name exists in the collection, otherwise false. + public bool Contains(string customDevicePropertyFullName) + { + if (String.IsNullOrEmpty(customDevicePropertyFullName)) throw new ArgumentException("customDevicePropertyFullName cannot be null or empty."); + + lock (_Properties) + { + return _Properties.ContainsKey(customDevicePropertyFullName); + } + } + + #endregion + + #region Public Properties + + /// + /// Returns the number of items in the collection. + /// + public int Count + { + get { return _Properties.Count; } + } + + /// + /// Returns the instance from the collection that has the specified value. + /// + /// The full name of the property to return. + /// A instance from the collection. + /// Thrown if no item exists in the collection with the specified value. + public SsdpDeviceProperty this[string fullName] + { + get + { + return _Properties[fullName]; + } + } + + #endregion + + #region IEnumerable Members + + /// + /// Returns an enumerator of instances in this collection. + /// + /// An enumerator of instances in this collection. + public IEnumerator GetEnumerator() + { + lock (_Properties) + { + return _Properties.Values.GetEnumerator(); + } + } + + #endregion + + #region IEnumerable Members + + /// + /// Returns an enumerator of instances in this collection. + /// + /// An enumerator of instances in this collection. + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + lock (_Properties) + { + return _Properties.Values.GetEnumerator(); + } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpDeviceProperty.cs b/src/Rssdp.Shared/SsdpDeviceProperty.cs new file mode 100644 index 0000000..3a8dd2e --- /dev/null +++ b/src/Rssdp.Shared/SsdpDeviceProperty.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + /// + /// Represents a custom property of an . + /// + public sealed class SsdpDeviceProperty + { + + /// + /// Sets or returns the namespace this property exists in. + /// + public string Namespace { get; set; } + + /// + /// Sets or returns the name of this property. + /// + public string Name { get; set; } + + /// + /// Returns the full name of this property (namespace and name). + /// + public string FullName { get { return String.IsNullOrEmpty(this.Namespace) ? this.Name : this.Namespace + ":" + this.Name; } } + + /// + /// Sets or returns the value of this property. + /// + public string Value { get; set; } + + } +} diff --git a/src/Rssdp.Shared/SsdpDevicePublisherBase.cs b/src/Rssdp.Shared/SsdpDevicePublisherBase.cs new file mode 100644 index 0000000..05c5cd0 --- /dev/null +++ b/src/Rssdp.Shared/SsdpDevicePublisherBase.cs @@ -0,0 +1,1078 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Provides the platform independent logic for publishing SSDP devices (notifications and search responses). + /// + public abstract class SsdpDevicePublisherBase : DisposableManagedObjectBase, ISsdpDevicePublisher + { + + #region Fields & Constants + + private ISsdpCommunicationsServer _CommsServer; + private string _OSName; + private string _OSVersion; + + private bool _SupportPnpRootDevice; + private SsdpStandardsMode _StandardsMode; + + private IList _Devices; + private ReadOnlyEnumerable _ReadOnlyDevices; + + private System.Threading.Timer _RebroadcastAliveNotificationsTimer; + private TimeSpan _RebroadcastAliveNotificationsTimeSpan; + private DateTime _LastNotificationTime; + + private IDictionary _RecentSearchRequests; + private IUpnpDeviceValidator _DeviceValidator; + + private Random _Random; + private TimeSpan _MinCacheTime; + private TimeSpan _NotificationBroadcastInterval; + + private const string ServerVersion = "1.0"; + + #endregion + + #region Message Format Constants + + private const string DeviceSearchResponseMessageFormat = @"HTTP/1.1 200 OK +EXT: +DATE: {7} +{0} +ST:{1} +SERVER: {4}/{5} UPnP/1.0 RSSDP/{6} +USN:{2} +LOCATION:{3}{8} + +"; //Blank line at end important, do not remove. + + + private const string AliveNotificationMessageFormat = @"NOTIFY * HTTP/1.1 +HOST: {8}:{9} +DATE: {7} +NT: {0} +NTS: ssdp:alive +SERVER: {4}/{5} UPnP/1.0 RSSDP/{6} +USN: {1} +LOCATION: {2} +{3}{10} + +"; //Blank line at end important, do not remove. + + private const string ByeByeNotificationMessageFormat = @"NOTIFY * HTTP/1.1 +HOST: {6}:{7} +DATE: {5} +NT: {0} +NTS: ssdp:byebye +SERVER: {2}/{3} UPnP/1.0 RSSDP/{4} +USN: {1} + +"; + + #endregion + + #region Constructors + + /// + /// Default constructor. + /// + /// The implementation, used to send and receive SSDP network messages. + /// Then name of the operating system running the server. + /// The version of the operating system running the server. + protected SsdpDevicePublisherBase(ISsdpCommunicationsServer communicationsServer, string osName, string osVersion) : this(communicationsServer, osName, osVersion, NullLogger.Instance) + { + } + + /// + /// Full constructor. + /// + /// The implementation, used to send and receive SSDP network messages. + /// Then name of the operating system running the server. + /// The version of the operating system running the server. + /// An implementation of to be used for logging activity. May be null, in which case no logging is performed. + protected SsdpDevicePublisherBase(ISsdpCommunicationsServer communicationsServer, string osName, string osVersion, ISsdpLogger log) + { + if (communicationsServer == null) throw new ArgumentNullException("communicationsServer"); + if (osName == null) throw new ArgumentNullException("osName"); + if (osName.Length == 0) throw new ArgumentException("osName cannot be an empty string.", "osName"); + if (osVersion == null) throw new ArgumentNullException("osVersion"); + if (osVersion.Length == 0) throw new ArgumentException("osVersion cannot be an empty string.", "osName"); + + Log = log ?? NullLogger.Instance; + _SupportPnpRootDevice = true; + _Devices = new List(); + _ReadOnlyDevices = new ReadOnlyEnumerable(_Devices); + _RecentSearchRequests = new Dictionary(StringComparer.OrdinalIgnoreCase); + _Random = new Random(); + _DeviceValidator = new Upnp10DeviceValidator(); //Should probably inject this later, but for now we only support 1.0. + + _CommsServer = communicationsServer; + _CommsServer.RequestReceived += CommsServer_RequestReceived; + _OSName = osName; + _OSVersion = osVersion; + + Log.LogInfo("Publisher started."); + _CommsServer.BeginListeningForBroadcasts(); + Log.LogInfo("Publisher started listening for broadcasts."); + } + + #endregion + + #region Public Methods + + /// + /// Adds a device (and it's children) to the list of devices being published by this server, making them discoverable to SSDP clients. + /// + /// + /// Adding a device causes "alive" notification messages to be sent immediately, or very soon after. Ensure your device/description service is running before adding the device object here. + /// Devices added here with a non-zero cache life time will also have notifications broadcast periodically. + /// This method ignores duplicate device adds (if the same device instance is added multiple times, the second and subsequent add calls do nothing). + /// + /// The instance to add. + /// Thrown if the argument is null. + /// Thrown if the contains property values that are not acceptable to the UPnP 1.0 specification. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capture task to local variable supresses compiler warning, but task is not really needed.")] + public void AddDevice(SsdpRootDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + + ThrowIfDisposed(); + + _DeviceValidator.ThrowIfDeviceInvalid(device); + + TimeSpan minCacheTime = TimeSpan.Zero; + bool wasAdded = false; + lock (_Devices) + { + if (!_Devices.Contains(device)) + { + _Devices.Add(device); + wasAdded = true; + minCacheTime = GetMinimumNonZeroCacheLifetime(); + } + } + + if (wasAdded) + { + LogDeviceEvent("Device added", device); + + _MinCacheTime = minCacheTime; + + ConnectToDeviceEvents(device); + + SetRebroadcastAliveNotificationsTimer(minCacheTime); + + SendAliveNotifications(device, true); + } + else + LogDeviceEventWarning("AddDevice ignored (duplicate add)", device); + } + + /// + /// Removes a device (and it's children) from the list of devices being published by this server, making them undiscoverable. + /// + /// + /// Removing a device causes "byebye" notification messages to be sent immediately, advising clients of the device/service becoming unavailable. We recommend removing the device from the published list before shutting down the actual device/service, if possible. + /// This method does nothing if the device was not found in the collection. + /// + /// The instance to add. + /// Thrown if the argument is null. + public void RemoveDevice(SsdpRootDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + + ThrowIfDisposed(); + + bool wasRemoved = false; + TimeSpan minCacheTime = TimeSpan.Zero; + lock (_Devices) + { + if (_Devices.Contains(device)) + { + _Devices.Remove(device); + wasRemoved = true; + minCacheTime = GetMinimumNonZeroCacheLifetime(); + } + } + + if (wasRemoved) + { + _MinCacheTime = minCacheTime; + + DisconnectFromDeviceEvents(device); + + LogDeviceEvent("Device Removed", device); + + SendByeByeNotifications(device, true); + + SetRebroadcastAliveNotificationsTimer(minCacheTime); + } + else + LogDeviceEventWarning("RemoveDevice ignored (device not in publisher)", device); + } + + #endregion + + #region Public Properties + + /// + /// Returns a reference to the injected instance. + /// + /// + /// Should never return null. If null was injected a reference to an internal null logger should be returned. + /// + public ISsdpLogger Log { get; set; } + + /// + /// Returns a read only list of devices being published by this instance. + /// + public IEnumerable Devices + { + get + { + return _ReadOnlyDevices; + } + } + + /// + /// If true (default) treats root devices as both upnp:rootdevice and pnp:rootdevice types. + /// + /// + /// Enabling this option will cause devices to show up in Microsoft Windows Explorer's network screens (if discovery is enabled etc.). Windows Explorer appears to search only for pnp:rootdeivce and not upnp:rootdevice. + /// If false, the system will only use upnp:rootdevice for notifiation broadcasts and and search responses, which is correct according to the UPnP/SSDP spec. + /// + [Obsolete("Set StandardsMode to SsdpStandardsMode.Relaxed instead.")] + public bool SupportPnpRootDevice + { + get { return _SupportPnpRootDevice; } + set + { + if (_SupportPnpRootDevice != value) + { + _SupportPnpRootDevice = value; + Log.LogInfo("SupportPnpRootDevice set to " + value.ToString()); + } + } + } + + /// + /// Sets or returns a value from the controlling how strictly the publisher obeys the SSDP standard. + /// + /// + /// Using relaxed mode will process search requests even if the MX header is missing. + /// + public SsdpStandardsMode StandardsMode + { + get { return _StandardsMode; } + set + { + if (_StandardsMode != value) + { + _StandardsMode = value; + Log.LogInfo("StandardsMode set to " + value.ToString()); + } + } + } + + /// + /// Sets or returns a fixed interval at which alive notifications for services exposed by this publisher instance are broadcast. + /// + /// + /// If this is set to then the system will follow the process recommended + /// by the SSDP spec and calculate a randomised interval based on the cache life times of the published services. + /// The default and recommended value is TimeSpan.Zero. + /// + /// While (zero and) any positive value are allowed, the SSDP specification says + /// notifications should not be broadcast more often than 15 minutes. If you wish to remain compatible with the SSDP + /// specification, do not set this property to a value greater than zero but less than 15 minutes. + /// + /// + public TimeSpan NotificationBroadcastInterval + { + get { return _NotificationBroadcastInterval; } + set + { + if (value.TotalSeconds < 0) throw new ArgumentException("Cannot be less than zero.", nameof(value)); + + if (_NotificationBroadcastInterval != value) + { + _NotificationBroadcastInterval = value; + Log.LogInfo("NotificationBroadcastInterval set to " + value.ToString()); + SetRebroadcastAliveNotificationsTimer(_MinCacheTime); + } + } + } + + #endregion + + #region Overrides + + /// + /// Stops listening for requests, stops sending periodic broadcasts, disposes all internal resources. + /// + /// + protected override void Dispose(bool disposing) + { + if (disposing) + { + Log.LogInfo("Publisher disposed."); + + DisposeRebroadcastTimer(); + + var commsServer = _CommsServer; + _CommsServer = null; + + if (commsServer != null) + { + commsServer.RequestReceived -= this.CommsServer_RequestReceived; + if (!commsServer.IsShared) + commsServer.Dispose(); + } + + foreach (var device in this.Devices) + { + DisconnectFromDeviceEvents(device); + } + + _RecentSearchRequests = null; + } + } + + #endregion + + #region Private Methods + + #region Search Related Methods + + private void ProcessSearchRequest(string mx, string searchTarget, UdpEndPoint endPoint) + { + if (String.IsNullOrEmpty(searchTarget)) + { + Log.LogWarning(String.Format("Invalid search request received From {0}, Target is null/empty.", endPoint.ToString())); + return; + } + + Log.LogInfo(String.Format("Search Request Received From {0}, Target = {1}", endPoint.ToString(), searchTarget)); + + if (IsDuplicateSearchRequest(searchTarget, endPoint)) + { + Log.LogWarning("Search Request is Duplicate, ignoring."); + return; + } + + //Wait on random interval up to MX, as per SSDP spec. + //Also, as per UPnP 1.1/SSDP spec ignore missing/bank MX header (strict mode only). If over 120, assume random value between 0 and 120. + //Using 16 as minimum as that's often the minimum system clock frequency anyway. + int maxWaitInterval = 0; + if (String.IsNullOrEmpty(mx)) + { + //Windows Explorer is poorly behaved and doesn't supply an MX header value. + if (IsWindowsExplorerSupportEnabled) + mx = "1"; + else + { + Log.LogWarning("Search Request ignored due to missing MX header. Set StandardsMode to relaxed to respond to these requests."); + return; + } + } + + if (!Int32.TryParse(mx, out maxWaitInterval) || maxWaitInterval <= 0) return; + + if (maxWaitInterval > 120) + maxWaitInterval = _Random.Next(0, 120); + + //Do not block synchronously as that may tie up a threadpool thread for several seconds. + TaskEx.Delay(_Random.Next(16, (maxWaitInterval * 1000))).ContinueWith((parentTask) => + { + //Copying devices to local array here to avoid threading issues/enumerator exceptions. + IEnumerable devices = null; + devices = GetDevicesMatchingSearchTarget(searchTarget, devices); + + if (devices != null) + SendSearchResponses(searchTarget, endPoint, devices); + else + Log.LogWarning("Sending search responses for 0 devices (no matching targets)."); + }); + } + + private IEnumerable GetDevicesMatchingSearchTarget(string searchTarget, IEnumerable devices) + { + lock (_Devices) + { + if (Devices.Any(x => x.NotificationType == searchTarget)) + devices = Devices.Where(x => x.NotificationType == searchTarget).ToArray(); + else if (String.Compare(SsdpConstants.SsdpDiscoverAllSTHeader, searchTarget, + StringComparison.OrdinalIgnoreCase) == 0) + devices = GetAllDevicesAsFlatEnumerable().ToArray(); + else if (String.Compare(SsdpConstants.UpnpDeviceTypeRootDevice, searchTarget, + StringComparison.OrdinalIgnoreCase) == 0 || + (IsWindowsExplorerSupportEnabled && String.Compare(SsdpConstants.PnpDeviceTypeRootDevice, searchTarget, + StringComparison.OrdinalIgnoreCase) == 0)) + devices = _Devices.ToArray(); + else if (searchTarget.Trim().StartsWith("uuid:", StringComparison.OrdinalIgnoreCase)) + { + devices = ( + from device + in GetAllDevicesAsFlatEnumerable() + where String.Compare(device.Uuid, searchTarget.Substring(5), StringComparison.OrdinalIgnoreCase) == 0 + select device + ).ToArray(); + } + else if (searchTarget.StartsWith("urn:", StringComparison.OrdinalIgnoreCase)) + { + if (searchTarget.Contains(":service:")) + { + devices = + ( + from device in GetAllDevicesAsFlatEnumerable() + where + ( + from s in + device.Services + where String.Compare(s.FullServiceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 + select s + ).Any() + select device + ).ToArray(); + } + else + { + devices = + ( + from device + in GetAllDevicesAsFlatEnumerable() + where String.Compare(device.FullDeviceType, searchTarget, StringComparison.OrdinalIgnoreCase) == 0 + select device + ).ToArray(); + } + } + } + + return devices; + } + + private bool IsWindowsExplorerSupportEnabled + { + get + { +#pragma warning disable CS0618 // Type or member is obsolete + return SupportPnpRootDevice || IsRelaxedStandardsMode; +#pragma warning restore CS0618 // Type or member is obsolete + } + } + + private bool IsRelaxedStandardsMode + { + get + { + return this.StandardsMode != SsdpStandardsMode.Strict; + } + } + + private IEnumerable GetAllDevicesAsFlatEnumerable() + { + return _Devices.Union(_Devices.SelectManyRecursive((d) => d.Devices)); + } + + private void SendSearchResponses(string searchTarget, UdpEndPoint endPoint, IEnumerable devices) + { + Log.LogInfo(String.Format("Sending search (target = {1}) responses for {0} devices", devices.Count(), searchTarget)); + + if (searchTarget.Contains(":service:")) + { + foreach (var device in devices) + { + SendServiceSearchResponses(device, searchTarget, endPoint); + } + } + else + { + foreach (var device in devices) + { + SendDeviceSearchResponses(device, searchTarget, endPoint); + } + } + } + + private void SendDeviceSearchResponses(SsdpDevice device, string searchTarget, UdpEndPoint endPoint) + { + //http://www.upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.0-20080424.pdf - page 21 + //For ssdp:all - Respond 3+2d+k times for a root device with d embedded devices and s embedded services but only k distinct service types + //Root devices - Respond once (special handling when in related/Win Explorer support mode) + //Udn (uuid) - Response once + //Device type - response once + //Service type - respond once per service type + + bool isRootDevice = (device as SsdpRootDevice) != null; + bool sendAll = searchTarget == SsdpConstants.SsdpDiscoverAllSTHeader; + bool sendRootDevices = searchTarget == SsdpConstants.UpnpDeviceTypeRootDevice || searchTarget == SsdpConstants.PnpDeviceTypeRootDevice; + + if (isRootDevice && device.NotificationType == searchTarget) + { + SendSearchResponse(device.NotificationType, device, device.Usn ?? GetUsn(device.Udn, searchTarget), endPoint); + } + else if (isRootDevice && (sendAll || sendRootDevices)) + { + SendSearchResponse(device.NotificationType ?? SsdpConstants.UpnpDeviceTypeRootDevice, device, device.Usn ?? GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice), endPoint); + if (IsWindowsExplorerSupportEnabled) + SendSearchResponse(device.NotificationType ?? SsdpConstants.PnpDeviceTypeRootDevice, device, device.Usn ?? GetUsn(device.Udn, SsdpConstants.PnpDeviceTypeRootDevice), endPoint); + } + + if (sendAll || searchTarget.StartsWith("uuid:", StringComparison.Ordinal)) + SendSearchResponse(device.Udn, device, device.Udn, endPoint); + + if (sendAll || searchTarget.Contains(":device:")) + SendSearchResponse(device.FullDeviceType, device, GetUsn(device.Udn, device.FullDeviceType), endPoint); + + if (searchTarget == SsdpConstants.SsdpDiscoverAllSTHeader) + { + //Send 1 search response for each unique service type for all devices found + var serviceTypes = + ( + from s + in device.Services + select s.FullServiceType + ).Distinct().ToArray(); + + foreach (var st in serviceTypes) + { + SendServiceSearchResponses(device, st, endPoint); + } + } + } + + private void SendServiceSearchResponses(SsdpDevice device, string searchTarget, UdpEndPoint endPoint) + { + //uuid:device-UUID::urn:domain-name:service:serviceType:ver + SendSearchResponse(searchTarget, device, device.Udn + "::" + searchTarget, endPoint); + } + + private static string GetUsn(string udn, string fullDeviceType) + { + return String.Format("{0}::{1}", udn, fullDeviceType); + } + + private void SendSearchResponse(string searchTarget, SsdpDevice device, string uniqueServiceName, UdpEndPoint endPoint) + { + var rootDevice = device.ToRootDevice(); + + var additionalheaders = FormatCustomHeadersForResponse(device); + + var message = String.Format(DeviceSearchResponseMessageFormat, + CacheControlHeaderFromTimeSpan(rootDevice), + searchTarget, + uniqueServiceName, + rootDevice.Location, + _OSName, + _OSVersion, + ServerVersion, + DateTime.UtcNow.ToString("r"), + additionalheaders + ); + + _CommsServer.SendMessage(System.Text.UTF8Encoding.UTF8.GetBytes(message), endPoint); + + LogDeviceEventVerbose(String.Format("Sent search response ({0}) to {1}", uniqueServiceName, endPoint.ToString()), device); + } + + private bool IsDuplicateSearchRequest(string searchTarget, UdpEndPoint endPoint) + { + var isDuplicateRequest = false; + + var newRequest = new SearchRequest() { EndPoint = endPoint, SearchTarget = searchTarget, Received = DateTime.UtcNow }; + lock (_RecentSearchRequests) + { + if (_RecentSearchRequests.ContainsKey(newRequest.Key)) + { + var lastRequest = _RecentSearchRequests[newRequest.Key]; + if (lastRequest.IsOld()) + _RecentSearchRequests[newRequest.Key] = newRequest; + else + isDuplicateRequest = true; + } + else + { + _RecentSearchRequests.Add(newRequest.Key, newRequest); + if (_RecentSearchRequests.Count > 10) + CleanUpRecentSearchRequestsAsync(); + } + } + + return isDuplicateRequest; + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capturing task to local variable avoids compiler warning, but value is otherwise not required.")] + private void CleanUpRecentSearchRequestsAsync() + { + var t = TaskEx.Run(() => + { + lock (_RecentSearchRequests) + { + foreach (var requestKey in (from r in _RecentSearchRequests where r.Value.IsOld() select r.Key).ToArray()) + { + _RecentSearchRequests.Remove(requestKey); + } + } + }); + } + + #endregion + + #region Notification Related Methods + + #region Alive + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] + private void SendAllAliveNotifications(object state) + { + try + { + if (IsDisposed) return; + + try + { + //Only dispose the timer so it gets re-created if we're following + //the SSDP Spec and randomising the broadcast interval. + //If we're using a fixed interval, no need to kill the timer as it's + //already set to go off on the correct interval. + if (_NotificationBroadcastInterval == TimeSpan.Zero) + DisposeRebroadcastTimer(); + } + finally + { + // Must reset this here, otherwise if the next reset interval + // is calculated to be the same as the previous one we won't + // reset the timer. + // Reset it to _NotificationBroadcastInterval which is either TimeSpan.Zero + // which will cause the system to calculate a new random interval, or it's the + // current fixed interval which is fine. + _RebroadcastAliveNotificationsTimeSpan = _NotificationBroadcastInterval; + } + + Log.LogInfo("Sending Alive Notifications For All Devices"); + + _LastNotificationTime = DateTime.Now; + + IEnumerable devices; + lock (_Devices) + { + devices = _Devices.ToArray(); + } + + foreach (var device in devices) + { + if (IsDisposed) return; + + SendAliveNotifications(device, true); + } + } + catch (Exception ex) + { + Log.LogError("Publisher stopped, exception " + ex.Message); + Dispose(); + } + finally + { + if (!this.IsDisposed) + SetRebroadcastAliveNotificationsTimer(_MinCacheTime); + } + } + + private void SendAliveNotifications(SsdpDevice device, bool isRoot) + { + if (isRoot) + { + SendAliveNotification(device, device.NotificationType ?? SsdpConstants.UpnpDeviceTypeRootDevice, device.Usn ?? GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice)); +#pragma warning disable CS0618 // Type or member is obsolete + if (this.SupportPnpRootDevice) +#pragma warning restore CS0618 // Type or member is obsolete + SendAliveNotification(device, SsdpConstants.PnpDeviceTypeRootDevice, GetUsn(device.Udn, SsdpConstants.PnpDeviceTypeRootDevice)); + } + + SendAliveNotification(device, device.Udn, device.Udn); + SendAliveNotification(device, device.FullDeviceType, GetUsn(device.Udn, device.FullDeviceType)); + + foreach (var service in device.Services) + { + SendAliveNotification(device, service); + } + + foreach (var childDevice in device.Devices) + { + SendAliveNotifications(childDevice, false); + } + } + + private void SendAliveNotification(SsdpDevice device, string notificationType, string uniqueServiceName) + { + string multicastIpAddress = _CommsServer.DeviceNetworkType.GetMulticastIPAddress(); + + var multicastMessage = BuildAliveMessage(device, notificationType, uniqueServiceName, multicastIpAddress); + + _CommsServer.SendMessage(multicastMessage, new UdpEndPoint + { + IPAddress = multicastIpAddress, + Port = SsdpConstants.MulticastPort + }); + + LogDeviceEvent(String.Format("Sent alive notification NT={0}, USN={1}", notificationType, uniqueServiceName), device); + } + + private void SendAliveNotification(SsdpDevice device, SsdpService service) + { + SendAliveNotification(device, service.FullServiceType, device.Udn + "::" + service.FullServiceType); + } + + private byte[] BuildAliveMessage(SsdpDevice device, string notificationType, string uniqueServiceName, string hostAddress) + { + var rootDevice = device.ToRootDevice(); + + var additionalheaders = FormatCustomHeadersForResponse(device); + + return System.Text.UTF8Encoding.UTF8.GetBytes + ( + String.Format + ( + AliveNotificationMessageFormat, + notificationType, + uniqueServiceName, + rootDevice.Location, + CacheControlHeaderFromTimeSpan(rootDevice), + _OSName, + _OSVersion, + ServerVersion, + DateTime.UtcNow.ToString("r"), + hostAddress, + SsdpConstants.MulticastPort, + additionalheaders + ) + ); + } + + #endregion + + #region ByeBye + + private void SendByeByeNotifications(SsdpDevice device, bool isRoot) + { + if (isRoot) + { + SendByeByeNotification(device, SsdpConstants.UpnpDeviceTypeRootDevice, GetUsn(device.Udn, SsdpConstants.UpnpDeviceTypeRootDevice)); +#pragma warning disable CS0618 // Type or member is obsolete + if (this.SupportPnpRootDevice) +#pragma warning restore CS0618 // Type or member is obsolete + SendByeByeNotification(device, "pnp:rootdevice", GetUsn(device.Udn, "pnp:rootdevice")); + } + + SendByeByeNotification(device, device.Udn, device.Udn); + SendByeByeNotification(device, String.Format("urn:{0}", device.FullDeviceType), GetUsn(device.Udn, device.FullDeviceType)); + + foreach (var service in device.Services) + { + SendByeByeNotification(device, service); + } + + foreach (var childDevice in device.Devices) + { + SendByeByeNotifications(childDevice, false); + } + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "byebye", Justification = "Correct value for this type of notification in SSDP.")] + private void SendByeByeNotification(SsdpDevice device, string notificationType, string uniqueServiceName) + { + string multicastIpAddress = _CommsServer.DeviceNetworkType.GetMulticastIPAddress(); + + var multicastMessage = BuildByeByeMessage(notificationType, uniqueServiceName, multicastIpAddress); + + _CommsServer.SendMessage(multicastMessage, new UdpEndPoint + { + IPAddress = multicastIpAddress, + Port = SsdpConstants.MulticastPort + }); + + LogDeviceEvent(String.Format("Sent byebye notification, NT={0}, USN={1}", notificationType, uniqueServiceName), device); + } + + private void SendByeByeNotification(SsdpDevice device, SsdpService service) + { + SendByeByeNotification(device, service.FullServiceType, device.Udn + "::" + service.FullServiceType); + } + + private byte[] BuildByeByeMessage(string notificationType, string uniqueServiceName, string hostAddress) + { + var message = String.Format(ByeByeNotificationMessageFormat, + notificationType, + uniqueServiceName, + _OSName, + _OSVersion, + ServerVersion, + DateTime.UtcNow.ToString("r"), + hostAddress, + SsdpConstants.MulticastPort + ); + + return System.Text.UTF8Encoding.UTF8.GetBytes(message); + } + + #endregion + + #region Rebroadcast Timer + + private void DisposeRebroadcastTimer() + { + var timer = _RebroadcastAliveNotificationsTimer; + _RebroadcastAliveNotificationsTimer = null; + if (timer != null) + timer.Dispose(); + } + + private void SetRebroadcastAliveNotificationsTimer(TimeSpan minCacheTime) + { + TimeSpan rebroadCastInterval = TimeSpan.Zero; + if (this.NotificationBroadcastInterval != TimeSpan.Zero) + { + if (_RebroadcastAliveNotificationsTimeSpan == this.NotificationBroadcastInterval) return; + + rebroadCastInterval = this.NotificationBroadcastInterval; + } + else + { + if (minCacheTime == _RebroadcastAliveNotificationsTimeSpan) return; + if (minCacheTime == TimeSpan.Zero) return; + + // According to UPnP/SSDP spec, we should randomise the interval at + // which we broadcast notifications, to help with network congestion. + // Specs also advise to choose a random interval up to *half* the cache time. + // Here we do that, but using the minimum non-zero cache time of any device we are publishing. + rebroadCastInterval = new TimeSpan(Convert.ToInt64((_Random.Next(1, 50) / 100D) * (minCacheTime.Ticks / 2))); + } + + DisposeRebroadcastTimer(); + + // If we were already setup to rebroadcast sometime in the future, + // don't just blindly reset the next broadcast time to the new interval + // as repeatedly changing the interval might end up causing us to over + // delay in sending the next one. + var nextBroadcastInterval = rebroadCastInterval; + if (_LastNotificationTime != DateTime.MinValue) + { + nextBroadcastInterval = rebroadCastInterval.Subtract(DateTime.Now.Subtract(_LastNotificationTime)); + if (nextBroadcastInterval.Ticks < 0) + nextBroadcastInterval = TimeSpan.Zero; + else if (nextBroadcastInterval > rebroadCastInterval) + nextBroadcastInterval = rebroadCastInterval; + } + + _RebroadcastAliveNotificationsTimeSpan = rebroadCastInterval; + _RebroadcastAliveNotificationsTimer = new System.Threading.Timer(SendAllAliveNotifications, null, nextBroadcastInterval, rebroadCastInterval); + + Log.LogInfo(String.Format("Rebroadcast Interval = {0}, Next Broadcast At = {1}", rebroadCastInterval.ToString(), nextBroadcastInterval.ToString())); + } + + private TimeSpan GetMinimumNonZeroCacheLifetime() + { + var nonzeroCacheLifetimesQuery = (from device + in _Devices + where device.CacheLifetime != TimeSpan.Zero + select device.CacheLifetime); + + if (nonzeroCacheLifetimesQuery.Any()) + return nonzeroCacheLifetimesQuery.Min(); + else + return TimeSpan.Zero; + } + + #endregion + + #endregion + + private static string GetFirstHeaderValue(System.Net.Http.Headers.HttpRequestHeaders httpRequestHeaders, string headerName) + { + string retVal = null; + IEnumerable values = null; + if (httpRequestHeaders.TryGetValues(headerName, out values) && values != null) + retVal = values.FirstOrDefault(); + + return retVal; + } + + private static string CacheControlHeaderFromTimeSpan(SsdpRootDevice device) + { + if (device.CacheLifetime == TimeSpan.Zero) + return "CACHE-CONTROL: no-cache"; + else + return String.Format("CACHE-CONTROL: public, max-age={0}", device.CacheLifetime.TotalSeconds); + } + + private void LogDeviceEvent(string text, SsdpDevice device) + { + Log.LogInfo(GetDeviceEventLogMessage(text, device)); + } + + private void LogDeviceEventWarning(string text, SsdpDevice device) + { + Log.LogWarning(GetDeviceEventLogMessage(text, device)); + } + + private void LogDeviceEventVerbose(string text, SsdpDevice device) + { + Log.LogVerbose(GetDeviceEventLogMessage(text, device)); + } + + private static string GetDeviceEventLogMessage(string text, SsdpDevice device) + { + var rootDevice = device as SsdpRootDevice; + if (rootDevice != null) + return text + " " + device.DeviceType + " - " + device.Uuid + " - " + rootDevice.Location; + else + return text + " " + device.DeviceType + " - " + device.Uuid; + } + + private void ConnectToDeviceEvents(SsdpDevice device) + { + device.DeviceAdded += device_DeviceAdded; + device.DeviceRemoved += device_DeviceRemoved; + device.ServiceAdded += device_ServiceAdded; + device.ServiceRemoved += device_ServiceRemoved; + + foreach (var childDevice in device.Devices) + { + ConnectToDeviceEvents(childDevice); + } + } + + private void DisconnectFromDeviceEvents(SsdpDevice device) + { + device.DeviceAdded -= device_DeviceAdded; + device.DeviceRemoved -= device_DeviceRemoved; + device.ServiceAdded -= device_ServiceAdded; + device.ServiceRemoved -= device_ServiceRemoved; + + foreach (var childDevice in device.Devices) + { + DisconnectFromDeviceEvents(childDevice); + } + } + + private static string FormatCustomHeadersForResponse(SsdpDevice device) + { + if (device.CustomResponseHeaders.Count == 0) return String.Empty; + + StringBuilder returnValue = new StringBuilder(); + foreach (var header in device.CustomResponseHeaders) + { + returnValue.Append("\r\n"); + + returnValue.Append(header.ToString()); + } + return returnValue.ToString(); + } + + private static bool DeviceHasServiceOfType(SsdpDevice device, string fullServiceType) + { + int retries = 0; + while (retries < 5) + { + try + { + return (from s in device.Services where s.FullServiceType == fullServiceType select s).Any(); + } + catch (InvalidOperationException) // Collection modified during enumeration + { + retries++; + } + } + + return true; + } + + #endregion + + #region Event Handlers + + private void device_DeviceAdded(object sender, DeviceEventArgs e) + { + SendAliveNotifications(e.Device, false); + ConnectToDeviceEvents(e.Device); + } + + private void device_DeviceRemoved(object sender, DeviceEventArgs e) + { + SendByeByeNotifications(e.Device, false); + DisconnectFromDeviceEvents(e.Device); + } + + private void device_ServiceAdded(object sender, ServiceEventArgs e) + { + //Technically we should only do this once per service type, + //but if we add services during runtime there is no way to + //notify anyone except by resending this notification. + Log.LogInfo(String.Format("Service added: {0} ({1})", e.Service.ServiceId, e.Service.FullServiceType)); + + SendAliveNotification((SsdpDevice)sender, e.Service); + } + + private void device_ServiceRemoved(object sender, ServiceEventArgs e) + { + Log.LogInfo(String.Format("Service removed: {0} ({1})", e.Service.ServiceId, e.Service.FullServiceType)); + + var device = (SsdpDevice)sender; + //Only say this service type has disappeared if there are no + //services of this type left. + if (!DeviceHasServiceOfType(device, e.Service.FullServiceType)) + SendByeByeNotification(device, e.Service); + } + + private void CommsServer_RequestReceived(object sender, RequestReceivedEventArgs e) + { + if (this.IsDisposed) return; + + if (e.Message.Method.Method == SsdpConstants.MSearchMethod) + { + //According to SSDP/UPnP spec, ignore message if missing these headers. + if (!e.Message.Headers.Contains("MX") && !IsRelaxedStandardsMode) + Log.LogWarning("Ignoring search request - missing MX header. Set StandardsMode to relaxed to process these search requests."); + else if (!e.Message.Headers.Contains("MAN") && !IsRelaxedStandardsMode) + Log.LogWarning("Ignoring search request - missing MAN header. Set StandardsMode to relaxed to process these search requests."); + else + ProcessSearchRequest(GetFirstHeaderValue(e.Message.Headers, "MX"), GetFirstHeaderValue(e.Message.Headers, "ST"), e.ReceivedFrom); + } + else if (String.Compare(e.Message.Method.Method, "NOTIFY", StringComparison.OrdinalIgnoreCase) != 0) + Log.LogWarning(String.Format("Unknown request \"{0}\"received, ignoring.", e.Message.Method.Method)); + } + + #endregion + + #region Private Classes + + private class SearchRequest + { + public UdpEndPoint EndPoint { get; set; } + public DateTime Received { get; set; } + public string SearchTarget { get; set; } + + public string Key + { + get { return this.SearchTarget + ":" + this.EndPoint.ToString(); } + } + + public bool IsOld() + { + return DateTime.UtcNow.Subtract(this.Received).TotalMilliseconds > 500; + } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpEmbeddedDevice.cs b/src/Rssdp.Shared/SsdpEmbeddedDevice.cs new file mode 100644 index 0000000..c03106b --- /dev/null +++ b/src/Rssdp.Shared/SsdpEmbeddedDevice.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Represents a device that is a descendant of a instance. + /// + public class SsdpEmbeddedDevice : SsdpDevice + { + + #region Fields + + private SsdpRootDevice _RootDevice; + + #endregion + + #region Constructors + + /// + /// Default constructor. + /// + public SsdpEmbeddedDevice() + { + } + + /// + /// Deserialisation constructor. + /// + /// A UPnP device description XML document. + /// Thrown if the argument is null. + /// Thrown if the argument is empty. + public SsdpEmbeddedDevice(string deviceDescriptionXml) + : base(deviceDescriptionXml) + { + } + + #endregion + + #region Public Properties + + /// + /// Returns the that is this device's first ancestor. If this device is itself an , then returns a reference to itself. + /// + public SsdpRootDevice RootDevice + { + get + { + return _RootDevice; + } + internal set + { + _RootDevice = value; + lock (this.Devices) + { + foreach (var embeddedDevice in this.Devices) + { + ((SsdpEmbeddedDevice)embeddedDevice).RootDevice = _RootDevice; + } + } + } + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpRootDevice.cs b/src/Rssdp.Shared/SsdpRootDevice.cs new file mode 100644 index 0000000..faf851b --- /dev/null +++ b/src/Rssdp.Shared/SsdpRootDevice.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + /// + /// Represents a 'root' device, a device that has no parent. Used for publishing devices and for the root device in a tree of discovered devices. + /// + /// + /// Child (embedded) devices are represented by the in the property. + /// Root devices contain some information that applies to the whole device tree and is therefore not present on child devices, such as and . + /// + public class SsdpRootDevice : SsdpDevice + { + + #region Fields + + private Uri _UrlBase; + + #endregion + + #region Constructors + + /// + /// Default constructor. + /// + public SsdpRootDevice() : base() + { + } + + /// + /// Deserialisation constructor. + /// + /// The url from which the device description document was retrieved. + /// A representing the time maximum period of time the device description can be cached for. + /// The device description XML as a string. + /// Thrown if the or arguments are null. + /// Thrown if the argument is empty. + public SsdpRootDevice(Uri location, TimeSpan cacheLifetime, string deviceDescriptionXml) + : base(deviceDescriptionXml) + { + if (location == null) throw new ArgumentNullException("location"); + + this.CacheLifetime = cacheLifetime; + this.Location = location; + + LoadFromDescriptionDocument(deviceDescriptionXml); + } + + #endregion + + #region Public Properties + + /// + /// Specifies how long clients can cache this device's details for. Optional but defaults to which means no-caching. Recommended value is half an hour. + /// + /// + /// Specifiy to indicate no caching allowed. + /// Also used to specify how often to rebroadcast alive notifications. + /// The UPnP/SSDP specifications indicate this should not be less than 1800 seconds (half an hour), but this is not enforced by this library. + /// + public TimeSpan CacheLifetime + { + get; set; + } + + /// + /// Gets or sets the URL used to retrieve the description document for this device/tree. Required. + /// + public Uri Location { get; set; } + + + /// + /// The base URL to use for all relative url's provided in other propertise (and those of child devices). Optional. + /// + /// + /// Defines the base URL. Used to construct fully-qualified URLs. All relative URLs that appear elsewhere in the description are combined with this base URL. If URLBase is empty or not given, the base URL is the URL from which the device description was retrieved (which is the preferred implementation; use of URLBase is no longer recommended). Specified by UPnP vendor. Single URL. + /// + public Uri UrlBase + { + get + { + return _UrlBase ?? this.Location; + } + + set + { + _UrlBase = value; + } + } + + #endregion + + #region Public Methods + + /// + /// Saves the property values of this device object to an a string in the full UPnP device description XML format, including child devices and outer root node and XML document declaration. + /// + /// A string containing XML in the UPnP device description format + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispsoing memory stream twice is 'safe' and easier to read than correct code for ensuring it is only closed once.")] + public virtual string ToDescriptionDocument() + { + if (String.IsNullOrEmpty(this.Uuid)) throw new InvalidOperationException("Must provide a UUID value."); + + //This would have been so much nicer with Xml.Linq, but that's + //not available until .NET 4.03 at the earliest, and I want to + //target 4.0 :( + using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) + { + System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(ms, new XmlWriterSettings() { Encoding = System.Text.UTF8Encoding.UTF8, Indent = true, NamespaceHandling = NamespaceHandling.OmitDuplicates }); + writer.WriteStartDocument(); + writer.WriteStartElement("root", SsdpConstants.SsdpDeviceDescriptionXmlNamespace); + + writer.WriteStartElement("specVersion"); + writer.WriteElementString("major", "1"); + writer.WriteElementString("minor", "0"); + writer.WriteEndElement(); + + if (this.UrlBase != null && this.UrlBase != this.Location) + writer.WriteElementString("URLBase", this.UrlBase.ToString()); + + WriteDeviceDescriptionXml(writer, this); + + writer.WriteEndElement(); + writer.Flush(); + + ms.Seek(0, System.IO.SeekOrigin.Begin); + using (var reader = new System.IO.StreamReader(ms)) + { + return reader.ReadToEnd(); + } + } + } + + #endregion + + #region Private Methods + + #region Deserialisation Methods + + private void LoadFromDescriptionDocument(string deviceDescriptionXml) + { + using (var ms = new System.IO.MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(deviceDescriptionXml))) + { + var reader = XmlReader.Create(ms); + while (!reader.EOF) + { + reader.Read(); + if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "root") continue; + + while (!reader.EOF) + { + reader.Read(); + + if (reader.NodeType != XmlNodeType.Element) continue; + + if (reader.LocalName == "URLBase") + { + this.UrlBase = StringToUri(reader.ReadElementContentAsString()); + break; + } + } + } + } + } + + #endregion + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpService.cs b/src/Rssdp.Shared/SsdpService.cs new file mode 100644 index 0000000..f4bfca9 --- /dev/null +++ b/src/Rssdp.Shared/SsdpService.cs @@ -0,0 +1,284 @@ +using Rssdp.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml; + +namespace Rssdp +{ + /// + /// Represents an SSDP service to be published. + /// + public class SsdpService + { + + #region Constructors + + /// + /// Default constructor. + /// + public SsdpService() + { + this.ServiceTypeNamespace = SsdpConstants.UpnpDeviceTypeNamespace; + this.ServiceVersion = 1; + } + + /// + /// Deserialisation constructor. + /// + /// Uses the provided XML string to set the properties of the object. The XML provided must be a valid UPnP service description document. + /// A UPnP service description XML document. + /// Thrown if the argument is null. + /// Thrown if the argument is empty. + public SsdpService(string serviceDescriptionXml) : this() + { + if (serviceDescriptionXml == null) throw new ArgumentNullException(nameof(serviceDescriptionXml)); + if (serviceDescriptionXml.Length == 0) throw new ArgumentException(nameof(serviceDescriptionXml) + " cannot be an empty string.", nameof(serviceDescriptionXml)); + + using (var ms = new System.IO.MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(serviceDescriptionXml))) + { + var reader = XmlReader.Create(ms); + + LoadServiceProperties(reader, this); + } + } + + #endregion + + #region Public Properties + + /// + /// Sets or returns the service type (not including namespace, version etc) of the exposed service. Required. + /// + /// + /// + /// + public string ServiceType { get; set; } + + /// + /// Sets or returns the namespace for the of this service. Optional but defaults to the UPnP schema so should be changed if is not an official UPnP service type. + /// + /// + /// + /// + public string ServiceTypeNamespace { get; set; } + + /// + /// Sets or returns the version of the service type. Optional, defaults to 1. + /// + /// Defaults to a value of 1. + /// + /// + /// + public int ServiceVersion { get; set; } + + /// + /// Returns the full service type string. + /// + /// + /// The format used is urn::service:: + /// + public string FullServiceType + { + get + { + //From the spec; Period characters in the Vendor Domain Name MUST be replaced with hyphens in accordance with RFC 2141 + return String.Format("urn:{0}:service:{1}:{2}", + (this.ServiceTypeNamespace ?? String.Empty).Replace(".", "-"), + this.ServiceType ?? String.Empty, + this.ServiceVersion); + } + } + + /// + /// Sets or returns the universally unique identifier for this service (without the uuid: prefix). Required. + /// + /// + /// Must be the same over time for a specific service instance (i.e. must survive reboots). + /// For UPnP 1.0 this can be any unique string. For UPnP 1.1 this should be a 128 bit number formatted in a specific way, preferably generated using the time and MAC based algorithm. See section 1.1.4 of http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf for details. + /// Technically this library implements UPnP 1.0, so any value is allowed, but we advise using UPnP 1.1 compatible values for good behaviour and forward compatibility with future versions. + /// + public string Uuid { get; set; } + + /// + /// Returns the full service type string. + /// + /// + /// The format used is urn::serviceid: + /// + public string ServiceId + { + get + { + //From the spec; Period characters in the Vendor Domain Name MUST be replaced with hyphens in accordance with RFC 2141 + return String.Format + ( + "urn:{0}:serviceId:{1}", + (this.ServiceTypeNamespace == SsdpConstants.UpnpDeviceTypeNamespace ? "upnp-org" : this.ServiceTypeNamespace).Replace(".", "-"), + this.Uuid ?? String.Empty + ); + } + } + + /// + /// REQUIRED. URL for service description. (See section 2.5, “Service description” below.) MUST be relative to the URL at which the device description is located in accordance with section 5 of RFC 3986. Specified by UPnP vendor. Single URL. + /// + public Uri ScpdUrl { get; set; } + /// + /// REQUIRED. URL for control (see section 3, “Control”). MUST be relative to the URL at which the device description is located in accordance with section 5 of RFC 3986. Specified by UPnP vendor. Single URL. + /// + public Uri ControlUrl { get; set; } + /// + /// URL for eventing (see section 4, “Eventing”). MUST be relative to the URL at which the device description is located in accordance with section 5 of RFC 3986. MUST be unique within the device; any two services MUST NOT have the same URL for eventing. If the service has no evented variables, this element MUST be present but MUST be empty(i.e., .) Specified by UPnP vendor.Single URL. + /// + public Uri EventSubUrl { get; set; } + + #endregion + + #region Public Methods + + /// + /// Writes this service to the specified as a service node and it's content. + /// + /// The to output to. + /// Thrown if the argument is null. + public virtual void WriteServiceDescriptionXml(XmlWriter writer) + { + if (writer == null) throw new ArgumentNullException("writer"); + + writer.WriteStartElement("service"); + + if (!String.IsNullOrEmpty(this.ServiceType)) + WriteNodeIfNotEmpty(writer, "serviceType", FullServiceType); + + WriteNodeIfNotEmpty(writer, "serviceId", ServiceId); + WriteNodeIfNotEmpty(writer, "SCPDURL", ScpdUrl); + WriteNodeIfNotEmpty(writer, "controlURL", ControlUrl); + WriteNodeIfNotEmpty(writer, "eventSubURL", EventSubUrl); + + writer.WriteEndElement(); + } + + #endregion + + #region Private Methods + + private static void WriteNodeIfNotEmpty(XmlWriter writer, string nodeName, string value) + { + if (!String.IsNullOrEmpty(value)) + writer.WriteElementString(nodeName, value); + } + + private static void WriteNodeIfNotEmpty(XmlWriter writer, string nodeName, Uri value) + { + if (value != null) + writer.WriteElementString(nodeName, value.ToString()); + } + + private void LoadServiceProperties(XmlReader reader, SsdpService service) + { + ReadUntilServiceNode(reader); + + while (!reader.EOF) + { + if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "service") + { + reader.Read(); + break; + } + + if (!SetPropertyFromReader(reader, service)) + reader.Read(); + } + } + + private static void ReadUntilServiceNode(XmlReader reader) + { + while (!reader.EOF && (reader.LocalName != "service" || reader.NodeType != XmlNodeType.Element)) + { + reader.Read(); + } + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Yes, there is a large switch statement, not it's not really complex and doesn't really need to be rewritten at this point.")] + private bool SetPropertyFromReader(XmlReader reader, SsdpService service) + { + switch (reader.LocalName) + { + case "serviceType": + SetServiceTypePropertiesFromFullDeviceType(service, reader.ReadElementContentAsString()); + break; + + case "serviceId": + SetServiceIdPropertiesFromFullServiceId(service, reader.ReadElementContentAsString()); + break; + + case "SCPDURL": + this.ScpdUrl = StringToUri(reader.ReadElementContentAsString()); + break; + + case "controlURL": + this.ControlUrl = StringToUri(reader.ReadElementContentAsString()); + break; + + case "eventSubURL": + this.EventSubUrl = StringToUri(reader.ReadElementContentAsString()); + break; + + default: + return false; + } + return true; + } + + private static void SetServiceIdPropertiesFromFullServiceId(SsdpService service, string value) + { + if (String.IsNullOrEmpty(value) || !value.Contains(":")) + service.ServiceType = value; + else + { + var parts = value.Split(':'); + if (parts.Length == 4) + service.Uuid = parts[3]; + else + service.Uuid = value; + } + } + + private static void SetServiceTypePropertiesFromFullDeviceType(SsdpService service, string value) + { + if (String.IsNullOrEmpty(value) || !value.Contains(":")) + service.ServiceType = value; + else + { + var parts = value.Split(':'); + if (parts.Length == 5) + { + int serviceVersion = 1; + if (Int32.TryParse(parts[4], out serviceVersion)) + { + service.ServiceTypeNamespace = parts[1]; + service.ServiceType = parts[3]; + service.ServiceVersion = serviceVersion; + } + else + service.ServiceType = value; + } + else + service.ServiceType = value; + } + } + + private static Uri StringToUri(string value) + { + if (!String.IsNullOrEmpty(value)) + return new Uri(value, UriKind.RelativeOrAbsolute); + + return null; + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Rssdp.Shared/SsdpStandardsMode.cs b/src/Rssdp.Shared/SsdpStandardsMode.cs new file mode 100644 index 0000000..54419f0 --- /dev/null +++ b/src/Rssdp.Shared/SsdpStandardsMode.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// An enum whose values control how strictly RSSDP follows the SSDP specification. + /// + public enum SsdpStandardsMode + { + /// + /// Equivalent to + /// + Default, + /// + /// RSSDP will not strictly follow the specification, but will instead behave in ways that are compatible with most SSDP devices. + /// + /// + /// This mode provides maximum compatibility with other SSDP based systems. + /// + Relaxed, + /// + /// RSSDP will strictly follow the SSDP specification even where other implementations commonly deviate. + /// + Strict + } +} diff --git a/src/Rssdp.Shared/TaskEx.cs b/src/Rssdp.Shared/TaskEx.cs new file mode 100644 index 0000000..681308b --- /dev/null +++ b/src/Rssdp.Shared/TaskEx.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Rssdp +{ + internal static class TaskEx + { + + // Sadly Task.Run() is missing from this PCL profile, + // so attempt to build our own for convenience. + // According to; + // http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx + // "Task.Run is exactly equivalent to" Task.Factory.StartNew(someAction, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + // Sadly, we don't have DenyChildAttach either, so I guess this is as good as it gets. + public static Task Run(Action work) + { + return Task.Factory.StartNew(work, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); + } + + public static Task Run(Func work) + { + return Task.Factory.StartNew(work, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); + } + + + public static Task Delay(TimeSpan period) + { + if (period.TotalMilliseconds > int.MaxValue) throw new ArgumentOutOfRangeException("period", String.Format("period cannot be more than {0} millseconds.", period.TotalMilliseconds)); + + return Delay(Convert.ToInt32(period.TotalMilliseconds)); + } + + public static Task Delay(int millisecondsDelay) + { + if (millisecondsDelay < -1) throw new ArgumentOutOfRangeException("millisecondsDelay", "millisecondsDelay must be -1 or greater."); + + var tcs = new TaskCompletionSource(); + var timer = new Timer((state) => + { + tcs.SetResult(null); + }, + null, + millisecondsDelay, + System.Threading.Timeout.Infinite); + + return tcs.Task.ContinueWith((t) => timer.Dispose()); + } + + } +} diff --git a/src/Rssdp.Shared/UPnP10DeviceValidator.cs b/src/Rssdp.Shared/UPnP10DeviceValidator.cs new file mode 100644 index 0000000..8a55a82 --- /dev/null +++ b/src/Rssdp.Shared/UPnP10DeviceValidator.cs @@ -0,0 +1,223 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Validates a object's properties meet the UPnP 1.0 specification. + /// + /// + /// This is a best effort validation for known rules, it doesn't guarantee 100% compatibility with the specification. Reading the specification yourself is the best way to ensure compatibility. + /// + public class Upnp10DeviceValidator : IUpnpDeviceValidator + { + + #region Public Methods + + /// + /// Returns an enumerable set of strings, each one being a description of an invalid property on the specified root device. + /// + /// + /// If no errors are found, an empty (but non-null) enumerable is returned. + /// + /// The to validate. + /// Thrown if the argument is null. + /// A non-null enumerable set of strings, empty if there are no validation errors, otherwise each string represents a discrete problem. + public IEnumerable GetValidationErrors(SsdpRootDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + + var retVal = GetValidationErrors((SsdpDevice)device) as IList; + + if (device.Location == null) + retVal.Add("Location cannot be null."); + else if (!device.Location.IsAbsoluteUri) + retVal.Add("Location must be an absolute URL."); + + return retVal; + } + + /// + /// Returns an enumerable set of strings, each one being a description of an invalid property on the specified device. + /// + /// + /// If no errors are found, an empty (but non-null) enumerable is returned. + /// + /// The to validate. + /// Thrown if the argument is null. + /// A non-null enumerable set of strings, empty if there are no validation errors, otherwise each string represents a discrete problem. + public IEnumerable GetValidationErrors(SsdpDevice device) + { + if (device == null) throw new ArgumentNullException("device"); + + var retVal = new List(); + + if (String.IsNullOrEmpty(device.Uuid)) + retVal.Add("Uuid is not set."); + + if (!String.IsNullOrEmpty(device.Upc)) + ValidateUpc(device, retVal); + + if (String.IsNullOrEmpty(device.Udn)) + retVal.Add("UDN is not set."); + else + ValidateUdn(device, retVal); + + if (String.IsNullOrEmpty(device.DeviceType)) + retVal.Add("DeviceType is not set."); + + if (String.IsNullOrEmpty(device.DeviceTypeNamespace)) + retVal.Add("DeviceTypeNamespace is not set."); + else + { + if (IsOverLength(device.DeviceTypeNamespace, 64)) + retVal.Add("DeviceTypeNamespace cannot be longer than 64 characters."); + + if (device.DeviceTypeNamespace.Contains(".")) + retVal.Add("Period (.) characters in the DeviceTypeNamespace property must be replaced with hyphens (-)."); + } + + if (device.DeviceVersion <= 0) + retVal.Add("DeviceVersion must be 1 or greater."); + + if (IsOverLength(device.ModelName, 32)) + retVal.Add("ModelName cannot be longer than 32 characters."); + + if (IsOverLength(device.ModelNumber, 32)) + retVal.Add("ModelNumber cannot be longer than 32 characters."); + + if (IsOverLength(device.FriendlyName, 64)) + retVal.Add("FriendlyName cannot be longer than 64 characters."); + + if (IsOverLength(device.Manufacturer, 64)) + retVal.Add("Manufacturer cannot be longer than 64 characters."); + + if (IsOverLength(device.SerialNumber, 64)) + retVal.Add("SerialNumber cannot be longer than 64 characters."); + + if (IsOverLength(device.ModelDescription, 128)) + retVal.Add("ModelDescription cannot be longer than 128 characters."); + + if (String.IsNullOrEmpty(device.FriendlyName)) + retVal.Add("FriendlyName is required."); + + if (String.IsNullOrEmpty(device.Manufacturer)) + retVal.Add("Manufacturer is required."); + + if (String.IsNullOrEmpty(device.ModelName)) + retVal.Add("ModelName is required."); + + if (device.Icons.Any()) + ValidateIcons(device, retVal); + + ValidateChildServices(device, retVal); + + ValidateChildDevices(device, retVal); + + return retVal; + } + + /// + /// Validates the specified device and throws an if there are any validation errors. + /// + /// The to validate. + /// Thrown if the argument is null. + /// Thrown if the device object does not pass validation. + public void ThrowIfDeviceInvalid(SsdpDevice device) + { + var errors = this.GetValidationErrors(device); + if (errors != null && errors.Any()) throw new InvalidOperationException("Invalid device settings : " + String.Join(Environment.NewLine, errors)); + } + + #endregion + + #region Private Methods + + private static void ValidateUpc(SsdpDevice device, List retVal) + { + if (device.Upc.Length != 12) + retVal.Add("Upc, if provided, should be 12 digits."); + + foreach (char c in device.Upc) + { + if (!Char.IsDigit(c)) + { + retVal.Add("Upc, if provided, should contain only digits (numeric characters)."); + break; + } + } + } + + private static void ValidateUdn(SsdpDevice device, List retVal) + { + if (!device.Udn.StartsWith("uuid:", StringComparison.OrdinalIgnoreCase)) + retVal.Add("UDN must begin with uuid:. Correct format is uuid:"); + else if (device.Udn.Substring(5).Trim() != device.Uuid) + retVal.Add("UDN incorrect. Correct format is uuid:"); + } + + private static void ValidateIcons(SsdpDevice device, List retVal) + { + if (device.Icons.Any((di) => di.Url == null)) + retVal.Add("Device icon is missing URL."); + + if (device.Icons.Any((di) => String.IsNullOrEmpty(di.MimeType))) + retVal.Add("Device icon is missing mime type."); + + if (device.Icons.Any((di) => di.Width <= 0 || di.Height <= 0)) + retVal.Add("Device icon has zero (or negative) height, width or both."); + + if (device.Icons.Any((di) => di.ColorDepth <= 0)) + retVal.Add("Device icon has zero (or negative) colordepth."); + } + + private void ValidateChildDevices(SsdpDevice device, List retVal) + { + foreach (var childDevice in device.Devices) + { + foreach (var validationError in this.GetValidationErrors(childDevice)) + { + retVal.Add("Embedded Device : " + childDevice.Uuid + ": " + validationError); + } + + ValidateChildServices(childDevice, retVal); + } + } + + private static bool IsOverLength(string value, int maxLength) + { + return !String.IsNullOrEmpty(value) && value.Length > maxLength; + } + + private static void ValidateChildServices(SsdpDevice device, List retVal) + { + foreach (var service in device.Services) + { + ValidateService(service, retVal); + } + } + + private static void ValidateService(SsdpService service, List retVal) + { + if (String.IsNullOrEmpty(service.ServiceType)) + retVal.Add("ServiceType is missing"); + else if (service.ServiceType.Contains("#")) + retVal.Add("ServiceType cannot contain #"); + + if (String.IsNullOrEmpty(service.Uuid)) + retVal.Add("ServiceId is missing"); + + if (service.ScpdUrl == null) + retVal.Add("ScpdUrl is missing"); + + if (service.ControlUrl == null) + retVal.Add("ControlUrl is missing"); + } + + #endregion + + } +} diff --git a/src/Rssdp.Shared/UdpEndPoint.cs b/src/Rssdp.Shared/UdpEndPoint.cs new file mode 100644 index 0000000..617769c --- /dev/null +++ b/src/Rssdp.Shared/UdpEndPoint.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp.Infrastructure +{ + /// + /// Cross platform representation of a UDP end point, being an IP address (either IPv4 or IPv6) and a port. + /// + public sealed class UdpEndPoint + { + + /// + /// The IP Address of the end point. + /// + /// + /// Can be either IPv4 or IPv6, up to the code using this instance to determine which was provided. + /// + public string IPAddress { get; set; } + + /// + /// The port of the end point. + /// + public int Port { get; set; } + + /// + /// Returns the and values separated by a colon. + /// + /// A string containing :. + public override string ToString() + { + return (this.IPAddress ?? String.Empty) + ":" + this.Port.ToString(); + } + } +} diff --git a/src/Shared/AssemblyInfoCommon.cs b/src/Shared/AssemblyInfoCommon.cs new file mode 100644 index 0000000..9ab3896 --- /dev/null +++ b/src/Shared/AssemblyInfoCommon.cs @@ -0,0 +1,15 @@ +using System.Reflection; + +[assembly: AssemblyProduct("Really Simple Service Discovery Protocol")] +[assembly: AssemblyCompany("Created by Yort. https://github.com/Yortw/RSSDP")] +[assembly: AssemblyCopyright("Released under the MIT license; http://choosealicense.com/licenses/mit/; https://github.com/Yortw/RSSDP")] +[assembly: AssemblyTrademark("")] + +[assembly: AssemblyVersion("3.0.3.0")] +[assembly: AssemblyFileVersion("3.0.3.0")] + +#if DEBUG +[assembly: AssemblyConfiguration("DEBUG")] +#else +[assembly: AssemblyConfiguration("RELEASE")] +#endif \ No newline at end of file diff --git a/src/Shared/CodeAnalysisDictionary.xml b/src/Shared/CodeAnalysisDictionary.xml new file mode 100644 index 0000000..a2926ca --- /dev/null +++ b/src/Shared/CodeAnalysisDictionary.xml @@ -0,0 +1,31 @@ + + + + + + + Ssdp + Rssdp + Uuid + Upc + Udn + iOS + nuget + os + Usn + + + + + + + Ssdp + Rssdp + UPNP + Uuid + Upc + Udn + iOS + + + \ No newline at end of file diff --git a/src/Shared/ExceptionExtensions.cs b/src/Shared/ExceptionExtensions.cs new file mode 100644 index 0000000..94c0386 --- /dev/null +++ b/src/Shared/ExceptionExtensions.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + /// + /// Provides extension methods to and derived objects. + /// + public static class ExceptionExtensions + { + /// + /// Returns true of the specified exception is one that indicates some form of memory corruption, out of memory state or other fatal exception that should *never* be handled by user code. + /// + /// The exception to check. + /// + /// Doesn't check for System.StackOverflowExceptions as if the stack really is full calling this method might check, therefore calling code must explicitly handle that exception type itself. + /// Specifically checks for the following exception types; + /// + /// + /// System.AccessViolationException + /// System.OutOfMemoryException + /// System.InvalidProgramException + /// + /// + /// + /// True if the specified exception is considered critical and should be re-thrown and not otherwise handled by user code. + public static bool IsCritical(this Exception exception) + { +#if NETSTANDARD + // Unrecoverable exceptions should not be getting caught and will be dealt with on a broad level by a high-level catch-all handler + // https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/breaking-change-rules.md#exceptions + return (exception is System.OutOfMemoryException) + || (exception is System.InvalidProgramException); +#else + return (exception is System.AccessViolationException) + || (exception is System.OutOfMemoryException) + || (exception is System.InvalidProgramException); + +#endif + } + } +} \ No newline at end of file diff --git a/src/Shared/SsdpDeviceLocator.cs b/src/Shared/SsdpDeviceLocator.cs new file mode 100644 index 0000000..da09d15 --- /dev/null +++ b/src/Shared/SsdpDeviceLocator.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS + // Be careful to check any changes compile and work for all platform projects it is shared in. + + /// + /// Allows you to search the network for a particular device, device types, or UPnP service types. Also listenings for broadcast notifications of device availability and raises events to indicate changes in status. + /// + public sealed class SsdpDeviceLocator : SsdpDeviceLocatorBase + { + + /// + /// Default constructor. Constructs a new instance using the default and implementations for this platform. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Can't expose along exception paths here (exceptions should be very rare anyway, and probably fatal too) and we shouldn't dipose the items we pass to base in any other case.")] + public SsdpDeviceLocator() : base(new SsdpCommunicationsServer(new SocketFactory(null))) + { + // This is not the problem you are looking for; + // Yes, this is poor man's dependency injection which some call an anti-pattern. + // However, it makes the library really simple to get started with or to use if the calling code isn't using IoC/DI. + // The fact we have injected dependencies is really an internal architectural implementation detail to allow for the + // cross platform and testing concerns of this library. It shouldn't be something calling code worries about and is + // not a deliberate extension point, except where adding new platform support in which case... + // There is a constructor that takes a manually injected dependency anyway, so proper DI using + // a container or whatever can be done anyway. + } + + /// + /// Partial constructor. + /// + /// The IP address of the local network adapter to bind sockets to. + /// Null or empty string will use an IP address selected by the OS or runtime. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Can't expose along exception paths here (exceptions should be very rare anyway, and probably fatal too) and we shouldn't dipose the items we pass to base in any other case.")] + public SsdpDeviceLocator(string ipAddress) : base(new SsdpCommunicationsServer(new SocketFactory(ipAddress))) + { + // This is not the problem you are looking for; + // Yes, this is poor man's dependency injection which some call an anti-pattern. + // However, it makes the library really simple to get started with or to use if the calling code isn't using IoC/DI. + // The fact we have injected dependencies is really an internal architectural implementation detail to allow for the + // cross platform and testing concerns of this library. It shouldn't be something calling code worries about and is + // not a deliberate extension point, except where adding new platform support in which case... + // There is a constructor that takes a manually injected dependency anyway, so proper DI using + // a container or whatever can be done anyway. + } + + /// + /// Full constructor. Constructs a new instance using the provided implementation. + /// + public SsdpDeviceLocator(ISsdpCommunicationsServer communicationsServer) + : base(communicationsServer) + { + } + + } +} \ No newline at end of file diff --git a/src/Shared/SsdpTraceLogger.cs b/src/Shared/SsdpTraceLogger.cs new file mode 100644 index 0000000..26461da --- /dev/null +++ b/src/Shared/SsdpTraceLogger.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Rssdp +{ + /// + /// Implementation of that writes to the .Net tracing system on platforms that support it, or on those that don't. + /// + /// + /// On platforms that only support no log entries will be output unless running a debug build, and this effectively becomes a null logger for release builds. + /// + public class SsdpTraceLogger : ISsdpLogger + { + /// + /// Records a regular log message. + /// + /// The text to be logged. + public void LogInfo(string message) + { + WriteLogMessage("Information", message); + } + + /// + /// Records a frequent or large log message usually only required when trying to trace a problem. + /// + /// The text to be logged. + public void LogVerbose(string message) + { + WriteLogMessage("Verbose", message); + } + + /// + /// Records an important message, but one that may not neccesarily be an error. + /// + /// The text to be logged. + public void LogWarning(string message) + { + WriteLogMessage("Warning", message); + } + + /// + /// Records a message that represents an error. + /// + /// The text to be logged. + public void LogError(string message) + { + WriteLogMessage("Error", message); + } + + private static void WriteLogMessage(string category, string message) + { +#if SUPPORTS_TRACE + System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString("G") + " " + message, category); +#else + System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("G") + " [" + category + "] " + message); +#endif + } + + } +} \ No newline at end of file diff --git a/src/Shared/SystemNetSockets/SocketFactory.cs b/src/Shared/SystemNetSockets/SocketFactory.cs new file mode 100644 index 0000000..ec57a97 --- /dev/null +++ b/src/Shared/SystemNetSockets/SocketFactory.cs @@ -0,0 +1,206 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Security; +using System.Text; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS + // Be careful to check any changes compile and work for all platform projects it is shared in. + + // Not entirely happy with this. Would have liked to have done something more generic/reusable, + // but that wasn't really the point so kept to YAGNI principal for now, even if the + // interfaces are a bit ugly, specific and make assumptions. + + /// + /// Used by RSSDP components to create implementations of the interface, to perform platform agnostic socket communications. + /// + public sealed class SocketFactory : ISocketFactory + { + private readonly DeviceNetworkType _DeviceNetworkType; + private IPAddress _LocalIP; + + /// + /// Default constructor. + /// + /// The IP address of the local network adapter to bind sockets to. + /// Null or empty string will use . + public SocketFactory(string ipAddress) + { + if (String.IsNullOrEmpty(ipAddress)) + _LocalIP = IPAddress.Any; + else + _LocalIP = IPAddress.Parse(ipAddress); + + _DeviceNetworkType = GetDeviceNetworkType(_LocalIP.AddressFamily); + } + + #region ISocketFactory Members + + /// + /// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port. + /// + /// An integer specifying the local port to bind the socket to. + /// An implementation of the interface used by RSSDP components to perform socket operations. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] + public IUdpSocket CreateUdpSocket(int localPort) + { + if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort"); + + var retVal = new Socket(_LocalIP.AddressFamily, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); + try + { + retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + + SetMulticastSocketOptions(retVal, SsdpConstants.SsdpDefaultMulticastTimeToLive); + + return new UdpSocket(retVal, _LocalIP.ToString(), localPort); + } + catch + { + if (retVal != null) + retVal.Dispose(); + + throw; + } + } + + /// + /// Creates a new UDP socket that is a member of the specified multicast IP address, and binds it to the specified local port. + /// + /// The multicast time to live value for the socket. + /// The number of the local port to bind to. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "ip"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The purpose of this method is to create and returns a disposable result, it is up to the caller to dispose it when they are done with it.")] + public IUdpSocket CreateUdpMulticastSocket(int multicastTimeToLive, int localPort) + { + if (multicastTimeToLive <= 0) throw new ArgumentException("multicastTimeToLive cannot be zero or less.", "multicastTimeToLive"); + if (localPort < 0) throw new ArgumentException("localPort cannot be less than zero.", "localPort"); + + var retVal = new Socket(_LocalIP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); + + try + { +#if NETSTANDARD1_3 + // The ExclusiveAddressUse socket option is a Windows-specific option that, when set to "true," tells Windows not to allow another socket to use the same local address as this socket + // See https://github.com/dotnet/corefx/pull/11509 for more details + if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) + { + retVal.ExclusiveAddressUse = false; + } +#else + retVal.ExclusiveAddressUse = false; +#endif + retVal.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + + SetMulticastSocketOptions(retVal, multicastTimeToLive); + + retVal.MulticastLoopback = true; + + return new UdpSocket(retVal, _LocalIP.ToString(), localPort); + } + catch + { + if (retVal != null) + retVal.Dispose(); + + throw; + } + } + + /// + /// What type of sockets will be created: ipv6 or ipv4 + /// + public DeviceNetworkType DeviceNetworkType + { + get + { + return _DeviceNetworkType; + } + } + + #endregion + + /// + /// Set options for multicast depending on the type of the local address + /// + /// Socket for setting options + /// Multicast Time to live for multicast options + /// + private void SetMulticastSocketOptions(Socket retVal, int multicastTimeToLive) + { + string multicastIpAddress = _DeviceNetworkType.GetMulticastIPAddress(); + IPAddress ipAddress = IPAddress.Parse(multicastIpAddress); + + switch (_DeviceNetworkType) + { + case DeviceNetworkType.IPv4: + retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive); + retVal.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipAddress, _LocalIP)); + break; + + case DeviceNetworkType.IPv6: + retVal.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, multicastTimeToLive); + long interfaceIndex = -1; + +#if !NETSTANDARD + if (_LocalIP != null & _LocalIP != IPAddress.IPv6Any) + interfaceIndex = GetInterfaceIndexFromIPAddress(_LocalIP); +#endif + + if (interfaceIndex >= 0) + retVal.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(ipAddress, interfaceIndex)); + else + retVal.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(ipAddress)); + break; + + default: + throw new InvalidOperationException($"{nameof(_DeviceNetworkType)} is not equal to Ipv4 or Ipv6"); + } + } + +#if !NETSTANDARD + private static long GetInterfaceIndexFromIPAddress(IPAddress ipAddress) + { + foreach (var networkInterface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()) + { + if (networkInterface.Supports(NetworkInterfaceComponent.IPv6)) + { + var ipProperties = networkInterface.GetIPProperties(); + if (ipProperties != null) + { + foreach (var address in ipProperties.UnicastAddresses) + { + if (address.Address?.ToString() == ipAddress.ToString()) + { + var ipv6Properties = ipProperties?.GetIPv6Properties(); + return ipv6Properties?.Index ?? -1; + } + } + } + } + } + + return -1; + } +#endif + + private static DeviceNetworkType GetDeviceNetworkType(AddressFamily addressFamily) + { + switch (addressFamily) + { + case AddressFamily.InterNetwork: + return DeviceNetworkType.IPv4; + case AddressFamily.InterNetworkV6: + return DeviceNetworkType.IPv6; + default: + throw new ArgumentOutOfRangeException(nameof(addressFamily), addressFamily, null); + } + } + } +} \ No newline at end of file diff --git a/src/Shared/SystemNetSockets/UdpSocket.cs b/src/Shared/SystemNetSockets/UdpSocket.cs new file mode 100644 index 0000000..8183c6c --- /dev/null +++ b/src/Shared/SystemNetSockets/UdpSocket.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Security; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS + // Be careful to check any changes compile and work for all platform projects it is shared in. + internal sealed class UdpSocket : DisposableManagedObjectBase, IUdpSocket + { + + #region Fields + + private System.Net.Sockets.Socket _Socket; + private int _LocalPort; + + #endregion + + #region Constructors + + public UdpSocket(Socket socket, string ipAddress, int localPort) + { + if (socket == null) throw new ArgumentNullException("socket"); + + _Socket = socket; + _LocalPort = localPort; + + var ip = String.IsNullOrEmpty(ipAddress) ? GetDefaultIpAddress(socket) : IPAddress.Parse(ipAddress); + + _Socket.Bind(new IPEndPoint(ip, _LocalPort)); + if (_LocalPort == 0) + _LocalPort = ((IPEndPoint) _Socket.LocalEndPoint).Port; + } + + #endregion + + #region IUdpSocket Members + + public System.Threading.Tasks.Task ReceiveAsync() + { + ThrowIfDisposed(); + + var tcs = new TaskCompletionSource(); + + System.Net.EndPoint receivedFromEndPoint = new IPEndPoint(GetDefaultIpAddress(_Socket), 0); + var state = new AsyncReceiveState(_Socket, receivedFromEndPoint); + state.TaskCompletionSource = tcs; +#if NETSTANDARD1_3 + _Socket.ReceiveFromAsync(new System.ArraySegment(state.Buffer), System.Net.Sockets.SocketFlags.None, state.EndPoint) + .ContinueWith((task, asyncState) => + { + if (this.IsDisposed) return; + + try + { + if (task.Status != TaskStatus.Faulted) + { + var receiveState = asyncState as AsyncReceiveState; + receiveState.EndPoint = task.Result.RemoteEndPoint; + ProcessResponse(receiveState, () => task.Result.ReceivedBytes); + } + } + catch (ObjectDisposedException) { if (!this.IsDisposed) throw; } //Only rethrow disposed exceptions if we're NOT disposed, because then they are unexpected. + }, state); +#else + _Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, System.Net.Sockets.SocketFlags.None, ref state.EndPoint, + new AsyncCallback((result) => ProcessResponse(state, () => state.Socket.EndReceiveFrom(result, ref state.EndPoint))), state); +#endif + + return tcs.Task; + } + + public void SendTo(byte[] messageData, UdpEndPoint endPoint) + { + ThrowIfDisposed(); + + if (messageData == null) throw new ArgumentNullException("messageData"); + if (endPoint == null) throw new ArgumentNullException("endPoint"); + + _Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IPAddress), endPoint.Port)); + } + + #endregion + + #region Overrides + + protected override void Dispose(bool disposing) + { + if (disposing) + { + var socket = _Socket; + if (socket != null) + socket.Dispose(); + } + } + + #endregion + + #region Private Methods + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions via task methods should be reported by task completion source, so this should be ok.")] + private static void ProcessResponse(AsyncReceiveState state, Func receiveData) + { + try + { + var bytesRead = receiveData(); + + var ipEndPoint = state.EndPoint as IPEndPoint; + state.TaskCompletionSource.SetResult( + new ReceivedUdpData() + { + Buffer = state.Buffer, + ReceivedBytes = bytesRead, + ReceivedFrom = new UdpEndPoint() + { + IPAddress = ipEndPoint.Address.ToString(), + Port = ipEndPoint.Port + } + } + ); + } + catch (ObjectDisposedException) + { + state.TaskCompletionSource.SetCanceled(); + } + catch (SocketException se) + { + if (se.SocketErrorCode != SocketError.Interrupted && se.SocketErrorCode != SocketError.OperationAborted && se.SocketErrorCode != SocketError.Shutdown) + state.TaskCompletionSource.SetException(se); + else + state.TaskCompletionSource.SetCanceled(); + } +#if NETSTANDARD + // Unrecoverable exceptions should not be getting caught and will be dealt with on a broad level by a high-level catch-all handler + // https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/breaking-change-rules.md#exceptions +#else + catch (StackOverflowException) // Handle this manually as we may not be able to call a sub method to check exception type etc. + { + throw; + } +#endif + catch (Exception ex) + { + if (ex.IsCritical()) //Critical exceptions that indicate memory corruption etc. shouldn't be caught. + throw; + + state.TaskCompletionSource.SetException(ex); + } + } + + #endregion + + #region Private Classes + + private class AsyncReceiveState + { + public AsyncReceiveState(System.Net.Sockets.Socket socket, EndPoint endPoint) + { + this.Socket = socket; + this.EndPoint = endPoint; + } + + public EndPoint EndPoint; + public byte[] Buffer = new byte[SsdpConstants.DefaultUdpSocketBufferSize]; + + public System.Net.Sockets.Socket Socket { get; private set; } + + public TaskCompletionSource TaskCompletionSource { get; set; } + + } + + private static IPAddress GetDefaultIpAddress(Socket socket) + { + switch (socket.AddressFamily) + { + case AddressFamily.InterNetwork: + return IPAddress.Any; + case AddressFamily.InterNetworkV6: + return IPAddress.IPv6Any; + } + + return IPAddress.None; + } + #endregion + + } +} \ No newline at end of file diff --git a/src/Shared/WinRTSockets/SocketsFactory.cs b/src/Shared/WinRTSockets/SocketsFactory.cs new file mode 100644 index 0000000..fcc0918 --- /dev/null +++ b/src/Shared/WinRTSockets/SocketsFactory.cs @@ -0,0 +1,69 @@ +using Rssdp.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rssdp +{ + // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS + // Be careful to check any changes compile and work for all platform projects it is shared in. + + /// + /// Used by RSSDP components to create implementations of the interface, to perform platform agnostic socket communications. + /// + public class SocketFactory : ISocketFactory + { + private readonly DeviceNetworkType _DeviceNetworkType; + private string _LocalIP; + + /// + /// Default constructor. + /// + /// A string containing the IP address of the local network adapter to bind sockets to. Null or empty string will use IPAddress.Any. + public SocketFactory(string localIP) + { + _DeviceNetworkType = DeviceNetworkType.IPv4; + _LocalIP = localIP; + if (!String.IsNullOrEmpty(localIP)) + { + var hostName = new Windows.Networking.HostName(localIP); + if (hostName.Type == Windows.Networking.HostNameType.Ipv6) _DeviceNetworkType = DeviceNetworkType.IPv6; + } + } + + /// + /// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port. + /// + /// An integer specifying the local port to bind the socket to. + /// An implementation of the interface used by RSSDP components to perform socket operations. + public IUdpSocket CreateUdpSocket(int localPort) + { + return new UwaUdpSocket(localPort, _LocalIP); + } + + /// + /// Creates a new UDP socket that is a member of the SSDP multicast local admin group and binds it to the specified local port. + /// + /// + /// An integer specifying the local port to bind the socket to. + /// An implementation of the interface used by RSSDP components to perform socket operations. + public IUdpSocket CreateUdpMulticastSocket(int multicastTimeToLive, int localPort) + { + return new UwaUdpSocket(SsdpConstants.MulticastLocalAdminAddress, multicastTimeToLive, localPort, _LocalIP); + } + + /// + /// What type of sockets will be created: ipv6 or ipv4 + /// For WinRT it will be IPv4 + /// + public DeviceNetworkType DeviceNetworkType + { + get + { + return _DeviceNetworkType; + } + } + } +} \ No newline at end of file diff --git a/src/Shared/WinRTSockets/SsdpDevicePublisher.cs b/src/Shared/WinRTSockets/SsdpDevicePublisher.cs new file mode 100644 index 0000000..58b0e8f --- /dev/null +++ b/src/Shared/WinRTSockets/SsdpDevicePublisher.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Rssdp.Infrastructure; + +namespace Rssdp +{ + /// + /// Allows publishing devices both as notification and responses to search requests. + /// + /// + /// This is the 'server' part of the system. You add your devices to an instance of this class so clients can find them. + /// + public class SsdpDevicePublisher : SsdpDevicePublisherBase + { + + #region Constructors + + /// + /// Default constructor. + /// + /// + /// Uses the default implementation and network settings for Windows Phone (Silverlight) and the SSDP specification. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")] + public SsdpDevicePublisher() + : this(new SsdpCommunicationsServer(new SocketFactory(null))) + { + + } + + /// + /// Full constructor. + /// + /// + /// Allows the caller to specify their own implementation for full control over the networking, or for mocking/testing purposes.. + /// + public SsdpDevicePublisher(ISsdpCommunicationsServer communicationsServer) + : base(communicationsServer, GetOSName(), GetOSVersion()) + { + + } + + /// + /// Partial constructor. + /// + /// The local port to use for socket communications, specify 0 to have the system choose it's own. + /// + /// Uses the default implementation and network settings for Windows Phone (Silverlight) and the SSDP specification, but specifies the local port to use for socket communications. Specify 0 to indicate the system should choose it's own port. + /// 3 + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")] + public SsdpDevicePublisher(int localPort) + : this(new SsdpCommunicationsServer(new SocketFactory(null), localPort)) + { + + } + + /// + /// Partial constructor. + /// + /// The local port to use for socket communications, specify 0 to have the system choose it's own. + /// The number of hops a multicast packet can make before it expires. Must be 1 or greater. + /// + /// Uses the default implementation and network settings for Windows Phone (Silverlight) and the SSDP specification, but specifies the local port to use and multicast time to live setting for socket communications. + /// Specify 0 for the argument to indicate the system should choose it's own port. + /// The is actually a number of 'hops' on the network and not a time based argument. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No way to do this here, and we don't want to dispose it except in the (rare) case of an exception anyway.")] + public SsdpDevicePublisher(int localPort, int multicastTimeToLive) + : this(new SsdpCommunicationsServer(new SocketFactory(null), localPort, multicastTimeToLive)) + { + } + + #endregion + + #region Private Methods + + private static string GetOSName() + { + return "Windows"; + } + + private static string GetOSVersion() + { + return "Windows Runtime 8.1"; + } + + #endregion + + } +} \ No newline at end of file diff --git a/src/Shared/WinRTSockets/UdpSocket.cs b/src/Shared/WinRTSockets/UdpSocket.cs new file mode 100644 index 0000000..b195d1c --- /dev/null +++ b/src/Shared/WinRTSockets/UdpSocket.cs @@ -0,0 +1,150 @@ +using System; +using System.Threading.Tasks; +using Rssdp.Infrastructure; +using System.IO; +using System.Linq; + +namespace Rssdp +{ + internal sealed class UwaUdpSocket : IUdpSocket + { + private string _LocalIPAddress; + private int _LocalPort; + private int _MulticastTimeToLive; + + private System.Threading.ManualResetEvent _DataAvailableSignal; + private System.Collections.Concurrent.ConcurrentQueue _ReceivedData; + + private Windows.Networking.Sockets.DatagramSocket _Socket; + + public UwaUdpSocket(string ipAddress, int multicastTimeToLive, int localPort, string localIPAddress) + { + _LocalIPAddress = localIPAddress; + _DataAvailableSignal = new System.Threading.ManualResetEvent(false); + _ReceivedData = new System.Collections.Concurrent.ConcurrentQueue(); + + _MulticastTimeToLive = multicastTimeToLive; + _LocalPort = localPort; + + _Socket = new Windows.Networking.Sockets.DatagramSocket(); +#if !WINRT + _Socket.Control.MulticastOnly = true; +#endif + _Socket.MessageReceived += _Socket_MessageReceived; + + BindSocket(); + _Socket.JoinMulticastGroup(new Windows.Networking.HostName(ipAddress)); + } + + public UwaUdpSocket(int localPort, string localIPAddress) + { + _DataAvailableSignal = new System.Threading.ManualResetEvent(false); + _ReceivedData = new System.Collections.Concurrent.ConcurrentQueue(); + + this._LocalPort = localPort; + + _Socket = new Windows.Networking.Sockets.DatagramSocket(); + _Socket.MessageReceived += _Socket_MessageReceived; + + BindSocket(); + } + + public Task ReceiveAsync() + { + return Task.Run(() => + { + ReceivedUdpData data = null; + + while (!_ReceivedData.TryDequeue(out data)) + { + _DataAvailableSignal.WaitOne(); + } + _DataAvailableSignal.Reset(); + + return data; + }); + } + + public void SendTo(byte[] messageData, UdpEndPoint endPoint) + { + using (var stream = (_Socket.GetOutputStreamAsync(new Windows.Networking.HostName(endPoint.IPAddress), endPoint.Port.ToString()).AsTask().Result)) + { + using (var outStream = stream.AsStreamForWrite()) + { + outStream.Write(messageData, 0, messageData.Length); + outStream.Flush(); + } + } + } + + public void Dispose() + { + try + { + var socket = _Socket; + if (socket != null) + { + _Socket = null; + socket.Dispose(); + } + } + finally + { + GC.SuppressFinalize(this); + } + } + + private void _Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) + { + using (var reader = args.GetDataReader()) + { + var data = new ReceivedUdpData() + { + ReceivedBytes = Convert.ToInt32(reader.UnconsumedBufferLength), + ReceivedFrom = new UdpEndPoint() + { + IPAddress = args.RemoteAddress.RawName, + Port = Convert.ToInt32(args.RemotePort) + } + }; + + data.Buffer = new byte[data.ReceivedBytes]; + reader.ReadBytes(data.Buffer); + + _ReceivedData.Enqueue(data); + _DataAvailableSignal.Set(); + } + } + + private Windows.Networking.HostName GetLocalIPInfo() + { + var localIpInfo = ( + from n + in Windows.Networking.Connectivity.NetworkInformation.GetHostNames() + where n.DisplayName == _LocalIPAddress + && n.IPInformation != null + && n.IPInformation.NetworkAdapter != null + select n + ).FirstOrDefault(); + + if (localIpInfo == null) throw new InvalidOperationException("Could not find adapter from local IP address"); + return localIpInfo; + } + + private void BindSocket() + { + Task t; + if (!String.IsNullOrEmpty(_LocalIPAddress)) + { + Windows.Networking.HostName localIpInfo = GetLocalIPInfo(); + + t = _Socket.BindServiceNameAsync(this._LocalPort.ToString(), localIpInfo.IPInformation.NetworkAdapter).AsTask(); + } + else + t = _Socket.BindServiceNameAsync(this._LocalPort.ToString()).AsTask(); + + t.Wait(); + } + + } +} \ No newline at end of file diff --git a/src/Ukor.sln b/src/Ukor.sln new file mode 100644 index 0000000..37524d3 --- /dev/null +++ b/src/Ukor.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29728.190 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ukor", "Ukor\Ukor.csproj", "{95133A01-FDE8-48CF-B04C-28DC1179C2D8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rssdp.NetCore", "Rssdp.NetCore\Rssdp.NetCore.csproj", "{AB769ED2-0A3D-41B7-A702-0E5B08DA158E}" +EndProject +Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Rssdp.Shared", "Rssdp.Shared\Rssdp.Shared.shproj", "{CC2B3FBE-239E-4967-B566-1A7E4D9EAF5D}" +EndProject +Global + GlobalSection(SharedMSBuildProjectFiles) = preSolution + Rssdp.Shared\Rssdp.Shared.projitems*{ab769ed2-0a3d-41b7-a702-0e5b08da158e}*SharedItemsImports = 4 + Rssdp.Shared\Rssdp.Shared.projitems*{cc2b3fbe-239e-4967-b566-1a7e4d9eaf5d}*SharedItemsImports = 13 + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {95133A01-FDE8-48CF-B04C-28DC1179C2D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {95133A01-FDE8-48CF-B04C-28DC1179C2D8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {95133A01-FDE8-48CF-B04C-28DC1179C2D8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {95133A01-FDE8-48CF-B04C-28DC1179C2D8}.Release|Any CPU.Build.0 = Release|Any CPU + {AB769ED2-0A3D-41B7-A702-0E5B08DA158E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB769ED2-0A3D-41B7-A702-0E5B08DA158E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB769ED2-0A3D-41B7-A702-0E5B08DA158E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB769ED2-0A3D-41B7-A702-0E5B08DA158E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AC90D7BE-340B-4628-A7E3-EF5206FDA8E4} + EndGlobalSection +EndGlobal diff --git a/src/Ukor/Configuration/Application.cs b/src/Ukor/Configuration/Application.cs new file mode 100644 index 0000000..a428002 --- /dev/null +++ b/src/Ukor/Configuration/Application.cs @@ -0,0 +1,81 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using System.Xml.Serialization; + +namespace Ukor.Configuration +{ + [XmlType("app")] + public class Application + { + public enum ActionType + { + None, + CSharp, + CommandLine + } + + [JsonConverter(typeof(StringEnumConverter))] + public enum KeyPress + { + Home, + Rev, + Fwd, + Play, + Select, + Left, + Right, + Down, + Up, + Back, + InstantReplay, + Info, + Backspace, + Search, + Enter + } + + [XmlAttribute("id")] + [JsonRequired, JsonProperty("Id")] + public int Id { get; set; } + + [XmlText] + [JsonRequired] + public string Name { get; set; } + + [XmlIgnore] + [JsonProperty] + public string Description { get; set; } + + [XmlIgnore] + [JsonRequired, JsonConverter(typeof(StringEnumConverter))] + public ActionType Action { get; set; } + + [XmlIgnore] + [JsonProperty] + public CommandLineDetails CommandLineDetails { get; set; } + + [XmlIgnore] + [JsonProperty] + public CSharpDetails CSharpDetails { get; set; } + + [XmlAttribute("subtype")] + [JsonIgnore] + public string SubType { get; set; } = "rsga"; + + [XmlAttribute("type")] + [JsonIgnore] + public string Type { get; set; } = "appl"; + + [XmlAttribute("version")] + [JsonIgnore] + public string Version { get; set; } = "1.0.0"; + + [XmlIgnore] + [JsonProperty] + public KeyPress[] LaunchKeySequence { get; set; } + + [XmlIgnore] + [JsonIgnore] + internal ICSharpAction ActionClass { get; set; } + } +} diff --git a/src/Ukor/Configuration/ApplicationList.cs b/src/Ukor/Configuration/ApplicationList.cs new file mode 100644 index 0000000..f7843a7 --- /dev/null +++ b/src/Ukor/Configuration/ApplicationList.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Xml.Serialization; + +namespace Ukor.Configuration +{ + [XmlRoot("apps", Namespace = "foo")] + public class ApplicationList : List + { + public ApplicationList(Application[] applications) : base(applications){} + } +} diff --git a/src/Ukor/Configuration/ApplicationOptions.cs b/src/Ukor/Configuration/ApplicationOptions.cs new file mode 100644 index 0000000..183e3c6 --- /dev/null +++ b/src/Ukor/Configuration/ApplicationOptions.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace Ukor.Configuration +{ + public class ApplicationOptions + { + [JsonProperty("Applications")] + public Application[] Applications { get; set; } + + [JsonIgnore] + public ApplicationList List => new ApplicationList(Applications); + } +} diff --git a/src/Ukor/Configuration/CSharpDetails.cs b/src/Ukor/Configuration/CSharpDetails.cs new file mode 100644 index 0000000..56360e8 --- /dev/null +++ b/src/Ukor/Configuration/CSharpDetails.cs @@ -0,0 +1,13 @@ +using Newtonsoft.Json; + +namespace Ukor.Configuration +{ + public class CSharpDetails + { + [JsonRequired] + public string AssemblyPath { get; set; } + + [JsonRequired] + public string ClassName { get; set; } + } +} diff --git a/src/Ukor/Configuration/CommandLineDetails.cs b/src/Ukor/Configuration/CommandLineDetails.cs new file mode 100644 index 0000000..6e67fcf --- /dev/null +++ b/src/Ukor/Configuration/CommandLineDetails.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace Ukor.Configuration +{ + public class CommandLineDetails + { + [JsonRequired] + public string Executable { get; set; } + + [JsonProperty] + public string Arguments { get; set; } + + [JsonProperty] + public string WorkingFolder { get; set; } + + [JsonRequired] + public bool WaitForExit { get; set; } + } +} diff --git a/src/Ukor/Configuration/GeneralOptions.cs b/src/Ukor/Configuration/GeneralOptions.cs new file mode 100644 index 0000000..8ef65e6 --- /dev/null +++ b/src/Ukor/Configuration/GeneralOptions.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace Ukor.Configuration +{ + public class GeneralOptions + { + [JsonProperty] + public int KeySequenceLength { get; set; } = 2; + } +} diff --git a/src/Ukor/Configuration/ICSharpAction.cs b/src/Ukor/Configuration/ICSharpAction.cs new file mode 100644 index 0000000..60ef58c --- /dev/null +++ b/src/Ukor/Configuration/ICSharpAction.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Ukor.Configuration +{ + public interface ICSharpAction + { + Task DoActionAsync(Application application); + } +} diff --git a/src/Ukor/Configuration/LocalServerOptions.cs b/src/Ukor/Configuration/LocalServerOptions.cs new file mode 100644 index 0000000..1b2ef0f --- /dev/null +++ b/src/Ukor/Configuration/LocalServerOptions.cs @@ -0,0 +1,8 @@ +namespace Ukor.Configuration +{ + public class LocalServerOptions + { + public string IpAddress { get; set; } + public string RootUrl => $"http://{IpAddress}:8060"; + } +} diff --git a/src/Ukor/Controllers/DiscoveryController.cs b/src/Ukor/Controllers/DiscoveryController.cs new file mode 100644 index 0000000..0cae54d --- /dev/null +++ b/src/Ukor/Controllers/DiscoveryController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using Rssdp; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Ukor.Configuration; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class DiscoveryController : ControllerBase + { + private readonly IOptions _localOptions; + + public DiscoveryController(IOptions localOptions) + { + _localOptions = localOptions; + } + + [HttpGet] + public async Task> ListAsync([FromQuery]string filter) + { + using var deviceLocator = new SsdpDeviceLocator(_localOptions.Value.IpAddress); + return await deviceLocator.SearchAsync(filter ?? "roku:ecp"); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/InputController.cs b/src/Ukor/Controllers/InputController.cs new file mode 100644 index 0000000..1509b78 --- /dev/null +++ b/src/Ukor/Controllers/InputController.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class InputController : ControllerBase + { + [HttpPost] + public IActionResult ReceiveInput() + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/InstallController.cs b/src/Ukor/Controllers/InstallController.cs new file mode 100644 index 0000000..65c0067 --- /dev/null +++ b/src/Ukor/Controllers/InstallController.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class InstallController : ControllerBase + { + [HttpPost] + [Route("{id}")] + public IActionResult Install(string id) + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/KeyDownController.cs b/src/Ukor/Controllers/KeyDownController.cs new file mode 100644 index 0000000..a5827cc --- /dev/null +++ b/src/Ukor/Controllers/KeyDownController.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class KeyDownController : ControllerBase + { + [HttpPost] + [Route("{id}")] + public IActionResult DoKeyDown(string id) + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/KeyPressController.cs b/src/Ukor/Controllers/KeyPressController.cs new file mode 100644 index 0000000..65aa02f --- /dev/null +++ b/src/Ukor/Controllers/KeyPressController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; +using Ukor.Configuration; +using Ukor.Services; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class KeyPressController : ControllerBase + { + private readonly IKeyPressService _service; + + public KeyPressController( + IKeyPressService service) + { + _service = service; + } + + [HttpPost] + [Route("{id}")] + public async Task DoKeyPress(Application.KeyPress id) + { + await _service.HandleKeyPress(id); + return Ok(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/KeyUpController.cs b/src/Ukor/Controllers/KeyUpController.cs new file mode 100644 index 0000000..4bee632 --- /dev/null +++ b/src/Ukor/Controllers/KeyUpController.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class KeyUpController : ControllerBase + { + [HttpPost] + [Route("{id}")] + public IActionResult DoKeyUp(string id) + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/LaunchController.cs b/src/Ukor/Controllers/LaunchController.cs new file mode 100644 index 0000000..82a2074 --- /dev/null +++ b/src/Ukor/Controllers/LaunchController.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using System.Linq; +using System.Threading.Tasks; +using Ukor.Configuration; +using Ukor.Services; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class LaunchController : ControllerBase + { + private readonly IApplicationService _service; + private readonly IOptions _options; + + public LaunchController( + IApplicationService service, + IOptions options) + { + _service = service; + _options = options; + } + + [HttpPost] + [Route("{id}")] + public async Task LaunchAppAsync(string id) + { + if (int.TryParse(id, out var appId)) + { + var app = _options.Value.Applications.FirstOrDefault(x => x.Id == appId); + + if (app == null) + return NotFound(); + + await _service.DoActionAsync(app); + + return Ok(); + } + + return Forbid(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/QueryController.cs b/src/Ukor/Controllers/QueryController.cs new file mode 100644 index 0000000..e6d195a --- /dev/null +++ b/src/Ukor/Controllers/QueryController.cs @@ -0,0 +1,61 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Ukor.Configuration; +using Ukor.StaticResponses; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + [Produces("application/xml")] + public class QueryController : ControllerBase + { + private readonly IWebHostEnvironment _env; + private readonly IOptions _options; + + public QueryController( + IWebHostEnvironment env, + IOptions options) + { + _env = env; + _options = options; + } + + [HttpGet] + [Route("apps")] + public ApplicationList GetApplications() + { + return _options.Value.List; + } + + [HttpGet] + [Route("active-app")] + public QueryActiveAppResponse GetActiveApp() + { + return new QueryActiveAppResponse(); + } + + [HttpGet] + [Route("media-player")] + public QueryMediaPlayerResponse GetMediaPlayer() + { + return new QueryMediaPlayerResponse(); + } + + [HttpGet] + [Route("device-info")] + public QueryDeviceInfoResponse GetDeviceInfo() + { + return new QueryDeviceInfoResponse(); + } + + [HttpGet] + [Route("icon/{id}")] + public IActionResult GetIcon(int id) + { + var bytes = System.IO.File.ReadAllBytes(_env.WebRootFileProvider.GetFileInfo("app.jpg")?.PhysicalPath); + return File(bytes, "image/jpeg"); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Controllers/SearchController.cs b/src/Ukor/Controllers/SearchController.cs new file mode 100644 index 0000000..dc2bbc8 --- /dev/null +++ b/src/Ukor/Controllers/SearchController.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Ukor.Controllers +{ + [Route("[controller]")] + [ApiController] + public class SearchController : ControllerBase + { + [HttpPost] + [Route("browse")] + public IActionResult Browse() + { + return Ok(); + } + } +} \ No newline at end of file diff --git a/src/Ukor/Logging/SsdpLogger.cs b/src/Ukor/Logging/SsdpLogger.cs new file mode 100644 index 0000000..69f032e --- /dev/null +++ b/src/Ukor/Logging/SsdpLogger.cs @@ -0,0 +1,49 @@ +using Microsoft.Extensions.Logging; +using Rssdp; +using Rssdp.Infrastructure; +using System; +using System.Reflection; + +namespace Ukor.Logging +{ + public class SsdpLogger : ISsdpLogger + { + private readonly ILogger _logger; + + public SsdpLogger(ILogger logger) + { + _logger = logger; + } + + public void LogInfo(string message) + { + _logger.LogInformation(message); + } + + public void LogVerbose(string message) + { + _logger.LogTrace(message); + } + + public void LogWarning(string message) + { + _logger.LogWarning(message); + } + + public void LogError(string message) + { + _logger.LogError(message); + } + + public void ApplyTo(SsdpDevicePublisher publisher) + { + var type = typeof(SsdpDevicePublisherBase); + var field = type.GetField("_Log", BindingFlags.NonPublic | BindingFlags.Instance); + + if (field == null) + throw new NullReferenceException("Cannot find _Log private field in SsdpDevicePublisher class."); + + field.SetValue(publisher, this); + } + } +} diff --git a/src/Ukor/Program.cs b/src/Ukor/Program.cs new file mode 100644 index 0000000..e35731d --- /dev/null +++ b/src/Ukor/Program.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Ukor +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + private static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureLogging(logging => logging.AddConsole()) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup() + .UseUrls("http://*:8060"); + }); + } +} diff --git a/src/Ukor/Properties/launchSettings.json b/src/Ukor/Properties/launchSettings.json new file mode 100644 index 0000000..7843ac6 --- /dev/null +++ b/src/Ukor/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "Ukor": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "query/apps", + "applicationUrl": "http://localhost:8060", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/Ukor/Services/ApplicationService.cs b/src/Ukor/Services/ApplicationService.cs new file mode 100644 index 0000000..37914a4 --- /dev/null +++ b/src/Ukor/Services/ApplicationService.cs @@ -0,0 +1,86 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Ukor.Configuration; + +namespace Ukor.Services +{ + public class ApplicationService : IApplicationService + { + private readonly ILogger _logger; + + public ApplicationService(ILogger logger) + { + _logger = logger; + + AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => + { + // check for assemblies already loaded + var dependency = AppDomain.CurrentDomain.GetAssemblies() + .FirstOrDefault(a => a.FullName == args.Name); + + if (dependency != null) + return dependency; + + // Try to load by filename - split out the filename of the full assembly name + // and append the base path of the original assembly (ie. look in the same dir) + var filename = args.Name.Split(',')[0] + ".dll".ToLower(); + var folder = new FileInfo(args.RequestingAssembly.Location).DirectoryName; + + var asmFile = Path.Combine(folder, filename); + + try + { + return Assembly.LoadFrom(asmFile); + } + catch (Exception) + { + return null; + } + }; + } + + public async Task DoActionAsync(Application application) + { + switch (application.Action) + { + case Application.ActionType.None: + return; + case Application.ActionType.CSharp: + _logger.LogInformation($"Launching '{application.Name}' application.."); + + if (application.ActionClass == null) + { + var assembly = Assembly.LoadFile(application.CSharpDetails.AssemblyPath); + var type = assembly.GetTypes().FirstOrDefault(x => x.Name == application.CSharpDetails.ClassName); + + if (type != null) + application.ActionClass = (ICSharpAction) Activator.CreateInstance(type); + } + + if (application.ActionClass != null) + await application.ActionClass.DoActionAsync(application); + break; + case Application.ActionType.CommandLine: + _logger.LogInformation($"Launching '{application.Name}' application.."); + + var i = new ProcessStartInfo(application.CommandLineDetails.Executable, + application.CommandLineDetails.Arguments) + { + WorkingDirectory = application.CommandLineDetails.WorkingFolder + }; + + var p = Process.Start(i); + + if (application.CommandLineDetails.WaitForExit) + p?.WaitForExit(); + + return; + } + } + } +} diff --git a/src/Ukor/Services/IApplicationService.cs b/src/Ukor/Services/IApplicationService.cs new file mode 100644 index 0000000..ec01b8a --- /dev/null +++ b/src/Ukor/Services/IApplicationService.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using Ukor.Configuration; + +namespace Ukor.Services +{ + public interface IApplicationService + { + Task DoActionAsync(Application application); + } +} \ No newline at end of file diff --git a/src/Ukor/Services/IKeyPressService.cs b/src/Ukor/Services/IKeyPressService.cs new file mode 100644 index 0000000..07f4345 --- /dev/null +++ b/src/Ukor/Services/IKeyPressService.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using Ukor.Configuration; + +namespace Ukor.Services +{ + public interface IKeyPressService + { + Task HandleKeyPress(Application.KeyPress keyPress); + } +} \ No newline at end of file diff --git a/src/Ukor/Services/KeyPressService.cs b/src/Ukor/Services/KeyPressService.cs new file mode 100644 index 0000000..eac080b --- /dev/null +++ b/src/Ukor/Services/KeyPressService.cs @@ -0,0 +1,53 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Concurrent; +using System.Linq; +using System.Threading.Tasks; +using Ukor.Configuration; + +namespace Ukor.Services +{ + public class KeyPressService : IKeyPressService + { + private readonly IOptions _generalOptions; + private readonly IOptions _applicationOptions; + private readonly IApplicationService _appService; + private readonly ILogger _logger; + private readonly ConcurrentQueue _presses = new ConcurrentQueue(); + + public KeyPressService( + IOptions generalOptions, + IOptions applicationOptions, + IApplicationService appService, + ILogger logger) + { + _generalOptions = generalOptions; + _applicationOptions = applicationOptions; + _appService = appService; + _logger = logger; + } + + public async Task HandleKeyPress(Application.KeyPress keyPress) + { + _logger.LogInformation($"Received KeyPress {keyPress} at {DateTime.Now.ToLongTimeString()}"); + + _presses.Enqueue(keyPress); + + while (_presses.Count > _generalOptions.Value.KeySequenceLength) + { + _presses.TryDequeue(out _); + } + + var matched = _applicationOptions.Value.Applications.FirstOrDefault(x => + x.LaunchKeySequence != null && x.LaunchKeySequence.SequenceEqual(_presses)); + + if (matched != null) + { + _logger.LogInformation($"Launching '{matched.Name}' application.."); + _presses.Clear(); + await _appService.DoActionAsync(matched); + } + } + } +} diff --git a/src/Ukor/Startup.cs b/src/Ukor/Startup.cs new file mode 100644 index 0000000..9140f47 --- /dev/null +++ b/src/Ukor/Startup.cs @@ -0,0 +1,220 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.AspNetCore.Rewrite; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Rssdp; +using System; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using Ukor.Configuration; +using Ukor.Logging; +using Ukor.Services; + +namespace Ukor +{ + public class Startup + { + private SsdpDevicePublisher _publisher; + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + // ReSharper disable once UnusedAutoPropertyAccessor.Global + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + var appOptions = Configuration.Get(); + var generalOptions = Configuration.Get(); + + ValidateOptions(generalOptions, appOptions); + + services.Configure(Configuration); + services.Configure(Configuration); + services.Configure(x => x.IpAddress = GetLocalIpAddress()); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.AddControllers() + .AddXmlSerializerFormatters() + .AddMvcOptions(x => + { + var formatter = (XmlSerializerOutputFormatter) + x.OutputFormatters.FirstOrDefault(y => y.GetType() == typeof(XmlSerializerOutputFormatter)); + + if (formatter != null) + { + formatter.WriterSettings.OmitXmlDeclaration = false; + formatter.WriterSettings.Indent = true; + } + }); + } + + private void ValidateOptions(GeneralOptions generalOptions, ApplicationOptions appOptions) + { + if (appOptions.Applications.Any(x => + x.LaunchKeySequence != null && x.LaunchKeySequence.Length != generalOptions.KeySequenceLength)) + { + throw new InvalidDataException( + $"All LaunchKeySequence arrays must have a length of {generalOptions.KeySequenceLength}."); + } + + if (appOptions.Applications.Any(x => x.Action == Application.ActionType.CSharp && x.CSharpDetails == null)) + { + throw new InvalidDataException("All CSharp actions must have a CSharpDetails property defined and populated."); + } + + if (appOptions.Applications.Any(x => + x.Action == Application.ActionType.CommandLine && x.CommandLineDetails == null)) + { + throw new InvalidDataException("All CommandLine actions must have a CommandLineDetails property defined and populated."); + } + + if (appOptions.Applications.Any(x => + x.Action == Application.ActionType.CSharp && + (string.IsNullOrEmpty(x.CSharpDetails.AssemblyPath) || + string.IsNullOrEmpty(x.CSharpDetails.ClassName)))) + { + throw new InvalidDataException("All CSharp actions must have both an AssemblyPath and ClassName defined."); + } + + if (appOptions.Applications.Any(x => + x.Action == Application.ActionType.CommandLine && + string.IsNullOrEmpty(x.CommandLineDetails.Executable))) + { + throw new InvalidDataException("All CommandLine actions must have an Executable defined."); + } + + if (appOptions.Applications.Any(x => x.Id < 1000)) + { + throw new InvalidDataException("All applicaiton Id values must be 1000 or above."); + } + + var duplicates = appOptions.Applications.GroupBy(x => x.Id).Select(x => new {Id = x.Key, Count = x.Count()}) + .Where(x => x.Count > 1).ToArray(); + + if (duplicates.Any()) + { + var duplicated = duplicates.Select(x => x.Id).ToArray(); + throw new InvalidDataException($"The following application Id values are duplicated: {string.Join(',', duplicated)}"); + } + } + + private string GetLocalIpAddress() + { + // Find the network card being used for internet-based traffic + // and then get the IP address associated with it. + using var client = new UdpClient(); + var ep = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53); + client.Connect(ep); + var local = (IPEndPoint) client.Client.LocalEndPoint; + var ip = local.Address.ToString(); + client.Close(); + return ip; + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseRewriter(new RewriteOptions() + .AddRewrite("dial/dd\\.xml", "Device.xml", true)); + + var options = new DefaultFilesOptions(); + options.DefaultFileNames.Clear(); + options.DefaultFileNames.Add("Device.xml"); + app.UseDefaultFiles(options); + + app.UseStaticFiles(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + + ConfigureSsdp( + app.ApplicationServices.GetService>(), + app.ApplicationServices.GetService()); + } + + private void ConfigureSsdp( + IOptions localServerOptions, + SsdpLogger logger) + { + var device = new SsdpRootDevice() + { + CacheLifetime = TimeSpan.FromHours(1), + Location = new Uri($"{localServerOptions.Value.RootUrl}/"), + UrlBase = new Uri($"{localServerOptions.Value.RootUrl}/"), + DeviceType = "urn:roku-com:device:player:1-0:1", + DeviceTypeNamespace = "schemas-upnp-org", + DeviceVersion = 1, + Uuid = "29600009-5406-1005-8080-1234567890ab", + Udn = "uuid:29600009-5406-1005-8080-1234567890ab", + FriendlyName = "Ukor Server", + Manufacturer = "Roku", + ManufacturerUrl = new Uri("http://www.roku.com/"), + ModelDescription = "Roku Streaming Player Network Media", + ModelName = "Ukor Server", + ModelNumber = "3810EU", + ModelUrl = new Uri("http://www.roku.com/"), + SerialNumber = "YH009E000001", + Usn = "uuid:roku:ecp:YH009E000001", + NotificationType = "roku:ecp" + }; + + device.CustomResponseHeaders.Add(new CustomHttpHeader("Server", "Roku/9.2.0, UPnP/1.0")); + device.CustomResponseHeaders.Add(new CustomHttpHeader("device-group.roku.com", "1E3DE502613555ACA315")); + device.CustomResponseHeaders.Add(new CustomHttpHeader("WAKEUP", "MAC=ac:ae:01:02:03:04, Timeout=10")); + + var ecpService = new SsdpService + { + ServiceType = "ecp", + ServiceTypeNamespace = "roku-com", + ServiceVersion = 1, + Uuid = "ecp1-0", + ScpdUrl = new Uri("ecp_SCPD.xml", UriKind.Relative), + ControlUrl = new Uri("roku:0") + }; + + device.AddService(ecpService); + + var dialService = new SsdpService + { + ServiceType = "dial", + ServiceTypeNamespace = "dial-multiscreen-org", + ServiceVersion = 1, + Uuid = "dial1-0", + ScpdUrl = new Uri("dial_SCPD.xml", UriKind.Relative), + ControlUrl = new Uri("roku:1") + }; + + device.AddService(dialService); + + _publisher = + new SsdpDevicePublisher {StandardsMode = SsdpStandardsMode.Relaxed, Log = logger}; + + _publisher.AddDevice(device); + _publisher.NotificationBroadcastInterval = TimeSpan.FromMinutes(10); + } + } +} diff --git a/src/Ukor/StaticResponses/QueryActiveAppResponse.cs b/src/Ukor/StaticResponses/QueryActiveAppResponse.cs new file mode 100644 index 0000000..80bace9 --- /dev/null +++ b/src/Ukor/StaticResponses/QueryActiveAppResponse.cs @@ -0,0 +1,10 @@ +using System.Xml.Serialization; + +namespace Ukor.StaticResponses +{ + [XmlType("active-app")] + public class QueryActiveAppResponse + { + [XmlElement("app")] public string App { get; set; } = "Roku"; + } +} diff --git a/src/Ukor/StaticResponses/QueryDeviceInfoResponse.cs b/src/Ukor/StaticResponses/QueryDeviceInfoResponse.cs new file mode 100644 index 0000000..6f60b3e --- /dev/null +++ b/src/Ukor/StaticResponses/QueryDeviceInfoResponse.cs @@ -0,0 +1,80 @@ +using System.Xml.Serialization; + +namespace Ukor.StaticResponses +{ + [XmlType("device-info")] + public class QueryDeviceInfoResponse + { + [XmlElement("udn")] public string Udn { get; set; } = "29600009-5406-1005-8080-1234567890ab"; + [XmlElement("serial-number")] public string SerialNumber { get; set; } = "YH009E000001"; + [XmlElement("device-id")] public string DeviceId { get; set; } = "E43979390000"; + + [XmlElement("advertising-id")] + public string AdvertisingId { get; set; } = "a5f89592-c53c-5ea5-9f84-dc663195d91d"; + + [XmlElement("vendor-name")] public string VendorName { get; set; } = "Roku"; + [XmlElement("model-name")] public string ModelName { get; set; } = "Ukor Server"; + [XmlElement("model-number")] public string ModelNumber { get; set; } = "3810EU"; + [XmlElement("model-region")] public string ModelRegion { get; set; } = "US"; + [XmlElement("is-tv")] public bool IsTv { get; set; } + [XmlElement("is-stick")] public bool IsStick { get; set; } = true; + [XmlElement("supports-ethernet")] public bool SupportsEthernet { get; set; } + [XmlElement("wifi-mac")] public string WifiMac { get; set; } = "ac:ae:01:02:03:04"; + [XmlElement("wifi-driver")] public string WifiDriver { get; set; } = "realtek"; + [XmlElement("network-type")] public string NetworkType { get; set; } = "wifi"; + [XmlElement("network-name")] public string NetworkName { get; set; } = "not-real"; + [XmlElement("friendly-device-name")] public string FriendlyDeviceName { get; set; } = "Ukor Server"; + [XmlElement("friendly-model-name")] public string FriendlyModelName { get; set; } = "Ukor Server"; + + [XmlElement("default-device-name")] + public string DefaultDeviceName { get; set; } = "Ukor Server - YH009E000001"; + + [XmlElement("user-device-name")] public string UserDeviceName { get; set; } = "Ukor Server"; + [XmlElement("user-device-location")] public string UserDeviceLocation { get; set; } = "The moon."; + [XmlElement("build-number")] public string BuildNumber { get; set; } = "509.20E04807A"; + [XmlElement("software-version")] public string SoftwareVersion { get; set; } = "9.2.0"; + [XmlElement("software-build")] public string SoftwareBuild { get; set; } = "4807"; + [XmlElement("secure-device")] public bool SecureDevice { get; set; } = true; + [XmlElement("language")] public string Language { get; set; } = "en"; + [XmlElement("country")] public string Country { get; set; } = "GB"; + [XmlElement("locale")] public string Locale { get; set; } = "en_GB"; + [XmlElement("time-zone-auto")] public bool TimeZoneAuto { get; set; } = true; + [XmlElement("time-zone")] public string TimeZone { get; set; } = "Europe/United Kingdom"; + [XmlElement("time-zone-name")] public string TimeZoneName { get; set; } = "Europe/United Kingdom"; + [XmlElement("time-zone-tz")] public string TimeZoneTz { get; set; } = "Europe/London"; + [XmlElement("time-zone-offset")] public int TimeZoneOffset { get; set; } = 60; + [XmlElement("clock-format")] public string ClockFormat { get; set; } = "24-hour"; + [XmlElement("uptime")] public int Uptime { get; set; } = 100; + [XmlElement("power-mode")] public string PowerMode { get; set; } = "PowerOn"; + [XmlElement("supports-suspend")] public bool SupportsSuspend { get; set; } + [XmlElement("supports-find-remote")] public bool SupportsFindRemote { get; set; } = true; + [XmlElement("find-remote-is-possible")] public bool FindRemoteIsPossible { get; set; } + [XmlElement("supports-audio-guide")] public bool SupportsAudioGuide { get; set; } + [XmlElement("developer-enabled")] public bool DeveloperEnabled { get; set; } + [XmlElement("keyed-developer-id")] public string KeyedDeveloperId { get; set; } = string.Empty; + [XmlElement("search-enabled")] public bool SearchEnabled { get; set; } = true; + + [XmlElement("search-channels-enabled")] + public bool SearchChannelsEnabled { get; set; } = true; + + [XmlElement("voice-search-enabled")] public bool VoiceSearchEnabled { get; set; } = true; + [XmlElement("notifications-enabled")] public bool NotificationsEnabled { get; set; } = true; + [XmlElement("notifications-first-use")] public bool NotificationsFirstUse { get; set; } + + [XmlElement("supports-private-listening")] + public bool SupportsPrivateListening { get; set; } = true; + + [XmlElement("headphones-connected")] public bool HeadphonesConnected { get; set; } + [XmlElement("supports-ecs-textedit")] public bool SupportsEcsTextEdit { get; set; } = true; + [XmlElement("supports-ecs-microphone")] public bool SupportsEcsMicrophone { get; set; } = true; + [XmlElement("supports-wake-on-wlan")] public bool SupportsWakeOnWlan { get; set; } + [XmlElement("has-play-on-roku")] public bool HasPlayOnRoku { get; set; } = true; + [XmlElement("has-mobile-screensaver")] public bool HasMobileScreensaver { get; set; } + [XmlElement("support-url")] public string SupportUrl { get; set; } = "github.com/cpwood/ukor"; + [XmlElement("grandcentral-version")] public string GrandCentralVersion { get; set; } = "2.9.57"; + [XmlElement("has-wifi-extender")] public bool HasWifiExtender { get; set; } + [XmlElement("has-wifi-5G-support")] public bool HasWifi5gSupport { get; set; } = true; + [XmlElement("can-use-wifi-extender")] public bool CanUseWifiExtender { get; set; } = true; + + } +} diff --git a/src/Ukor/StaticResponses/QueryMediaPlayerResponse.cs b/src/Ukor/StaticResponses/QueryMediaPlayerResponse.cs new file mode 100644 index 0000000..6a6a0c7 --- /dev/null +++ b/src/Ukor/StaticResponses/QueryMediaPlayerResponse.cs @@ -0,0 +1,27 @@ +using System.Xml.Serialization; + +namespace Ukor.StaticResponses +{ + [XmlType("player")] + public class QueryMediaPlayerResponse + { + [XmlAttribute("error")] + public bool Error { get; set; } + + [XmlAttribute("state")] public string State { get; set; } = "close"; + + [XmlElement("format")] + public QueryMediaPlayerResponseFormat Format { get; set; } = new QueryMediaPlayerResponseFormat(); + + [XmlElement("is_live")] + public bool IsLive { get; set; } + } + + public class QueryMediaPlayerResponseFormat + { + [XmlAttribute("audio")] public string Audio { get; set; } = "aac_adts"; + [XmlAttribute("captions")] public string Captions { get; set; } = "none"; + [XmlAttribute("drm")] public string Drm { get; set; } = "none"; + [XmlAttribute("video")] public string Video { get; set; } = "mpeg4_10b"; + } +} diff --git a/src/Ukor/Ukor.csproj b/src/Ukor/Ukor.csproj new file mode 100644 index 0000000..a7da6ef --- /dev/null +++ b/src/Ukor/Ukor.csproj @@ -0,0 +1,17 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + diff --git a/src/Ukor/appsettings.Development.json b/src/Ukor/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/src/Ukor/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/src/Ukor/appsettings.json b/src/Ukor/appsettings.json new file mode 100644 index 0000000..aedeeee --- /dev/null +++ b/src/Ukor/appsettings.json @@ -0,0 +1,27 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + "KeySequenceLength": 1, + "Applications": [ + { + "Id": 1000, + "Name": "Launch Notepad", + "Action": "CommandLine", + "CommandLineDetails": { + "Executable": "notepad.exe", + "Arguments": null, + "WorkingFolder": null, + "WaitForExit": false + }, + "LaunchKeySequence": [ + "Fwd" + ] + } + ] +} diff --git a/src/Ukor/wwwroot/Device.xml b/src/Ukor/wwwroot/Device.xml new file mode 100644 index 0000000..3da5b05 --- /dev/null +++ b/src/Ukor/wwwroot/Device.xml @@ -0,0 +1,34 @@ + + + 1 + 0 + + + urn:roku-com:device:player:1-0 + Ukor Server + Roku + http://www.roku.com/ + Roku Streaming Player Network Media + Ukor Server + 3810EU + http://www.roku.com/ + YH009E000001 + uuid:29600009-5406-1005-8080-1234567890ab + + + urn:roku-com:service:ecp:1 + urn:roku-com:serviceId:ecp1-0 + + + ecp_SCPD.xml + + + urn:dial-multiscreen-org:service:dial:1 + urn:dial-multiscreen-org:serviceId:dial1-0 + + + dial_SCPD.xml + + + + \ No newline at end of file diff --git a/src/Ukor/wwwroot/app.jpg b/src/Ukor/wwwroot/app.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d19c385e0e6fedf726a53e6cefed15ce8059765e GIT binary patch literal 10937 zcmd6NcT`i|w(q7_=|yTpP^1fj0s;a200EIEy+oyhG^Gh4K@boS5D*X)qVytFLYIIv zQ5316cceo?34w(0;&;cm=iM{TJ>$Oj#~W{DteKU)*WP>n=3HyGowM&P zpr8QmlRv=O0^qI_;^7JaMn-@n008s=H3dIFLmp9(y9R~8e~xu2t^ibjAEzXrln2NZ zU?=yx7KC-* zQ&UmX($LcW-RT(UY3Uf~Xld!0=ouLQH1dd1gTxp7MW0 z&e{QXdSH^`A{B)QK*>%)#ZGb71%Sxh)BMfe9}s^x3Q8(!8d^Fs8%*R0NH#M2R8(Z9 zX~-OtXNQx|1Jvv^93t{sw4A07=q~zmDLhO0KrgCY)4^>vh!MN?@NpyqBMn|N$z1#W*=JzZtt*mWqot#}<-P}Dqp9BO31&2Hhjd~s(6Z;}A zJ~i!idPe4(tn9q}g2JNWlG3u;Pj$%phQ_AmFP&Z8J-uK1z6}kJjE>p@d`{ z=LM;2=xGV(3^{i>dcp2NphDC{rE`~iXXCw;aQ?PbOcjyM4M&MNE$b*>bCg2!S)IFH zx~Qk?3qNpq0ZTuwbmH%$(zocr#wUW(U!y4d4n$F`8(q0Qu>?`ZsU>e7lU z@r3s<87~4Sa(*{&aF=cq@@ftfPP4%mt-pcKjrpHg#X;TAfT$;}6k%>kyIV)tBa7jk z*F3St*ua8dldoZNIX*RYPIKR1E!AvV-*22!$TtmtAkkZy0hjVPs>)VTsN(+AuO4`~ z*7H$y;!0nrQgL5JSpNP#uIMxd?%_MWYU2Q2a_d~L_~@4Ty#`AY6`pV0qVAV=rTr0Q zkg`?#&uOa^tMw2yR}d&RF;o@{A4Oz?pMKlFBkcbyMvh6=-Eh}o*nxi|GJZq-f1q)y zari^Xc;{m9_GfJB+(K-G`BujDjRxV1=-)5uR2?d}ctu5_iQfwDM{ZAFafQr+Cfsla zcqzgK;*Hpa;tn{|rH^42NEx&H!u#fLnE8s+_*^%m&s)$jmMK^L44G0)d*`P(n_vS= zI3y%@Lai4W3RO%Qk=hFwOCB#wjwcj zmBD(N7w}D(c!7#r3-zUe)ePtRu1EQ;t~8`K*WC$8GXDwFyIEg{c+hcNQ2l1N+=!5V zeJ|4(<=awo5*#C_CEcKW60JvQo|Ka~fa8w75ZWAjd$_DN=??r%HEFnje@7fK=vbw> z^Z5AAFB-f<*J1gXH8OI>@)e*kDmO2c!)}wJ zQ1b{#O+yUoA$(%W5bx$j%^4DjwLI#cEP%dk!I?JQ3Qm$I)Cry|ZxP6fJKEM5_Wvn` zvG3%T4!T|#jC%{0O<1l2CtZ8jQ{?^Dvd3_{52X{%+h+q!ESCX`?u@-wy)Syx>*YS3 zevxzdWd76Q`)=NVlP8a+Tl{jH81yCv-VLGJm^-I&Va;#DRu<`Wsi@G&4<%ir+;K!5 z$b9u)gcdcG)!l;9d*%tB?rx6qn^u^!+A4Xp7|19R6nC$KdN(d(LhSuh@}oWOZ#pHVlq{kVz{X55Dm?>&D8Z0&G&-!rxnwm1WPk)V$2B)zRi zOwF27PwqVThs1&UR&N#CMkPsk4j415nowu*2XWr?5;d)(QZu2YEe7~1VI=ib*9Z;P zGazz@^o@<^e43SyKU(;41O7ro=&M_!`|xjsL3+BI`Q}P}-!nDIkBx*;d;_G?&qsX9 zd4qLJzbb7M27h)cwcrEZgvypQR7iXSi|@*vN>sfvg;>LqxJ)Q?fd7^N9DX$LTM*1$ z&_om51wMY%aRx9~J)-9C8Us(l#J(*go_^)HU9sUrBQj8Mu?>_|Blw2@p4DVJ$rSQ? z@K`)ye8BDX156+BiSG(G>9JnRBxrVw^jen&<_HH1U4DJfNomld>O55-Uad^3@)_g4iJxd~@%oa#z@$Z(NxZ$%}KxVsJHl!%N)6ilAz! zc{J&<5BXlV^1N%QtSX^43j<7(6}xdzEtldiKaj3%&9`4S6j>*EEhc}vTdXrGQ(3Xp z@~Kf@B;{7G*dsm%V{Q8R_?f!43vPjczCXMPb_z{6188)}x2%K;lBqZTVh@C4Z9gQl z$M)p*tPmzK=T~%LA`AcG)7!om0hq~@O-`f5O@~)h`+A)yOhECr#2LVlIuo*ldxTL( z$M(;)RuB19$?Q@;-BwFAh>kb|iZnUbAth%(ZSuA+CIL5&mnnt@;IEZ_znl}YO|z0X z8QnBo`HEFx(uF!YaJo41mXwr)Wcw>ty{d0=c;zUZFR2e3`yB3>$k_QvwN6|MOZ}pi z=}na3qt($|Ii+jknt5$$&#}UtYDLWkSi&fJG35v*u%uDkU1vV&X$n`IB$|O0uT)L2 zZm|8^P=gAWPolRWh&T`#Pv7}yDB}kV_Mlo=uI?wEsl~f82e0Y2mU3gv2IPA!+&_Io zPp9Ky@c5J8{k;eqW~m&DdmbE20gs+k|B&?Q6$xe^LgmWEJ6^@F66`|0EiLi(R>kv9 z{wyuI-Jd#Z;Ba3XKH7O+MC3&PeZ-Z6D#7ElS=9U+a09Am4liE3&9s1G*>F^88PVXy zCtJsS@5&W!Y;8hB8{*3Zrrnc;%b= z*rNp0d}%f=r=kww1Y!1IS{S}2l;DE95Ah{{J#IMM+}YIxT^PVv1|nx@s}rwlZ0be8 z$t%X-RGl1mILWkn6-^OchjyQL{uQ&SvzNZVlF;wSa2<}U)@vEp+tvh?2d<#%leeY{ zNMndk#_=%PjmzhE0u3oo)TcWW?C{B1zOtdw99PaI8C+vI9E|ExlaJzJ878K%2D=C!o`@4z&VUZ8!wR{sY~QZAsT)?+USTRci(rcNCr?RXN!kuSHH;z- zlhhY_1=_!Y<+{{v#eQ4+mVP6&!aZ|hAD*|l27~vsdPgk~r7;I$g>k91hFg$rLu7+t zaIul`i1mfYF=`|-+9^qiNzlK-HD6`9x6GlwnT7|Q0o2Q<1O#jInm}Dro7RDpbmuf#(@ehhY#bYt7){B~8pnqdU~Bw7O}sCrIyF== zTXMoti|*UdCa@gC#nB(XY7Rnl^_xrx_Tn~#Bs zQX5Ye25|F&j43>hsSB+EQ8cs7RFT)ccw8+~5<2I4_D5K=Q{@je?A@{K?!$~Q=91T+$*NCvKZkUiMbv_bY+g&f zJ>ClFUQnIf3)BK+4b514tKhAvMunQQGtbLc854B)KEEf;@{lm%IkGKRA$1^Vpl{!_ zvC1VVo%het>j|bk1D+mUWvf2}UfWdlf7+3)uq-IK50SP^a)1=OthVfdsg_tdYOLdj zJ605yi|PV-yJd-0Ta`aNkBmSjA;BG?kl2I90ArT5vF7l^=Iw@0H4WBzapBze0J>k6 zbueM&?=n#7LJ5Q2CwqJb7Gf*nK}V0Q1}*5O%Ra4t%b~ew@VLiGxxmpjLPj&)g>8v4 zg8S98A6g0~G%C9MDfepH-!oa_L{8!xc#@xAR*pqEL;NZc;NRq`! z`h6|x$aD9tHbyqgD~#?%HG8ff-66f~MCZrn^N$q%;1e6RQ#`zLrQ%#9#?A>`X?YV2$*kYHe+drTHtpl5+oI!LjG@ zeb3s<_GJL*9KnrjY>mNA$>Tv^7C|rJTopTNT7qHkF+P#sgUjytzhX9M*Q8(DSM&_y z2&w8nCg!EW_qY)}9=dtK6O9 z%6x)rSwJP$fnJbk2Vo-khi+*`=(@Mwdd8`LP5z+9O4p>`6#A%KS=L|Zu3zd|UERqT z{uAMS(M5-xsi@uynYZNL$$jvs5P-fV6R!qkxL-`(=YC1sT>ylh5kU_LMoTmBcO5u=XYdZN1~Gy>(jxN?P^ z{n-dG7g_QPa`;O>$>QYkL}5&fBW>>;Wy_YUd1FNlBh`Bin(o&Wy?@Vq%<+on&Z={P zyqM8*V!nBl^@xt>>>1!PhnLXX9w3Ol2&jsf108->m*q^yY!hT~xx9^bVG--XVQ;=P zpbQ9x3%x`kErJ=|%6N|?59@bi#d2z+lzI|_1B-4_jMOjrHJt$gS8$cSmFC6fDW8q= z+Wjj1vt4!tVx#BZ+Trw3{o`11qR=yJR9E&r1m+{y$(VJ$0URi+RK;lz(XH4?>GZjc zo7OI{mQOq;&w!pLqGlK&G-mdgG>$&71D|tOg$4zwhJOqiv#hSj{u)i&&a{} zlKn-kkc5B|)K+n}`IJj|FXiY;zvS?)!08Wo)(QH#axC-4XV%}v@ORFcr0D^MJd_Fe z{XdyCR+G~>HBq``@FQ?EiK8Wi^ud?mCmK=m6{x)VJlqFA(ri?F8R=znx@<+(-(0f#pI| zpmpCq(0DHA3jV1d4UN*r8C|lORngnilOt%5dg2z7X@y)LS_K}&=WiX)(+DFA2^O%g zxLk6bQAtj~T1(WZVA}4xs5jv4VJX@JUM~v8l}76uDwvV>&H&Mw#nxh`n0b329!6rF zn=c_q7Lu)U2H-Et5jlKu$>i1h7OGGBi4jIzj+!^RyZ|R})3{RGhVg*$9wr@-aouz} zSX%PQS6Neqr4zx^Y?PbtGf3^+7xf$SWC1NaD-O}qG^?5Z9@8|sGfts&*dM`q-Q%jp zBLJ{4DJd(*-CG(Vfl{X5l|L+u-}r0SmcusIw3n}(p#+;a0u&LKcJFaIQmX! zh$lWEd*x`T2yGm}84vHiUgOP=j@Zs#Kz&xUvXW0b1EBP=3lNKr+LfQFyL@u!1F#OJ zzF3^Q8?Ja6MSTX`nxB+9TspD;FK#m)I1wN*=sXZEByZPqcspN7AEqjOS@oeYH@baW z(zEitSyecu&sF#dd!= zr#;i;2R}>-bB)yb_O(&zcjuxWS(0w7+&?NpTrYhd+7VFANcMvVKK`trd{Y3_MK{9U zO?FquqYS_ymF+QM>`8gHB-gfx6{SOuo552))KCFGAfdY|*qAnJ;V*BW=uC zM&!fv6uKzY&S?Mjs}|SfZuIIcad=39h6Uvj0xrKAbZf`d{gS)HCBf+$D4!UZ$iaW(>-X;Y2{Z{{#a-q*e zLt8+IUTsVfJex;E{|*=D90^pg_gq^aIRl1l4s|oJqMBH`=!3auXTT>^=S>hLQFU;M z&7+wm#YL#L_32JZLqj7yBVo$;2BXI2p#ve) zx2Ex2P6$r)47>4$?if$+#P;QWD5xa&*s#G^wR zUQ_=e!=SDjkei9uey(!+qML25q`Z%$TIG!N6uNH?E{d7ssf7mGI|zPxCs$wkCRi(Xg$0qErPp0AKm7)eRf4w5<+fCQw;e(@W$CqBv;QC0Aq|~T|5SLPD>{dm7 zNZOKHoEVnT?(5{;+$lAjdA-LALc(U9Ai_9vtf_Xxr(S^}AeeyEi0YCKR4b0ENiW!1 zp`-b|y0gCa78ZwiOq9XMG(>Ni*<570JonnE_;U~@ViV#A=Sn|hvaj^F-lGz5;sTa^ zREqyp0Uw5JE-e<)X+?T?pk(eL^52CT@vWO$f>mTmM#o;!^U9fH$eyIbQ~RMEz!8uC%Ks8(^@+o3DSfLq|qjh=YS9esIn} z>Q80y*e7L=aj)-PCXMh)p@O|Zr>$8h69jJ3IGhFLT@`7GIx}Uhj9e`XxNGNfghL9n@sobYvWA)nnNI;IN8s;Xj5f zRS`MB3wm7@j=t=8W0Mi0T=>n*w*;Y-ih_qvotP-e!CRwA82@t5{dFggy@8^En!|UZ zJ$%Kqzgan-*S$_JW`JZE7EKm4DyF_uWcQ_mpj_u~4UO#ry=W$VX7*tnw=nz3 zr?R#ZtODApMTDLZ7RaGHf)1+qtD=sT#XX}wBg6kW5{Y~%?O*jP`0!Bta&7y{fuBTU zOT%bxEITts!C)-4jHx(%Gh#TTXs6*f;}b5(X~~ z8y@GHmT#)|pkOCOpUbA_9Q6A$dDH4r!J}_dJ(1yp5A!}IH@$SCFX^_qke8+PFG}!q zYsN%@PHfPAC!#Jmq3xW#1G~$jFH^#rlk5D zfB61fx#ETUqZv@^KwEzP>lR8Kbnh%yn8Hs12Sq z-a#K|yp8hP0G5OO>~i}$LS zouRMTMaC&+HC*RTT;{O50LBej4~#I9B5EKR*ijApZ|?}5d%nbh)hb^1z10!VGeAkP z&C~1*P=pMhztUH372JNAqACu2MSjOkp-H^ZxJcGmk)p}xYEz!g)hiS=`cV;{dY!WF zR8r=5S)}9z^IgJBXzLO1y_dsC2Ft&`zGqH5D&K>XSXA-JG)G+r?|SyRguu;SSeFTE-MXgXq|H=XZN&w1+$A*sp)&X)t>zB@^GO>_Og-D zmAgALi$}-hkX`U~tX!<88=pb9iB+jK`RI24XNxq)M4y3Xk;XkV*Y||j%+D|b_;v0i z!~6UrLrW^O5lYV(-&s6$>1$U6MqigZkG`3{`kvAD-BUqJ=_VV7AAmk}Qmm9E|L)`a z*#HJK1t&4GVx(|F7%xvpwNWcG?4_9&jo1YNzSzksfmcE&$85__q?rA*V;|I!xM0JG zy4WuGWbcwj1-4}|aYUxn^JTeEMOk{}&l%eB`4=3wbs%Q?z`d zUAQXhttCWbbyt`RvwcTh;`eC!JG39`^Y5x=90xsFS+nE>=15lIdqU8yr3SsV}#}n!m^1Wvyv55C0HsS zcXJ{JLKsKpqnMg2_a_# z9pt^^!v}vbFfonq?7Uu&c9cf%fZIzAVRA@ISO^I7;CSgJ+dM3N-F*90Gh1$FZ_k0HzK`Kk_O zw4KCKn-0a_Q$^%cXp;ZF!kr9h87^kLH*(=)YOO;mxcbUqacn;Sz0rKGR72;CI2vx< zXaB;Ok)w;hB_0ad^2dJjo%n?NfC2Y(XQj^FLVpN1{QmQIZJNA&$D#k>JPW^z% z!8^iXU>&R+4q1%{=WhrHAuwlvjbV#7+pu|~ zZ5yW7j!#Un{Mi`Z%>LQHIOkG}WnJpAmrs2yST8R_CEw;lii6NCDT%N5!{W2tL_*O^ z#ryO#%k@ULt2W#z`bk9}f>hqqG5nn7G)+s(296tWa`@tLHr9FQ@XUv=UEH!oE-b0ge!b{ zV>bJ#eM{3xjk+@TNivNK@+#hDu}n&3LJm`jx^Zkzioz6&R6P5>;mW<_X8G;LTuR_U zTIf?ToT~#jZgo@S-wRL&2u84gJ&h;L7ij8&Kum2KA)T7YA_$2I?hUD8`N7ihYWV6VVy{Wv*+avxV#?+BrHsVv>=wF+`=9 zb>%pbkF+hB=ppuTcL+E4bF~-CO0(Sbo2PikiT}qQ?V zXZBb40E&h+y~6yaV~UD4I2b8Y3Jwf|+)E9Wg1H?u&|x&pGgku2ov!o*F28=8ijq0h z7tgDmnLKK15jIyGw!ees<)}-&-Cd9}CEXvI8kX%H{@~>LAUs`?vSE>kz*nu~+*sa^Xp z+0&A3WM~(8arRqb?J~}SI=3DD+l|OoPGa)##fo=M#3>eFI2;hr7tm4~ z(Tj;06kz4Qx=?t(QcJ`FhTU)U6{$+@(XFsX-j&_i45~LoAyO9o#OW6IUk;srr*pl; jJlMNjLaEN;2)(%OeZ!rI&O + + 1 + 0 + + + + A_ARG_TYPE_DeviceID + string + + + \ No newline at end of file diff --git a/src/Ukor/wwwroot/ecp_SCPD.xml b/src/Ukor/wwwroot/ecp_SCPD.xml new file mode 100644 index 0000000..b1cf859 --- /dev/null +++ b/src/Ukor/wwwroot/ecp_SCPD.xml @@ -0,0 +1,12 @@ + + + 1 + 0 + + + + A_ARG_TYPE_DeviceID + string + + + \ No newline at end of file diff --git a/two-button-permutations.txt b/two-button-permutations.txt new file mode 100644 index 0000000..ef04d06 --- /dev/null +++ b/two-button-permutations.txt @@ -0,0 +1,225 @@ +Home,Home +Home,Rev +Home,Fwd +Home,Play +Home,Select +Home,Left +Home,Right +Home,Down +Home,Up +Home,Back +Home,InstantReplay +Home,Info +Home,Backspace +Home,Search +Home,Enter +Rev,Home +Rev,Rev +Rev,Fwd +Rev,Play +Rev,Select +Rev,Left +Rev,Right +Rev,Down +Rev,Up +Rev,Back +Rev,InstantReplay +Rev,Info +Rev,Backspace +Rev,Search +Rev,Enter +Fwd,Home +Fwd,Rev +Fwd,Fwd +Fwd,Play +Fwd,Select +Fwd,Left +Fwd,Right +Fwd,Down +Fwd,Up +Fwd,Back +Fwd,InstantReplay +Fwd,Info +Fwd,Backspace +Fwd,Search +Fwd,Enter +Play,Home +Play,Rev +Play,Fwd +Play,Play +Play,Select +Play,Left +Play,Right +Play,Down +Play,Up +Play,Back +Play,InstantReplay +Play,Info +Play,Backspace +Play,Search +Play,Enter +Select,Home +Select,Rev +Select,Fwd +Select,Play +Select,Select +Select,Left +Select,Right +Select,Down +Select,Up +Select,Back +Select,InstantReplay +Select,Info +Select,Backspace +Select,Search +Select,Enter +Left,Home +Left,Rev +Left,Fwd +Left,Play +Left,Select +Left,Left +Left,Right +Left,Down +Left,Up +Left,Back +Left,InstantReplay +Left,Info +Left,Backspace +Left,Search +Left,Enter +Right,Home +Right,Rev +Right,Fwd +Right,Play +Right,Select +Right,Left +Right,Right +Right,Down +Right,Up +Right,Back +Right,InstantReplay +Right,Info +Right,Backspace +Right,Search +Right,Enter +Down,Home +Down,Rev +Down,Fwd +Down,Play +Down,Select +Down,Left +Down,Right +Down,Down +Down,Up +Down,Back +Down,InstantReplay +Down,Info +Down,Backspace +Down,Search +Down,Enter +Up,Home +Up,Rev +Up,Fwd +Up,Play +Up,Select +Up,Left +Up,Right +Up,Down +Up,Up +Up,Back +Up,InstantReplay +Up,Info +Up,Backspace +Up,Search +Up,Enter +Back,Home +Back,Rev +Back,Fwd +Back,Play +Back,Select +Back,Left +Back,Right +Back,Down +Back,Up +Back,Back +Back,InstantReplay +Back,Info +Back,Backspace +Back,Search +Back,Enter +InstantReplay,Home +InstantReplay,Rev +InstantReplay,Fwd +InstantReplay,Play +InstantReplay,Select +InstantReplay,Left +InstantReplay,Right +InstantReplay,Down +InstantReplay,Up +InstantReplay,Back +InstantReplay,InstantReplay +InstantReplay,Info +InstantReplay,Backspace +InstantReplay,Search +InstantReplay,Enter +Info,Home +Info,Rev +Info,Fwd +Info,Play +Info,Select +Info,Left +Info,Right +Info,Down +Info,Up +Info,Back +Info,InstantReplay +Info,Info +Info,Backspace +Info,Search +Info,Enter +Backspace,Home +Backspace,Rev +Backspace,Fwd +Backspace,Play +Backspace,Select +Backspace,Left +Backspace,Right +Backspace,Down +Backspace,Up +Backspace,Back +Backspace,InstantReplay +Backspace,Info +Backspace,Backspace +Backspace,Search +Backspace,Enter +Search,Home +Search,Rev +Search,Fwd +Search,Play +Search,Select +Search,Left +Search,Right +Search,Down +Search,Up +Search,Back +Search,InstantReplay +Search,Info +Search,Backspace +Search,Search +Search,Enter +Enter,Home +Enter,Rev +Enter,Fwd +Enter,Play +Enter,Select +Enter,Left +Enter,Right +Enter,Down +Enter,Up +Enter,Back +Enter,InstantReplay +Enter,Info +Enter,Backspace +Enter,Search +Enter,Enter \ No newline at end of file