private async Task PingSendingLoop()
        {
            sendPings = true;
            while (sendPings)
            {
                pingIdCounter = (byte)((pingIdCounter + 1) % 128);
                sentPings.Remove(pingIdCounter);
                sentPings.Add(pingIdCounter, new PingTracker());

                pingPongMessageBuffer[0] = pingIdCounter;

                if (isServer)
                {
                    foreach (User user in connectedUsers.Values)
                    {
#if UNITY_SERVER
                        SteamGameServerNetworking.SendP2PPacket(user.SteamId, pingPongMessageBuffer, (uint)pingPongMessageBuffer.Length, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Ping);
#else
                        SteamNetworking.SendP2PPacket(user.SteamId, pingPongMessageBuffer, (uint)pingPongMessageBuffer.Length, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Ping);
#endif
                    }
                }
                else
                {
#if UNITY_SERVER
                    SteamGameServerNetworking.SendP2PPacket(serverUser.SteamId, pingPongMessageBuffer, (uint)pingPongMessageBuffer.Length, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Ping);
#else
                    SteamNetworking.SendP2PPacket(serverUser.SteamId, pingPongMessageBuffer, (uint)pingPongMessageBuffer.Length, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Ping);
#endif
                }

                await Task.Delay(TimeSpan.FromSeconds(PingInterval));
            }
        }
        private void CloseP2PSessions()
        {
            foreach (User user in connectedUsers.Values)
            {
#if UNITY_SERVER
                SteamGameServerNetworking.CloseP2PSessionWithUser(user.SteamId);
#else
                SteamNetworking.CloseP2PSessionWithUser(user.SteamId);
#endif
            }

            if (serverUser != null)
            {
#if UNITY_SERVER
                SteamGameServerNetworking.CloseP2PSessionWithUser(serverUser.SteamId);
#else
                SteamNetworking.CloseP2PSessionWithUser(serverUser.SteamId);
#endif
            }

            connectedUsers.Clear();
            serverUser = null;
            if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                UnityEngine.Debug.Log(nameof(SteamNetworkingTransport) + " - CloseP2PSessions - has Closed P2P Sessions With all Users");
            }
        }
        public override bool StartClient()
        {
            serverUser = new User(new CSteamID(ConnectToSteamID));

#if UNITY_SERVER
            if (SteamGameServerNetworking.SendP2PPacket(serverUser.SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Connect))
#else
            if (SteamNetworking.SendP2PPacket(serverUser.SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Connect))
#endif
            {
                _p2PSessionConnectFailCallback = Callback <P2PSessionConnectFail_t> .Create((sessionConnectFailInfo) =>
                {
                    OnP2PSessionConnectFail(sessionConnectFailInfo);
                });
            }
            else
            {
                P2PSessionConnectFail_t sessionConnectFailInfo = new P2PSessionConnectFail_t()
                {
                    m_eP2PSessionError = (byte)EP2PSessionError.k_EP2PSessionErrorMax,
                    m_steamIDRemote    = serverUser.SteamId
                };

                OnP2PSessionConnectFail(sessionConnectFailInfo);

                return(false);
            }

            return(true);
        }
        public override void Send(ulong clientId, ArraySegment <byte> data, NetworkDelivery delivery)
        {
            EP2PSend sendType = NetworkDeliveryToEP2PSend(delivery);

            if (clientId == ServerClientId)
            {
#if UNITY_SERVER
                SteamGameServerNetworking.SendP2PPacket(serverUser.SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData);
#else
                SteamNetworking.SendP2PPacket(serverUser.SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData);
#endif
            }
            else
            {
                if (connectedUsers.ContainsKey(clientId))
                {
#if UNITY_SERVER
                    SteamGameServerNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData);
#else
                    SteamNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, data.Array, (uint)data.Count, sendType, (int)InternalChannelType.NetcodeData);
#endif
                }
                else
                {
                    if (NetworkManager.Singleton.LogLevel <= LogLevel.Error)
                    {
                        NetworkLog.LogErrorServer(nameof(SteamNetworkingTransport) + " - Can't Send to client, client not connected, clientId: " + clientId);
                    }
                }
            }
        }
