Version 1 vs 2
Version 1 vs 2
Edits
Edits
- Edit by 0xseantasker, Version 2
- Jun 5 2024 10:37 PM
- Edit by 0xseantasker, Version 1
- Jun 5 2024 10:05 PM
Original Change | Most Recent Change |
Edit Older Version 1... | Edit Current Version 2... |
Content Changes
Content Changes
Echo's networking abstracts the networking later and allows you to write code that is implementation agnostic.
=Quick Start=
If you're using the Application class then networking is managed for you. Just enable networking from you configuration.
```
network.enabled=true
```
After that you can use `Application::GetNetworkManager()` to get a reference to the NetworkManager and do the following operations.
```
lang=c++
```
Echo's networking abstracts the networking later and allows you to write code that is implementation agnostic.
=Quick Start=
If you're using the Application class then networking is managed for you. Just enable networking from you configuration.
```
network.enabled=true
```
After that you can use `Application::GetNetworkManager()` to get a reference to the `NetworkManager` and do the following operations.
Listen for a connection:
```
lang=c++
// You need a network listener
class NewConnectionHandler : public IncomingConnectionListener
{
public:
void IncomingConnection(shared_ptr<Connection> newConnection)
{
// Do something with newConnection
};
};
// This handler needs to outlive the listen request
NewConnectionHandler newConnectionHandler;
NetworkManager& network = GetNetworkManager();
network.Listen("direct:ANY:9876",&newConnectionHandler);
```
Connect to an endpoint
```
lang=c++
NetworkManager& network = GetNetworkManager();
shared_ptr<Connection> connection = network.Connect("direct:127.0.0.1:9876",[](shared_ptr<Connection>)
{
// Optional connect callback
});
```
Send data using a Connection
```
lang=c++
...
const u32 HELLO_WORLD_PACKET_ID = 100;
const u32 SOME_DATA_PACKET_ID = 101;
// Send a string
connection->SendMessage("Hello World",HELLO_WORLD_PACKET_ID);
// Send a data buffer
connection->SendData(buffer,bufferSize,SOME_DATA_PACKET_ID);
...
```
Receive some data using a Connection
```
lang=c++
...
const u32 HELLO_WORLD_PACKET_ID = 100;
const u32 SOME_DATA_PACKET_ID = 101;
connection->RegisterPacketCallback(HELLO_WORLD_PACKET_ID,[](shared_ptr<Connection>, shared_ptr<DataPacket> packet)
{
// This could be a bind instead of a lambda
std::string message = packet->GetString(0);
});
// Send a data buffer
connection->RegisterPacketCallbnack(SOME_DATA_PACKET_ID,[](shared_ptr<Connection>, shared_ptr<DataPacket> packet)
{
// Do something with the data
// packet->GetData()
// packet->GetDataSize()
});
...
```
Send data and setup a response handler
```
lang=c++
const u32 PING_PACKET_ID = 102;
// Sender
connection->SendMessage("Ping",PING_PACKET_ID,(shared_ptr<Connection>, shared_ptr<DataPacket> packet)
{
ECHO_LOG_INFO("Response was: " << packet->GetString(0));
});
// The receiver would have
connection->RegisterPacketCallbnack(PING_PACKET_ID,[](shared_ptr<Connection> c, shared_ptr<DataPacket> packet)
{
// Lets build a response manually. The connected factory via connection might be give us a new packet from the pool. You can also use new.
shared_ptr<DataPacket> response = c->NewDataPacket();
// Packet ID does not matter for a response packet since they aren't queued for dispatch via the packet id lookup.
response->Configure(0,.{"Pong"});
c->SendResponsePacket(response,packet);
});
```
=Not-quite-as-Quick Start=
You can manage a `NetworkManager` manually
```
lang=c++
NetworkManager network;
network.InstallSystem(make_shared<SocketNetworkSystem>());
// You need to update the network manager to have the callbacks fire (connect, disconnect and received packet)
// Updating may be required to send and receive data for some systems. Other systems will spawn their own threads to ensure data isn't dropped.
// If you have a kernel available
kernel.AddTask(network);
// Or if you are going to update manually
network.UpdateTasks(Seconds(0));
```
=Other features=
- Auto reconnect can be toggled by calling `SetAutoReconnect()` time can also be adjusted
- There is a TypeScript implementation of Connection
- You can use Websockets by specifying the `WebSocket` system in your connection string `(WebSocket)direct:destination:port`
- There is a Java version of Connection and packet protocol. This hasn't been updated for a long time but the protocol hasn't changed.
- The protocol has been implemented in Python before but it hasn't been released yet.
=Advanced=
As with a lot of Echo's features Networking can be used some what independently from other systems. There is a `NetworKExecutionModel` that may be handy if you're only using Echo for networking and happy to use a `Kernel`.
- Implementing your own NetworkSystem
- Encryption (TLS/SSL)
-- Needs to be supported by the network system.
-- For `ASIONetworkSystem` see `NetworkTests.cpp` to see connection string parameters to add to enable encryption. Needs ca and a cert root.
Echo's networking abstracts the networking later and allows you to write code that is implementation agnostic.
=Quick Start=
If you're using the Application class then networking is managed for you. Just enable networking from you configuration.
```
network.enabled=true
```
After that you can use `Application::GetNetworkManager()` to get a reference to the `NetworkManager` and do the following operations.
Listen for a connection:
```
lang=c++
// You need a network listener
class NewConnectionHandler : public IncomingConnectionListener
{
public:
void IncomingConnection(shared_ptr<Connection> newConnection)
{
// Do something with newConnection
};
};
// This handler needs to outlive the listen request
NewConnectionHandler newConnectionHandler;
NetworkManager& network = GetNetworkManager();
network.Listen("direct:ANY:9876",&newConnectionHandler);
```
Connect to an endpoint
```
lang=c++
NetworkManager& network = GetNetworkManager();
shared_ptr<Connection> connection = network.Connect("direct:127.0.0.1:9876",[](shared_ptr<Connection>)
{
// Optional connect callback
});
```
Send data using a Connection
```
lang=c++
...
const u32 HELLO_WORLD_PACKET_ID = 100;
const u32 SOME_DATA_PACKET_ID = 101;
// Send a string
connection->SendMessage("Hello World",HELLO_WORLD_PACKET_ID);
// Send a data buffer
connection->SendData(buffer,bufferSize,SOME_DATA_PACKET_ID);
...
```
Receive some data using a Connection
```
lang=c++
...
const u32 HELLO_WORLD_PACKET_ID = 100;
const u32 SOME_DATA_PACKET_ID = 101;
connection->RegisterPacketCallback(HELLO_WORLD_PACKET_ID,[](shared_ptr<Connection>, shared_ptr<DataPacket> packet)
{
// This could be a bind instead of a lambda
std::string message = packet->GetString(0);
});
// Send a data buffer
connection->RegisterPacketCallbnack(SOME_DATA_PACKET_ID,[](shared_ptr<Connection>, shared_ptr<DataPacket> packet)
{
// Do something with the data
// packet->GetData()
// packet->GetDataSize()
});
...
```
Send data and setup a response handler
```
lang=c++
const u32 PING_PACKET_ID = 102;
// Sender
connection->SendMessage("Ping",PING_PACKET_ID,(shared_ptr<Connection>, shared_ptr<DataPacket> packet)
{
ECHO_LOG_INFO("Response was: " << packet->GetString(0));
});
// The receiver would have
connection->RegisterPacketCallbnack(PING_PACKET_ID,[](shared_ptr<Connection> c, shared_ptr<DataPacket> packet)
{
// Lets build a response manually. The connected factory via connection might be give us a new packet from the pool. You can also use new.
shared_ptr<DataPacket> response = c->NewDataPacket();
// Packet ID does not matter for a response packet since they aren't queued for dispatch via the packet id lookup.
response->Configure(0,.{"Pong"});
c->SendResponsePacket(response,packet);
});
```
=Not-quite-as-Quick Start=
You can manage a `NetworkManager` manually
```
lang=c++
NetworkManager network;
network.InstallSystem(make_shared<SocketNetworkSystem>());
// You need to update the network manager to have the callbacks fire (connect, disconnect and received packet)
// Updating may be required to send and receive data for some systems. Other systems will spawn their own threads to ensure data isn't dropped.
// If you have a kernel available
kernel.AddTask(network);
// Or if you are going to update manually
network.UpdateTasks(Seconds(0));
```
=Other features=
- Auto reconnect can be toggled by calling `SetAutoReconnect()` time can also be adjusted
- There is a TypeScript implementation of Connection
- You can use Websockets by specifying the `WebSocket` system in your connection string `(WebSocket)direct:destination:port`
- There is a Java version of Connection and packet protocol. This hasn't been updated for a long time but the protocol hasn't changed.
- The protocol has been implemented in Python before but it hasn't been released yet.
=Advanced=
As with a lot of Echo's features Networking can be used some what independently from other systems. There is a `NetworKExecutionModel` that may be handy if you're only using Echo for networking and happy to use a `Kernel`.
- Implementing your own NetworkSystem
- Encryption (TLS/SSL)
-- Needs to be supported by the network system.
-- For `ASIONetworkSystem` see `NetworkTests.cpp` to see connection string parameters to add to enable encryption. Needs ca and a cert root.