A C++11 networking library with examples.
Limited to "Linux" environments. Scripts are tested and used on CentOS 7.
Uses:
- Lots of C.
- CryptoPP for symmetric key encryption.
- OpenSSL for private key encryption.
- libpqxx for PostgreSQL integration.
- DigitalOcean API for quick deployments.
- Execute some commands to load and build the library:
git clone https://github.com/bwackwat/libjaypea
cd libjaypea
bin/setup-centos7.sh
bin/build-library.sh
- Implement the library e.g. a simple echo server (echo.cpp):
#include "tcp-server.hpp"
int main(int, char**){
EpollServer server(10000, 10);
server.on_read = [&](int fd, const char* packet, size_t length)->ssize_t{
if(server.send(fd, packet, length)){
return -1;
}
return length;
};
server.run();
return 0;
}
- Build your code:
bin/build-with-library.sh
> Usage: <relative path to cpp file> <executable name> <optional additional libraries/compiler flags>
bin/build-with-library.sh echo.cpp echo
- Run your code:
build/echo
The example above creates a multithreaded tcp epoll server listening on port 10000, with a maximum of 10 open sockets per thread.
It is a basic echo server.
server.on_read
is the definitive function that will be called every time a fd (socket connection) has provided data to read.
server.send
will return true on error. If it fails, the lambda returns -1; this will close the connection for safety purposes.
If the send succeeds, then the function returns with a success code (the given length).
server.run
will start the server with a number of threads usually equal to the number of available hyperthreads (std::thread::hardware_concurrency()).
In order of coolness.
- bwackwat. Hopefully this example will replace NGINX running on bwackwat.com...
- comd/com. An AES encrypted server and client. The server provides access to a shell, the client sends commands to that remote shell. Like sshd/ssh.
- message-api. An HTTPS JSON API.
- pgsql-provider. A secure database abstraction layer using pgsql-model as an ORM.
- pgsql-model-test. A simple class to query a PostgreSQL database in a common way.
- ponal-server/ponal-client. A simple redis-inspired key-value store server and corresponding client.
- tcp-poll-server. A TCP server able to manage many connections using poll.
- tcp-client. You can speed test an TCP server.
- echo-server. A fast, simple echo server.
- chat-server/chat-client. An incomplete chat server and client.
- read-stdin-tty. A tool to read from STDIN tty in raw mode.
- keyfile-gen. Creates a key for comd/com.
- http-redirecter. Redirects HTTP traffic. Good practice.
- json-test. Tests JSON parsing.
- queue-test. Tests the queue implementation.