MqttNetworkChannel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright (c) 2013, 2014 Paolo Patierno
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License v1.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. http://www.eclipse.org/legal/epl-v10.html
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. Contributors:
  11. Paolo Patierno - initial API and implementation and/or initial documentation
  12. */
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using Windows.Networking;
  19. using Windows.Networking.Sockets;
  20. using System.Runtime.InteropServices.WindowsRuntime;
  21. using Windows.Storage.Streams;
  22. namespace uPLibrary.Networking.M2Mqtt
  23. {
  24. public class MqttNetworkChannel : IMqttNetworkChannel
  25. {
  26. // stream socket for communication
  27. private StreamSocket socket;
  28. // remote host information
  29. private HostName remoteHostName;
  30. private int remotePort;
  31. // using SSL
  32. private bool secure;
  33. /// <summary>
  34. /// Constructor
  35. /// </summary>
  36. /// <param name="socket">Socket opened with the client</param>
  37. public MqttNetworkChannel(StreamSocket socket)
  38. {
  39. this.socket = socket;
  40. }
  41. /// <summary>
  42. /// Constructor
  43. /// </summary>
  44. /// <param name="remoteHostName">Remote Host name</param>
  45. /// <param name="remotePort">Remote port</param>
  46. /// <param name="secure">Using SSL</param>
  47. public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure)
  48. {
  49. this.remoteHostName = new HostName(remoteHostName);
  50. this.remotePort = remotePort;
  51. this.secure = secure;
  52. }
  53. public bool DataAvailable
  54. {
  55. get { return true; }
  56. }
  57. public int Receive(byte[] buffer)
  58. {
  59. IBuffer result;
  60. // read all data needed (until fill buffer)
  61. int idx = 0;
  62. while (idx < buffer.Length)
  63. {
  64. // read is executed synchronously
  65. result = this.socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.None).AsTask().Result;
  66. idx += (int)result.Length;
  67. }
  68. return buffer.Length;
  69. }
  70. public int Send(byte[] buffer)
  71. {
  72. // send is executed synchronously
  73. return (int)this.socket.OutputStream.WriteAsync(buffer.AsBuffer()).AsTask().Result;
  74. }
  75. public void Close()
  76. {
  77. this.socket.Dispose();
  78. }
  79. public void Connect()
  80. {
  81. this.socket = new StreamSocket();
  82. // connection is executed synchronously
  83. this.socket.ConnectAsync(this.remoteHostName,
  84. this.remotePort.ToString(),
  85. this.secure ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket).AsTask().Wait();
  86. }
  87. }
  88. }