citrun

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

commit 4b56bf0a076f9c9d6e999617f05479198542bc7a
parent a9c538103e75c7c6bde1d72edb1215036766413b
Author: Kyle Milz <kyle@0x30.net>
Date:   Tue, 19 Jul 2016 22:29:57 -0600

src: massage more functions into af_unix

Diffstat:
Msrc/af_unix.cc | 69++++++++++++++++++++++++++++++++++++++++++++-------------------------
Msrc/af_unix.h | 4++++
Msrc/gl_main.cc | 3++-
3 files changed, 50 insertions(+), 26 deletions(-)

diff --git a/src/af_unix.cc b/src/af_unix.cc @@ -1,7 +1,21 @@ +// +// Copyright (c) 2016 Kyle Milz <kyle@0x30.net> +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// #include <sys/socket.h> // accept, socket #include <sys/un.h> // sockaddr_un -#include <err.h> // err #include <cerrno> // EWOULDBLOCK #include <cstring> // memset, strcpy #include <fcntl.h> // fcntl, F_GETFL @@ -12,6 +26,15 @@ af_unix::af_unix() { + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + err(1, "socket"); +} + +af_unix::~af_unix() +{ + close(fd); + if (m_bound) + unlink("/tmp/citrun-gl.socket"); } af_unix::af_unix(int f) : @@ -20,23 +43,30 @@ af_unix::af_unix(int f) : } void -af_unix::set_listen() +af_unix::set_nonblock() { -#if defined(__APPLE__) - // OS X socket() doesn't take SOCK_NONBLOCK so roll it by hand - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) - err(1, "socket"); + int flags; - int flags = fcntl(fd, F_GETFL, 0); - if (flags < 0) + if ((flags = fcntl(fd, F_GETFL, 0)) < 0) err(1, "fcntl(F_GETFL)"); if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) err(1, "fcntl(F_SETFL)"); -#else - if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) - err(1, "socket"); -#endif +} + +void +af_unix::set_block() +{ + int flags; + if ((flags = fcntl(fd, F_GETFL, 0)) < 0) + err(1, "fcntl(F_GETFL)"); + if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) + err(1, "fcntl(F_SETFL)"); +} + +void +af_unix::set_listen() +{ struct sockaddr_un addr; std::memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; @@ -45,6 +75,8 @@ af_unix::set_listen() if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) err(1, "bind"); + m_bound = 1; + // Size 1024 backlog if (listen(fd, 1024)) err(1, "listen"); @@ -66,13 +98,6 @@ af_unix::accept() return NULL; } - // Turn off non blocking mode - int flags; - if ((flags = fcntl(new_fd, F_GETFL, 0)) < 0) - err(1, "fcntl(F_GETFL)"); - if (fcntl(new_fd, F_SETFL, flags & ~O_NONBLOCK) < 0) - err(1, "fcntl(F_SETFL)"); - std::cerr << "accepted new connection" << std::endl; return new af_unix(new_fd); } @@ -118,9 +143,3 @@ af_unix::read_all(uint8_t *buf, size_t bytes_total) return bytes_read; } - -af_unix::~af_unix() -{ - close(fd); - unlink("/tmp/citrun-gl.socket"); -} diff --git a/src/af_unix.h b/src/af_unix.h @@ -1,6 +1,7 @@ #ifndef AF_UNIX_H #define AF_UNIX_H +#include <err.h> // err #include <unistd.h> // read #include <vector> @@ -11,6 +12,8 @@ public: ~af_unix(); void set_listen(); + void set_block(); + void set_nonblock(); af_unix *accept(); // Makes sure reads don't overflow or underflow types @@ -40,6 +43,7 @@ public: int write_all(uint8_t *, size_t); private: int fd; + int m_bound; }; #endif diff --git a/src/gl_main.cc b/src/gl_main.cc @@ -88,7 +88,7 @@ window::window(int argc, char *argv[]) static_vu->setup(); - // This creates the socket with SOCK_NONBLOCK + socket.set_nonblock(); socket.set_listen(); static_vu->toggle_animation(); @@ -153,6 +153,7 @@ window::next_frame(View *vu) { af_unix *temp_socket = window::socket.accept(); if (temp_socket) { + temp_socket->set_block(); demo_buffer_clear(buffer); window::drawables.push_back(new RuntimeProcess(temp_socket, buffer, font)); }