Iface.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Iface_H
  16. #define Iface_H
  17. #include <stdint.h>
  18. #include "wire/Error.h"
  19. #include "wire/Message.h"
  20. #include "util/Defined.h"
  21. struct Iface;
  22. /**
  23. * @param thisInterface the interface which contains the sendMessage function pointer.
  24. * @param message the message
  25. */
  26. typedef struct Error_s (* Iface_Callback)(struct Message* message, struct Iface* thisInterface);
  27. #define Iface_DEFUN __attribute__ ((warn_unused_result)) struct Error_s
  28. struct Iface
  29. {
  30. /** Send a message through this interface. */
  31. Iface_Callback send;
  32. #ifdef PARANOIA
  33. /** This is for checking currentMsg Iface_next() has not been called incorrectly. */
  34. struct Message* currentMsg;
  35. #endif
  36. /** Interface to which this one is connected (if connected) */
  37. struct Iface* connectedIf;
  38. };
  39. /**
  40. * Send a message to an Iface.
  41. * Whatever interface has been *plumbed* to this interface using Iface_plumb() will receive
  42. * the message. If you are in an Iface_Callback function, you must not use this function to
  43. * forward the message which you have received as your input, to do so will cause a runtime
  44. * assertion error, in order to forward the message which you received, you must use Iface_next()
  45. * and it must be a tail-call.
  46. */
  47. static inline struct Error_s Iface_send(struct Iface* iface, struct Message* msg)
  48. {
  49. struct Iface* conn = iface->connectedIf;
  50. #ifdef PARANOIA
  51. Assert_true(conn);
  52. Assert_true(conn->send);
  53. Assert_true(msg);
  54. struct Message* currentMsg = conn->currentMsg;
  55. msg->currentIface = conn;
  56. conn->currentMsg = msg;
  57. #endif
  58. struct Error_s ret = conn->send(msg, conn);
  59. #ifdef PARANOIA
  60. msg->currentIface = NULL;
  61. conn->currentMsg = currentMsg;
  62. #endif
  63. return ret;
  64. }
  65. /**
  66. * Forward a message from inside of an Iface_Callback function.
  67. */
  68. static inline Iface_DEFUN Iface_next(struct Iface* iface, struct Message* msg)
  69. {
  70. #ifdef PARANOIA
  71. struct Iface* conn = iface->connectedIf;
  72. struct Message* currentMsg = conn->currentMsg;
  73. Assert_true(msg->currentIface);
  74. Assert_true(msg->currentIface->currentMsg == msg);
  75. msg->currentIface->currentMsg = NULL;
  76. #endif
  77. #ifdef PARANOIA
  78. // done inside of Iface_send()
  79. msg->currentIface = NULL;
  80. conn->currentMsg = NULL;
  81. #endif
  82. struct Error_s ret = Iface_send(iface, msg);
  83. #ifdef PARANOIA
  84. conn->currentMsg = currentMsg;
  85. #endif
  86. return ret;
  87. }
  88. /**
  89. * Call a function which might use Iface_next()
  90. * This macro will call a function, passing it a message and other arguments, if the function
  91. * you are calling might use Iface_next(), you must call it with this macro instead of calling
  92. * it directly.
  93. * If you are calling from a Iface_Callback function, you must not use this function to call
  94. * the next function with the message passed to you. In that case just call the function directly.
  95. *
  96. * Why is there code using this weird function rather than simply calling Iface_send() ?
  97. * Iface_send() only works when you send to *your* iface which is plumbed to the iface that you want
  98. * the data to go to, this macro will be used when you want to bypass the need to create a local
  99. * iface and plumb it to the remote one just in order to send one message.
  100. */
  101. #ifdef PARANOIA
  102. #define Iface_CALL(func, msg, ...) \
  103. (__extension__ ({ \
  104. Assert_true(!msg->currentIface); \
  105. struct Iface Iface_y = { .currentMsg = msg }; \
  106. msg->currentIface = &Iface_y; \
  107. struct Error_s ret = func(msg, __VA_ARGS__); \
  108. msg->currentIface = NULL; \
  109. ret; \
  110. }))
  111. // CHECKFILES_IGNORE missing ;
  112. #else
  113. #define Iface_CALL(func, msg, ...) func(msg, __VA_ARGS__)
  114. #endif
  115. static inline void Iface_plumb(struct Iface* a, struct Iface* b)
  116. {
  117. Assert_true(!a->connectedIf);
  118. Assert_true(!b->connectedIf);
  119. a->connectedIf = b;
  120. b->connectedIf = a;
  121. }
  122. static inline void Iface_unplumb(struct Iface* a, struct Iface* b)
  123. {
  124. Assert_true(a->connectedIf == b);
  125. Assert_true(b->connectedIf == a);
  126. a->connectedIf = NULL;
  127. b->connectedIf = NULL;
  128. }
  129. #endif