UdpSocket.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.net;
  23. import haxe.extern.Rest;
  24. import sys.net.Socket;
  25. import cs.NativeArray;
  26. import cs.system.collections.ArrayList;
  27. import cs.system.net.IPEndPoint;
  28. import cs.system.net.EndPoint;
  29. import cs.system.net.IPAddress;
  30. import cs.system.net.sockets.AddressFamily;
  31. import cs.system.net.sockets.NetworkStream;
  32. import cs.system.net.sockets.ProtocolType;
  33. import cs.system.net.sockets.SocketFlags;
  34. import cs.system.net.sockets.SocketShutdown;
  35. import cs.system.net.sockets.SocketType;
  36. import cs.system.threading.Thread;
  37. import cs.system.net.sockets.Socket in NativeSocket;
  38. import cs.types.UInt8;
  39. import cs.Ref;
  40. import haxe.io.Bytes;
  41. import haxe.io.Error;
  42. import haxe.io.Input;
  43. import haxe.io.Output;
  44. @:coreapi
  45. class UdpSocket extends Socket {
  46. public function new() {
  47. super();
  48. }
  49. override private function init():Void {
  50. sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  51. }
  52. override public function bind(host:Host, port:Int):Void {
  53. sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  54. var endpoint:IPEndPoint = new IPEndPoint(host.ipAddress, port);
  55. sock.Bind(endpoint);
  56. }
  57. public function sendTo(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
  58. var data = new NativeArray<UInt8>(len);
  59. var indices:NativeArray<Int>;
  60. for (i in 0...len) {
  61. indices = NativeArray.make(i);
  62. data.SetValue(cast buf.get(pos + i), indices);
  63. }
  64. var host = addr.getHost();
  65. var ip:IPAddress = IPAddress.Parse(host.toString());
  66. var endpoint:IPEndPoint = new IPEndPoint(ip, addr.port);
  67. return this.sock.SendTo(data, endpoint);
  68. }
  69. public function readFrom(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
  70. var endpoint:EndPoint = cast new IPEndPoint(IPAddress.Any, 0);
  71. var data:NativeArray<UInt8> = new NativeArray(len);
  72. var length:Int = -1;
  73. try {
  74. length = this.sock.ReceiveFrom(data, endpoint);
  75. } catch (e:Dynamic) {
  76. return length;
  77. }
  78. var ipEndpoint:IPEndPoint = cast endpoint;
  79. addr.host = ipEndpoint.Address.Address.high;
  80. addr.port = ipEndpoint.Port;
  81. var i:Int = 0;
  82. for (each in data.iterator()) {
  83. buf.set(pos + i, each);
  84. i += 1;
  85. }
  86. return length;
  87. }
  88. public function setBroadcast(b:Bool):Void {
  89. sock.EnableBroadcast = b;
  90. }
  91. }