No weird complicated parameters to configure, no confusing function names, no bullcrap.
The working man's transport layer IP communications.
You can either:
- Add it as a git submodule
- Install it and include it
- Download a release build
- Build it yourself
All of these are easy and convenient options.
- Enter the directory of your own git project
- git submodule add https://github.com/Bixkitts/bb-net-lib.git path/to/submodule
- git submodule update --init --recursive
And then to update this library while it's a submodule:
- git submodule update --remote path/to/submodule (After being updated, you need to commit and push the changes to your remote)
- git clone https://github.com/Bixkitts/bb-net-lib.git
- cd bb-net-lib
- mkdir build
- cd build
- cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
- cmake --build . --target install
Add this to your CMakeLists.txt
- target_link_libraries(${PROJECT_NAME} PRIVATE bbnetlib)
Now you can include <bbnetlib.h> in your own project and get to coding.
If you're feeling fancy:
- find_package(bbnetlib 0.1 CONFIG REQUIRED)
If you don't have cmake, well then link the library via your own methods.
- Download a release through the browser or wget/curl
- But the library and include header into a place your own project can use it
- include "bbnetlib.h" and link libbbnetlib.
Follow the steps to Install and Include It, except you do
- cmake --build .
without --target install. Then do with the resultant libbbnetlib.a and include header as you see fit.
Here's TCP:
// We want to store the localhost.
// I've used a global, but you can
// use accessors or a database or anything.
struct host *localhost = NULL;
void your_tcp_packet_handler (char* received_packet_data,
ssize_t size_of_packet,
struct host *host_that_sent_the_packet)
{
// Enable TLS encryption
enable_encryption();
struct host *remotehost = host_that_sent_the_packet;
// Send some data back
const char data[] = "Hello there!";
ssize_t data_size = 13;
send_data_tcp(data, datasize, remotehost);
// We can cache this host
// in up to 16 caches for
// later multicasts!
int cache_index = 0;
cache_host(remotehost, cache_index);
// Send the same packet to a cache full of hosts
multicast_tcp(data, datasize, cache_index);
// TCP:
// We've decided we're completely done with this host,
// Stop communicating with them.
close_connections(remotehost);
// We just need to close connections on the localhost
// and then we escape the top level listen_tcp()
// function!
if (WERE_BORED) {
close_connections(localhost)
}
}
int main()
{
localhost = create_host("YOUR.IP", 1234);
listen_tcp (localhost, your_tcp_packet_handler);
return 0;
}
Here's UDP, it's almost the same:
// We want to store the localhost.
// I've used a global, but you can
// use accessors or a database or anything.
struct host *local_host = NULL;
struct host *cached_remote_host;
void your_upd_packet_handler (char* received_packet_data,
ssize_t size_of_packet,
struct host *host_that_sent_the_packet)
{
struct host *remotehost = host_that_sent_the_packet;
// Get the IP of the remotehost:
char ip[IP_ADDR_LEN] = get_ip(remotehost);
// Send some data back
const char data[] = "Hello there!";
ssize_t data_size = 13;
// UDP is connectionless, we just send a packet
// back to a remote host.
send_data_udp(data, 13, remotehost);
// We just need to close connections on the localhost
// and then we escape the top level listenUDP()
// function!
if (WERE_BORED) {
close_connections(localhost)
}
}
int main()
{
localhost = create_host("YOUR.IP", 1234);
listen_udp (localhost, your_udp_packet_handler);
return 0;
}
Maximum packet size is 1024 bytes, so you know it's a full packet when that's the received size.
Receiving a packet size of 0 in the packet_handler means the remote host negotiated a disconnect,
and -1 packet size means an error (typically a timeout).
TCP connections time out after 5 seconds (this is compiled into the binary, you can change it if necessary).
Hit me up and help me write it.
Request features by opening an issue.
Yes, just enable_encryption() for TLS and have a certificate and key for it next to the binary.
- Nothing really right now, I'm just using this to power my own webservers so I'll adjust it to fit those needs
I haven't touched UDP code in months, it probably doesn't work but I'm actively using TCP and TLS in another project so that should work fine as described here.