Esempio n. 5
0
        public static string PlayerIP(CSteamID cSteamID)
        {
            P2PSessionState_t State;

            SteamGameServerNetworking.GetP2PSessionState(cSteamID, out State);
            return(Parser.getIPFromUInt32(State.m_nRemoteIP));
        }
Esempio n. 6
0
        private object IOnUserApprove(BoltConnection connection)
        {
            var id       = connection.RemoteEndPoint.SteamId.Id.ToString();
            var cSteamId = new CSteamID(connection.RemoteEndPoint.SteamId.Id);

            // Get IP address from Steam
            P2PSessionState_t sessionState;

            SteamGameServerNetworking.GetP2PSessionState(cSteamId, out sessionState);
            var remoteIp = sessionState.m_nRemoteIP;
            var ip       = string.Concat(remoteIp >> 24 & 255, ".", remoteIp >> 16 & 255, ".", remoteIp >> 8 & 255, ".", remoteIp & 255);

            // Call out and see if we should reject
            var canLogin = Interface.Call("CanClientLogin", connection) ?? Interface.Call("CanUserLogin", "Unnamed", id, ip);

            if (canLogin is string || (canLogin is bool && !(bool)canLogin))
            {
                var coopKickToken = new CoopKickToken
                {
                    KickMessage = canLogin is string?canLogin.ToString() : "Connection was rejected",   // TODO: Localization
                                      Banned = false
                };
                connection.Disconnect(coopKickToken);
                return(true);
            }

            return(Interface.Call("OnUserApprove", connection) ?? Interface.Call("OnUserApproved", "Unnamed", id, ip));
        }
Esempio n. 7
0
        private static string GetIP(UnturnedPlayer player)
        {
            try
            {
                uint mNRemoteIP;
                P2PSessionState_t p2PSessionStateT;
                CSteamID          pid = player.CSteamID;

                if (!SteamGameServerNetworking.GetP2PSessionState(pid, out p2PSessionStateT))
                {
                    mNRemoteIP = 0;
                }
                else
                {
                    mNRemoteIP = p2PSessionStateT.m_nRemoteIP;
                }

                return(Parser.getIPFromUInt32(mNRemoteIP));
            }
            catch (Exception e)
            {
                Rocket.Core.Logging.Logger.LogException(e);
                return(null);
            }
        }
Esempio n. 8
0
        private object IOnUserApprove(BoltConnection connection)
        {
            var id       = connection.RemoteEndPoint.SteamId.Id.ToString();
            var cSteamId = new CSteamID(connection.RemoteEndPoint.SteamId.Id);
            var name     = SteamFriends.GetFriendPersonaName(cSteamId);
            P2PSessionState_t sessionState;

            SteamGameServerNetworking.GetP2PSessionState(cSteamId, out sessionState);
            var remoteIp = sessionState.m_nRemoteIP;
            var ip       = string.Concat(remoteIp >> 24 & 255, ".", remoteIp >> 16 & 255, ".", remoteIp >> 8 & 255, ".", remoteIp & 255);

            // Call out and see if we should reject
            var canLogin = Interface.Call("CanClientLogin", connection) ?? Interface.Call("CanUserLogin", name, id, ip);

            if (canLogin is string)
            {
                var coopKickToken = new CoopKickToken {
                    KickMessage = canLogin.ToString(), Banned = false
                };
                connection.Disconnect(coopKickToken);
                return(true);
            }

            return(Interface.Call("OnUserApprove", connection) ?? Interface.Call("OnUserApproved", name, id, ip));
        }
Esempio n. 9
0
        public static string GetIP(Player plr)
        {
            P2PSessionState_t State;

            SteamGameServerNetworking.GetP2PSessionState(GetSteamPlayer(plr).playerID.steamID, out State);
            return(Parser.getIPFromUInt32(State.m_nRemoteIP));
        }
