commit d058bad6dd38c8d823af73841124889b4a0803e6
parent 20c42c844382d3f5ec2eb5e82a67d119fbc3e84c
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Tue, 15 Mar 2016 20:41:34 -0600
viewer: sync with recent runtime changes
Diffstat:
4 files changed, 122 insertions(+), 67 deletions(-)
diff --git a/viewer/af_unix.cxx b/viewer/af_unix.cxx
@@ -26,7 +26,7 @@ af_unix_nonblock::set_listen()
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, "socket", sizeof(addr.sun_path) - 1);
+ strncpy(addr.sun_path, "../while/viewer_test.socket", sizeof(addr.sun_path) - 1);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
err(1, "bind");
@@ -79,12 +79,66 @@ af_unix_nonblock::write_all(uint8_t *buf, size_t bytes_total)
}
int
-af_unix_nonblock::read_all(uint8_t *buf, size_t bytes_total)
+af_unix_nonblock::read_block(uint64_t &buf)
+{
+ int bytes_left = sizeof(uint64_t);
+ int bytes_read = 0;
+ ssize_t n;
+
+ // Read bytes, busy waiting when the socket returns EAGAIN
+ while (bytes_left > 0) {
+ n = read(fd, &buf + bytes_read, bytes_left);
+
+ if (n == 0)
+ errx(1, "read(): read 0 bytes on socket");
+ if (n < 0 && errno != EAGAIN)
+ err(1, "read()");
+ if (n < 0 && errno == EAGAIN)
+ // Don't let counters increment when n == -1!
+ continue;
+
+ bytes_read += n;
+ bytes_left -= n;
+ }
+
+ return bytes_read;
+}
+
+int
+af_unix_nonblock::read_block(uint8_t *buf, size_t bytes_total)
+{
+ int bytes_left = bytes_total;
+ int bytes_read = 0;
+ ssize_t n;
+
+ // Read bytes, busy waiting when the socket returns EAGAIN
+ while (bytes_left > 0) {
+ n = read(fd, buf + bytes_read, bytes_left);
+
+ if (n == 0)
+ errx(1, "read(): read 0 bytes on socket");
+ if (n < 0 && errno != EAGAIN)
+ err(1, "read()");
+ if (n < 0 && errno == EAGAIN)
+ // Don't let counters increment when n == -1!
+ continue;
+
+ bytes_read += n;
+ bytes_left -= n;
+ }
+
+ return bytes_read;
+}
+
+int
+af_unix_nonblock::read_nonblock(uint8_t *buf, size_t bytes_total)
{
int bytes_left = bytes_total;
int bytes_read = 0;
ssize_t n;
+ // Try and read some bytes, returning the number of bytes read so far if
+ // the socket returns EAGAIN
while (bytes_left > 0) {
n = read(fd, buf + bytes_read, bytes_left);
@@ -93,7 +147,7 @@ af_unix_nonblock::read_all(uint8_t *buf, size_t bytes_total)
if (n < 0 && errno != EAGAIN)
err(1, "read()");
if (n < 0 && errno == EAGAIN)
- /* Do not try to continue reading data */
+ // Break out of the loop and return bytes read so far
break;
bytes_read += n;
diff --git a/viewer/af_unix.h b/viewer/af_unix.h
@@ -9,9 +9,13 @@ public:
af_unix_nonblock(int);
~af_unix_nonblock();
- af_unix_nonblock *accept();
void set_listen();
- int read_all(uint8_t *, size_t);
+ af_unix_nonblock *accept();
+
+ int read_block(uint64_t &);
+ int read_block(uint8_t *, size_t);
+
+ int read_nonblock(uint8_t *, size_t);
int write_all(uint8_t *, size_t);
private:
int fd;
diff --git a/viewer/text.cxx b/viewer/text.cxx
@@ -1,5 +1,6 @@
#include <err.h>
+#include <cassert>
#include <iostream>
#include <vector>
@@ -7,15 +8,36 @@
text::text(af_unix_nonblock *sock) :
socket(sock),
- state(WRITE_REQUEST),
- font(FTGLPixmapFont("DejaVuSansMono.ttf"))
+ state(WRITE),
+ font(FTGLPixmapFont("DejaVuSansMono.ttf")),
+ buffer(NULL)
{
if (font.Error())
errx(1, "%s", "font error");
- font.FaceSize(72);
- font.Render("Hello World!");
- font.Render("Hello World!");
+ uint8_t msg_type = 0;
+ assert(socket->write_all(&msg_type, 1) == 1);
+
+ assert(socket->read_block(num_tus) == 8);
+ std::cerr << "text::text() num tus = " << num_tus << std::endl;
+
+ for (int i = 0; i < num_tus; i++) {
+ uint64_t file_name_sz;
+ assert(socket->read_block(file_name_sz) == 8);
+ std::cerr << "text::text() file name size= " << file_name_sz << std::endl;
+
+ file_name = (char *)malloc(file_name_sz + 1);
+ assert(socket->read_block((uint8_t *)file_name, file_name_sz) == file_name_sz);
+ file_name[file_name_sz] = '\0';
+ std::cerr << "text::text() file name = " << file_name << std::endl;
+
+ assert(socket->read_block(num_lines) == 8);
+ std::cerr << "text::text() num lines = " << num_lines << std::endl;
+ }
+
+ font.FaceSize(36);
+ font.Render("Hello World!", 12, FTPoint(0, 36, 0));
+ font.Render(file_name);
}
void
@@ -24,70 +46,43 @@ text::draw()
}
void
-text::parse_buffer()
+text::idle()
{
- uint64_t off = 0;
- /* Read 8 bytes from beginning */
- uint64_t num_tus = buffer[off];
- off += 8;
+ if (state == READ) {
+ size_t n = 0;
+ n = socket->read_nonblock((uint8_t *)buffer + bytes_read, bytes_left);
- std::cerr << __func__ << ": num tus = " << num_tus << std::endl;
+ bytes_read += n;
+ bytes_left -= n;
- for (int i = 0; i < num_tus; i++) {
- //file_name_sz = buffer[off];
- off += 8;
+ if (bytes_left > 0)
+ // There's more data coming
+ return;
- // file_name =
- }
-}
+ std::cerr << "---" << std::endl;
+ for (int i = 0; i < num_lines; i++) {
+ std::cerr << "line " << i << ": " << buffer[i] << std::endl;
+ }
-void
-text::idle()
-{
- std::cerr << "text::idle() enter" << std::endl;
- std::cerr << "text::idle() state = " << state << std::endl;
-
- if (state == WRITE_REQUEST) {
- uint8_t zero = 0;
- if (socket->write_all(&zero, 1) == 1)
- state = READ_HEADER;
- else
- errx(1, "%s", "write_all() failed");
+ state = WRITE;
}
- if (state == READ_HEADER) {
- msg_size = 0;
- size_t n = socket->read_all((uint8_t *)&msg_size, 8);
+ if (state == WRITE) {
+ uint8_t msg_type = 1;
+ if (socket->write_all(&msg_type, 1) != 1)
+ // Couldn't write a request, try again later
+ err(1, "write()");
- if (n == 0)
- return;
- else if (n == 8)
- state = READ_MSG;
- else
- errx(1, "%s %zu bytes", "read_all():", n);
+ // Sent a successful request, listen for reply
+ state = READ;
- std::cerr << "text::idle() msg size is " << msg_size << std::endl;
+ if (buffer != NULL)
+ free(buffer);
- buffer = (uint8_t *)malloc(msg_size);
+ buffer = (uint64_t *)malloc(num_lines * sizeof(uint64_t));
if (buffer == NULL)
err(1, "malloc");
- bytes_left = msg_size;
+ bytes_left = num_lines * sizeof(uint64_t);
bytes_read = 0;
}
- if (state == READ_MSG) {
- size_t n = socket->read_all(buffer + bytes_read, bytes_left);
-
- std::cerr << "text::idle() READ_MSG read " << n << " bytes" << std::endl;
-
- bytes_read += n;
- bytes_left -= n;
-
- if (bytes_left <= 0) {
- //parse_buffer();
- free(buffer);
- state = WRITE_REQUEST;
- std::cerr << "text::idle() got full message" << std::endl;
- std::cerr << "text::idle() ==> resetting" << std::endl;
- }
- }
}
diff --git a/viewer/text.h b/viewer/text.h
@@ -17,19 +17,21 @@ public:
private:
af_unix_nonblock *socket;
+ uint64_t num_tus;
+ char *file_name;
+ uint64_t num_lines;
+
enum states {
- WRITE_REQUEST,
- READ_HEADER,
- READ_MSG
+ READ,
+ WRITE
};
enum states state;
uint64_t msg_size;
uint64_t bytes_left;
uint64_t bytes_read;
- uint8_t *buffer;
+ uint64_t *buffer;
void render_text(const char *, float x, float y, float sx, float sy);
- void parse_buffer();
FTGLPixmapFont font;
};