You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/TCP.md
+15-16Lines changed: 15 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,26 +5,24 @@ As mentioned in the [README](/readme.md), the library has 2 major class that you
5
5
6
6
For readability’s sake, namespaces `polymorph::network::` and `polymorph::network::tcp::` will be omitted but keep in mind you need to mention them or use the following directive :
7
7
```c++
8
-
usingnamespacepolymorph::networ;
8
+
usingnamespacepolymorph::network;
9
9
using namespace polymorph::network::tcp;
10
10
```
11
11
12
12
13
13
## Server
14
14
15
15
### 1) Creating a server
16
-
The `Server` have a constructor that takes a port and a `SessionStore` as parameters. You can create a new Session for the server sake or use one that is already used by **ONE UDP server**.
17
-
```cpp
18
-
SessionStore sessionStoreServer; // Session store of the server (store any client connection)
19
-
// Then, you can create the server
20
-
// Simply pass the session store, the mapping and the port you want the server to use
21
-
Server server(4242, sessionStoreServer);
16
+
The `Server` has a construction method create that takes a port as parameter. It retiurns a `std::unique_ptr<Server>` that you will have to store as long as you want the server to be running.
17
+
```c++
18
+
// Create a server on port 4242
19
+
std::unique_ptr<Server> server = Server::create(4242);
22
20
```
23
21
24
22
### 2) Register packet handlers
25
23
You can now register callbacks to handle received packet. Here is an example of registering a callback for a packet with a payload of type `SampleDto` :
std::cout << "Server received \"" << payload.value << "\" from client with session id " << header.sId << std::endl;
29
27
return true; // or false if you want to disconnect the client
30
28
});
@@ -36,31 +34,32 @@ It is recommended to put a public static variable in all your DTOs to always hav
36
34
### 3) Start the server
37
35
Now you will have to start it in order to accept incoming connections. To do so, call the `Server::start()` method.
38
36
```cpp
39
-
server.start();
37
+
server->start();
40
38
```
41
39
42
40
### 4) Send packets
43
41
You can now send packets to your clients. To do so, you will have to get the `SessionId` of the client you want to send a packet to. You can find it in received packet callbacks in the header.
std::cout << "Server sent packet to client" << std::endl;
47
45
});
48
46
// or, to send to all clients
49
-
server.send(SampleDto::opId, payload);
47
+
server->send(SampleDto::opId, payload);
50
48
```
51
49
52
50
## Client
53
51
54
52
### 1) Creating a client
55
-
The `Client` have a constructor that takes a host string and a port as parameters . You have to pass the address and the port of a running (or soon) server.
53
+
The `Client` has a construction method `create` that takes a host string and a port as parameters . You have to pass the address and the port of a running (or soon) server.
54
+
It retiurns a `std::unique_ptr<Client>` that you will have to store as long as you want the client to be running.
You can now register callbacks to handle received packet. Here is an example of registering a callback for a packet with a payload of type `SampleDto` :
You can now call the `Client::connect()` to initiate the connection with the server. You pass a callback which will be called when the server has accepted/refused the connection.
Copy file name to clipboardExpand all lines: docs/UDP.md
+15-25Lines changed: 15 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
# Polymorph Network Library (UDP part)
2
2
> Here is the documentation for the UDP part. If you want to implement an TCP connection check [this](TCP.md) out.
3
3
4
-
As mentioned in the [README](/readme.md), the library has 2 major class that you will have to instantiate. For UDP those class are `polymorph::network::udp::Server` and `polymorph::network::udp::Client`. There is also `polymorph::network::udp::Connector` which is really important as it handles send and receive operations
4
+
As mentioned in the [README](/readme.md), the library has 2 major class that you will have to instantiate. For UDP those class are `polymorph::network::udp::Server` and `polymorph::network::udp::Client`.
5
5
6
6
For readability’s sake, namespaces `polymorph::network::` and `polymorph::network::udp::` will be omitted but keep in mind you need to mention them or use the following directive :
7
7
```c++
8
-
usingnamespacepolymorph::networ;
8
+
usingnamespacepolymorph::network;
9
9
using namespace polymorph::network::udp;
10
10
```
11
11
@@ -17,23 +17,16 @@ This is why you have to create a "safeties mapping", which is a map of `OpId` to
17
17
## Server
18
18
19
19
### 1) Creating a server
20
-
The `Server` have a constructor that takes a port and a `SessionStore` as parameters. You can create a new Session for the server sake or use one that is already used by **ONE UDP server**.
21
-
You will also have to create a connector with a reference of the created server. Then, you will have to reference the connector in the server with the `Server::setConnector(std::shared_ptr<Connector>)` method.
20
+
The `Server` has a construction method `create` that takes a port as parameter.
22
21
```cpp
23
-
SessionStore sessionStoreServer; // Session store of the server (store any client connection)
24
-
// Then, you can create the server
25
-
// Simply pass the session store, the mapping and the port you want the server to use
26
-
22
+
//create the safeties map
27
23
std::map<OpId, bool> safetiesMapping = {
28
24
{ SampleDto::opId, true }, // SampleDto is an important packet, it will be resent if we do not receive a confirmation of its reception. It will also send an ACK packet if the server receives a packet with this OpId
29
25
{ AnotherDto::opId, false } // This packet is not important, it will be sent once
30
26
};
31
27
32
-
Server server(4242, safetiesMapping, sessionStoreServer);
33
-
// Then, you have to create a connector
34
-
auto connector = std::make_shared<Connector>(server);
35
-
// And finally, you have to reference the connector in the server
36
-
server.setConnector(connector);
28
+
// Then create the server that will listen on port 4242
29
+
std::unique_ptr<Server> server = Server::create(4242, safetiesMapping);
37
30
```
38
31
It is recommended to put a public static variable in all your DTOs to always have their associated opId. This is why ```SampleDto::opId``` is used in the snippet.
🎉 Congratulations, you have created your server and registered your first callback !
51
44
52
45
### 3) Start the server
53
-
Now you will have to start it in order to accept incoming connections. To do so, call the `Connector::start()` method.
54
-
> __Warning__
55
-
> Event if there is a `Server::start()` method, you have to call the `Connector::start()` method. This is because the server is not the one that will handle the incoming connections, it is the connector. The server will only handle the packets received by the connector.
56
-
46
+
Now you will have to start it in order to accept incoming connections.
57
47
```c++
58
-
connector->start();
48
+
server->start();
59
49
```
60
50
61
51
### 4) Send packets
62
52
You can now send packets to your clients. To do so, you will have to get the `SessionId` of the client you want to send a packet to. You can find it in received packet callbacks in the header.
The `Client`have a constructor that takes a host string, a port and a `std::map<OpId, bool>` as parameters . You have to pass the address and the port of a running (or soon) server.
75
-
The safety mapping should be the same as the one used by the server. You will also have to create a connector with a reference of the created client. Then, you will have to reference the connector in the client with the `Client::setConnector(std::shared_ptr<Connector>)` method.
64
+
The `Client`has a construction method that takes a host string, a port and a `std::map<OpId, bool>` as parameters . You have to pass the address and the port of a running (or soon) server.
65
+
The safety mapping should be the same as the one used by the server. You will also have to create a connector with a reference of the created client.
76
66
```cpp
77
67
std::map<OpId, bool> safetiesMapping = {
78
68
{ SampleDto::opId, true }, // SampleDto is an important packet, it will be resent if we do not receive a confirmation of its reception. It will also send an ACK packet if the client receives a packet with this OpId
79
69
{ AnotherDto::opId, false } // This packet is not important, it will be sent once
You can now register callbacks to handle received packet. Here is an example of registering a callback for a packet with a payload of type `SampleDto` :
You can now call the `Client::connect()` to initiate the connection with the server. You pass a callback which will be called when the server has accepted/refused the connection.
0 commit comments