Skip to content

Commit 6028077

Browse files
committed
advance messenger
1 parent 6327330 commit 6028077

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

R/messenger.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,30 @@ messenger <- function(dial = NULL, listen = NULL) {
3737
return(invisible(sock))
3838
}
3939
on.exit(expr = {
40+
s <- .Call(rnng_send, sock, as.raw(0L), 0L)
4041
close(sock)
4142
invisible()
4243
})
43-
. <- unlist(strsplit("nanonext messenger", ""))
44-
.. <- .[length(.):1]
45-
for (i in seq_along(..)) {
46-
cat("\r", `length<-`(.., i), sep = " ", file = stdout())
47-
if (i %in% c(1:5, 15:20)) Sys.sleep(0.03) else Sys.sleep(0.01)
48-
}
49-
for (i in seq_along(.)) {
50-
cat("\r", `length<-`(., i), sep = " ", file = stdout())
51-
if (i %in% c(1:5, 15:20)) Sys.sleep(0.01) else Sys.sleep(0.03)
44+
intro <- unlist(strsplit("nanonext messenger", ""))
45+
for (i in seq_along(intro)) {
46+
cat("\r", `length<-`(intro, i), sep = " ", file = stdout())
47+
Sys.sleep(0.02)
5248
}
5349
cat(" | type your message:\n", file = stdout())
50+
s <- .Call(rnng_send, sock, as.raw(0L), 0L)
51+
if (is.integer(s)) {
52+
cat("[ peer offline ] waiting for connection...\n", file = stdout())
53+
} else {
54+
cat("[ peer online ] connected\n", file = stdout())
55+
}
5456
repeat {
5557
data <- readline()
5658
if (identical(data, ":q")) break
59+
if (identical(data, "")) next
5760
data <- writeBin(object = data, con = raw())
58-
s <- .Call(rnng_send, sock, data, FALSE)
59-
if (is.integer(s)) message(sprintf("%s [ no connection ] message not sent", format.POSIXct(Sys.time())))
61+
s <- .Call(rnng_send, sock, data, 0L)
62+
if (is.integer(s)) message(sprintf("%s [ peer offline ] message not sent", format.POSIXct(Sys.time())))
6063
}
61-
on.exit()
62-
close(sock)
63-
invisible()
6464

6565
}
6666

src/sockets.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,26 @@ static void rnng_thread(void *arg) {
9999

100100
unsigned char *buf = NULL;
101101
size_t sz;
102+
int xc;
102103
nng_socket *sock = (nng_socket *) arg;
103104

104105
while (1) {
105-
int xc = nng_recv(*sock, &buf, &sz, 1u);
106+
xc = nng_recv(*sock, &buf, &sz, 1u);
106107
if (xc) {
107-
REprintf("Messenger session ended\n");
108+
REprintf("messenger session ended\n");
109+
break;
110+
}
111+
if (!strcmp((const char *) buf, ":q")) {
112+
nng_free(buf, sz);
108113
break;
109114
}
110-
if (!strcmp((const char *) buf, ":q")) break;
111-
REprintf("> %s\n", buf);
115+
if (!strcmp((const char *) buf, "")) {
116+
nng_free(buf, sz);
117+
Rprintf("[ peer status ] changed\n");
118+
continue;
119+
}
120+
121+
Rprintf("> %s\n", buf);
112122
nng_free(buf, sz);
113123
}
114124

0 commit comments

Comments
 (0)