public async Task <ConnectedSession> JoinLobby(TempSessionWithSecrets userSession, string lobbyId)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await lobbyApi.SessionPutAsync(lobbyId, userSession.Id));
        }
        public async Task <IEnumerable <ConnectedSession> > GetSessionsInLobby(TempSessionWithSecrets userSession, string lobbyId)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await lobbyApi.SessionsGetAsync(lobbyId));
        }
        public async Task <LobbyInfo> UpdateLobby(TempSessionWithSecrets userSession, LobbyInfo lobby, string name = null, int?maxSessions = null)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await lobbyApi.LobbyPostAsync(lobby.Id, name, maxSessions));
        }
        public async Task <IEnumerable <LobbyInfo> > GetLobbies(TempSessionWithSecrets userSession)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await lobbyApi.LobbiesGetAsync());
        }
        public async Task <List <NATEndpoint> > LookupEndpoints(TempSessionWithSecrets userSession, string targetSession)
        {
            var natPunchthroughApi = new NATPunchthroughApi();

            natPunchthroughApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await natPunchthroughApi.EndpointsGetAsync(targetSession));
        }
        public async Task <LobbyInfo> CreateLobby(TempSessionWithSecrets userSession, string name, int maxSessions)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await lobbyApi.LobbyPutAsync(name, maxSessions));
        }
예제 #7
0
        public async Task <IEnumerable <AttributeKey> > GetAttributeKeys(TempSessionWithSecrets userSession, string objectId)
        {
            var attributeApi = new AttributeApi();

            attributeApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            return(await attributeApi.AttributesGetAsync(objectId));
        }
예제 #8
0
        public async Task <T> GetAttribute <T>(TempSessionWithSecrets userSession, string objectId, string ownerId, string key)
        {
            var attributeApi = new AttributeApi();

            attributeApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            try
            {
                var result = await attributeApi.AttributeGetAsync(objectId, key, ownerId);

                return(JsonConvert.DeserializeObject <T>(result.Value));
            }
            catch (JsonException)
            {
                // Badly formatted data, to prevent other sessions
                // potentially causing issues by storing incorrect data,
                // we just return the default value for T here.
                return(default(T));
            }
            catch (ApiException ex)
            {
                if (ex.ErrorCode == 404)
                {
                    return(default(T));
                }

                throw;
            }
        }
