No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TcpConnection.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Net.Sockets;
  3. namespace Iot.Device.Pigpio
  4. {
  5. internal class TcpConnection
  6. {
  7. #region # event
  8. public event EventHandler StreamChanged;
  9. #endregion
  10. #region # private field
  11. private TcpClient tcp = null;
  12. private string ipOrHost;
  13. private int port;
  14. #endregion
  15. #region # public property
  16. public bool IsOpened
  17. {
  18. get
  19. {
  20. return tcp != null;
  21. }
  22. }
  23. private NetworkStream _stream = null;
  24. public NetworkStream Stream
  25. {
  26. get
  27. {
  28. return _stream;
  29. }
  30. set
  31. {
  32. _stream = value;
  33. if (StreamChanged != null)
  34. {
  35. StreamChanged.Invoke(this, new EventArgs());
  36. }
  37. }
  38. }
  39. #endregion
  40. #region # Implementation of IDisposable
  41. bool disposed = false;
  42. public void Dispose()
  43. {
  44. Dispose(true);
  45. GC.SuppressFinalize(this);
  46. }
  47. protected virtual void Dispose(bool disposing)
  48. {
  49. if (disposed)
  50. return;
  51. if (disposing)
  52. {
  53. // Release managed objects
  54. Close();
  55. }
  56. // Release unmanaged objects
  57. disposed = true;
  58. }
  59. ~TcpConnection()
  60. {
  61. Dispose(false);
  62. }
  63. #endregion
  64. #region # public method
  65. public bool Open(string ipOrHost, int port)
  66. {
  67. if (tcp == null)
  68. {
  69. try
  70. {
  71. this.ipOrHost = ipOrHost;
  72. this.port = port;
  73. tcp = new TcpClient();
  74. tcp.BeginConnect(ipOrHost, port, new AsyncCallback(NetConnectCallback), null);
  75. Console.WriteLine("Connecting to {0}:{1}...", ipOrHost, port);
  76. }
  77. catch (Exception ex)
  78. {
  79. Console.WriteLine("Connection failed({0}).", ex.Message);
  80. Close();
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. public void Close()
  87. {
  88. if (Stream != null)
  89. {
  90. // Execute handlers of StreamChanged event, and call Dispose()
  91. var stream = Stream;
  92. Stream = null;
  93. stream.Dispose();
  94. }
  95. if (tcp != null)
  96. {
  97. tcp.Close();
  98. tcp = null;
  99. Console.WriteLine("{0}:{1} was disconnected.", ipOrHost, port);
  100. }
  101. ipOrHost = string.Empty;
  102. port = 0;
  103. }
  104. #endregion
  105. #region # private method
  106. private void NetConnectCallback(IAsyncResult result)
  107. {
  108. if (tcp == null)
  109. return;
  110. if (tcp.Connected)
  111. {
  112. Console.WriteLine("Connected to LAN {0}:{1}.",
  113. ((System.Net.IPEndPoint)tcp.Client.RemoteEndPoint).Address,
  114. ((System.Net.IPEndPoint)tcp.Client.RemoteEndPoint).Port);
  115. var stream = tcp.GetStream();
  116. stream.ReadTimeout = 10000;
  117. stream.WriteTimeout = 10000;
  118. Stream = stream;
  119. }
  120. }
  121. #endregion
  122. }
  123. }