Pinger.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Pinger_H
  16. #define Pinger_H
  17. #include "memory/Allocator.h"
  18. #include "util/log/Log.h"
  19. #include "crypto/random/Random.h"
  20. #include "util/events/EventBase.h"
  21. #include "util/Linker.h"
  22. Linker_require("util/Pinger.c")
  23. /**
  24. * On pong received callback.
  25. * This is called when a response to the ping comes in.
  26. *
  27. * @param data the response content or NULL if the ping timed out, if Pinger_ping() was called with
  28. * null, this will be an empty string.
  29. * @param milliseconds the number of milliseconds between the sending of the ping and this event.
  30. * @param context the context which is set in the Pinger_Ping struct returned by Pinger_ping().
  31. */
  32. #define Pinger_ON_RESPONSE(x) \
  33. void (* x)(String* data, uint32_t milliseconds, void* context)
  34. /**
  35. * Sender callback.
  36. * The pinger uses this user provided function to send the ping.
  37. * This function does not return, in the event of error, it is the user's responsibility to
  38. * communicate the error through the context back to the function which called Pinger_ping().
  39. *
  40. * @param data the content of the ping to send.
  41. * @param context the context which is set in the Pinger_Ping struct returned by Pinger_ping().
  42. */
  43. #define Pinger_SEND_PING(x) void (* x)(String* data, void* context)
  44. struct Pinger;
  45. struct Pinger_Ping
  46. {
  47. /**
  48. * The allocator which is set by Pinger_ping() and can be used to
  49. * allocate space which will be freed when the ping completes.
  50. */
  51. struct Allocator* pingAlloc;
  52. /** How the ping will be identified on the wire. */
  53. uint32_t handle;
  54. /**
  55. * This is NULL by default and is set by the caller of Pinger_ping(),
  56. * when sendPing() and onResponse() are called, whatever this is, will be passed to them.
  57. */
  58. void* context;
  59. };
  60. /**
  61. * @param data this is the bytes that you want to be reflected back to you.
  62. * @param onResponse this function will be called when the ping is responded to or times out.
  63. * @param sendPing the function which will be called to send the ping out to the other node.
  64. * @param timeoutMilliseconds the number of milliseconds to wait before timeout.
  65. * @param allocator cancel the ping by freeing this allocator.
  66. * @param pinger
  67. * @return a new Pinger_Ping if all goes well, NULL if there is no space.
  68. */
  69. struct Pinger_Ping* Pinger_newPing(String* data,
  70. Pinger_ON_RESPONSE(onResponse),
  71. Pinger_SEND_PING(sendPing),
  72. uint32_t timeoutMilliseconds,
  73. struct Allocator* allocator,
  74. struct Pinger* pinger);
  75. /**
  76. * Function to call when data comes in which appears to be a ping response.
  77. *
  78. * @param data the data as it comes in.
  79. * @param pinger the pinger context.
  80. */
  81. void Pinger_pongReceived(String* data, struct Pinger* pinger);
  82. /**
  83. * Create a new pinger.
  84. *
  85. * @param eventBase
  86. * @param logger
  87. * @param alloc
  88. */
  89. struct Pinger* Pinger_new(EventBase_t* eventBase,
  90. struct Random* rand,
  91. struct Log* logger,
  92. struct Allocator* alloc);
  93. #endif