WatsonTcpServerEvents.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. namespace WatsonTcp
  2. {
  3. using System;
  4. /// <summary>
  5. /// Watson TCP server events.
  6. /// </summary>
  7. public class WatsonTcpServerEvents
  8. {
  9. #region Public-Members
  10. /// <summary>
  11. /// Event to fire when authentication is requested from a client.
  12. /// </summary>
  13. public event EventHandler<AuthenticationRequestedEventArgs> AuthenticationRequested;
  14. /// <summary>
  15. /// Event to fire when a client successfully authenticates.
  16. /// </summary>
  17. public event EventHandler<AuthenticationSucceededEventArgs> AuthenticationSucceeded;
  18. /// <summary>
  19. /// Event to fire when a client fails authentication.
  20. /// </summary>
  21. public event EventHandler<AuthenticationFailedEventArgs> AuthenticationFailed;
  22. /// <summary>
  23. /// Event to fire when a client connects to the server.
  24. /// The IP:port of the client is passed in the arguments.
  25. /// </summary>
  26. public event EventHandler<ConnectionEventArgs> ClientConnected;
  27. /// <summary>
  28. /// Event to fire when a client disconnects from the server.
  29. /// The IP:port is passed in the arguments along with the reason for the disconnection.
  30. /// </summary>
  31. public event EventHandler<DisconnectionEventArgs> ClientDisconnected;
  32. /// <summary>
  33. /// This event is fired when a message is received from a client and it is desired that WatsonTcp pass the byte array containing the message payload.
  34. /// If MessageReceived is set, StreamReceived will not be used.
  35. /// </summary>
  36. public event EventHandler<MessageReceivedEventArgs> MessageReceived;
  37. /// <summary>
  38. /// This event is fired when a stream is received from a client and it is desired that WatsonTcp pass the stream containing the message payload to your application.
  39. /// If MessageReceived is set, StreamReceived will not be used.
  40. /// </summary>
  41. public event EventHandler<StreamReceivedEventArgs> StreamReceived;
  42. /// <summary>
  43. /// This event is fired when the server is started.
  44. /// </summary>
  45. public event EventHandler ServerStarted;
  46. /// <summary>
  47. /// This event is fired when the server is stopped.
  48. /// </summary>
  49. public event EventHandler ServerStopped;
  50. /// <summary>
  51. /// This event is fired when an exception is encountered.
  52. /// </summary>
  53. public event EventHandler<ExceptionEventArgs> ExceptionEncountered;
  54. #endregion
  55. #region Internal-Members
  56. internal bool IsUsingMessages
  57. {
  58. get
  59. {
  60. if (MessageReceived != null && MessageReceived.GetInvocationList().Length > 0) return true;
  61. return false;
  62. }
  63. }
  64. internal bool IsUsingStreams
  65. {
  66. get
  67. {
  68. if (StreamReceived != null && StreamReceived.GetInvocationList().Length > 0) return true;
  69. return false;
  70. }
  71. }
  72. #endregion
  73. #region Private-Members
  74. #endregion
  75. #region Constructors-and-Factories
  76. /// <summary>
  77. /// Instantiate.
  78. /// </summary>
  79. public WatsonTcpServerEvents()
  80. {
  81. }
  82. #endregion
  83. #region Public-Methods
  84. #endregion
  85. #region Internal-Methods
  86. internal void HandleAuthenticationRequested(object sender, AuthenticationRequestedEventArgs args)
  87. {
  88. WrappedEventHandler(() => AuthenticationRequested?.Invoke(sender, args), "AuthenticationRequested", sender);
  89. }
  90. internal void HandleAuthenticationSucceeded(object sender, AuthenticationSucceededEventArgs args)
  91. {
  92. WrappedEventHandler(() => AuthenticationSucceeded?.Invoke(sender, args), "AuthenticationSucceeded", sender);
  93. }
  94. internal void HandleAuthenticationFailed(object sender, AuthenticationFailedEventArgs args)
  95. {
  96. WrappedEventHandler(() => AuthenticationFailed?.Invoke(sender, args), "AuthenticationFailed", sender);
  97. }
  98. internal void HandleClientConnected(object sender, ConnectionEventArgs args)
  99. {
  100. WrappedEventHandler(() => ClientConnected?.Invoke(sender, args), "ClientConnected", sender);
  101. }
  102. internal void HandleClientDisconnected(object sender, DisconnectionEventArgs args)
  103. {
  104. WrappedEventHandler(() => ClientDisconnected?.Invoke(sender, args), "ClientDisconnected", sender);
  105. }
  106. internal void HandleMessageReceived(object sender, MessageReceivedEventArgs args)
  107. {
  108. WrappedEventHandler(() => MessageReceived?.Invoke(sender, args), "MessageReceived", sender);
  109. }
  110. internal void HandleStreamReceived(object sender, StreamReceivedEventArgs args)
  111. {
  112. WrappedEventHandler(() => StreamReceived?.Invoke(sender, args), "StreamReceived", sender);
  113. }
  114. internal void HandleServerStarted(object sender, EventArgs args)
  115. {
  116. WrappedEventHandler(() => ServerStarted?.Invoke(sender, args), "ServerStarted", sender);
  117. }
  118. internal void HandleServerStopped(object sender, EventArgs args)
  119. {
  120. WrappedEventHandler(() => ServerStopped?.Invoke(sender, args), "ServerStopped", sender);
  121. }
  122. internal void HandleExceptionEncountered(object sender, ExceptionEventArgs args)
  123. {
  124. WrappedEventHandler(() => ExceptionEncountered?.Invoke(sender, args), "ExceptionEncountered", sender);
  125. }
  126. #endregion
  127. #region Private-Methods
  128. internal void WrappedEventHandler(Action action, string handler, object sender)
  129. {
  130. if (action == null) return;
  131. Action<Severity, string> logger = ((WatsonTcpServer)sender).Settings.Logger;
  132. try
  133. {
  134. action.Invoke();
  135. }
  136. catch (Exception e)
  137. {
  138. logger?.Invoke(Severity.Error, "Event handler exception in " + handler + ": " + Environment.NewLine + e.ToString());
  139. }
  140. }
  141. #endregion
  142. }
  143. }