Socket.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 sys.net;
  23. import sys.io.File;
  24. @:coreApi
  25. class Socket {
  26. private var __s : FileHandle;
  27. private var protocol : String;
  28. public var input(default,null) : haxe.io.Input;
  29. public var output(default,null) : haxe.io.Output;
  30. public var custom : Dynamic;
  31. public function new() : Void {
  32. input = untyped new sys.io.FileInput(null);
  33. output = untyped new sys.io.FileOutput(null);
  34. protocol = "tcp";
  35. }
  36. private function assignHandler() : Void {
  37. untyped input.__f = __s;
  38. untyped output.__f = __s;
  39. }
  40. public function close() : Void {
  41. untyped __call__('fclose', __s);
  42. untyped {
  43. input.__f = null;
  44. output.__f = null;
  45. }
  46. input.close();
  47. output.close();
  48. }
  49. public function read() : String {
  50. var b = '';
  51. untyped __php__("while (!feof($this->__s)) $b .= fgets($this->__s)");
  52. return b;
  53. }
  54. public function write( content : String ) : Void {
  55. return untyped __call__('fwrite', __s, content);
  56. }
  57. public function connect(host : Host, port : Int) : Void {
  58. var errs = null;
  59. var errn = null;
  60. var r = untyped __call__('stream_socket_client', protocol + '://' +host._ip + ':' + port, errn, errs);
  61. Socket.checkError(r, errn, errs);
  62. __s = cast r;
  63. assignHandler();
  64. }
  65. public function listen(connections : Int) : Void {
  66. throw "Not implemented";
  67. /* TODO: ??????
  68. var r = untyped __call__('socket_listen', __s, connections);
  69. checkError(r);
  70. */
  71. }
  72. public function shutdown( read : Bool, write : Bool ) : Void {
  73. var r;
  74. if(untyped __call__("function_exists", "stream_socket_shutdown")) {
  75. var rw = read && write ? 2 : (write ? 1 : (read ? 0 : 2));
  76. r = untyped __call__('stream_socket_shutdown', __s, rw);
  77. } else {
  78. r = untyped __call__('fclose', __s);
  79. }
  80. checkError(r, 0, 'Unable to Shutdown');
  81. }
  82. public function bind(host : Host, port : Int) : Void {
  83. var errs = null;
  84. var errn = null;
  85. var r = untyped __call__('stream_socket_server', protocol + '://' +host._ip + ':' + port, errn, errs, (protocol=="udp") ? __php__('STREAM_SERVER_BIND') : __php__('STREAM_SERVER_BIND | STREAM_SERVER_LISTEN'));
  86. Socket.checkError(r, errn, errs);
  87. __s = cast r;
  88. assignHandler();
  89. }
  90. public function accept() : Socket {
  91. var r = untyped __call__('stream_socket_accept', __s);
  92. checkError(r, 0, 'Unable to accept connections on socket');
  93. var s = new Socket();
  94. s.__s = cast r;
  95. s.assignHandler();
  96. return s;
  97. }
  98. private function hpOfString(s : String) : { host : Host, port : Int } {
  99. var parts = s.split(':');
  100. if(parts.length == 2) {
  101. return { host : new Host(parts[0]), port : Std.parseInt(parts[1]) };
  102. } else {
  103. return { host : new Host(parts[1].substr(2)), port : Std.parseInt(parts[2]) };
  104. }
  105. }
  106. public function peer() : { host : Host, port : Int } {
  107. var r : String = untyped __call__('stream_socket_get_name', __s, true);
  108. checkError(cast r, 0, 'Unable to retrieve the peer name');
  109. return hpOfString(r);
  110. }
  111. public function host() : { host : Host, port : Int } {
  112. var r : String = untyped __call__('stream_socket_get_name', __s, false);
  113. checkError(cast r, 0, 'Unable to retrieve the host name');
  114. return hpOfString(r);
  115. }
  116. public function setTimeout( timeout : Float ) : Void {
  117. var s = Std.int(timeout);
  118. var ms = Std.int((timeout-s)*1000000);
  119. var r = untyped __call__('stream_set_timeout', __s, s, ms);
  120. checkError(r, 0, 'Unable to set timeout');
  121. }
  122. public function setBlocking( b : Bool ) : Void {
  123. var r = untyped __call__('stream_set_blocking', __s, b);
  124. checkError(r, 0, 'Unable to block');
  125. }
  126. public function setFastSend( b : Bool ) : Void {
  127. throw "Not implemented";
  128. }
  129. public function waitForRead() : Void {
  130. select([this],null,null);
  131. }
  132. private static function checkError(r : Bool, code : Int, msg : String) : Void {
  133. if(!untyped __physeq__(r, false)) return;
  134. throw haxe.io.Error.Custom('Error ['+code+']: ' +msg);
  135. }
  136. private static function getType(isUdp : Bool) : Int {
  137. return isUdp ? untyped __php__('SOCK_DGRAM') : untyped __php__('SOCK_STREAM');
  138. }
  139. private static function getProtocol(protocol : String) : Int {
  140. return untyped __call__('getprotobyname', protocol);
  141. }
  142. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> }
  143. {
  144. throw "Not implemented";
  145. return null;
  146. }
  147. }