Poll.hx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C)2005-2015 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. class Poll {
  25. var mPollHandle : Dynamic;
  26. public var readIndexes : Array<Dynamic>;
  27. public var writeIndexes : Array<Dynamic>;
  28. public function new( n : Int ) {
  29. mPollHandle = socket_poll_alloc(n);
  30. readIndexes = [];
  31. writeIndexes = [];
  32. }
  33. public function prepare( read : Array<Socket>, write : Array<Socket> ) {
  34. var k = socket_poll_prepare(mPollHandle,read,write);
  35. readIndexes = k[0];
  36. writeIndexes = k[1];
  37. }
  38. public function events( ?t : Float ) {
  39. if (t==null) t=-1.0;
  40. socket_poll_events(mPollHandle,t);
  41. }
  42. public function poll( a : Array<Socket>, ?t : Float ) : Array<Socket> {
  43. if (t==null) t=-1.0;
  44. var read:Array<Dynamic> = socket_poll(a,mPollHandle,t);
  45. // Convert to array of sockets...
  46. var result = new Array<Socket>();
  47. if (read!=null && read.length>0) {
  48. result[read.length-1]=null;
  49. for(i in 0...read.length)
  50. result[i] = read[i];
  51. }
  52. return result;
  53. }
  54. static var socket_poll_alloc = cpp.Lib.load("std","socket_poll_alloc",1);
  55. static var socket_poll = cpp.Lib.load("std","socket_poll",3);
  56. static var socket_poll_prepare = cpp.Lib.loadLazy("std","socket_poll_prepare",3);
  57. static var socket_poll_events = cpp.Lib.loadLazy("std","socket_poll_events",2);
  58. }