public void Test() { byte[] encryptionKey = new byte[16]; Random.NextBytes(encryptionKey); Console.WriteLine("Starting server"); DoubleServerHandler serverHandler = new DoubleServerHandler(); DoubleServer server = new DoubleServer(serverHandler, 1, 1, Port); serverHandler.Server = server; Console.WriteLine("Starting client"); DoubleClientHandler clientHandler = new DoubleClientHandler(); DoubleClient client = new DoubleClient(clientHandler, encryptionKey, encryptionKey, Ip, Port); clientHandler.Client = client; client.Start(); lock (client) { clientHandler.MaySend = true; Monitor.Pulse(client); Console.WriteLine("Main thread waiting"); Monitor.Wait(client); } Console.WriteLine("Closing client"); client.Close(); Console.WriteLine("Closing server"); server.Close(); }
/// <summary> /// Initializes the networking and starts to connect. /// </summary> public static void Start(IPAddress ip, OnConnected onConnected) { Assert.IsNull(_client, "The NetworkClient is already initialized."); LocalId = 0; _handler = new DoubleClientHandler(onConnected); byte[] encryptionKey = new byte[16]; byte[] authenticationData = encryptionKey; _client = new DoubleClient(_handler, encryptionKey, authenticationData, ip, NetworkUtils.Port); _client.Start(); }
/// <summary> /// Deinitializes the networking, disconnecting from the server. /// Always called internally when the connection fails or this client gets disconnected, /// so should only be to make this client disconnect. /// </summary> public static void Stop() { Assert.IsNotNull(_client, "The NetworkClient is not initialized."); UdpPayload = null; _tickingThread?.Stop(); _tickingThread = null; NetworkUtils.GlobalBaseTimestamp = -1; UdpNetDelay = 0; _resetPacketTimestamp = false; DisconnectHandler = null; UdpHandler = null; Array.Clear(TcpHandlers, 0, TcpHandlers.Length); _handler = null; _client.Close(); _client = null; }
private void OnConnectButtonClick(object sender, RoutedEventArgs e) { if (!IPAddress.TryParse(AddressInput.Text, out IPAddress ip)) { StatusText.Text = "Invalid IP"; return; } if (!Regex.IsMatch(PortInput.Text, "[0-9]+") || !int.TryParse(PortInput.Text, out int port)) { StatusText.Text = "Invalid port"; return; } ConnectButton.IsEnabled = false; StatusText.Text = "Connecting..."; byte[] encryptionKey = new byte[16]; //This is obviously not how it should be done _client = new DoubleClient(this, encryptionKey, encryptionKey, ip, port); _client.Start(); }