WatsonTcpClientCallbacks.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. namespace WatsonTcp
  2. {
  3. using System;
  4. using System.Threading.Tasks;
  5. /// <summary>
  6. /// Watson TCP client callbacks.
  7. /// </summary>
  8. public class WatsonTcpClientCallbacks
  9. {
  10. #region Public-Members
  11. /// <summary>
  12. /// Function called when authentication is requested from the server. Expects the 16-byte preshared key.
  13. /// </summary>
  14. public Func<string> AuthenticationRequested = null;
  15. /// <summary>
  16. /// Callback to invoke when receiving a synchronous request that demands a response.
  17. /// </summary>
  18. [Obsolete("Please migrate to async methods.")]
  19. public Func<SyncRequest, SyncResponse> SyncRequestReceived
  20. {
  21. get
  22. {
  23. return _SyncRequestReceived;
  24. }
  25. set
  26. {
  27. _SyncRequestReceived = value;
  28. }
  29. }
  30. /// <summary>
  31. /// Callback to invoke when receiving a synchronous request that demands a response.
  32. /// </summary>
  33. public Func<SyncRequest, Task<SyncResponse>> SyncRequestReceivedAsync
  34. {
  35. get
  36. {
  37. return _SyncRequestReceivedAsync;
  38. }
  39. set
  40. {
  41. _SyncRequestReceivedAsync = value;
  42. }
  43. }
  44. #endregion
  45. #region Private-Members
  46. private Func<SyncRequest, SyncResponse> _SyncRequestReceived = null;
  47. private Func<SyncRequest, Task<SyncResponse>> _SyncRequestReceivedAsync = null;
  48. #endregion
  49. #region Constructors-and-Factories
  50. /// <summary>
  51. /// Instantiate.
  52. /// </summary>
  53. public WatsonTcpClientCallbacks()
  54. {
  55. }
  56. #endregion
  57. #region Public-Methods
  58. #endregion
  59. #region Internal-Methods
  60. internal string HandleAuthenticationRequested()
  61. {
  62. string ret = null;
  63. if (AuthenticationRequested != null)
  64. {
  65. try
  66. {
  67. ret = AuthenticationRequested();
  68. }
  69. catch (Exception)
  70. {
  71. }
  72. }
  73. return ret;
  74. }
  75. internal SyncResponse HandleSyncRequestReceived(SyncRequest req)
  76. {
  77. SyncResponse ret = null;
  78. #pragma warning disable CS0618 // Type or member is obsolete
  79. if (SyncRequestReceived != null)
  80. {
  81. try
  82. {
  83. ret = SyncRequestReceived(req);
  84. }
  85. catch (Exception)
  86. {
  87. }
  88. }
  89. #pragma warning restore CS0618 // Type or member is obsolete
  90. return ret;
  91. }
  92. internal async Task<SyncResponse> HandleSyncRequestReceivedAsync(SyncRequest req)
  93. {
  94. SyncResponse ret = null;
  95. if (SyncRequestReceivedAsync != null)
  96. {
  97. try
  98. {
  99. ret = await SyncRequestReceivedAsync(req);
  100. }
  101. catch (Exception)
  102. {
  103. }
  104. }
  105. return ret;
  106. }
  107. #endregion
  108. #region Private-Methods
  109. #endregion
  110. }
  111. }