Iface.h 5.1 KB

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