Skip to content

Commit cbce7f0

Browse files
committed
chore: simplify some code
1 parent e7cbc57 commit cbce7f0

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

src/dgram.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn string_from_i8_slice(slice: &[i8]) -> Result<String> {
2323
String::from_utf8(copy).map_err(|_| error("failed to parse i8 slice as string".to_string()))
2424
}
2525

26-
struct MsgItem {
26+
struct MsgInfoItem {
2727
msg: Vec<u8>,
2828
sockaddr: sockaddr_un,
2929
cb: Option<Ref<()>>,
@@ -34,10 +34,13 @@ pub struct DgramSocketWrap {
3434
fd: i32,
3535
env: Env,
3636
handle: *mut sys::uv_poll_t,
37-
msg_queue: LinkedList<MsgItem>,
37+
msg_queue: LinkedList<MsgInfoItem>,
3838
emitter: Emitter,
3939
}
4040

41+
/**
42+
* We implement sockets with uv_poll_t like how we do in SeqpackSocket.
43+
*/
4144
#[napi]
4245
impl DgramSocketWrap {
4346
#[napi(constructor)]
@@ -271,7 +274,7 @@ impl DgramSocketWrap {
271274
Some(cb) => Some(env.create_reference(cb)?),
272275
};
273276

274-
let m = MsgItem {
277+
let m = MsgInfoItem {
275278
sockaddr: addr,
276279
msg,
277280
cb,

src/seqpacket.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@ const DEFAULT_READ_BUF_SIZE: usize = 256 * 1024;
1616

1717
#[derive(Eq, Ord, PartialEq, PartialOrd, Copy, Clone)]
1818
enum State {
19+
/**
20+
* Socket created.
21+
*/
1922
NewSocket = 1,
23+
/**
24+
* Socket is marked to be shut down(write end).
25+
*/
2026
ShuttingDown = 2,
27+
/**
28+
* Socket shut down(write end).
29+
*/
2130
ShutDown = 3,
2231
// Stopped = 4,
32+
/**
33+
* Both read side and write side of the socket have been closed.
34+
*/
2335
Closed = 5,
2436
}
2537

26-
struct MsgItem {
38+
struct MsgInfoItem {
2739
msg: Vec<u8>,
2840
cb: Option<Ref<()>>,
2941
}
@@ -33,7 +45,10 @@ pub struct SeqpacketSocketWrap {
3345
fd: i32,
3446
env: Env,
3547
handle: *mut sys::uv_poll_t,
36-
msg_queue: LinkedList<MsgItem>,
48+
msg_queue: LinkedList<MsgInfoItem>,
49+
/**
50+
* The length of bytes that we use to read buffers.
51+
*/
3752
read_buf_size: usize,
3853
state: State,
3954
poll_events: i32,
@@ -46,6 +61,15 @@ impl UvRefence for SeqpacketSocketWrap {
4661
}
4762
}
4863

64+
/**
65+
* We implement sockets with uv_poll_t:
66+
*
67+
* uv_poll_init()
68+
* uv_poll_start()
69+
* -> UV_READABLE, read(), socket()
70+
* -> UV_WRITABLE, connect(), write()
71+
* uv_poll_stop()
72+
*/
4973
#[napi]
5074
impl SeqpacketSocketWrap {
5175
#[napi(constructor)]
@@ -249,7 +273,7 @@ impl SeqpacketSocketWrap {
249273
}
250274
}
251275

252-
fn finish_msg(&self, mut msg: MsgItem) -> Result<()> {
276+
fn finish_msg(&self, mut msg: MsgInfoItem) -> Result<()> {
253277
let env = self.env;
254278

255279
if msg.cb.is_none() {
@@ -282,7 +306,7 @@ impl SeqpacketSocketWrap {
282306
}
283307

284308
fn _flush(&mut self) -> Result<()> {
285-
let mut finished_msgs: LinkedList<MsgItem> = LinkedList::new();
309+
let mut finished_msgs: LinkedList<MsgInfoItem> = LinkedList::new();
286310

287311
loop {
288312
let msg = self.msg_queue.pop_front();
@@ -547,7 +571,7 @@ impl SeqpacketSocketWrap {
547571
let offset = offset.get_int32()?;
548572
let length = length.get_int32()?;
549573
let msg = buf_into_vec(buf, offset, length)?;
550-
self.msg_queue.push_back(MsgItem {
574+
self.msg_queue.push_back(MsgInfoItem {
551575
msg,
552576
cb: match cb {
553577
Some(cb) => {
@@ -584,39 +608,22 @@ extern "C" fn on_close(handle: *mut sys::uv_handle_t) {
584608
};
585609
}
586610

587-
// TODO use macro to simplify these
588-
extern "C" fn on_socket(handle: *mut sys::uv_poll_t, status: c_int, events: c_int) {
589-
if status == sys::uv_errno_t::UV_ECANCELED as i32 {
590-
return;
591-
}
592-
593-
unsafe { assert!(!(*handle).data.is_null(), "unexpected null handle data"); };
594-
let data = unsafe { Box::from_raw((*handle).data as *mut HandleData) };
595-
let wrap = data.inner_mut_ref::<&mut SeqpacketSocketWrap>().unwrap();
596-
wrap.handle_socket(status, events);
597-
598-
Box::into_raw(data);
599-
}
600-
601-
extern "C" fn on_connect(handle: *mut sys::uv_poll_t, status: c_int, events: c_int) {
602-
if status == sys::uv_errno_t::UV_ECANCELED as i32 {
603-
return;
604-
}
611+
macro_rules! on_event {
612+
($event: ident, $fn: ident) => {
613+
extern "C" fn $event(handle: *mut sys::uv_poll_t, status: c_int, events: c_int) {
614+
if status == sys::uv_errno_t::UV_ECANCELED as i32 {
615+
return;
616+
}
605617

606-
unsafe { assert!(!(*handle).data.is_null(), "unexpected null handle data"); };
607-
let data = unsafe { Box::from_raw((*handle).data as *mut HandleData) };
608-
let wrap = data.inner_mut_ref::<&mut SeqpacketSocketWrap>().unwrap();
609-
wrap.handle_connect(status, events);
610-
Box::into_raw(data);
618+
unsafe { assert!(!(*handle).data.is_null(), "unexpected null handle data"); };
619+
let data = unsafe { Box::from_raw((*handle).data as *mut HandleData) };
620+
let wrap = data.inner_mut_ref::<&mut SeqpacketSocketWrap>().unwrap();
621+
wrap.$fn(status, events);
622+
Box::into_raw(data);
623+
}
624+
};
611625
}
612626

613-
extern "C" fn on_io(handle: *mut sys::uv_poll_t, status: c_int, events: c_int) {
614-
if status == sys::uv_errno_t::UV_ECANCELED as i32 {
615-
return;
616-
}
617-
unsafe { assert!(!(*handle).data.is_null(), "unexpected null handle data"); };
618-
let data = unsafe { Box::from_raw((*handle).data as *mut HandleData) };
619-
let wrap = data.inner_mut_ref::<&mut SeqpacketSocketWrap>().unwrap();
620-
wrap.handle_io(status, events);
621-
Box::into_raw(data);
622-
}
627+
on_event!(on_socket, handle_socket);
628+
on_event!(on_connect, handle_connect);
629+
on_event!(on_io, handle_io);

src/socket.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ impl Drop for Emitter {
6666
}
6767
}
6868

69+
/**
70+
* Helper to use the emit() of js "EventEmitter".
71+
*/
6972
impl Emitter {
7073
pub fn new(env: Env, emit: JsFunction) -> Result<Self> {
7174
let emit_ref = env.create_reference(emit)?;
@@ -154,6 +157,10 @@ impl HandleData {
154157
}
155158
}
156159

160+
/**
161+
* Add references to prevent Node.js from automatically exiting if there is
162+
* no references in the loop.
163+
*/
157164
pub trait UvRefence {
158165
fn get_handle(&self) -> *mut sys::uv_poll_t;
159166

0 commit comments

Comments
 (0)