UpnpMapper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Authors:
  3. // Nicholas Terry <nick.i.terry@gmail.com>
  4. //
  5. // Copyright (C) 2014 Nicholas Terry
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Diagnostics;
  29. using System.Linq;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Threading.Tasks;
  35. using MediaBrowser.Common.Net;
  36. using MediaBrowser.Model.Logging;
  37. namespace Mono.Nat.Upnp.Mappers
  38. {
  39. internal class UpnpMapper : Upnp, IMapper
  40. {
  41. public event EventHandler<DeviceEventArgs> DeviceFound;
  42. public UdpClient Client { get; set; }
  43. public UpnpMapper(ILogger logger, IHttpClient httpClient)
  44. : base(logger, httpClient)
  45. {
  46. //Bind to local port 1900 for ssdp responses
  47. Client = new UdpClient(1900);
  48. }
  49. public void Map(IPAddress gatewayAddress)
  50. {
  51. //Get the httpu request payload
  52. byte[] data = DiscoverDeviceMessage.EncodeUnicast(gatewayAddress);
  53. Client.Send(data, data.Length, new IPEndPoint(gatewayAddress, 1900));
  54. new Thread(Receive).Start();
  55. }
  56. public async void Receive()
  57. {
  58. while (true)
  59. {
  60. IPEndPoint received = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 5351);
  61. if (Client.Available > 0)
  62. {
  63. IPAddress localAddress = ((IPEndPoint)Client.Client.LocalEndPoint).Address;
  64. byte[] data = Client.Receive(ref received);
  65. await Handle(localAddress, data, received);
  66. }
  67. }
  68. }
  69. public void Handle(IPAddress localAddres, byte[] response)
  70. {
  71. }
  72. public override async Task<UpnpNatDevice> Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  73. {
  74. // No matter what, this method should never throw an exception. If something goes wrong
  75. // we should still be in a position to handle the next reply correctly.
  76. try
  77. {
  78. var d = await base.Handle(localAddress, response, endpoint).ConfigureAwait(false);
  79. var result = await d.GetServicesList().ConfigureAwait(false);
  80. if (result)
  81. {
  82. DeviceSetupComplete(d);
  83. }
  84. return d;
  85. }
  86. catch (Exception ex)
  87. {
  88. Logger.ErrorException("Error mapping port. Data string: {0}", ex, Encoding.UTF8.GetString(response));
  89. return null;
  90. }
  91. }
  92. private void DeviceSetupComplete(INatDevice device)
  93. {
  94. OnDeviceFound(new DeviceEventArgs(device));
  95. }
  96. private void OnDeviceFound(DeviceEventArgs args)
  97. {
  98. if (DeviceFound != null)
  99. DeviceFound(this, args);
  100. }
  101. }
  102. }