|
@@ -22,6 +22,7 @@
|
|
|
package sys.net;
|
|
|
|
|
|
import haxe.io.Error;
|
|
|
+import cpp.NativeSocket;
|
|
|
|
|
|
private class SocketInput extends haxe.io.Input {
|
|
|
|
|
@@ -33,7 +34,7 @@ private class SocketInput extends haxe.io.Input {
|
|
|
|
|
|
public override function readByte() {
|
|
|
return try {
|
|
|
- socket_recv_char(__s);
|
|
|
+ NativeSocket.socket_recv_char(__s);
|
|
|
} catch( e : Dynamic ) {
|
|
|
if( e == "Blocking" )
|
|
|
throw Blocked;
|
|
@@ -49,7 +50,7 @@ private class SocketInput extends haxe.io.Input {
|
|
|
if (__s==null)
|
|
|
throw "Invalid handle";
|
|
|
try {
|
|
|
- r = socket_recv(__s,buf.getData(),pos,len);
|
|
|
+ r = NativeSocket.socket_recv(__s,buf.getData(),pos,len);
|
|
|
} catch( e : Dynamic ) {
|
|
|
if( e == "Blocking" )
|
|
|
throw Blocked;
|
|
@@ -63,13 +64,9 @@ private class SocketInput extends haxe.io.Input {
|
|
|
|
|
|
public override function close() {
|
|
|
super.close();
|
|
|
- if( __s != null ) socket_close(__s);
|
|
|
+ if( __s != null ) NativeSocket.socket_close(__s);
|
|
|
}
|
|
|
|
|
|
- private static var socket_recv = cpp.Lib.load("std","socket_recv",4);
|
|
|
- private static var socket_recv_char = cpp.Lib.load("std","socket_recv_char",1);
|
|
|
- private static var socket_close = cpp.Lib.load("std","socket_close",1);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
private class SocketOutput extends haxe.io.Output {
|
|
@@ -84,7 +81,7 @@ private class SocketOutput extends haxe.io.Output {
|
|
|
if (__s==null)
|
|
|
throw "Invalid handle";
|
|
|
try {
|
|
|
- socket_send_char(__s, c);
|
|
|
+ NativeSocket.socket_send_char(__s, c);
|
|
|
} catch( e : Dynamic ) {
|
|
|
if( e == "Blocking" )
|
|
|
throw Blocked;
|
|
@@ -95,7 +92,7 @@ private class SocketOutput extends haxe.io.Output {
|
|
|
|
|
|
public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
|
|
|
return try {
|
|
|
- socket_send(__s, buf.getData(), pos, len);
|
|
|
+ NativeSocket.socket_send(__s, buf.getData(), pos, len);
|
|
|
} catch( e : Dynamic ) {
|
|
|
if( e == "Blocking" )
|
|
|
throw Blocked;
|
|
@@ -108,13 +105,9 @@ private class SocketOutput extends haxe.io.Output {
|
|
|
|
|
|
public override function close() {
|
|
|
super.close();
|
|
|
- if( __s != null ) socket_close(__s);
|
|
|
+ if( __s != null ) NativeSocket.socket_close(__s);
|
|
|
}
|
|
|
|
|
|
- private static var socket_close = cpp.Lib.load("std","socket_close",1);
|
|
|
- private static var socket_send_char = cpp.Lib.load("std","socket_send_char",2);
|
|
|
- private static var socket_send = cpp.Lib.load("std","socket_send",4);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
@@ -127,13 +120,13 @@ class Socket {
|
|
|
public var custom : Dynamic;
|
|
|
|
|
|
public function new() : Void {
|
|
|
- __s = socket_new(false);
|
|
|
+ __s = NativeSocket.socket_new(false);
|
|
|
input = new SocketInput(__s);
|
|
|
output = new SocketOutput(__s);
|
|
|
}
|
|
|
|
|
|
public function close() : Void {
|
|
|
- socket_close(__s);
|
|
|
+ NativeSocket.socket_close(__s);
|
|
|
untyped {
|
|
|
var input : SocketInput = cast input;
|
|
|
var output : SocketOutput = cast output;
|
|
@@ -145,20 +138,20 @@ class Socket {
|
|
|
}
|
|
|
|
|
|
public function read() : String {
|
|
|
- var bytes:haxe.io.BytesData = socket_read(__s);
|
|
|
+ var bytes:haxe.io.BytesData = NativeSocket.socket_read(__s);
|
|
|
if (bytes==null) return "";
|
|
|
return bytes.toString();
|
|
|
}
|
|
|
|
|
|
public function write( content : String ) : Void {
|
|
|
- socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
|
|
|
+ NativeSocket.socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
|
|
|
}
|
|
|
|
|
|
public function connect(host : Host, port : Int) : Void {
|
|
|
try {
|
|
|
- socket_connect(__s, host.ip, port);
|
|
|
+ NativeSocket.socket_connect(__s, host.ip, port);
|
|
|
} catch( s : String ) {
|
|
|
- if( s == "std@socket_connect" )
|
|
|
+ if( s == "Invalid socket handle" )
|
|
|
throw "Failed to connect on "+host.toString()+":"+port;
|
|
|
else if (s == "Blocking") {
|
|
|
// Do nothing, this is not a real error, it simply indicates
|
|
@@ -170,19 +163,19 @@ class Socket {
|
|
|
}
|
|
|
|
|
|
public function listen(connections : Int) : Void {
|
|
|
- socket_listen(__s, connections);
|
|
|
+ NativeSocket.socket_listen(__s, connections);
|
|
|
}
|
|
|
|
|
|
public function shutdown( read : Bool, write : Bool ) : Void {
|
|
|
- socket_shutdown(__s,read,write);
|
|
|
+ NativeSocket.socket_shutdown(__s,read,write);
|
|
|
}
|
|
|
|
|
|
public function bind(host : Host, port : Int) : Void {
|
|
|
- socket_bind(__s, host.ip, port);
|
|
|
+ NativeSocket.socket_bind(__s, host.ip, port);
|
|
|
}
|
|
|
|
|
|
public function accept() : Socket {
|
|
|
- var c = socket_accept(__s);
|
|
|
+ var c = NativeSocket.socket_accept(__s);
|
|
|
var s = Type.createEmptyInstance(Socket);
|
|
|
s.__s = c;
|
|
|
s.input = new SocketInput(c);
|
|
@@ -191,7 +184,7 @@ class Socket {
|
|
|
}
|
|
|
|
|
|
public function peer() : { host : Host, port : Int } {
|
|
|
- var a : Dynamic = socket_peer(__s);
|
|
|
+ var a : Dynamic = NativeSocket.socket_peer(__s);
|
|
|
if (a == null) {
|
|
|
return null;
|
|
|
}
|
|
@@ -201,7 +194,7 @@ class Socket {
|
|
|
}
|
|
|
|
|
|
public function host() : { host : Host, port : Int } {
|
|
|
- var a : Dynamic = socket_host(__s);
|
|
|
+ var a : Dynamic = NativeSocket.socket_host(__s);
|
|
|
if (a == null) {
|
|
|
return null;
|
|
|
}
|
|
@@ -211,7 +204,7 @@ class Socket {
|
|
|
}
|
|
|
|
|
|
public function setTimeout( timeout : Float ) : Void {
|
|
|
- socket_set_timeout(__s, timeout);
|
|
|
+ NativeSocket.socket_set_timeout(__s, timeout);
|
|
|
}
|
|
|
|
|
|
public function waitForRead() : Void {
|
|
@@ -219,38 +212,22 @@ class Socket {
|
|
|
}
|
|
|
|
|
|
public function setBlocking( b : Bool ) : Void {
|
|
|
- socket_set_blocking(__s,b);
|
|
|
+ NativeSocket.socket_set_blocking(__s,b);
|
|
|
}
|
|
|
|
|
|
public function setFastSend( b : Bool ) : Void {
|
|
|
- socket_set_fast_send(__s,b);
|
|
|
+ NativeSocket.socket_set_fast_send(__s,b);
|
|
|
}
|
|
|
|
|
|
public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float ) : {read: Array<Socket>,write: Array<Socket>,others: Array<Socket>} {
|
|
|
- var neko_array = socket_select(read,write,others, timeout);
|
|
|
+ var neko_array = NativeSocket.socket_select(read,write,others, timeout);
|
|
|
if (neko_array==null)
|
|
|
throw "Select error";
|
|
|
- return {
|
|
|
+ return @:fixed {
|
|
|
read: neko_array[0],
|
|
|
write: neko_array[1],
|
|
|
others: neko_array[2]
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private static var socket_new = cpp.Lib.load("std","socket_new",1);
|
|
|
- private static var socket_close = cpp.Lib.load("std","socket_close",1);
|
|
|
- private static var socket_write = cpp.Lib.load("std","socket_write",2);
|
|
|
- private static var socket_read = cpp.Lib.load("std","socket_read",1);
|
|
|
- private static var socket_connect = cpp.Lib.load("std","socket_connect",3);
|
|
|
- private static var socket_listen = cpp.Lib.load("std","socket_listen",2);
|
|
|
- private static var socket_select = cpp.Lib.load("std","socket_select",4);
|
|
|
- private static var socket_bind = cpp.Lib.load("std","socket_bind",3);
|
|
|
- private static var socket_accept = cpp.Lib.load("std","socket_accept",1);
|
|
|
- private static var socket_peer = cpp.Lib.load("std","socket_peer",1);
|
|
|
- private static var socket_host = cpp.Lib.load("std","socket_host",1);
|
|
|
- private static var socket_set_timeout = cpp.Lib.load("std","socket_set_timeout",2);
|
|
|
- private static var socket_shutdown = cpp.Lib.load("std","socket_shutdown",3);
|
|
|
- private static var socket_set_blocking = cpp.Lib.load("std","socket_set_blocking",2);
|
|
|
- private static var socket_set_fast_send = cpp.Lib.loadLazy("std","socket_set_fast_send",2);
|
|
|
-
|
|
|
}
|