RequestSocket.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using NetMQ.Core;
  3. namespace NetMQ.Sockets
  4. {
  5. /// <summary>
  6. /// A RequestSocket is a NetMQSocket intended to be used as the Request part of the Request-Response pattern.
  7. /// This is generally paired with a ResponseSocket.
  8. /// </summary>
  9. public class RequestSocket : NetMQSocket
  10. {
  11. /// <summary>
  12. /// Create a new RequestSocket and attach socket to zero or more endpoints.
  13. /// </summary>
  14. /// <param name="connectionString">List of NetMQ endpoints, separated by commas and prefixed by '@' (to bind the socket) or '>' (to connect the socket).
  15. /// Default action is connect (if endpoint doesn't start with '@' or '>')</param>
  16. /// <example><code>var socket = new RequestSocket(">tcp://127.0.0.1:5555,@tcp://127.0.0.1:55556");</code></example>
  17. public RequestSocket(string? connectionString = null) : base(ZmqSocketType.Req, connectionString, DefaultAction.Connect)
  18. {
  19. }
  20. /// <summary>
  21. /// Create a new RequestSocket based upon the given SocketBase.
  22. /// </summary>
  23. /// <param name="socketHandle">the SocketBase to create the new socket from</param>
  24. internal RequestSocket(SocketBase socketHandle)
  25. : base(socketHandle)
  26. {
  27. }
  28. private enum ProgressTopic
  29. {
  30. Send,
  31. Retry,
  32. Failure,
  33. Success
  34. }
  35. /// <summary>
  36. /// Try to send request message and return the response as a message, or return null if not successful
  37. /// </summary>
  38. /// <param name="address">a string denoting the address to connect to</param>
  39. /// <param name="requestMessage">The request message</param>
  40. /// <param name="numTries">The number of times to try</param>
  41. /// <param name="requestTimeout">The timeout for each request</param>
  42. /// <param name="progressPublisher">Report topics: Failure, Retry, Send, Success</param>
  43. /// <returns>the response message, or null if not successful</returns>
  44. public static NetMQMessage? RequestResponseMultipartMessageWithRetry(string address, NetMQMessage requestMessage,
  45. int numTries, TimeSpan requestTimeout, PublisherSocket? progressPublisher = null)
  46. {
  47. var responseMessage = new NetMQMessage();
  48. while (numTries-- > 0)
  49. {
  50. using (var requestSocket = new RequestSocket(address))
  51. {
  52. progressPublisher?.SendFrame(ProgressTopic.Send.ToString());
  53. requestSocket.SendMultipartMessage(requestMessage);
  54. if (requestSocket.TryReceiveMultipartMessage(requestTimeout, ref responseMessage))
  55. {
  56. progressPublisher?.SendFrame(ProgressTopic.Success.ToString());
  57. return responseMessage;
  58. }
  59. progressPublisher?.SendFrame(ProgressTopic.Retry.ToString());
  60. }
  61. }
  62. progressPublisher?.SendFrame(ProgressTopic.Failure.ToString());
  63. return null;
  64. }
  65. /// <summary>
  66. /// Try to send request string and return the response string, or return null if not successful
  67. /// </summary>
  68. /// <param name="address">a string denoting the address to connect to</param>
  69. /// <param name="requestString">The request string</param>
  70. /// <param name="numTries">The number of times to try</param>
  71. /// <param name="requestTimeout">The timeout for each request</param>
  72. /// <param name="progressPublisher">Report topics: Failure, Retry, Send, Success</param>
  73. /// <returns>the response message, or null if not successful</returns>
  74. public static string? RequestResponseStringWithRetry(string address, string requestString,
  75. int numTries, TimeSpan requestTimeout, PublisherSocket? progressPublisher = null)
  76. {
  77. while (numTries-- > 0)
  78. {
  79. using (var requestSocket = new RequestSocket(address))
  80. {
  81. progressPublisher?.SendFrame(ProgressTopic.Send.ToString());
  82. requestSocket.SendFrame(requestString);
  83. if (requestSocket.TryReceiveFrameString(requestTimeout, out string? frameString))
  84. {
  85. progressPublisher?.SendFrame(ProgressTopic.Success.ToString());
  86. return frameString;
  87. }
  88. progressPublisher?.SendFrame(ProgressTopic.Retry.ToString());
  89. }
  90. }
  91. progressPublisher?.SendFrame(ProgressTopic.Failure.ToString());
  92. return null;
  93. }
  94. }
  95. }