Socket.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 isUdp(default, null) : Bool;
  36. public function new( ?s ) {
  37. __s = s;
  38. input = new SocketInput(__s);
  39. output = new SocketOutput(__s);
  40. }
  41. private function assignHandler() {
  42. untyped input.__f = __s;
  43. untyped output.__f = __s;
  44. }
  45. public function close() : Void {
  46. untyped __call__('fclose', __s);
  47. untyped {
  48. input.__f = null;
  49. output.__f = null;
  50. }
  51. input.close();
  52. output.close();
  53. }
  54. public function read() : String {
  55. var b = '';
  56. untyped __php__('while (!feof($this->__s)) $b .= fgets($this->__s)');
  57. return b;
  58. }
  59. public function write( content : String ) {
  60. return untyped __call__('fwrite', __s, content);
  61. }
  62. public function connect(host : Host, port : Int) {
  63. var errs = null;
  64. var errn = null;
  65. var r = untyped __call__('stream_socket_client', (isUdp ? 'udp' : 'tcp') + '://' +host._ip + ':' + port, errn, errs);
  66. checkError(r, errn, errs);
  67. __s = cast r;
  68. assignHandler();
  69. }
  70. public function listen(connections : Int) {
  71. /* TODO: ??????
  72. var r = untyped __call__('socket_listen', __s, connections);
  73. checkError(r);
  74. */
  75. }
  76. public function shutdown( read : Bool, write : Bool ){
  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) {
  87. var errs = null;
  88. var errn = null;
  89. var r = untyped __call__('stream_socket_server', (isUdp ? 'udp' : 'tcp') + '://' +host._ip + ':' + port, errn, errs, isUdp ? __php__('STREAM_SERVER_BIND') : __php__('STREAM_SERVER_BIND | STREAM_SERVER_LISTEN'));
  90. 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. return untyped new Socket(cast r);
  98. }
  99. private function hpOfString(s : String) {
  100. var parts = s.split(':');
  101. if(parts.length == 2) {
  102. return { host : new Host(parts[0]), port : Std.parseInt(parts[1]) };
  103. } else {
  104. return { host : new Host(parts[1].substr(2)), port : Std.parseInt(parts[2]) };
  105. }
  106. }
  107. public function peer() : { host : Host, port : Int } {
  108. var r : String = untyped __call__('stream_socket_get_name', __s, true);
  109. checkError(cast r, 0, 'Unable to retrieve the peer name');
  110. return hpOfString(r);
  111. }
  112. public function host() : { host : Host, port : Int } {
  113. var r : String = untyped __call__('stream_socket_get_name', __s, false);
  114. checkError(cast r, 0, 'Unable to retrieve the host name');
  115. return hpOfString(r);
  116. }
  117. public function setTimeout( timeout : Float ) {
  118. var s = Std.int(timeout);
  119. var ms = Std.int((timeout-s)*1000000);
  120. var r = untyped __call__('stream_set_timeout', __s, s, ms);
  121. checkError(r, 0, 'Unable to set timeout');
  122. }
  123. public function setBlocking( b : Bool ) {
  124. var r = untyped __call__('stream_set_blocking', __s, b);
  125. checkError(r, 0, 'Unable to block');
  126. }
  127. // STATICS
  128. public static function newUdpSocket() {
  129. var s = new Socket();
  130. untyped s.isUdp = true;
  131. return s;
  132. }
  133. private static function checkError(r : Bool, code : Int, msg : String) {
  134. if(!untyped __physeq__(r, false)) return;
  135. throw haxe.io.Error.Custom('Error ['+code+']: ' +msg);
  136. }
  137. private static function getType(isUdp : Bool) : Int {
  138. return isUdp ? untyped __php__('SOCK_DGRAM') : untyped __php__('SOCK_STREAM');
  139. }
  140. private static function getProtocol(isUdp : Bool) : Int {
  141. return isUdp ? untyped __call__('getprotobyname', 'udp') : untyped __call__('getprotobyname', 'tcp');
  142. }
  143. }
  144. enum SocketDomain {
  145. AfInet;
  146. AfInet6;
  147. AfUnix;
  148. }