Client Setup

[TCP/UDP] ClientConnectionContainer

The ClientConnectionContainer contains a TCP and a UDP connection by default. In addition the ClientConnectionContainer supports:

  1. Reconnecting if the TCP and/or the UDP connection has been lost
  2. Offering various methods to send data (async, fast, slow)
  3. Buffering packets if there is no available connection
  4. 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;

 

Hinterlasse einen Kommentar

Please Login to comment
  Subscribe  
Notify of