Socket.hx 5.4 KB

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