Esempio n. 10
0
        void OnClientBeginAuthentication(CSteamID clientID, byte[] pToken, int uTokenLen)
        {
            // If the clientdata is already in use, refuse anymore connections
            if (ClientData.bActive)
            {
                // CallbackMsg_t msg;

                // Tell the client that authentication failed or server is full
                // BSendDataToClient(clientID, &msg, sizeof(msg));
                MsgServerFailAuthentication msg = new MsgServerFailAuthentication();
                byte[] msg_array = Converter.ObjectToByteArray(msg);
                uint   size      = (uint)(msg_array.Length * sizeof(byte)); // Another way to calculate size, dont know if it works

                SteamGameServerNetworking.SendP2PPacket(clientID, msg_array, (uint)Marshal.SizeOf(msg_array), EP2PSend.k_EP2PSendReliable);
                return;
            }
            else
            {
                // ClientData[i].TickCountSinceLastData = Current Game Tick Time

                // Authenticate User With Steam Back-End Servers
                if (SteamGameServer.BeginAuthSession(pToken, uTokenLen, clientID) != EBeginAuthSessionResult.k_EBeginAuthSessionResultOK)
                {
                    MsgServerFailAuthentication msg = new MsgServerFailAuthentication();
                    byte[] msg_array = Converter.ObjectToByteArray(msg);
                    uint   size      = (uint)(msg_array.Length * sizeof(byte)); // Another way to calculate size, dont know if it works

                    SteamGameServerNetworking.SendP2PPacket(clientID, msg_array, (uint)Marshal.SizeOf(msg_array), EP2PSend.k_EP2PSendReliable);
                }

                ClientData.SteamIDUser = clientID;
                ClientData.bActive     = true;
            }
        }
        public override void ServerStart()
        {
            if (!SteamManager.Initialized)
            {
                Debug.LogError("SteamWorks not initialized. Server could not be started.");
                return;
            }

            FetchSteamID();

            if (ClientActive())
            {
                Debug.LogError("Transport already running as client!");
                return;
            }

            if (!ServerActive())
            {
                Debug.Log("Starting server.");
                SteamGameServerNetworking.AllowP2PPacketRelay(AllowSteamRelay);
                server     = Server.CreateServer(this, NetworkManager.singleton.maxConnections);
                activeNode = server;
            }
            else
            {
                Debug.LogError("Server already started!");
            }
        }
        public override void ClientConnect(string address)
        {
            if (!SteamManager.Initialized)
            {
                Debug.LogError("SteamWorks not initialized. Client could not be started.");
                OnClientDisconnected.Invoke();
                return;
            }

            FetchSteamID();

            if (ServerActive())
            {
                Debug.LogError("Transport already running as server!");
                return;
            }

            if (!ClientActive() || client.Error)
            {
                Debug.Log($"Starting client, target address {address}.");

                SteamGameServerNetworking.AllowP2PPacketRelay(AllowSteamRelay);
                client     = Client.CreateClient(this, address);
                activeNode = client;
            }
            else
            {
                Debug.LogError("Client already running!");
            }
        }
Esempio n. 13
0
        public static string GetIP(CSteamID id)
        {
            P2PSessionState_t sessionState;

            SteamGameServerNetworking.GetP2PSessionState(id, out sessionState);
            return(Parser.getIPFromUInt32(sessionState.m_nRemoteIP));
        }
        // Token: 0x06001838 RID: 6200 RVA: 0x00089150 File Offset: 0x00087550
        public void write(ICommunityEntity entity, byte[] data, ulong length, ESendMethod method, int channel)
        {
            SteamworksCommunityEntity steamworksCommunityEntity = (SteamworksCommunityEntity)entity;
            CSteamID steamID = steamworksCommunityEntity.steamID;

            switch (method)
            {
            case ESendMethod.RELIABLE:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, EP2PSend.k_EP2PSendReliableWithBuffering, channel);
                return;

            case ESendMethod.RELIABLE_NODELAY:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, EP2PSend.k_EP2PSendReliable, channel);
                return;

            case ESendMethod.UNRELIABLE:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, EP2PSend.k_EP2PSendUnreliable, channel);
                return;

            case ESendMethod.UNRELIABLE_NODELAY:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, EP2PSend.k_EP2PSendUnreliableNoDelay, channel);
                return;

            default:
                return;
            }
        }
    //-----------------------------------------------------------------------------
    // Purpose: Handle clients connecting
    //-----------------------------------------------------------------------------
    void OnP2PSessionRequest(P2PSessionRequest_t pCallback)
    {
        Debug.Log("OnP2PSesssionRequest Called steamIDRemote: " + pCallback.m_steamIDRemote);         // Riley

        // we'll accept a connection from anyone
        SteamGameServerNetworking.AcceptP2PSessionWithUser(pCallback.m_steamIDRemote);
    }
        // Token: 0x06001837 RID: 6199 RVA: 0x00089128 File Offset: 0x00087528
        public void write(ICommunityEntity entity, byte[] data, ulong length)
        {
            SteamworksCommunityEntity steamworksCommunityEntity = (SteamworksCommunityEntity)entity;
            CSteamID steamID = steamworksCommunityEntity.steamID;

            SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, EP2PSend.k_EP2PSendUnreliable, 0);
        }
