/// <summary>
        /// Opens the new TCP connection and applies the already registered packet handlers.
        /// </summary>
        private async Task <bool> OpenNewTCPConnection()
        {
            Tuple <TcpConnection, ConnectionResult> result = await ConnectionFactory.CreateTcpConnectionAsync(IPAddress, Port);

            if (result.Item2 != ConnectionResult.Connected)
            {
                Reconnect(); return(false);
            }
            tcpConnection = result.Item1;
            tcpPacketHandlerBuffer.ForEach(t => tcpConnection.RegisterPacketHandler(t.Item1, t.Item2));
            tcpConnection.ConnectionClosed += (c, cc) => { Reconnect(); connectionLost?.Invoke(tcpConnection, ConnectionType.TCP, c); };
            sendSlowBuffer.ForEach(tcpConnection.Send);
            connectionEstablished?.Invoke(tcpConnection, ConnectionType.TCP);
            return(true);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a new TcpConnection.
 /// </summary>
 /// <returns>A TcpConnection.</returns>
 protected virtual async Task <Tuple <TcpConnection, ConnectionResult> > CreateTcpConnection() => await ConnectionFactory.CreateTcpConnectionAsync(IPAddress, Port);
        /// <summary>
        /// Opens the new TCP connection and applies the already registered packet handlers.
        /// </summary>
        private async Task OpenNewTCPConnection()
        {
            Tuple <TcpConnection, ConnectionResult> result = await ConnectionFactory.CreateTcpConnectionAsync(IPAddress, Port);

            if (result.Item2 != ConnectionResult.Connected)
            {
                Reconnect(); return;
            }
            tcpConnection = result.Item1;

            //Restore old state by adding old packets
            tcpConnection.RestorePacketHandler(tcpPacketHandlerBackup);
            //Restore new state by adding packets the user wanted to register while the connection was dead.
            tcpPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo registerPacketHandler = typeof(Connection).GetMethod("RegisterPacketHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                registerPacketHandler            = registerPacketHandler.MakeGenericMethod(t.Item1);
                registerPacketHandler.Invoke(tcpConnection, new object[] { t.Item2, t.Item3 });
            });
            tcpStaticPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo registerPacketHandler = typeof(Connection).GetMethod("RegisterStaticPacketHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                registerPacketHandler            = registerPacketHandler.MakeGenericMethod(t.Item1);
                registerPacketHandler.Invoke(tcpConnection, new object[] { t.Item2 });
            });
            tcpConnection.ConnectionClosed += (c, cc) =>
            {
                tcpPacketHandlerBackup = cc.ObjectMapper;
                connectionLost?.Invoke(tcpConnection, ConnectionType.TCP, c);
                Reconnect();
            };
            sendSlowBuffer.ForEach(tcpConnection.Send);
            sendSlowObjectBuffer.ForEach(p => tcpConnection.Send(p.Item1, p.Item2));
            //Restore new state by removing the packets the user wanted to unregister while the connection was dead.
            tcpUnPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo unRegisterPacketHandler = typeof(Connection).GetMethod("UnRegisterPacketHandler");
                unRegisterPacketHandler            = unRegisterPacketHandler.MakeGenericMethod(t.Item1);
                unRegisterPacketHandler.Invoke(tcpConnection, new object[] { t.Item2 });
            });
            tcpStaticUnPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo unRegisterPacketHandler = typeof(Connection).GetMethod("UnRegisterStaticPacketHandler");
                unRegisterPacketHandler            = unRegisterPacketHandler.MakeGenericMethod(t);
                unRegisterPacketHandler.Invoke(tcpConnection, null);
            });

            KnownTypes.ForEach(TcpConnection.AddExternalPackets);
            //Clear the buffers since we added and removed the packet types.
            sendSlowBuffer.Clear();
            sendSlowObjectBuffer.Clear();
            tcpPacketHandlerBuffer.Clear();
            tcpUnPacketHandlerBuffer.Clear();
            tcpStaticPacketHandlerBuffer.Clear();
            tcpStaticUnPacketHandlerBuffer.Clear();

            if (!tcpConnection.IsAlive)
            {
                return;                         //Connection could already be dead because of the prePackets.
            }
            connectionEstablished?.Invoke(tcpConnection, ConnectionType.TCP);
            tcpConnection.UnlockRemoteConnection();
        }