Пример #1
0
        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;
            }
        }
Пример #2
0
 private void InternalDisconnect(int connId, HSteamNetConnection socket)
 {
     OnDisconnected.Invoke(connId);
     SteamNetworkingSockets.CloseConnection(socket, 0, "Graceful disconnect", false);
     connToMirrorID.Remove(connId);
     steamIDToMirrorID.Remove(connId);
     Debug.Log($"Client with ConnectionID {connId} disconnected.");
 }
Пример #3
0
 private void InternalDisconnect(int connId, Connection socket)
 {
     OnDisconnected.Invoke(connId);
     socket.Close(false, 0, "Graceful disconnect");
     connToMirrorID.Remove(connId);
     steamIDToMirrorID.Remove(connId);
     Debug.Log($"Client with SteamID {connId} disconnected.");
 }
Пример #4
0
        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()}");
            }
        }
Пример #5
0
        protected override void OnReceiveInternalData(InternalMessages type, SteamId 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.Contains(clientSteamID))
                {
                    OnDisconnected?.Invoke(steamToMirrorIds[clientSteamID]);
                    steamToMirrorIds.Remove(clientSteamID);
                    CloseP2PSessionWithUser(clientSteamID);
                    Debug.Log($"Client with SteamID {clientSteamID} disconnected.");
                }
                else
                {
                    OnReceivedError?.Invoke(-1, new Exception("ERROR Unknown SteamID"));
                }

                break;

            default:
                Debug.Log("Received unknown message type");
                break;
            }
        }