PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/source/samples/ObviousCode.Interlace.BitTunnel/ObviousCode.Interlace.BitTunnel/Connectivity/Connection.cs

https://bitbucket.org/VahidN/interlace
C# | 182 lines | 144 code | 38 blank | 0 comment | 22 complexity | 9b5c40c42ca2834acb4b49fdc701341b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Interlace.ReactorService;
  5. using ObviousCode.Interlace.BitTunnelClient;
  6. using ObviousCode.Interlace.BitTunnelLibrary;
  7. using ObviousCode.Interlace.BitTunnelLibrary.Events;
  8. using ObviousCode.Interlace.BitTunnelLibrary.Interfaces;
  9. using ObviousCode.Interlace.BitTunnelServer;
  10. namespace ObviousCode.Interlace.BitTunnel.Connectivity
  11. {
  12. public class Connection : IDisposable
  13. {
  14. public event EventHandler ConnectionMade;
  15. public event EventHandler ConnectionTermainated;
  16. public event EventHandler<ExceptionEventArgs> LostConnection;
  17. public event EventHandler<MessageEventArgs> MessageReceived;
  18. public event EventHandler<MessageEventArgs> MessageSending;
  19. private ServiceHost _host;
  20. private IBitTunnelService _service;
  21. List<IService> _services;
  22. private bool _connected;
  23. public Connection(ConnectionType type, AppSettings settings) : this(type, settings, "") { }
  24. public Connection(ConnectionType type, AppSettings settings, string publicName)
  25. {
  26. _services = new List<IService>();
  27. LoadNetworkService(type, settings, publicName);
  28. }
  29. private void LoadNetworkService(ConnectionType type, AppSettings settings, string publicName)
  30. {
  31. if (type == ConnectionType.Unknown) throw new InvalidOperationException("Unknown Connection Type passed to Connection");
  32. if (type == ConnectionType.Client)
  33. {
  34. _service = new BitTunnelClientService(settings, publicName);
  35. }
  36. else
  37. {
  38. _service = new BitTunnelServerService(settings, publicName);
  39. }
  40. _service.LostConnection += new EventHandler<ExceptionEventArgs>(_service_LostConnection);
  41. _service.MessageReceived += new EventHandler<MessageEventArgs>(_service_MessageReceived);
  42. _service.MessageSending += new EventHandler<MessageEventArgs>(_service_MessageSending);
  43. _services.Add(_service);
  44. }
  45. public void AddCustomService(IService service)
  46. {
  47. if (_connected) throw new InvalidOperationException("Services cannot be added once the connection is open");
  48. _services.Add(service);
  49. }
  50. public bool IsConnected
  51. {
  52. get
  53. {
  54. return _connected;
  55. }
  56. }
  57. public bool Connect()
  58. {
  59. _service.ConnectionCompleted += new EventHandler(_service_ConnectionCompleted);
  60. if (_connected)
  61. {
  62. Disconnect();
  63. }
  64. _host = new ServiceHost();//Rebuild in case of show stopping exception killing the previous implementation
  65. foreach (IService service in _services)
  66. {
  67. _host.AddService(_service);
  68. }
  69. _host.StartServiceHost();
  70. _host.OpenServices();
  71. DateTime now = DateTime.Now;
  72. DateTime toolate = now.AddMilliseconds(_service.Settings.ClientConnectionTimeout);
  73. while (_connected == false && DateTime.Now < toolate)
  74. {
  75. Thread.Sleep(10);
  76. }
  77. _service.ConnectionCompleted -= new EventHandler(_service_ConnectionCompleted);
  78. if (_connected && ConnectionMade != null)
  79. {
  80. ConnectionMade(this, EventArgs.Empty);
  81. }
  82. return _connected;
  83. }
  84. void _service_ConnectionCompleted(object sender, EventArgs e)
  85. {
  86. _connected = true;
  87. }
  88. public void Disconnect()
  89. {
  90. if (_connected)
  91. {
  92. try
  93. {
  94. _host.CloseServices();
  95. _host.StopServiceHost();
  96. _host.Dispose();
  97. }
  98. finally
  99. {
  100. _connected = false;
  101. if (ConnectionTermainated != null)
  102. {
  103. ConnectionTermainated(this, EventArgs.Empty);
  104. }
  105. }
  106. }
  107. }
  108. void _service_LostConnection(object sender, ExceptionEventArgs e)
  109. {
  110. _connected = false;
  111. if (LostConnection != null)
  112. {
  113. LostConnection(sender, e);
  114. }
  115. }
  116. void _service_MessageReceived(object sender, MessageEventArgs e)
  117. {
  118. if (MessageReceived != null)
  119. {
  120. MessageReceived(sender, e);
  121. }
  122. }
  123. void _service_MessageSending(object sender, MessageEventArgs e)
  124. {
  125. if (MessageSending != null)
  126. {
  127. MessageSending(sender, e);
  128. }
  129. }
  130. public IBitTunnelService Service
  131. {
  132. get { return _service; }
  133. }
  134. #region IDisposable Members
  135. public void Dispose()
  136. {
  137. if (_connected)
  138. {
  139. Disconnect();
  140. }
  141. }
  142. #endregion
  143. }
  144. }