private void OnConnectionStatusChanged(SteamNetConnectionStatusChangedCallback_t param) { ulong clientSteamID = param.m_info.m_identityRemote.GetSteamID64(); if (param.m_info.m_eState == ESteamNetworkingConnectionState.k_ESteamNetworkingConnectionState_Connecting) { if (connToMirrorID.Count >= maxConnections) { Debug.Log($"Incoming connection {clientSteamID} would exceed max connection count. Rejecting."); SteamNetworkingSockets.CloseConnection(param.m_hConn, 0, "Max Connection Count", false); return; } EResult res; if ((res = SteamNetworkingSockets.AcceptConnection(param.m_hConn)) == EResult.k_EResultOK) { Debug.Log($"Accepting connection {clientSteamID}"); } else { Debug.Log($"Connection {clientSteamID} could not be accepted: {res.ToString()}"); } } else if (param.m_info.m_eState == ESteamNetworkingConnectionState.k_ESteamNetworkingConnectionState_Connected) { int connectionId = nextConnectionID++; connToMirrorID.Add(param.m_hConn, connectionId); steamIDToMirrorID.Add(param.m_info.m_identityRemote.GetSteamID(), connectionId); OnConnected.Invoke(connectionId); Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); } else if (param.m_info.m_eState == ESteamNetworkingConnectionState.k_ESteamNetworkingConnectionState_ClosedByPeer) { if (connToMirrorID.TryGetValue(param.m_hConn, out int connId)) { OnDisconnected.Invoke(connId); SteamNetworkingSockets.CloseConnection(param.m_hConn, 0, "Graceful disconnect", false); connToMirrorID.Remove(connId); steamIDToMirrorID.Remove(connId); Debug.Log($"Client with SteamID {clientSteamID} disconnected."); } } else { Debug.Log($"Connection {clientSteamID} state changed: {param.m_info.m_eState.ToString()}"); } }
private void OnConnectionStatusChanged(Connection conn, ConnectionInfo info) { ulong clientSteamID = info.Identity.SteamId; if (info.State == ConnectionState.Connecting) { if (connToMirrorID.Count >= maxConnections) { Debug.Log($"Incoming connection {clientSteamID} would exceed max connection count. Rejecting."); conn.Close(false, 0, "Max Connection Count"); return; } Result res; if ((res = conn.Accept()) == Result.OK) { Debug.Log($"Accepting connection {clientSteamID}"); } else { Debug.Log($"Connection {clientSteamID} could not be accepted: {res.ToString()}"); } } else if (info.State == ConnectionState.Connected) { int connectionId = nextConnectionID++; connToMirrorID.Add(conn, connectionId); steamIDToMirrorID.Add(clientSteamID, connectionId); OnConnected.Invoke(connectionId); Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); } else if (info.State == ConnectionState.ClosedByPeer) { if (connToMirrorID.TryGetValue(conn, out int connId)) { InternalDisconnect(connId, conn); } } else { Debug.Log($"Connection {clientSteamID} state changed: {info.State.ToString()}"); } }
protected override void OnReceiveInternalData(InternalMessages type, CSteamID clientSteamID) { switch (type) { case InternalMessages.CONNECT: if (steamToMirrorIds.Count >= maxConnections) { SendInternal(clientSteamID, InternalMessages.DISCONNECT); return; } SendInternal(clientSteamID, InternalMessages.ACCEPT_CONNECT); int connectionId = nextConnectionID++; steamToMirrorIds.Add(clientSteamID, connectionId); OnConnected.Invoke(connectionId); Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); break; case InternalMessages.DISCONNECT: if (steamToMirrorIds.TryGetValue(clientSteamID, out int connId)) { OnDisconnected.Invoke(connId); CloseP2PSessionWithUser(clientSteamID); steamToMirrorIds.Remove(clientSteamID); Debug.Log($"Client with SteamID {clientSteamID} disconnected."); } break; default: Debug.Log("Received unknown message type"); break; } }
public string ServerGetClientAddress(int connectionId) { if (steamIDToMirrorID.TryGetValue(connectionId, out CSteamID steamId)) { return(steamId.ToString()); } else { Debug.LogError("Trying to get info on unknown connection: " + connectionId); OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection")); return(string.Empty); } }