/// <summary> /// Registers a tcp packetHandler. This handler will be invoked if this connection /// receives the given type. /// </summary> /// <param name="packetType">Type of the packet we would like to receive.</param> /// <param name="handler">The handler which should be invoked.</param> public void TCP_RegisterPacketHandler(Type packetType, PacketReceivedHandler handler) { if (tcpConnection != null && tcpConnection.IsAlive) { tcpConnection.RegisterPacketHandler(packetType, handler); } tcpPacketHandlerBuffer.Add(new Tuple <Type, PacketReceivedHandler>(packetType, handler)); }
/// <summary> /// Registers a packetHandler for TCP. This handler will be invoked if this connection /// receives the given type. /// </summary> /// <typeparam name="T">The type we would like to receive.</typeparam> /// <param name="handler">The handler which should be invoked.</param> /// <param name="obj">The object which wants to receive the packet.</param> /// <exception cref="System.NotImplementedException"></exception> public void TCP_RegisterPacketHandler <T>(PacketReceivedHandler <T> handler, object obj) where T : Packet { if (IsAlive_TCP) { tcpConnection.RegisterPacketHandler <T>(handler, obj); } else { tcpPacketHandlerBuffer.Add(new Tuple <Type, Delegate, object>(typeof(T), handler, obj)); } }
/// <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); }
/// <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; }