Skip to content

Commit

Permalink
feat: add basic tcp listen
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronostasys committed Sep 9, 2024
1 parent 8448b48 commit fa11c27
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"lldb.showDisassembly": "auto",
"lldb.dereferencePointers": true,
"lldb.consoleMode": "commands",
"makefile.configureOnOpen": true,
// "rust-analyzer.check.targets": [
// "wasm32-unknown-unknown",
// ],
Expand Down
7 changes: 7 additions & 0 deletions planglib/core/gc.pi
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ impl string {
return arr_from_raw(self.data, self._byte_len);
}

pub fn cstr() *u8 {
let arr = [u8*self._byte_len + 1;];
memcpy(&arr[0], self.data, self._byte_len);
arr[self._byte_len] = 0 as u8;
return &arr[0];
}

pub fn chars() [char] {
let bytes = arr_from_raw(self.data, self._byte_len);
let chars = [char*self._len;];
Expand Down
1 change: 1 addition & 0 deletions planglib/std/__private.pi
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ use std::libuv;
use std::task::reactor;
use std::task::executor;
use std::task::delay;
use std::task::tcp;
27 changes: 27 additions & 0 deletions planglib/std/libuv.pi
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ var UV_RUN_NOWAIT = 2 as u32;
var UV_ASYNC = 1 as u32;
var UV_IDLE = 6 as u32;
var UV_TIMER = 13 as u32;
var UV_TCP = 12 as u32;

pub struct uv_loop_t {}
pub struct uv_idle_t {}
pub struct uv_async_t {}
pub struct uv_timer_t {}
pub struct uv_tcp_t {}
pub struct uv_stream_t {}
pub struct sockaddr_in {
a:i64;
b:i64;
c:i64;
d:i64;
}

pub fn uv_default_loop() *uv_loop_t;

Expand Down Expand Up @@ -45,11 +54,29 @@ pub fn uv_async_init(loop:*uv_loop_t, handle:*uv_async_t, cb:* ()) i32;

pub fn uv_async_send(handle:*uv_async_t) i32;

pub fn uv_tcp_init(loop:*uv_loop_t, handle:*uv_tcp_t) i32;

pub fn uv_ip4_addr(ip:*u8, port:i32, addr:*sockaddr_in) i32;

pub fn uv_tcp_bind(handle:*uv_tcp_t, addr:*sockaddr_in, flags:u32) i32;

pub fn uv_listen(handle:*uv_stream_t, backlog:i32, cb:*()) i32;

pub fn uv_accept(server:*uv_stream_t, client:*uv_stream_t) i32;

pub fn uv_read_start(stream:*uv_stream_t, alloc_cb:*(), read_cb:*()) i32;


pub fn new_uv_idle_t() *uv_idle_t {
let re = gc::malloc_pinned(uv_handle_size(UV_IDLE) as i64);
return unsafe_cast<uv_idle_t>(re);
}

pub fn new_uv_tcp_t() *uv_tcp_t {
let re = gc::malloc_pinned(uv_handle_size(UV_TCP) as i64);
return unsafe_cast<uv_tcp_t>(re);
}

pub fn new_uv_async_t() *uv_async_t {
let re = gc::malloc_pinned(uv_handle_size(UV_ASYNC) as i64);
return unsafe_cast<uv_async_t>(re);
Expand Down
59 changes: 58 additions & 1 deletion planglib/std/task/reactor.pi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::libuv;
use std::chan;


struct UVReactor {
async_t: *libuv::uv_async_t;
idle_t: *libuv::uv_idle_t;
Expand All @@ -9,14 +10,33 @@ struct UVReactor {
}


type EV = TimerEV;


type EV = TCPListenEV | TimerEV | TCPReadEV;




struct TimerEV {
wake: ||=>void;
timeout_ms: u64;
repeat_ms: u64;
}

struct TCPListenEV {
wake: ||=>void;
handle: *libuv::uv_tcp_t;
addr: libuv::sockaddr_in;
on_connect: ||=>void;

}

struct TCPReadEV {
wake: ||=>void;
buff: [u8];

}

var GLOBAL_REACTOR = new_uv_reactor();


Expand All @@ -35,9 +55,16 @@ fn close_cb(handle:*()) void {
}


fn conn_cb(server:*libuv::uv_stream_t, status:u32) void {
let on_conn = libuv::get_data_for_handle<libuv::uv_stream_t|||=>void>(server);
(*on_conn)();
return;
}

use std::io;



fn async_cb(async_t:*libuv::uv_async_t) void {
let reactor = libuv::get_data_for_handle<libuv::uv_async_t|UVReactor>(async_t);
let ch = reactor.ch;
Expand All @@ -51,6 +78,21 @@ fn async_cb(async_t:*libuv::uv_async_t) void {
libuv::set_data_for_handle(timer, &ev);
libuv::uv_timer_start(timer, cb, ev.timeout_ms, ev.repeat_ms);

}
TCPListenEV(ev) => {
gc::keep_alive_pinned(ev.handle);
libuv::uv_tcp_init(reactor.loop, ev.handle);
libuv::uv_tcp_bind(ev.handle, &ev.addr, 0 as u32);
libuv::set_data_for_handle(ev.handle, &ev.on_connect);
let cb = unsafe_cast<()>(&conn_cb);

libuv::uv_listen(unsafe_cast<libuv::uv_stream_t>(ev.handle), 128 as i32, cb);


}
TCPReadEV(ev) => {


}
_ => {
}
Expand Down Expand Up @@ -110,6 +152,21 @@ impl UVReactor {
return;
}

pub fn new_tcp_listener(tcp_h:*libuv::uv_tcp_t, ip:string, port:i32, on_conn:||=>void, wake: ||=>void) void {
let addr = libuv::sockaddr_in{};
libuv::uv_ip4_addr(ip.cstr(), port, &addr);
let ev = TCPListenEV{
wake: wake,
handle: tcp_h,
addr: addr,
on_connect: on_conn,
};
let e = ev as EV;
self.ch.send(e);
libuv::uv_async_send(self.async_t);
return;
}


}

Expand Down
8 changes: 8 additions & 0 deletions planglib/std/task/tcp.pi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::task::Task;
use std::task::reactor;
use std::task::executor;



pub struct TCPServer {
}
6 changes: 6 additions & 0 deletions test/main.pi
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub fn main() i64 {
return;
});
executor::GLOBAL_EXECUTOR.spawn(async_1());
reactor::GLOBAL_REACTOR.new_tcp_listener(libuv::new_uv_tcp_t(), "0.0.0.0", 8899 as i32, ||=>{
println!("on connect");
return;
}, ||=>{
return;
});
executor::GLOBAL_EXECUTOR.start_exec_loop();
return 0;
}
Expand Down

0 comments on commit fa11c27

Please sign in to comment.