citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 76436cae12dc035aeee04edae41893758978043c
parent 7072fbcb820d59021957be9e764a4b3f103976cb
Author: kyle <kyle@getaddrinfo.net>
Date:   Wed, 28 Oct 2015 23:46:54 -0600

viewer: change af_unix class abstraction

- before it was N descriptors per object, now it's 1 per object
- accept() now gives back a single new af_unix connection, or null
- create one text object per connection
  - this will be a single connected binary
  - it'll have it's own protocol (send source, then coverage, ...)

Diffstat:
Mviewer/af_unix.cpp | 35++++++++++++++++++++---------------
Mviewer/af_unix.h | 9++++++---
Mviewer/text.cpp | 3++-
Mviewer/text.h | 4+++-
Mviewer/viewer.cpp | 14+++++++++-----
5 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/viewer/af_unix.cpp b/viewer/af_unix.cpp @@ -10,7 +10,17 @@ af_unix_nonblock::af_unix_nonblock() { - if ((listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) +} + +af_unix_nonblock::af_unix_nonblock(int f) : + fd(f) +{ +} + +void +af_unix_nonblock::set_listen() +{ + if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) err(1, "socket"); struct sockaddr_un addr; @@ -18,45 +28,40 @@ af_unix_nonblock::af_unix_nonblock() addr.sun_family = AF_UNIX; strncpy(addr.sun_path, "socket", sizeof(addr.sun_path) - 1); - if (bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr))) + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) err(1, "bind"); - if (listen(listen_fd, 1024)) + if (listen(fd, 1024)) err(1, "listen"); } -void -af_unix_nonblock::accept_one() +af_unix_nonblock * +af_unix_nonblock::accept() { int new_fd; struct sockaddr_un addr; socklen_t len = sizeof(struct sockaddr_un); - new_fd = accept(listen_fd, (struct sockaddr *)&addr, &len); + new_fd = ::accept(fd, (struct sockaddr *)&addr, &len); if (new_fd == -1) { if (errno != EWOULDBLOCK) { perror("accept"); } - return; + return NULL; } - connected_fds.push_back(new_fd); std::cout << "accepted new connection" << std::endl; + return new af_unix_nonblock(new_fd); } void af_unix_nonblock::read() { - char buffer[512]; int nread; - if (connected_fds.size() == 0) - return; - - nread = ::read(connected_fds[0], buffer, sizeof buffer); + nread = ::read(fd, buffer, sizeof buffer); if (nread == 0) { // don't try to read from this socket anymore - connected_fds.clear(); std::cerr << __func__ << ": eof read!" << std::endl; } if (nread > 0) @@ -69,6 +74,6 @@ af_unix_nonblock::read() af_unix_nonblock::~af_unix_nonblock() { - close(listen_fd); + close(fd); unlink("socket"); } diff --git a/viewer/af_unix.h b/viewer/af_unix.h @@ -6,12 +6,15 @@ class af_unix_nonblock { public: af_unix_nonblock(); + af_unix_nonblock(int); ~af_unix_nonblock(); - void accept_one(); + + af_unix_nonblock *accept(); + void set_listen(); void read(); private: - int listen_fd; - std::vector<int> connected_fds; + int fd; + char buffer[4096]; }; #endif diff --git a/viewer/text.cpp b/viewer/text.cpp @@ -3,7 +3,8 @@ #include "text.h" -text::text() +text::text(af_unix_nonblock *sock) : + socket(sock) { font_file_name = "DejaVuSansMono.ttf"; diff --git a/viewer/text.h b/viewer/text.h @@ -7,12 +7,13 @@ #include <ft2build.h> #include FT_FREETYPE_H +#include "af_unix.h" #include "shader_utils.h" #include "draw.h" class text : public drawable { public: - text(); + text(af_unix_nonblock *); void draw(); void idle(); private: @@ -22,6 +23,7 @@ private: FT_GlyphSlot g; GLuint vbo; shader text_shader; + af_unix_nonblock *socket; void render_text(const char *, float x, float y, float sx, float sy); }; diff --git a/viewer/viewer.cpp b/viewer/viewer.cpp @@ -52,6 +52,9 @@ window::window(int argc, char *argv[]) glutDisplayFunc(display); glutIdleFunc(idle); + + // set socket to listening mode + socket.set_listen(); } void @@ -86,8 +89,12 @@ window::display(void) void window::idle(void) { - socket.accept_one(); - socket.read(); + af_unix_nonblock *temp_socket; + + temp_socket = socket.accept(); + if (temp_socket) + drawables.push_back(new text(temp_socket)); + // socket.read(); for (auto &i : drawables) i->idle(); @@ -97,9 +104,6 @@ int main(int argc, char *argv[]) { window gl_window(argc, argv); - text gl_text; - - gl_window.add(gl_text); gl_window.start(); return 0;