[TCP/UDP] ClientConnectionContainer
The ClientConnectionContainer contains a TCP and a UDP connection by default. In addition the ClientConnectionContainer supports:
- Reconnecting if the TCP and/or the UDP connection has been lost
- Offering various methods to send data (async, fast, slow)
- Buffering packets if there is no available connection
- Remember your settings and packet-configurations after a reconnect
public async void Demo() { //1. Create a new client connection container. ClientConnectionContainer clientConnectionContainer = ConnectionFactory.CreateClientConnectionContainer("127.0.0.1", 1234); //2. Setup events which will be fired if we receive a connection clientConnectionContainer.ConnectionEstablished += ClientConnectionContainer_ConnectionEstablished; clientConnectionContainer.ConnectionLost += ClientConnectionContainer_ConnectionLost; } private void ClientConnectionContainer_ConnectionLost(Connection connection, Network.Enums.ConnectionType connectionType, Network.Enums.CloseReason closeReason) { Console.WriteLine($"Connection {connection.IPRemoteEndPoint} {connectionType} lost. {closeReason}"); } private void ClientConnectionContainer_ConnectionEstablished(Connection connection, Network.Enums.ConnectionType connectionType) { Console.WriteLine($"{connectionType} Connection received {connection.IPRemoteEndPoint}."); }
Is the ClientConnectionContainer too heavy? Or don’t you need a UDPConnection? Check out the following methods to establish TCP/UDP connections without the container.
[TCP] Method 1
ConnectionResult connectionResult = ConnectionResult.Timeout; //1. Create a TcpConnection TcpConnection tcpConnection = ConnectionFactory.CreateTcpConnection("127.0.0.1", 1234, out connectionResult); if (connectionResult != ConnectionResult.Connected) return;
connectionResult contains the information about the current connection state.
[TCP] Method 2 (async)
//1. Create a TcpConnection var connectionWithResult = await ConnectionFactory.CreateTcpConnectionAsync("127.0.0.1", 1234); if (connectionWithResult.Item2 != ConnectionResult.Connected) return; //2. The TCP connection is in the first item of the tuple. TcpConnection tcpConnection = connectionWithResult.Item1;
[UDP] Method 1
ConnectionResult connectionResult = ConnectionResult.Timeout; //1. Create a UDP connection. A UDP connection requires an alive TCP connection. UdpConnection udpConnection = ConnectionFactory.CreateUdpConnection(tcpConnection, out connectionResult); if (connectionResult != ConnectionResult.Connected) return;
[UDP] Method 2 (async)
//1. Create a UDP connection. A UDP connection requires an alive TCP connection. var connectionWithResult = await ConnectionFactory.CreateUdpConnectionAsync(tcpConnection); if (connectionWithResult.Item2 != ConnectionResult.Connected) return; //2. The UDP connection is in the first item of the tuple. UdpConnection udpConnection = connectionWithResult.Item1;