/bin/std/cpp/net/Socket.hx

http://github.com/Yoomee/clippy · Haxe · 146 lines · 98 code · 21 blank · 27 comment · 6 complexity · eeecae8fc5d9fd26e7bd0528ecc5c332 MD5 · raw file

  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 cpp.net;
  28. typedef SocketHandle = Dynamic;
  29. class Socket {
  30. private var __s : SocketHandle;
  31. public var input(default,null) : SocketInput;
  32. public var output(default,null) : SocketOutput;
  33. public var custom : Dynamic;
  34. public function new( ?s ) {
  35. __s = if( s == null ) socket_new(false) else s;
  36. input = new SocketInput(__s);
  37. output = new SocketOutput(__s);
  38. }
  39. public function close() : Void {
  40. socket_close(__s);
  41. untyped {
  42. input.__s = null;
  43. output.__s = null;
  44. }
  45. input.close();
  46. output.close();
  47. }
  48. public function read() : String {
  49. var bytes:haxe.io.BytesData = socket_read(__s);
  50. if (bytes==null) return "";
  51. return bytes.toString();
  52. }
  53. public function write( content : String ) {
  54. socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
  55. }
  56. public function connect(host : Host, port : Int) {
  57. try {
  58. socket_connect(__s, host.ip, port);
  59. } catch( s : String ) {
  60. if( s == "std@socket_connect" )
  61. throw "Failed to connect on "+(try host.reverse() catch( e : Dynamic ) host.toString())+":"+port;
  62. else
  63. cpp.Lib.rethrow(s);
  64. }
  65. }
  66. public function listen(connections : Int) {
  67. socket_listen(__s, connections);
  68. }
  69. public function shutdown( read : Bool, write : Bool ){
  70. socket_shutdown(__s,read,write);
  71. }
  72. public function bind(host : Host, port : Int) {
  73. socket_bind(__s, host.ip, port);
  74. }
  75. public function accept() : Socket {
  76. return new Socket(socket_accept(__s));
  77. }
  78. public function peer() : { host : Host, port : Int } {
  79. var a : Dynamic = socket_peer(__s);
  80. var h = new Host("127.0.0.1");
  81. untyped h.ip = a[0];
  82. return { host : h, port : a[1] };
  83. }
  84. public function host() : { host : Host, port : Int } {
  85. var a : Dynamic = socket_host(__s);
  86. var h = new Host("127.0.0.1");
  87. untyped h.ip = a[0];
  88. return { host : h, port : a[1] };
  89. }
  90. public function setTimeout( timeout : Float ) {
  91. socket_set_timeout(__s, timeout);
  92. }
  93. public function waitForRead() {
  94. select([this],null,null,null);
  95. }
  96. public function setBlocking( b : Bool ) {
  97. socket_set_blocking(__s,b);
  98. }
  99. public static function newUdpSocket() {
  100. return new Socket(socket_new(true));
  101. }
  102. // STATICS
  103. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, timeout : Float) : {read: Array<Socket>,write: Array<Socket>,others: Array<Socket>} {
  104. var neko_array = socket_select(read,write,others, timeout);
  105. return {
  106. read: neko_array[0],
  107. write: neko_array[1],
  108. others: neko_array[2]
  109. };
  110. }
  111. private static var socket_new = cpp.Lib.load("std","socket_new",1);
  112. private static var socket_close = cpp.Lib.load("std","socket_close",1);
  113. private static var socket_write = cpp.Lib.load("std","socket_write",2);
  114. private static var socket_read = cpp.Lib.load("std","socket_read",1);
  115. private static var socket_connect = cpp.Lib.load("std","socket_connect",3);
  116. private static var socket_listen = cpp.Lib.load("std","socket_listen",2);
  117. private static var socket_select = cpp.Lib.load("std","socket_select",4);
  118. private static var socket_bind = cpp.Lib.load("std","socket_bind",3);
  119. private static var socket_accept = cpp.Lib.load("std","socket_accept",1);
  120. private static var socket_peer = cpp.Lib.load("std","socket_peer",1);
  121. private static var socket_host = cpp.Lib.load("std","socket_host",1);
  122. private static var socket_set_timeout = cpp.Lib.load("std","socket_set_timeout",2);
  123. private static var socket_shutdown = cpp.Lib.load("std","socket_shutdown",3);
  124. private static var socket_set_blocking = cpp.Lib.load("std","socket_set_blocking",2);
  125. }