Aidan63 / hxcpp_luv_io

WIP libuv powered async io and sys.thread.EventLoop for hxcpp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Libuv powered asynchronous IO and haxe event loop, more functions and classes will be needed as and when I need them.

File

cpp.asio.File.open(
    'file.txt',
    OpenMode.ReadWrite | OpenMode.Append,
    AccessMode.UserReadWriteExecute,
    result -> {
        switch result {
            case Success(file):
                // 'file' is the object to manipulate the opened file.
                // Below are some examples of file manipulation

                file.read(result -> {
                    switch result {
                        case Success(bytes):
                            // 'bytes' is a `haxe.io.Bytes` object containing the complete file contents.
                        case Error(code):
                            trace('failed to read file data : $code');
                    }
                });

                file.write(Bytes.ofString('hello, world!'), result -> {
                    switch result {
                        case Success(count):
                            // 'count' contains the number of bytes written to the file.
                        case Error(code):
                            trace('failed to write data : $code');
                    }
                });

                file.close(result -> {
                    switch result {
                        case Some(v):
                            trace('failed to close file $v');
                        case None:
                            trace('file closed');
                    }
                });
            case Error(code):
                trace('failed to open file : $code');
        }
    });

Directory

cpp.asio.Directory.open(
    'C:/Some/Folder',
    result -> {
        switch result {
            case Success(dir):
                // `dir` is the object to manipulate the opened directory.
                // Currently iterating over the items within the directory is the only supported operation.

                dir.iter(result -> {
                    // This callback can be invoked multiple times, once for each item in the directory.
                    // `Error(Code.eof)` is returned after all items have been iterated.

                    switch result {
                        case Success(entry):
                            trace(entry.name, entry.type);
                        case Error(error):
                            trace(error);

                            // Once error has been raised no further `iter` callbacks will be made

                            dir.close(result -> {
                                switch result {
                                    case Some(error):
                                        trace(error);
                                    case None:
                                        trace('dir closed');
                                }
                            });
                    }
                });
            case Error(error):
                trace(error);
        }
    });

Process

cpp.asio.Process.spawn(
    'C:/HaxeToolkit/haxe.exe',
    {
        env: [ "MY_TEST_ENV" => "TRUE", "SOME_DEFINE" => null ],
        args: [ '--help' ]
    },
    result -> {
        switch result {
            case Success(process):
                process.stdout.read(result -> {
                    switch result {
                        case Success(bytes):
                            Sys.print(bytes.toString());
                        case Error(error):
                            trace(error);
                    }
                });
            case Error(error):
                trace(error);
        }
    });

TTY

cpp.asio.TTY.open(Stdin, result ->
{
    switch result
    {
        case Success(_stdin):
            _stdin.read.read(result -> {
                switch result {
                    Success(bytes):
                        trace(bytes.toString());
                    case Error(error):
                        trace(error);
                }
            });
        case Error(error):
            throw new Exception(error.toString());
    }
});

cpp.asio.TTY.open(Stdout, result ->
{
    switch result
    {
        case Success(_stdout):
            _stdout.write.write(Bytes.ofString('Hello, World!'), result -> {
                switch result {
                    case Some(error):
                        trace(error)
                    case None:
                        //
                }
            });
        case Error(error):
            throw new Exception(error.toString());
    }
});

Signals

cpp.asio.Signal.open(result -> {
    switch result {
        case Success(signal):
            signal.start(Interrupt, result -> {
                switch result {
                    case Success(_):
                        trace('Ctrl+C pressed');
                    case Error(error):
                        trace(error);
                }
            });
        case Error(error):
            trace(error);
    }
});

About

WIP libuv powered async io and sys.thread.EventLoop for hxcpp


Languages

Language:C 71.4%Language:Roff 11.4%Language:Perl 5.2%Language:M4 4.2%Language:Makefile 2.3%Language:DIGITAL Command Language 1.6%Language:CMake 1.3%Language:D 0.6%Language:Shell 0.6%Language:Batchfile 0.5%Language:Python 0.4%Language:C++ 0.3%Language:Haxe 0.2%Language:Emacs Lisp 0.0%Language:JavaScript 0.0%