1
0

Event.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "util/events/libuv/UvWrapper.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/libuv/EventBase_pvt.h"
  18. #include "util/events/Event.h"
  19. #include "util/Identity.h"
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. struct Event_pvt
  23. {
  24. struct Event pub;
  25. void (* const callback)(void* callbackContext);
  26. void* const callbackContext;
  27. uv_poll_t handler;
  28. struct Allocator* alloc;
  29. Identity
  30. };
  31. static void handleEvent(uv_poll_t* handle, int status, int events)
  32. {
  33. struct Event_pvt* event =
  34. Identity_check((struct Event_pvt*) (((char*)handle) - offsetof(struct Event_pvt, handler)));
  35. if ((status == 0) && (events & UV_READABLE)) {
  36. event->callback(event->callbackContext);
  37. }
  38. }
  39. static void onClose(uv_handle_t* handle)
  40. {
  41. struct Event_pvt* event = Identity_check((struct Event_pvt*) handle->data);
  42. Allocator_free(event->alloc);
  43. }
  44. static int onFree(struct Allocator_OnFreeJob* job)
  45. {
  46. struct Event_pvt* event = Identity_check((struct Event_pvt*) job->userData);
  47. event->handler.data = event;
  48. uv_close((uv_handle_t*) &event->handler, onClose);
  49. return 0;
  50. }
  51. struct Event* Event_socketRead(void (* const callback)(void* callbackContext),
  52. void* const callbackContext,
  53. int s,
  54. struct EventBase* eventBase,
  55. struct Allocator* userAlloc)
  56. {
  57. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  58. struct Allocator* alloc = Allocator_child(base->alloc);
  59. struct Event_pvt* out = Allocator_clone(alloc, (&(struct Event_pvt) {
  60. .callback = callback,
  61. .callbackContext = callbackContext,
  62. .alloc = alloc
  63. }));
  64. Identity_set(out);
  65. // != 0 check, removed because uv_poll_init always returns 0
  66. uv_poll_init(base->loop, &out->handler, s);
  67. // == -1 check, removed because uv_poll_start always returns 0
  68. uv_poll_start(&out->handler, UV_READABLE, handleEvent);
  69. out->handler.data = out;
  70. Allocator_onFree(userAlloc, onFree, out);
  71. return &out->pub;
  72. }