Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/matt-attack/netlibrary
A simple networking library for C++. Designed with games in mind, but can be used for other things. Tested to work on Android and Windows.
https://github.com/matt-attack/netlibrary
Last synced: 2 months ago
JSON representation
A simple networking library for C++. Designed with games in mind, but can be used for other things. Tested to work on Android and Windows.
- Host: GitHub
- URL: https://github.com/matt-attack/netlibrary
- Owner: matt-attack
- License: mit
- Created: 2014-06-24T02:16:27.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-21T17:31:55.000Z (about 9 years ago)
- Last Synced: 2024-08-04T02:10:51.903Z (6 months ago)
- Language: C++
- Homepage:
- Size: 43.9 KB
- Stars: 6
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- AwesomeCppGameDev - netlibrary
README
Here's the basics of how to use this...
First, include the Connection.h file:
```cpp
#include "Netlibrary/Connection.h"
```Then you must open a connection on some port:
```cpp
NetConnection connection;
connection.Open(5432/*insert your port here*/);
```Then you can connect to another server, or just run a message receive loop as shown further down to operate a server.
This is a blocking function. It tries to connect 4 times, with a timeout of 1 second each before failing:
```cpp
int status = connection.Connect(Address(127,0,0,1,5007), "password", 0/*status string pointer if you want*/);
if (status < 0)
{
//connection failed
}
```To receive messages:
```cpp
Peer* sender; int size;
char* buffer;
while (buffer = connection.Receive(sender, size))
{
//do stuff with your message
delete[] buffer;//delete the message buffer when you are finished with it, or else it leaks
}
```Now, there are four different types of messages that you can send:
- Unreliable, Unsequenced Messages - these may or may not arrive in any order
- Reliable, Unordered Messages - these are guaranteed to arrive, but can be in any order
- Reliable, Ordered Messages - these are guaranteed to arrive in the order that they were sent in
- OOB Messages - these are unreliable messages used primarily for low level commands that are sent immediately```cpp
//There are two different ways to send messages, either by specifying the Peer* or not
netconnection.SendOOB(char* data, int size);
netconnection.SendOOB(Peer* peer, char* data, int size);netconnection.Send(char* data, int size);
netconnection.Send(Peer* peer, char* data, int size);netconnection.SendReliable(char* data, int size);
netconnection.SendReliable(Peer* peer, char* data, int size);
```You may access all of the connected peers through the peers std::map
```cpp
netconnection.peers
```