示例#1
0
 /// <summary>
 /// Sends the given packet over the UDP connection.
 /// The server wont be able to send an answer, since no instance object is given.
 /// </summary>
 /// <param name="packet">The packet to send.</param>
 public void SendFast(Packet packet)
 {
     if (udpConnection == null || !udpConnection.IsAlive)
     {
         sendFastBuffer.Add(packet);
     }
     else
     {
         udpConnection.Send(packet);
     }
 }
        /// <summary>
        /// Opens the new UDP connection and applies the already registered packet handlers.
        /// </summary>
        private async Task <bool> OpenNewUDPConnection()
        {
            Tuple <UdpConnection, ConnectionResult> result = await ConnectionFactory.CreateUdpConnectionAsync(tcpConnection);

            if (result.Item2 != ConnectionResult.Connected)
            {
                Reconnect(); return(false);
            }
            udpConnection = result.Item1;
            udpPacketHandlerBuffer.ForEach(u => udpConnection.RegisterPacketHandler(u.Item1, u.Item2));
            udpConnection.ConnectionClosed += (c, cc) => { Reconnect(); connectionLost?.Invoke(udpConnection, ConnectionType.UDP, c); };
            sendFastBuffer.ForEach(s => udpConnection.Send(s));
            connectionEstablished?.Invoke(udpConnection, ConnectionType.UDP);
            return(true);
        }
 /// <summary>
 /// Opens the new UDP connection and applies the already registered packet handlers.
 /// </summary>
 private async Task<bool> OpenNewUDPConnection()
 {
     Tuple<UdpConnection, ConnectionResult> result = await ConnectionFactory.CreateUdpConnectionAsync(tcpConnection);
     if (result.Item2 != ConnectionResult.Connected) { Reconnect(); return false; }
     udpConnection = result.Item1;
     udpPacketHandlerBuffer.ForEach(u => udpConnection.RegisterPacketHandler(u.Item1, u.Item2));
     udpConnection.ConnectionClosed += (c, cc) => { Reconnect(); connectionLost?.Invoke(udpConnection, ConnectionType.UDP, c); };
     sendFastBuffer.ForEach(s => udpConnection.Send(s));
     connectionEstablished?.Invoke(udpConnection, ConnectionType.UDP);
     return true;
 }
示例#4
0
        /// <summary>
        /// Opens the new UDP connection and applies the already registered packet handlers.
        /// </summary>
        private async Task OpenNewUDPConnection()
        {
            Tuple <UdpConnection, ConnectionResult> result = await CreateUdpConnection();

            if (result.Item2 != ConnectionResult.Connected)
            {
                Reconnect(); return;
            }
            udpConnection = result.Item1;
            //Restore old state by adding old packets
            udpConnection.RestorePacketHandler(udpPacketHandlerBackup);
            //Restore new state by adding packets the user wanted to register while the connection was dead.
            udpPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo registerPacketHandler = typeof(Connection).GetMethod("RegisterPacketHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                registerPacketHandler            = registerPacketHandler.MakeGenericMethod(t.Item1);
                registerPacketHandler.Invoke(udpConnection, new object[2] {
                    t.Item2, t.Item3
                });
            });
            udpStaticPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo registerPacketHandler = typeof(Connection).GetMethod("RegisterStaticPacketHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                registerPacketHandler            = registerPacketHandler.MakeGenericMethod(t.Item1);
                registerPacketHandler.Invoke(udpConnection, new object[] { t.Item2 });
            });
            udpConnection.ConnectionClosed += (c, cc) =>
            {
                udpPacketHandlerBackup = cc.ObjectMapper;
                connectionLost?.Invoke(udpConnection, ConnectionType.UDP, c);
                Reconnect();
            };
            sendFastBuffer.ForEach(udpConnection.Send);
            sendFastObjectBuffer.ForEach(p => udpConnection.Send(p.Item1, p.Item2));
            //Restore new state by removing the packets the user wanted to unregister while the connection was dead.
            udpUnPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo unRegisterPacketHandler = typeof(Connection).GetMethod("UnRegisterPacketHandler");
                unRegisterPacketHandler            = unRegisterPacketHandler.MakeGenericMethod(t.Item1);
                unRegisterPacketHandler.Invoke(udpConnection, new object[] { t.Item2 });
            });
            udpStaticUnPacketHandlerBuffer.ForEach(t =>
            {
                MethodInfo unRegisterPacketHandler = typeof(Connection).GetMethod("UnRegisterStaticPacketHandler");
                unRegisterPacketHandler            = unRegisterPacketHandler.MakeGenericMethod(t);
                unRegisterPacketHandler.Invoke(udpConnection, null);
            });

            KnownTypes.ForEach(UdpConnection.AddExternalPackets);
            //Clear the buffers since we added and removed the packet types.
            sendFastBuffer.Clear();
            sendFastObjectBuffer.Clear();
            udpPacketHandlerBuffer.Clear();
            udpUnPacketHandlerBuffer.Clear();
            udpStaticPacketHandlerBuffer.Clear();
            udpStaticUnPacketHandlerBuffer.Clear();

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