Esempio n. 17
0
        public void write(ICommunityEntity entity, byte[] data, ulong length, ESendMethod method, int channel)
        {
            SteamworksCommunityEntity steamworksCommunityEntity = (SteamworksCommunityEntity)entity;
            CSteamID steamID = steamworksCommunityEntity.steamID;

            switch (method)
            {
            case ESendMethod.RELIABLE:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, 3, channel);
                return;

            case ESendMethod.RELIABLE_NODELAY:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, 2, channel);
                return;

            case ESendMethod.UNRELIABLE:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, 0, channel);
                return;

            case ESendMethod.UNRELIABLE_NODELAY:
                SteamGameServerNetworking.SendP2PPacket(steamID, data, (uint)length, 1, channel);
                return;

            default:
                return;
            }
        }
Esempio n. 18
0
 private bool SendP2PPacket(bool isServer, CSteamID id, byte[] data, int length, EP2PSend mode)
 {
     if (isServer)
     {
         return(SteamGameServerNetworking.SendP2PPacket(id, data, (uint)length, mode, 0));
     }
     return(SteamNetworking.SendP2PPacket(id, data, (uint)length, mode, 0));
 }
Esempio n. 19
0
 private bool IsP2PPacketAvailable(bool isServer, out uint size)
 {
     if (isServer)
     {
         return(SteamGameServerNetworking.IsP2PPacketAvailable(out size, 0));
     }
     return(SteamNetworking.IsP2PPacketAvailable(out size, 0));
 }
Esempio n. 20
0
        protected override void OnNewConnection(P2PSessionRequest_t result)
        {
#if UNITY_SERVER
            SteamGameServerNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote);
#else
            SteamNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote);
#endif
        }
Esempio n. 21
0
        public static string GetIP(this CSteamID cSteamID)
        {
            // Grab an active players ip address from CSteamID.
            P2PSessionState_t sessionState;

            SteamGameServerNetworking.GetP2PSessionState(cSteamID, out sessionState);
            return(Parser.getIPFromUInt32(sessionState.m_nRemoteIP));
        }
Esempio n. 22
0
 private bool ReadP2PPacket(bool isServer, byte[] data, out uint msgSize, out CSteamID id)
 {
     if (isServer)
     {
         return(SteamGameServerNetworking.ReadP2PPacket(data, (uint)data.Length, out msgSize, out id, 0));
     }
     return(SteamNetworking.ReadP2PPacket(data, (uint)data.Length, out msgSize, out id, 0));
 }
Esempio n. 23
0
        protected void Send(CSteamID host, byte[] msgBuffer, int channel)
        {
#if UNITY_SERVER
            SteamGameServerNetworking.SendP2PPacket(host, msgBuffer, (uint)msgBuffer.Length, channels[Mathf.Min(channel, channels.Length - 1)], channel);
#else
            SteamNetworking.SendP2PPacket(host, msgBuffer, (uint)msgBuffer.Length, channels[Mathf.Min(channel, channels.Length - 1)], channel);
#endif
        }
Esempio n. 24
0
        protected void SendInternal(CSteamID target, InternalMessages type)
        {
#if UNITY_SERVER
            SteamGameServerNetworking.SendP2PPacket(target, new byte[] { (byte)type }, 1, EP2PSend.k_EP2PSendReliable, internal_ch);
#else
            SteamNetworking.SendP2PPacket(target, new byte[] { (byte)type }, 1, EP2PSend.k_EP2PSendReliable, internal_ch);
#endif
        }
