/// <summary> /// Called when a client connects to the match. /// </summary> /// <param name="conn"></param> public override void OnClientConnect(NetworkConnection conn) { base.OnClientConnect(conn); ConnectionID = conn.connectionId; ClientConnectionChanged?.Invoke(this, ConnectionStatus.Connected); ClientScene.AddPlayer(0); }
/// <summary> /// Called when a client disconnects from a match. /// </summary> /// <param name="conn"></param> public override void OnClientDisconnect(NetworkConnection conn) { if (!NetworkMultiplayerUI.Instance.Visible) { SceneManager.LoadScene(SceneManager.GetActiveScene().name); ClientConnectionChanged?.Invoke(this, ConnectionStatus.Disconnected); } else { ClientConnectionChanged?.Invoke(this, ConnectionStatus.Disconnected); } }
/// <summary> Event triggered when WatchdogManager detects a change in connection status. </summary> /// <param name="Sender"> If triggered internally by WatchdogManager, this will be "Watchdog Timer" </param> /// <param name="Args"> A ConnectionStatusChanged object containing the new status of the Client. </param> private static void ConnectionChange(object Sender, ConnectionStatusChanged Args) { // Whether or not the connection has changed state (this should usually be true, unless connecting for the first time) bool ChangedState = Args.StatusConnected ^ IsConnected; // Set the connection state of Client IsConnected = Args.StatusConnected; // Invoke a ClientConnectionChanged event (for end-user connection subscribers) if (ChangedState) { ClientConnectionChanged?.Invoke(Sender, Args); } // Outut the state if the state has changed from false to true if (ChangedState && IsConnected) { ConnectionAliveOutput(true); } // If the threads are still alive, then join the current thread if (RetryStartup != null && RetryStartup.IsAlive) { RetryStartup.Join(); } if (RetryConnection != null && RetryConnection.IsAlive) { RetryConnection.Join(); } // Determine if we are connecting or reconnecting if (!IsConnected && ThreadsStarted) { // Output the current connection status (disconnected) to the console ConnectionAliveOutput(false); // If the thread is alive, then join the current connection thread if (RetryConnection.IsAlive) { RetryConnection.Join(); } // Get a new thread from the factory and start retrying the connection // Using a factory because you cannot restart a thread RetryConnection = RetryConnectionThreadFactory(); RetryConnection.Start(); } else if (!IsConnected && !ThreadsStarted) { // Get a new thread from the thread factory RetryStartup = RetryStartupThreadFactory(); // Start the retry thread RetryStartup.Start(); // Output the current connectionstatus (disconnected) to the console ConnectionAliveOutput(false); } }
/// <summary> /// Called when a client disconnects from a match. /// </summary> /// <param name="conn"></param> public override void OnClientDisconnect(NetworkConnection conn) { base.OnClientDisconnect(conn); if (!NetworkMultiplayerUI.Instance.Visible) { SceneManager.LoadScene(SceneManager.GetActiveScene().name); UserMessageManager.Dispatch("Lost connection to the match!", 8f); } else { ClientConnectionChanged?.Invoke(this, ConnectionStatus.Disconnected); } }
private async void receiveMessages(Socket client) { await Task.Run(() => { try { while (_running) { if (client.Connected) { byte[] buffer = new byte[1 << 20]; client.Receive(buffer); string msg = _encoder.GetString(buffer).Replace('\0', ' ').Trim(); if (msg != string.Empty && MessageReceived != null) { MessageReceived( $"{(client.RemoteEndPoint as IPEndPoint).Address}", (client.RemoteEndPoint as IPEndPoint).Port, msg ); } } else { ClientConnectionChanged?.Invoke( $"{(client.RemoteEndPoint as IPEndPoint).Address}", (client.RemoteEndPoint as IPEndPoint).Port, false ); break; } } } catch (SocketException) { ClientConnectionChanged?.Invoke( $"{(client.RemoteEndPoint as IPEndPoint).Address}", (client.RemoteEndPoint as IPEndPoint).Port, false ); } catch (Exception e) { Error?.Invoke(e); Dispose(); } }); }
private async void acceptNewClients() { try { while (_running) { Socket newClient = await _server.AcceptAsync(); _clients.Add(newClient); receiveMessages(newClient); ClientConnectionChanged?.Invoke( $"{(newClient.RemoteEndPoint as IPEndPoint).Address}", (newClient.RemoteEndPoint as IPEndPoint).Port, true ); } } catch (Exception e) { Error?.Invoke(e); Dispose(); } }
private void OnClientConnectionChanged(ClientConnectionChangedEventArgs e) { ClientConnectionChanged?.Invoke(this, e); }
private void OnClientConnectionChanged(int connections) { ClientConnectionChanged?.Invoke(this, new ClientConnectionChangedEventArgs(connections)); }