Poll.hx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cpp.net;
  23. import sys.net.Socket;
  24. import cpp.NativeSocket;
  25. /**
  26. * A wrapper around native socket polling functionality for monitoring multiple sockets for I/O readiness.
  27. * This class provides a high-level abstraction over native `poll` operations, allowing you to monitor sockets for read and write events.
  28. */
  29. class Poll {
  30. var mPollHandle:Dynamic;
  31. /**
  32. * An array of indices corresponding to sockets ready for reading after polling.
  33. */
  34. public var readIndexes:Array<Int>;
  35. /**
  36. * An array of indices corresponding to sockets ready for writing after polling.
  37. */
  38. public var writeIndexes:Array<Int>;
  39. /**
  40. * Creates a new `Poll` instance.
  41. *
  42. * @param n The maximum number of sockets to monitor.
  43. */
  44. public function new(n:Int) {
  45. mPollHandle = NativeSocket.socket_poll_alloc(n);
  46. readIndexes = [];
  47. writeIndexes = [];
  48. }
  49. /**
  50. * Prepares the poll structure for monitoring read and write events on the given sockets.
  51. *
  52. * @param read An array of sockets to monitor for readability.
  53. * @param write An array of sockets to monitor for writability.
  54. */
  55. public function prepare(read:Array<Socket>, write:Array<Socket>) {
  56. var k = NativeSocket.socket_poll_prepare(mPollHandle, read, write);
  57. readIndexes = k[0];
  58. writeIndexes = k[1];
  59. }
  60. /**
  61. * Waits for events on the prepared sockets.
  62. *
  63. * @param t The timeout in seconds. Use `-1.0` for an infinite wait. Defaults to `-1.0` if not specified.
  64. */
  65. public function events(?t:Float) {
  66. if (t == null)
  67. t = -1.0;
  68. NativeSocket.socket_poll_events(mPollHandle, t);
  69. }
  70. /**
  71. * Polls the given sockets for any events (e.g., readability or writability).
  72. *
  73. * @param a An array of sockets to monitor for events.
  74. * @param t The timeout in seconds. Use `-1.0` for an infinite wait. Defaults to `-1.0` if not specified.
  75. * @return An array of sockets that are ready for I/O operations.
  76. */
  77. public function poll(a:Array<Socket>, ?t:Float):Array<Socket> {
  78. if (t == null)
  79. t = -1.0;
  80. return NativeSocket.socket_poll(a, mPollHandle, t);
  81. }
  82. }