예제 #9
0
        public async Task SetAttribute <T>(TempSessionWithSecrets userSession, string objectId, string key, T value)
        {
            var attributeApi = new AttributeApi();

            attributeApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            await attributeApi.AttributePutAsync(objectId, key, JsonConvert.SerializeObject(value));
        }
        public async Task KickSessionFromLobby(TempSessionWithSecrets userSession, ConnectedSession lobbySession)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            await lobbyApi.SessionDeleteAsync(lobbySession.LobbyId, lobbySession.SessionId);
        }
        public async Task DeleteLobby(TempSessionWithSecrets userSession, LobbyInfo lobby)
        {
            var lobbyApi = new LobbyApi();

            lobbyApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
            await lobbyApi.LobbyDeleteAsync(lobby.Id);
        }
        private async Task<UdpClient> PerformNATPunchthroughInternal(TempSessionWithSecrets userSession, UdpClient specificUdpClient, int timeout)
        {
            var natPunchthroughApi = new NATPunchthroughApi();
            natPunchthroughApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;

            var start = DateTime.UtcNow;

            var udpClient = specificUdpClient ?? new UdpClient();

            while (true)
            {
                NATNegotation negotation;
                try
                {
                    negotation = await natPunchthroughApi.PunchthroughPutAsync(userSession.Id);
                }
                catch (HiveMP.NATPunchthrough.Client.ApiException ex)
                {
                    // There are no NAT punchthrough servers for us to use, send a UDP packet to 
                    // localhost so we get an outbound address and port.
                    var b = new byte[] { (byte)'a' };
                    await udpClient.SendAsync(
                        b,
                        1,
                        "localhost",
                        4242);
                    await Task.Delay(100);

                    return udpClient;
                }

                if (negotation.Port == null)
                {
                    throw new InvalidOperationException();
                }

                await udpClient.SendAsync(
                    negotation.Message,
                    negotation.Message.Length,
                    negotation.Host,
                    negotation.Port.Value);

                await Task.Delay(100);

                if (await natPunchthroughApi.PunchthroughGetAsync(userSession.Id) == true)
                {
                    // NAT punchthrough completed successfully.
                    return udpClient;
                }

                if (timeout > 0 && (DateTime.UtcNow - start).TotalMilliseconds > timeout)
                {
                    throw new TimeoutException("Unable to perform NAT punchthrough before the timeout occurred.");
                }

                await Task.Delay(100);
            }
        }
        public async Task<BackgroundNATPunchthrough> StartBackgroundNATPunchthrough(TempSessionWithSecrets userSession, UdpClient udpClient)
        {
            // Do the first operation inline, because we need to send at least one packet to
            // give the UDP client a port.
            await PerformNATPunchthroughInternal(userSession, udpClient, 60000);

            var backgroundPunchthrough = new BackgroundNATPunchthrough(userSession, udpClient);
            var thread = new Thread(() => ContinuousNATPunchthrough(backgroundPunchthrough));
            thread.Name = "NAT Punchthrough";
            thread.IsBackground = true;
            thread.Start();
            return backgroundPunchthrough;
        }
        public async Task<BackgroundNATPunchthrough> StartBackgroundNATPunchthrough(TempSessionWithSecrets userSession, UdpClient udpClient)
        {
            // Do the first operation inline, because we need to send at least one packet to
            // give the UDP client a port.
            await PerformNATPunchthroughInternal(userSession, udpClient, 60000);

            var backgroundPunchthrough = new BackgroundNATPunchthrough(userSession, udpClient);
            var thread = new Thread(() => ContinuousNATPunchthrough(backgroundPunchthrough));
            thread.Name = "NAT Punchthrough";
            thread.IsBackground = true;
            thread.Start();
            return backgroundPunchthrough;
        }
        private async Task <UdpClient> PerformNATPunchthroughInternal(TempSessionWithSecrets userSession, UdpClient specificUdpClient, int timeout)
        {
            var natPunchthroughApi = new NATPunchthroughApi();

            natPunchthroughApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;

            var start = DateTime.UtcNow;

            var udpClient = specificUdpClient ?? new UdpClient();

            while (true)
            {
                var negotation = await natPunchthroughApi.PunchthroughPutAsync(userSession.Id);

                if (negotation.Port == null)
                {
                    throw new InvalidOperationException();
                }

                await udpClient.SendAsync(
                    negotation.Message,
                    negotation.Message.Length,
                    negotation.Host,
                    negotation.Port.Value);

                await Task.Delay(100);

                if (await natPunchthroughApi.PunchthroughGetAsync(userSession.Id) == true)
                {
                    // NAT punchthrough completed successfully.
                    return(udpClient);
                }

                if (timeout > 0 && (DateTime.UtcNow - start).TotalMilliseconds > timeout)
                {
                    throw new TimeoutException("Unable to perform NAT punchthrough before the timeout occurred.");
                }

                await Task.Delay(100);
            }
        }
 public BackgroundNATPunchthrough(TempSessionWithSecrets userSession, UdpClient udpClient)
 {
     UserSession = userSession;
     UdpClient = udpClient;
 }
 public async Task<List<NATEndpoint>> LookupEndpoints(TempSessionWithSecrets userSession, string targetSession)
 {
     var natPunchthroughApi = new NATPunchthroughApi();
     natPunchthroughApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;
     return await natPunchthroughApi.EndpointsGetAsync(targetSession);
 }
 public async Task<UdpClient> PerformNATPunchthrough(TempSessionWithSecrets userSession, int timeout)
 {
     return await PerformNATPunchthroughInternal(userSession, null, timeout);
 }
 public async Task PerformNATPunchthrough(TempSessionWithSecrets userSession, UdpClient udpClient, int timeout)
 {
     await PerformNATPunchthroughInternal(userSession, udpClient, timeout);
 }
        private async Task<UdpClient> PerformNATPunchthroughInternal(TempSessionWithSecrets userSession, UdpClient specificUdpClient, int timeout)
        {
            var natPunchthroughApi = new NATPunchthroughApi();
            natPunchthroughApi.Configuration.ApiKey["api_key"] = userSession.ApiKey;

            var start = DateTime.UtcNow;

            var udpClient = specificUdpClient ?? new UdpClient();

            while (true)
            {
                var negotation = await natPunchthroughApi.PunchthroughPutAsync(userSession.Id);
                if (negotation.Port == null)
                {
                    throw new InvalidOperationException();
                }

                await udpClient.SendAsync(
                    negotation.Message,
                    negotation.Message.Length,
                    negotation.Host,
                    negotation.Port.Value);

                await Task.Delay(100);

                if (await natPunchthroughApi.PunchthroughGetAsync(userSession.Id) == true)
                {
                    // NAT punchthrough completed successfully.
                    return udpClient;
                }

                if (timeout > 0 && (DateTime.UtcNow - start).TotalMilliseconds > timeout)
                {
                    throw new TimeoutException("Unable to perform NAT punchthrough before the timeout occurred.");
                }

                await Task.Delay(100);
            }
        }
 public async Task PerformNATPunchthrough(TempSessionWithSecrets userSession, UdpClient udpClient, int timeout)
 {
     await PerformNATPunchthroughInternal(userSession, udpClient, timeout);
 }
 public async Task <UdpClient> PerformNATPunchthrough(TempSessionWithSecrets userSession, int timeout)
 {
     return(await PerformNATPunchthroughInternal(userSession, null, timeout));
 }
    private void UpdateTemporarySession()
    {
        if (_tempSessionProcessing)
        {
            return;
        }

        if (TemporarySession == PublicAuthenticationModeEnum.AutomaticallyCreateSession)
        {
            _shouldHaveTemporarySession = true;
        }
        else if (TemporarySession == PublicAuthenticationModeEnum.DisallowTemporarySessionCreation)
        {
            _shouldHaveTemporarySession = false;
        }

        if (_tempSessionWithSecrets == null)
        {
            Debug.Log("Creating temporary session");
            _tempSessionProcessing = true;
            lock (_queueLock)
            {
                _queuedOperations.Enqueue(() =>
                {
                    try
                    {
                        HiveMP.TemporarySession.Client.Configuration.ApiKey["api_key"] = PublicAPIKey;
                        _tempSessionWithSecrets = _tempSessionClient.SessionPUT();
                    }
                    finally
                    {
                        _tempSessionProcessing = false;
                    }
                });
            }
        }
        else if (!_shouldHaveTemporarySession)
        {
            // Delete the session since we have one.
            Debug.Log("Session is present, but should not be; deleting");
            _tempSessionProcessing = true;
            lock (_queueLock)
            {
                _queuedOperations.Enqueue(() =>
                {
                    try
                    {
                        HiveMP.TemporarySession.Client.Configuration.ApiKey["api_key"] = _tempSessionWithSecrets.ApiKey;
                        _tempSessionClient.SessionDELETE(_tempSessionWithSecrets.Id);
                        _tempSessionProcessing = false;
                    }
                    finally
                    {
                        _tempSessionWithSecrets = null;
                    }
                });
            }
        }
        else
        {
            // Check if we need to renew the session.
            if ((int)_tempSessionWithSecrets.Expiry.Value - GetCurrentUNIXTimestamp() < 15 * 60)
            {
                Debug.Log("Less than 15 minutes to session expiry; automatically renewing");
                _tempSessionProcessing = true;
                lock (_queueLock)
                {
                    _queuedOperations.Enqueue(() =>
                    {
                        try
                        {
                            HiveMP.TemporarySession.Client.Configuration.ApiKey["api_key"] = _tempSessionWithSecrets.ApiKey;
                            var result = _tempSessionClient.SessionPOST(_tempSessionWithSecrets.Id);
                            _tempSessionWithSecrets.Expiry = result.Expiry;
                        }
                        finally
                        {
                            _tempSessionProcessing = false;
                        }
                    });
                }
            }
        }
    }
 public BackgroundNATPunchthrough(TempSessionWithSecrets userSession, UdpClient udpClient)
 {
     UserSession = userSession;
     UdpClient   = udpClient;
 }