Esempio n. 25
0
        protected void CloseP2PSessionWithUser(CSteamID clientSteamID)
        {
#if UNITY_SERVER
            SteamGameServerNetworking.CloseP2PSessionWithUser(clientSteamID);
#else
            SteamNetworking.CloseP2PSessionWithUser(clientSteamID);
#endif
        }
Esempio n. 26
0
        protected override void execute(CSteamID executorID, string parameter)
        {
            if (!Dedicator.isDedicated)
            {
                return;
            }
            if (!Provider.isServer)
            {
                CommandWindow.LogError(this.localization.format("NotRunningErrorText"));
                return;
            }
            string[] componentsFromSerial = Parser.getComponentsFromSerial(parameter, '/');
            if (componentsFromSerial.Length != 1 && componentsFromSerial.Length != 2)
            {
                CommandWindow.LogError(this.localization.format("InvalidParameterErrorText"));
                return;
            }
            SteamPlayer steamPlayer;

            if (!PlayerTool.tryGetSteamPlayer(componentsFromSerial[0], out steamPlayer))
            {
                CommandWindow.LogError(this.localization.format("NoPlayerErrorText", new object[]
                {
                    componentsFromSerial[0]
                }));
                return;
            }
            P2PSessionState_t p2PSessionState_t;
            uint ip;

            if (SteamGameServerNetworking.GetP2PSessionState(steamPlayer.playerID.steamID, ref p2PSessionState_t))
            {
                ip = p2PSessionState_t.m_nRemoteIP;
            }
            else
            {
                ip = 0u;
            }
            if (componentsFromSerial.Length == 1)
            {
                SteamBlacklist.ban(steamPlayer.playerID.steamID, ip, executorID, this.localization.format("SlayTextReason"), SteamBlacklist.PERMANENT);
            }
            else if (componentsFromSerial.Length == 2)
            {
                SteamBlacklist.ban(steamPlayer.playerID.steamID, ip, executorID, componentsFromSerial[1], SteamBlacklist.PERMANENT);
            }
            if (steamPlayer.player != null)
            {
                EPlayerKill eplayerKill;
                steamPlayer.player.life.askDamage(101, Vector3.up * 101f, EDeathCause.KILL, ELimb.SKULL, executorID, out eplayerKill);
            }
            CommandWindow.Log(this.localization.format("SlayText", new object[]
            {
                steamPlayer.playerID.playerName
            }));
        }
Esempio n. 27
0
        /// <summary>
        /// Sends data to the client but is not reliable
        /// </summary>
        /// <param name="clientID"> ID of client to send to </param>
        /// <param name="data"> Data to send to client </param>
        /// <param name="sizeOfData"> Size of data </param>
        /// <returns> Whether the send suceeded or not </returns>
        bool BSendDataToClient(CSteamID clientID, byte[] data, uint sizeOfData)
        {
            if (SteamGameServerNetworking.SendP2PPacket(clientID, data, sizeOfData, EP2PSend.k_EP2PSendUnreliable))
            {
                Console.WriteLine("Failed to send data to client " + SteamFriends.GetFriendPersonaName(clientID));
                return(false);
            }

            return(true);
        }
Esempio n. 28
0
 public void OnP2PSessionRequest(P2PSessionRequest_t callback)
 {
     if (!SteamGameServerNetworking.AcceptP2PSessionWithUser(callback.m_steamIDRemote))
     {
         LogUtils.Debug("Failde to accept P2P Request: " + callback.m_steamIDRemote);
     }
     else
     {
         LogUtils.Debug("Accepted P2P Request: " + callback.m_steamIDRemote);
     }
 }
Esempio n. 29
0
        public static string GetIP(this ulong steamId)
        {
            P2PSessionState_t state;

            if (!SteamGameServerNetworking.GetP2PSessionState(new CSteamID(steamId), out state))
            {
                return(null);
            }

            return(Parser.getIPFromUInt32(state.m_nRemoteIP));
        }
 protected override void OnNewConnection(P2PSessionRequest_t result)
 {
     if (hostSteamID == result.m_steamIDRemote)
     {
         SteamGameServerNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote);
     }
     else
     {
         Debug.LogError("P2P Acceptance Request from unknown host ID.");
     }
 }