Skip to content

Commit

Permalink
Merge pull request #1 from kevinresol/master
Browse files Browse the repository at this point in the history
A better UV binding by @kevinresol
  • Loading branch information
Damilare Darmie Akinlaja authored Apr 27, 2019
2 parents f41465b + 5480ebd commit 340eff9
Show file tree
Hide file tree
Showing 30 changed files with 1,069 additions and 857 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@

[submodule "lib/libuv"]
path = lib/libuv
url = https://github.com/libuv/libuv
2 changes: 1 addition & 1 deletion lib/libuv
Submodule libuv updated 212 files
1 change: 0 additions & 1 deletion linc/linc_uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <hxcpp.h>


#include <windows.h>
namespace linc {

namespace uv {
Expand Down
223 changes: 122 additions & 101 deletions linc/linc_uv.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<xml>

<!-- For a more in depth guide: https://github.com/snowkit/hxcpp-guide -->
<!-- For a more in depth guide: https://github.com/snowkit/hxcpp-guide -->
<!-- References the original libuv build instructions: https://github.com/libuv/libuv/blob/v1.x/CMakeLists.txt -->

<set name="UV" value="${LINC_UV_PATH}lib/libuv" />

Expand All @@ -9,126 +10,146 @@
<compilerflag value="-I${UV}/src/"/>
<compilerflag value="-I${UV}/include/"/>
<compilerflag value='-DWIN32_LEAN_AND_MEAN' if="windows" />
<compilerflag value='-D_WIN32_WINNT=0x0600' if="windows" />
<compilerflag value='-D_DARWIN_USE_64_BIT_INODE=1' if="mac" />
<compilerflag value='-D_DARWIN_UNLIMITED_SELECT=1' if="mac" />
<compilerflag value='-D_DARWIN_USE_64_BIT_INODE=1' if="macos" />
<compilerflag value='-D_DARWIN_UNLIMITED_SELECT=1' if="macos" />
<compilerflag value='-D_GNU_SOURCE' if="linux" />
<compilerflag value='-D_WIN32_WINNT=0x0600' if="windows" />
<compilerflag value='-D_DARWIN_USE_64_BIT_INODE=1' if="mac" />
<compilerflag value='-D_DARWIN_UNLIMITED_SELECT=1' if="mac" />
<compilerflag value='-D_DARWIN_USE_64_BIT_INODE=1' if="macos" />
<compilerflag value='-D_DARWIN_UNLIMITED_SELECT=1' if="macos" />
<compilerflag value='-D_GNU_SOURCE' if="linux" />

<compilerflag value='-I${LINC_UV_PATH}linc/'/>
<file name='${LINC_UV_PATH}linc/linc_uv.cpp' />
</files>

<files id="haxe">
<file name="${UV}/src/fs-poll.c" />
<file name="${UV}/src/inet.c" />
<file name="${UV}/src/threadpool.c" />
<file name="${UV}/src/uv-common.c" />
<file name="${UV}/src/version.c" />
<file name="${UV}/src/fs-poll.c"/>
<file name="${UV}/src/idna.c"/>
<file name="${UV}/src/inet.c"/>
<file name="${UV}/src/threadpool.c"/>
<file name="${UV}/src/timer.c"/>
<file name="${UV}/src/uv-common.c"/>
<file name="${UV}/src/uv-data-getter-setters.c"/>
<file name="${UV}/src/version.c"/>

<section if="windows">
<compilerflag value="-I${UV}/src/win/"/>
<file name="${UV}/src/win/async.c" />
<file name="${UV}/src/win/core.c" />
<file name="${UV}/src/win/detect-wakeup.c" />
<file name="${UV}/src/win/dl.c" />
<file name="${UV}/src/win/error.c" />
<file name="${UV}/src/win/fs.c" />
<file name="${UV}/src/win/fs-event.c" />
<file name="${UV}/src/win/getaddrinfo.c" />
<file name="${UV}/src/win/getnameinfo.c" />
<file name="${UV}/src/win/handle.c" />

<file name="${UV}/src/win/loop-watcher.c" />
<file name="${UV}/src/win/pipe.c" />
<file name="${UV}/src/win/poll.c" />
<file name="${UV}/src/win/process.c" />
<file name="${UV}/src/win/process-stdio.c" />
<file name="${UV}/src/win/req.c" />

<file name="${UV}/src/win/signal.c" />
<file name="${UV}/src/win/snprintf.c" />
<file name="${UV}/src/win/stream.c" />

<file name="${UV}/src/win/tcp.c" />
<file name="${UV}/src/win/thread.c" />
<file name="${UV}/src/win/timer.c" />
<file name="${UV}/src/win/tty.c" />
<file name="${UV}/src/win/udp.c" />
<file name="${UV}/src/win/util.c" />
<file name="${UV}/src/win/winapi.c" />
<file name="${UV}/src/win/winapi.c" />
<file name="${UV}/src/win/winsock.c" />
</section>
<file name="${UV}/src/win/async.c"/>
<file name="${UV}/src/win/core.c"/>
<file name="${UV}/src/win/detect-wakeup.c"/>
<file name="${UV}/src/win/dl.c"/>
<file name="${UV}/src/win/error.c"/>
<file name="${UV}/src/win/fs.c"/>
<file name="${UV}/src/win/fs-event.c"/>
<file name="${UV}/src/win/getaddrinfo.c"/>
<file name="${UV}/src/win/getnameinfo.c"/>
<file name="${UV}/src/win/handle.c"/>
<file name="${UV}/src/win/loop-watcher.c"/>
<file name="${UV}/src/win/pipe.c"/>
<file name="${UV}/src/win/thread.c"/>
<file name="${UV}/src/win/poll.c"/>
<file name="${UV}/src/win/process.c"/>
<file name="${UV}/src/win/process-stdio.c"/>
<file name="${UV}/src/win/signal.c"/>
<file name="${UV}/src/win/snprintf.c"/>
<file name="${UV}/src/win/stream.c"/>
<file name="${UV}/src/win/tcp.c"/>
<file name="${UV}/src/win/tty.c"/>
<file name="${UV}/src/win/udp.c"/>
<file name="${UV}/src/win/util.c"/>
<file name="${UV}/src/win/winapi.c"/>
<file name="${UV}/src/win/winsock.c"/>
</section>

<section if="linux">
<compilerflag value="-I${UV}/src/unix/"/>
<file name="${UV}/src/unix/async.c" />
<file name="${UV}/src/unix/core.c" />
<file name="${UV}/src/unix/dl.c" />
<file name="${UV}/src/unix/fs.c" />
<file name="${UV}/src/unix/getaddrinfo.c" />
<file name="${UV}/src/unix/getnameinfo.c" />
<file name="${UV}/src/unix/loop.c" />
<file name="${UV}/src/unix/loop-watcher.c" />
<file name="${UV}/src/unix/pipe.c" />
<file name="${UV}/src/unix/poll.c" />
<file name="${UV}/src/unix/process.c" />
<file name="${UV}/src/unix/proctitle.c" />
<file name="${UV}/src/unix/linux-core.c" />
<file name="${UV}/src/unix/linux-notify.c" />
<file name="${UV}/src/unix/linux-syscalls.c" />
<file name="${UV}/src/unix/signal.c" />
<file name="${UV}/src/unix/stream.c" />
<file name="${UV}/src/unix/tcp.c" />
<file name="${UV}/src/unix/thread.c" />
<file name="${UV}/src/unix/timer.c" />
<file name="${UV}/src/unix/tty.c" />
<file name="${UV}/src/unix/udp.c" />

<file name="${UV}/src/unix/async.c"/>
<file name="${UV}/src/unix/core.c"/>
<file name="${UV}/src/unix/dl.c"/>
<file name="${UV}/src/unix/fs.c"/>
<file name="${UV}/src/unix/getaddrinfo.c"/>
<file name="${UV}/src/unix/getnameinfo.c"/>
<file name="${UV}/src/unix/loop-watcher.c"/>
<file name="${UV}/src/unix/loop.c"/>
<file name="${UV}/src/unix/pipe.c"/>
<file name="${UV}/src/unix/poll.c"/>
<file name="${UV}/src/unix/process.c"/>
<file name="${UV}/src/unix/signal.c"/>
<file name="${UV}/src/unix/stream.c"/>
<file name="${UV}/src/unix/tcp.c"/>
<file name="${UV}/src/unix/thread.c"/>
<file name="${UV}/src/unix/tty.c"/>
<file name="${UV}/src/unix/udp.c"/>

<file name="${UV}/src/unix/proctitle.c"/>

<file name="${UV}/src/unix/linux-core.c"/>
<file name="${UV}/src/unix/linux-inotify.c"/>
<file name="${UV}/src/unix/linux-syscalls.c"/>
<file name="${UV}/src/unix/procfs-exepath.c"/>
<file name="${UV}/src/unix/sysinfo-loadavg.c"/>
<file name="${UV}/src/unix/sysinfo-memory.c"/>
</section>

<section if="macos">
<compilerflag value="-I${UV}/src/unix/"/>
<file name="${UV}/src/unix/async.c" />
<file name="${UV}/src/unix/core.c" />
<file name="${UV}/src/unix/darwin.c" />
<file name="${UV}/src/unix/darwin-proctitle.c" />
<file name="${UV}/src/unix/dl.c" />
<file name="${UV}/src/unix/kqueue.c" />
<file name="${UV}/src/unix/pthread-barrier.c" />
<file name="${UV}/src/unix/fs.c" />
<file name="${UV}/src/unix/fsevents.c" />
<file name="${UV}/src/unix/getaddrinfo.c" />
<file name="${UV}/src/unix/getnameinfo.c" />
<file name="${UV}/src/unix/loop.c" />
<file name="${UV}/src/unix/loop-watcher.c" />
<file name="${UV}/src/unix/pipe.c" />
<file name="${UV}/src/unix/poll.c" />
<file name="${UV}/src/unix/process.c" />
<file name="${UV}/src/unix/proctitle.c" />
<file name="${UV}/src/unix/signal.c" />
<file name="${UV}/src/unix/stream.c" />
<file name="${UV}/src/unix/tcp.c" />
<file name="${UV}/src/unix/thread.c" />
<file name="${UV}/src/unix/timer.c" />
<file name="${UV}/src/unix/tty.c" />
<file name="${UV}/src/unix/udp.c" />

<file name="${UV}/src/unix/async.c"/>
<file name="${UV}/src/unix/core.c"/>
<file name="${UV}/src/unix/dl.c"/>
<file name="${UV}/src/unix/fs.c"/>
<file name="${UV}/src/unix/getaddrinfo.c"/>
<file name="${UV}/src/unix/getnameinfo.c"/>
<file name="${UV}/src/unix/loop-watcher.c"/>
<file name="${UV}/src/unix/loop.c"/>
<file name="${UV}/src/unix/pipe.c"/>
<file name="${UV}/src/unix/poll.c"/>
<file name="${UV}/src/unix/process.c"/>
<file name="${UV}/src/unix/signal.c"/>
<file name="${UV}/src/unix/stream.c"/>
<file name="${UV}/src/unix/tcp.c"/>
<file name="${UV}/src/unix/thread.c"/>
<file name="${UV}/src/unix/tty.c"/>
<file name="${UV}/src/unix/udp.c"/>

<file name="${UV}/src/unix/proctitle.c"/>

<file name="${UV}/src/unix/bsd-ifaddrs.c"/>
<file name="${UV}/src/unix/kqueue.c"/>

<file name="${UV}/src/unix/darwin-proctitle.c"/>
<file name="${UV}/src/unix/darwin.c"/>
<file name="${UV}/src/unix/fsevents.c"/>
</section>


</files>
</files>

<target id="haxe">
<target id="haxe">

<!-- add linker flags to the haxe build output -->
<section if="windows">
<lib name='ws2_32.lib' />
<lib name='advapi32.lib' />
<lib name='iphlpapi.lib' />
<lib name='psapi.lib' />
<lib name='shell32.lib' />
<lib name='userenv.lib' />
</section>
</target>
<!-- add linker flags to the haxe build output -->
<section if="windows">
<lib name='advapi32.lib'/>
<lib name='iphlpapi.lib'/>
<lib name='psapi.lib'/>
<lib name='shell32.lib'/>
<lib name='user32.lib'/>
<lib name='userenv.lib'/>
<lib name='ws2_32.lib'/>
</section>
<section if="linux">
<!--
pthread
dl
rt
-->
</section>
<section if="macos">
<!--
pthread
-->
</section>
</target>

</xml>
107 changes: 101 additions & 6 deletions test/Test.hx
Original file line number Diff line number Diff line change
@@ -1,13 +1,108 @@

package;
import node.Loop;

import cpp.*;
import uv.*;
import uv.Uv;
import haxe.io.Bytes;

class Test {
static function main() {
Foo.foo();
}
}

static function main() {
var loop = new Loop();
class Foo {
public static function foo() {
var loop = Loop.DEFAULT;

trace(loop.run());
}

var addr = new SockAddrIn();
trace(addr.ip4Addr('0.0.0.0', 7000));
var server = new Tcp();
server.init(loop);
server.bind(addr, 0);
addr.destroy();

server.asStream().listen(128, Callable.fromStaticFunction(onConnection));

var client = new Tcp();
client.init(loop);
var connect = new Connect();
var dest = new SockAddrIn();
dest.ip4Addr('127.0.0.1', 7000);
client.connect(connect, dest, Callable.fromStaticFunction(onConnect));

// DNS
var hint = new AddrInfo();
var resolver = new GetAddrInfo();
trace(resolver.get(loop, Callable.fromStaticFunction(onResolve), 'example.com', '80', hint));

loop.run(Uv.RUN_DEFAULT);
trace('quit');
}

static function onConnection(stream:RawPointer<Stream_t>, status:Int) {
trace('connected');
var server:Tcp = stream;
var client = new Tcp();
client.init(Loop.DEFAULT);
if(server.asStream().accept(client) == 0) {
trace('accepted');
trace(client.getPeerAddress());
trace(client.getSockAddress());
trace(client.asStream().readStop());
client.asStream().readStart(Callable.fromStaticFunction(onAlloc), Callable.fromStaticFunction(onRead));
} else {
client.asHandle().close(null);
}
}

static function onAlloc(handle:RawPointer<Handle_t>, suggestedSize:SizeT, buf:RawPointer<Buf_t>) {
var suggestedSize:Int = cast suggestedSize;
var base:Pointer<Char> = cast Stdlib.nativeMalloc(suggestedSize);
buf[0].base = cast base;
buf[0].len = suggestedSize;
}

static function onRead(handle:RawPointer<Stream_t>, nread:SSizeT, buf:RawConstPointer<Buf_t>) {
var client:Tcp = handle;
var nread:Int = cast nread;
if(nread > 0) {
var req = new Write();
var writeBuf = new Buf();
writeBuf.alloc(nread);
req.setData(writeBuf);
writeBuf.copyFrom(buf, nread);
client.asStream().write(req, writeBuf, 1, Callable.fromStaticFunction(onWrite));
}
if(nread < 0) {
if(nread != Uv.EOF) trace('read error $nread');
client.asHandle().close(null);
}
Buf.unmanaged(buf).free();
}

static function onWrite(handle:RawPointer<Write_t>, status:Int) {
var write:Write = handle;
var buf:Buf = write.getData();
buf.destroy();
write.destroy();
}

static function onConnect(req:RawPointer<Connect_t>, status:Int) {
var addr = Tcp.fromStream(Connect.fromRaw(req).handle).getPeerAddress();
trace('onconnect $status ${addr.host.toString()} ${addr.port}');
// var stream = Connect.fromRaw(req).handle;

}

static function onResolve(resolver:RawPointer<GetAddrInfo_t>, status:Int, res:RawPointer<AddrInfo_s>) {
trace('resolve $status');
var addr = AddrInfo.fromRaw(res);
var client = new Tcp();
client.init(Loop.DEFAULT);
var connect = new Connect();
client.connect(connect, addr, Callable.fromStaticFunction(onConnect));
addr.destroy();
}
}
Loading

0 comments on commit 340eff9

Please sign in to comment.