Stream.hx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package hl.uv;
  2. @:hlNative("uv")
  3. class Stream extends Handle {
  4. public function write( bytes : haxe.io.Bytes, ?onWrite : Bool -> Void, pos = 0, len = -1 ) {
  5. if( len < 0 ) len = bytes.length - pos;
  6. if( pos < 0 || len < 0 || pos+len > bytes.length ) throw haxe.io.Error.OutsideBounds;
  7. if( handle == null || !stream_write(handle, (bytes : hl.Bytes).offset(pos), len, onWrite) ) throw new haxe.io.Eof();
  8. }
  9. public function readStartRaw( onData : hl.Bytes -> Int -> Void ) {
  10. if( handle == null || !stream_read_start(handle, onData) ) throw new haxe.io.Eof();
  11. }
  12. public function readStart( onData : haxe.io.Bytes -> Void ) {
  13. readStartRaw(function(b, len) onData(if( len < 0 ) null else b.toBytes(len)));
  14. }
  15. public function readStop() {
  16. if( handle != null ) stream_read_stop(handle);
  17. }
  18. public function listen( n : Int, onConnect : Void -> Void ) {
  19. if( handle == null || !stream_listen(handle, n, onConnect) ) throw new haxe.io.Eof();
  20. }
  21. // --
  22. static function stream_write( handle : HandleData, bytes: hl.Bytes, len : Int, callb : Bool -> Void ) : Bool {
  23. return false;
  24. }
  25. static function stream_read_start( handle : HandleData, callb : hl.Bytes -> Int -> Void ) {
  26. return false;
  27. }
  28. static function stream_read_stop( handle : HandleData ) {
  29. }
  30. static function stream_listen( handle : HandleData, n : Int, callb : Void -> Void ) {
  31. return false;
  32. }
  33. }