Socket.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2005-2012, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. *
  25. */
  26. package sys.net;
  27. import sys.io.File;
  28. @:core_api
  29. class Socket {
  30. private var __s : FileHandle;
  31. private var protocol : String;
  32. public var input(default,null) : haxe.io.Input;
  33. public var output(default,null) : haxe.io.Output;
  34. public var custom : Dynamic;
  35. public function new() : Void {
  36. input = untyped new sys.io.FileInput(null);
  37. output = untyped new sys.io.FileOutput(null);
  38. protocol = "tcp";
  39. }
  40. private function assignHandler() : Void {
  41. untyped input.__f = __s;
  42. untyped output.__f = __s;
  43. }
  44. public function close() : Void {
  45. untyped __call__('fclose', __s);
  46. untyped {
  47. input.__f = null;
  48. output.__f = null;
  49. }
  50. input.close();
  51. output.close();
  52. }
  53. public function read() : String {
  54. var b = '';
  55. untyped __php__('while (!feof($this->__s)) $b .= fgets($this->__s)');
  56. return b;
  57. }
  58. public function write( content : String ) : Void {
  59. return untyped __call__('fwrite', __s, content);
  60. }
  61. public function connect(host : Host, port : Int) : Void {
  62. var errs = null;
  63. var errn = null;
  64. var r = untyped __call__('stream_socket_client', protocol + '://' +host._ip + ':' + port, errn, errs);
  65. Socket.checkError(r, errn, errs);
  66. __s = cast r;
  67. assignHandler();
  68. }
  69. public function listen(connections : Int) : Void {
  70. throw "Not implemented";
  71. /* TODO: ??????
  72. var r = untyped __call__('socket_listen', __s, connections);
  73. checkError(r);
  74. */
  75. }
  76. public function shutdown( read : Bool, write : Bool ) : Void {
  77. var r;
  78. if(untyped __call__("function_exists", "stream_socket_shutdown")) {
  79. var rw = read && write ? 2 : (write ? 1 : (read ? 0 : 2));
  80. r = untyped __call__('stream_socket_shutdown', __s, rw);
  81. } else {
  82. r = untyped __call__('fclose', __s);
  83. }
  84. checkError(r, 0, 'Unable to Shutdown');
  85. }
  86. public function bind(host : Host, port : Int) : Void {
  87. var errs = null;
  88. var errn = null;
  89. 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'));
  90. Socket.checkError(r, errn, errs);
  91. __s = cast r;
  92. assignHandler();
  93. }
  94. public function accept() : Socket {
  95. var r = untyped __call__('stream_socket_accept', __s);
  96. checkError(r, 0, 'Unable to accept connections on socket');
  97. var s = new Socket();
  98. s.__s = cast r;
  99. s.assignHandler();
  100. return s;
  101. }
  102. private function hpOfString(s : String) : { host : Host, port : Int } {
  103. var parts = s.split(':');
  104. if(parts.length == 2) {
  105. return { host : new Host(parts[0]), port : Std.parseInt(parts[1]) };
  106. } else {
  107. return { host : new Host(parts[1].substr(2)), port : Std.parseInt(parts[2]) };
  108. }
  109. }
  110. public function peer() : { host : Host, port : Int } {
  111. var r : String = untyped __call__('stream_socket_get_name', __s, true);
  112. checkError(cast r, 0, 'Unable to retrieve the peer name');
  113. return hpOfString(r);
  114. }
  115. public function host() : { host : Host, port : Int } {
  116. var r : String = untyped __call__('stream_socket_get_name', __s, false);
  117. checkError(cast r, 0, 'Unable to retrieve the host name');
  118. return hpOfString(r);
  119. }
  120. public function setTimeout( timeout : Float ) : Void {
  121. var s = Std.int(timeout);
  122. var ms = Std.int((timeout-s)*1000000);
  123. var r = untyped __call__('stream_set_timeout', __s, s, ms);
  124. checkError(r, 0, 'Unable to set timeout');
  125. }
  126. public function setBlocking( b : Bool ) : Void {
  127. var r = untyped __call__('stream_set_blocking', __s, b);
  128. checkError(r, 0, 'Unable to block');
  129. }
  130. public function setFastSend( b : Bool ) : Void {
  131. throw "Not implemented";
  132. }
  133. public function waitForRead() : Void {
  134. select([this],null,null);
  135. }
  136. private static function checkError(r : Bool, code : Int, msg : String) : Void {
  137. if(!untyped __physeq__(r, false)) return;
  138. throw haxe.io.Error.Custom('Error ['+code+']: ' +msg);
  139. }
  140. private static function getType(isUdp : Bool) : Int {
  141. return isUdp ? untyped __php__('SOCK_DGRAM') : untyped __php__('SOCK_STREAM');
  142. }
  143. private static function getProtocol(protocol : String) : Int {
  144. return untyped __call__('getprotobyname', protocol);
  145. }
  146. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> }
  147. {
  148. throw "Not implemented";
  149. return null;
  150. }
  151. }