Event.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 freeEvent2(uv_handle_t* handle)
  40. {
  41. Allocator_onFreeComplete((struct Allocator_OnFreeJob*)handle->data);
  42. }
  43. static int freeEvent(struct Allocator_OnFreeJob* job)
  44. {
  45. struct Event_pvt* event = Identity_check((struct Event_pvt*) job->userData);
  46. event->handler.data = job;
  47. uv_close((uv_handle_t*) &event->handler, freeEvent2);
  48. return Allocator_ONFREE_ASYNC;
  49. }
  50. struct Event* Event_socketRead(void (* const callback)(void* callbackContext),
  51. void* const callbackContext,
  52. int s,
  53. struct EventBase* eventBase,
  54. struct Allocator* allocator)
  55. {
  56. struct EventBase_pvt* base = EventBase_privatize(eventBase);
  57. struct Allocator* alloc = Allocator_child(allocator);
  58. struct Event_pvt* out = Allocator_clone(alloc, (&(struct Event_pvt) {
  59. .callback = callback,
  60. .callbackContext = callbackContext,
  61. .alloc = alloc
  62. }));
  63. Identity_set(out);
  64. // != 0 check, removed because uv_poll_init always returns 0
  65. uv_poll_init(base->loop, &out->handler, s);
  66. // == -1 check, removed because uv_poll_start always returns 0
  67. uv_poll_start(&out->handler, UV_READABLE, handleEvent);
  68. out->handler.data = out;
  69. Allocator_onFree(alloc, freeEvent, out);
  70. return &out->pub;
  71. }