spectrevert / libog Goto Github PK
View Code? Open in Web Editor NEWnon-blocking network I/O and events
License: MIT License
non-blocking network I/O and events
License: MIT License
--------------------------------------- libOG is a minimalist C++ network library providing just enough boiler-plate to use and monitor event-based, non-blocking TCP/UDP sockets. disclaimer ---------- The library is currently in active development. Interfaces and code are not stable and can change drastically. strictly conventional and simple -------------------------------- libOG is designed as a weightless object-oriented layer over the Unix socket functions. It can be used for writing simple servers and client or as a base for higher-level asynchronous code. As such, are avoided most c++ features presenting potential overhead in compilation time, code size or readability. Errors are communicated through return codes and error handling is left to the user. features -------- The typical TCP and UDP classes will be provided by libOG, as well as some base classes for extensibility reasons: <> Socket implemented, base class for socket objects <> TcpStream implemented, TCP client socket <> TcpListener implemented, TCP listener socket <> UdpSocket implemented, basic UDP socket +-------------------------------------+ | ISource | interface for monitorable source +-------------------------------------+ | Socket | base class for sockets +-----------+-------------+-----------+ | TcpStream | TcpListener | UdpSocket | specialised classes +-----------+-------------+-----------+ libOG also has classes aimed at monitoring events: <> Poll implemented (epoll only), used to monitor event sources <> Event implemented, used to manipulate/examine events +----------------------------------+ | IPoll | interface for monitor of event sources +----------------+-----------------+ | Poll (epoll) | Poll (kqueue) | specialised implementations +----------------+-----------------+ non-features ------------ libOG is not: * a general event notification library (libev) * an asynchronous sockets framework (libuv) * a threading library (libpthread) * a $h!tfest of template metaprogramming and dependencies (Boost.Asio) compilation ----------- Requirements: a c++14 compliant compiler documentation ------------- There will be extensive documentation generated from the source files and comments released on 1.0 As of now, the code is mainly self-explainable. Feel free to dive in. license ------- libOG is an open source, libre project licensed under the MIT license.
The TcpListener::accept
method uses intl::accept
under the hood; which itself calls either accept4 or accept and stores the newly created socket handle inside the referenced new_socket
. See:
Line 104 in 8d85736
The problem is that this newly created socket handle is used to replace the one from the referenced new_stream
from TcpListener::accept
using the TcpListener::handle
call.
This means that new_stream
is initialized, a socket is created, but then is closed and replaced by a new handle. See
libOG/src/og/net/TcpListener.cc
Line 45 in 8d85736
See attached strace output. I've lightly commented/modified it.
# start of program - let's say a TCP server
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 3
epoll_create1(EPOLL_CLOEXEC) = 4
bind(3, {sa_family=AF_INET, sin_port=htons(6970), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
listen(3, 128) = 0
epoll_ctl(4, EPOLL_CTL_ADD, 3, {events=EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, data={u32=1, u64=1}}) = 0
epoll_wait(4, [{events=EPOLLIN, data={u32=1, u64=1}}], 1024, -1) = 1
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 5 # new_stream initialized
accept4(3, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK) = 6 # and passed to ::accept
close(5) = 0 # as the new handle gets assigned the first one is closed
Obviously this presents some unwanted overhead and operations.
An optimal solution would be to use a class like expected<T, E>
that would make us be able to change the TcpListener::accept
call into the following method::
expected<TcpListener, og::net::Error> accept(SocketAddr& new_address, int flags);
Unfortunately the std doesn't have such expected
class available yet and I've yet to write the copy operators for all of the classes.
Socket destructor should not close socket fd at any given time.
Should make host_ip() and host_port() variants
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.