diff --git a/std/java/_std/sys/net/DatagramSocket.hx b/std/java/_std/sys/net/DatagramSocket.hx new file mode 100644 index 00000000000..60c04d8232c --- /dev/null +++ b/std/java/_std/sys/net/DatagramSocket.hx @@ -0,0 +1,109 @@ +package sys.net; + +import sys.net.SocketAddress; +import java.net.DatagramPacket; +import java.nio.ByteBuffer; +import java.net.InetSocketAddress; +import java.net.InetAddress; +import java.nio.channels.DatagramChannel; +import haxe.io.Bytes; +import sys.net.IpAddress; + +@:coreApi +class DatagramSocket { + public static function bind(address:SocketAddress):DatagramSocket { + var address = new InetSocketAddress(InetAddress.getByAddress(address.getBytes().getData()), address.getPort()); + var channel = DatagramChannel.open(); + channel.bind(address); + return new DatagramSocket(channel); + } + + final channel:DatagramChannel; + + function new(channel:DatagramChannel) { + this.channel = channel; + } + + public var broadcast(get, set):Bool; + public var multicastLoopV4(get, set):Bool; + public var multicastLoopV6(get, set):Bool; + public var ttl(get, set):Int; + public var onlyV6(get, set):Bool; + + public function joinMulticastV4(multiaddr:Ipv4Addr, inter:Ipv4Addr):Void { + // channel.join() + } + public function joinMulticastV6(multiaddr:Ipv6Addr, inter:Int):Void {} + public function leaveMulticastV4(multiaddr:Ipv4Addr):Void {} + public function leaveMulticastV6(multiaddr:Ipv6Addr):Void {} + + public function sendTo(buf:Bytes, bufOffset:Int, bufSize:Int, address:SocketAddress):Int { + var buf = ByteBuffer.wrap(buf.getData(), bufOffset, bufSize); + var address = new InetSocketAddress(InetAddress.getByAddress(address.getBytes().getData()), address.getPort()); + return channel.send(buf, address); + } + + public function receiveFrom(buf:Bytes, bufOffset:Int, bufSize:Int):{bytesRead:Int, address:SocketAddress} { + // channel.receive doesn't give information about the lenght of received data + var packet = new DatagramPacket(buf.getData(), bufOffset, bufSize); + channel.socket().receive(packet); + return { + bytesRead: packet.getLength(), + address: SocketAddressTools.fromBytes(null, Bytes.ofData(packet.getAddress().getAddress()), packet.getPort()) + }; + } + + public function connect(address:SocketAddress):Void { + var address = new InetSocketAddress(InetAddress.getByAddress(address.getBytes().getData()), address.getPort()); + channel.connect(address); + } + + public function send(buf:Bytes, bufOffset:Int, bufSize:Int):Int { + var buf = ByteBuffer.wrap(buf.getData(), bufOffset, bufSize); + return channel.write(buf); + } + public function receive(buf:Bytes, bufOffset:Int, bufSize:Int):Int { + var buf = ByteBuffer.wrap(buf.getData(), bufOffset, bufSize); + return channel.read(buf); + } + + function get_broadcast():Bool { + return false; + } + + function set_broadcast(value:Bool):Bool { + return false; + } + + function get_multicastLoopV4():Bool { + return false; + } + + function set_multicastLoopV4(value:Bool):Bool { + return false; + } + + function get_multicastLoopV6():Bool { + return false; + } + + function set_multicastLoopV6(value:Bool):Bool { + return false; + } + + function get_ttl():Int { + return 0; + } + + function set_ttl(value:Int):Int { + return 0; + } + + function get_onlyV6():Bool { + return false; + } + + function set_onlyV6(value:Bool):Bool { + return false; + } +} diff --git a/std/sys/net/DatagramSocket.hx b/std/sys/net/DatagramSocket.hx new file mode 100644 index 00000000000..b2f70fe90b5 --- /dev/null +++ b/std/sys/net/DatagramSocket.hx @@ -0,0 +1,50 @@ +package sys.net; + +import haxe.io.Bytes; +import sys.net.IpAddress; + +/** + A User Datagram Protocol socket. +**/ +@:coreApi +extern class DatagramSocket { + public static function bind(address:SocketAddress):DatagramSocket; + + public var broadcast(get, set):Bool; + public var multicastLoopV4(get, set):Bool; + public var multicastLoopV6(get, set):Bool; + public var ttl(get, set):Int; + public var onlyV6(get, set):Bool; + + public function joinMulticastV4(multiaddr:Ipv4Addr, inter:Ipv4Addr):Void; + public function joinMulticastV6(multiaddr:Ipv6Addr, inter:Int):Void; + public function leaveMulticastV4(multiaddr:Ipv4Addr):Void; + public function leaveMulticastV6(multiaddr:Ipv6Addr):Void; + + public function sendTo(buf:Bytes, bufOffset:Int, bufSize:Int, address:SocketAddress):Int; + public function receiveFrom(buf:Bytes, bufOffset:Int, bufSize:Int):{bytesRead:Int, address:SocketAddress}; + + public function connect(address:SocketAddress):Void; + public function send(buf:Bytes, bufOffset:Int, bufSize:Int):Int; + public function receive(buf:Bytes, bufOffset:Int, bufSize:Int):Int; + + private function get_broadcast():Bool; + + private function set_broadcast(value:Bool):Bool; + + private function get_multicastLoopV4():Bool; + + private function set_multicastLoopV4(value:Bool):Bool; + + private function get_multicastLoopV6():Bool; + + private function set_multicastLoopV6(value:Bool):Bool; + + private function get_ttl():Int; + + private function set_ttl(value:Int):Int; + + private function get_onlyV6():Bool; + + private function set_onlyV6(value:Bool):Bool; +} diff --git a/std/sys/net/SocketAddress.hx b/std/sys/net/SocketAddress.hx index 7eac18aab80..5bafd1de510 100644 --- a/std/sys/net/SocketAddress.hx +++ b/std/sys/net/SocketAddress.hx @@ -1,5 +1,6 @@ package sys.net; +import haxe.io.Bytes; import sys.net.IpAddress; @:using(sys.net.SocketAddress.SocketAddressTools) @@ -22,4 +23,12 @@ class SocketAddressTools { case Ipv6(_, p, _, _): p; } } + + public static function fromBytes(_:SocketAddress, bytes:Bytes, port:Int) { + return switch bytes.length { + case 4: Ipv4(Ipv4Addr.fromBytes(bytes), port); + case 16: Ipv6(Ipv6Addr.fromBytes(bytes), port, 0, 0); + case _: throw "invalid ip address"; + } + } } \ No newline at end of file diff --git a/std/sys/net/UDPSocket.hx b/std/sys/net/UDPSocket.hx deleted file mode 100644 index 44927b47715..00000000000 --- a/std/sys/net/UDPSocket.hx +++ /dev/null @@ -1,6 +0,0 @@ -package sys.net; - -@:coreApi -extern class UDPSocket { - // TODO -} \ No newline at end of file