// An opcode defined by client and your server script that represents a custom message type

    /// Initialize a client for GameLift Realtime and connect to a player session.
    /// <param name="endpoint">The DNS name that is assigned to Realtime server</param>
    /// <param name="remoteTcpPort">A TCP port for the Realtime server</param>
    /// <param name="listeningUdpPort">A local port for listening to UDP traffic</param>
    /// <param name="connectionType">Type of connection to establish between client and the Realtime server</param>
    /// <param name="playerSessionId">The player session ID that is assigned to the game client for a game session </param>
    /// <param name="connectionPayload">Developer-defined data to be used during client connection, such as for player authentication</param>
    public RealTimeClient(string endpoint, int remoteTcpPort, int listeningUdpPort, ConnectionType connectionType,
                          string playerSessionId, byte[] connectionPayload)
    {
        // Create a client configuration to specify a secure or unsecure connection type
        // Best practice is to set up a secure connection using the connection type RT_OVER_WSS_DTLS_TLS12.
        ClientConfiguration clientConfiguration = new ClientConfiguration()
        {
            // C# notation to set the field ConnectionType in the new instance of ClientConfiguration
            ConnectionType = connectionType
        };

        // Create a Realtime client with the client configuration
        Client = new Client(clientConfiguration);

        // Initialize event handlers for the Realtime client
        Client.ConnectionOpen         += OnOpenEvent;
        Client.ConnectionClose        += OnCloseEvent;
        Client.GroupMembershipUpdated += OnGroupMembershipUpdate;
        Client.DataReceived           += OnDataReceived;

        // Create a connection token to authenticate the client with the Realtime server
        // Player session IDs can be retrieved using AWS SDK for GameLift
        ConnectionToken connectionToken = new ConnectionToken(playerSessionId, connectionPayload);

        // Initiate a connection with the Realtime server with the given connection information
        ConnectionStatus res = Client.Connect(endpoint, remoteTcpPort, listeningUdpPort, connectionToken);
    }
    public void Connect(string endpoint, int remoteTcpPort, int listeningUdpPort, ConnectionType connectionType,
                        string playerSessionId, byte[] connectionPayload)
    {
        ConnectionToken connectionToken = new ConnectionToken(playerSessionId, connectionPayload);

        Client.Connect(endpoint, remoteTcpPort, listeningUdpPort, connectionToken);
    }
Example #3
0
        private static IConnectionProvider LoadProviderByConnectionToken(ConnectionToken token)
        {
            IConnectionProvider provider;

            if (TryLoadAssemblyUsingAttribute(token.ConnectionString, token.ProviderName, out provider))
            {
                return(provider);
            }

            provider = ComposeProvider(token.ProviderName);
            if (provider == null)
            {
                throw new InvalidOperationException(string.Format("Provider '{0}' could not be resolved.", token.ProviderName));
            }

            provider.SetConnectionString(token.ConnectionString);

            var schemaConnectionProvider = provider as ISchemaConnectionProvider;

            if (schemaConnectionProvider != null)
            {
                schemaConnectionProvider.SetSchema(token.SchemaName);
            }

            return(provider);
        }
    /// Initialize a client for GameLift Realtime and connect to a player session.
    /// <param name="endpoint">The DNS name that is assigned to Realtime server</param>
    /// <param name="remoteTcpPort">A TCP port for the Realtime server</param>
    /// <param name="listeningUdpPort">A local port for listening to UDP traffic</param>
    /// <param name="connectionType">Type of connection to establish between client and the Realtime server</param>
    /// <param name="playerSessionId">The player session ID that is assigned to the game client for a game session </param>
    /// <param name="connectionPayload">Developer-defined data to be used during client connection, such as for player authentication</param>
    public RealTimeClient(string endpoint, int remoteTcpPort, int listeningUdpPort, ConnectionType connectionType,
                          string playerSessionId, byte[] connectionPayload)
    {
        // Create a client configuration to specify a secure or unsecure connection type
        // Best practice is to set up a secure connection using the connection type RT_OVER_WSS_DTLS_TLS12.
        ClientConfiguration clientConfiguration = new ClientConfiguration()
        {
            // C# notation to set the field ConnectionType in the new instance of ClientConfiguration
            ConnectionType = connectionType
        };

        // Create a Realtime client with the client configuration
        Client = new Client(clientConfiguration);

        // Initialize event handlers for the Realtime client
        Client.ConnectionOpen  += new EventHandler(OnOpenEvent);
        Client.ConnectionClose += new EventHandler(OnCloseEvent);
        Client.DataReceived    += new EventHandler <DataReceivedEventArgs>(OnDataReceived);
        Client.ConnectionError += new EventHandler <Aws.GameLift.Realtime.Event.ErrorEventArgs>(OnConnectionErrorEvent);

        // Create a connection token to authenticate the client with the Realtime server
        // Player session IDs can be retrieved using AWS SDK for GameLift
        ConnectionToken connectionToken = new ConnectionToken(playerSessionId, connectionPayload);

        // Initiate a connection with the Realtime server with the given connection information
        Client.Connect(endpoint, remoteTcpPort, listeningUdpPort, connectionToken);
        activateListeners();
        Debug.Log("RTC instantiated");
    }
        private static IConnectionProvider LoadProviderByFilename(ConnectionToken token)
        {
            string extension = GetFileExtension(token.ConnectionString);

            var provider = ComposeProvider(extension);

            provider.SetConnectionString(string.Format("data source={0}", token.ConnectionString));
            return(provider);
        }
Example #6
0
        private static IConnectionProvider LoadProviderByConnectionToken(ConnectionToken token)
        {
            var provider = ComposeProvider(token.ProviderName);

            if (provider == null)
            {
                throw new InvalidOperationException("Provider could not be resolved.");
            }

            provider.SetConnectionString(token.ConnectionString);
            return(provider);
        }
        private IConnectionProvider LoadProviderByConnectionString(ConnectionToken token)
        {
            var dataSource = GetDataSourceName(token.ConnectionString);

            if (dataSource.EndsWith("sdf", StringComparison.CurrentCultureIgnoreCase) && File.Exists(dataSource))
            {
                return(GetProviderByFilename(dataSource));
            }

            var provider = ComposeProvider();

            provider.SetConnectionString(token.ConnectionString);
            return(provider);
        }
Example #8
0
 public byte[] Serialize()
 {
     using (var m = new MemoryStream()) {
         using (var writer = new BinaryWriter(m)) {
             writer.Write(Type);
             writer.Write((byte)_mode);
             if (Type != (short)MessageType.Empty && Type != (short)MessageType.Handshake)
             {
                 writer.Write(ConnectionToken.Serialize());
             }
             writer.Write(MessageId.ToShort());
         }
         return(m.ToArray());
     }
 }
        /// <summary>
        /// Enables synchronization of the object with the
        /// client specified by <paramref name="connectionId"/>.
        /// </summary>
        /// <param name="connectionId">Connection ID</param>
        /// <param name="key">The key that is used to access the object</param>
        internal void AddConnection(string connectionId, string key)
        {
            lock (_lock)
            {
                if (_connections.Any(t => t.Id == connectionId))
                {
                    return;
                }

                var token = new ConnectionToken(connectionId, key);
                _connections.Add(token);
            }

            // Push the object to the new client
            _syncHub.Clients.Client(connectionId).InitializeObject(key, Object);
        }
Example #10
0
        private static IConnectionProvider LoadProviderByConnectionToken(ConnectionToken token)
        {
            IConnectionProvider provider;

            if (TryLoadAssemblyUsingAttribute(token.ConnectionString, token.ProviderName, out provider))
            {
                return(provider);
            }

            provider = ComposeProvider(token.ProviderName);
            if (provider == null)
            {
                throw new InvalidOperationException("Provider could not be resolved.");
            }

            provider.SetConnectionString(token.ConnectionString);
            return(provider);
        }
Example #11
0
    private void RealtimeClientConnect(string endpoint, int remoteTcpPort, int listeningUdpPort, ConnectionType connectionType,
                                       string playerSessionId, byte[] connectionPayload)
    {
        ClientLogger.LogHandler = (x) => GD.Print(x);

        // Create a client configuration to specify a secure or unsecure connection type
        // Best practice is to set up a secure connection using the connection type RT_OVER_WSS_DTLS_TLS12.
        ClientConfiguration clientConfiguration = new ClientConfiguration()
        {
            // C# notation to set the field ConnectionType in the new instance of ClientConfiguration
            ConnectionType = connectionType
        };

        realtimeClient = new Aws.GameLift.Realtime.Client(clientConfiguration);
        realtimeClient.DataReceived += OnDataReceived;
        ConnectionToken connectionToken = new ConnectionToken(playerSessionId, connectionPayload);

        realtimeClient.Connect(endpoint, remoteTcpPort, listeningUdpPort, connectionToken);
    }
Example #12
0
    private IEnumerator ConnectToServer(string ipAddr, int port, string tokenUID)
    {
        ClientLogger.LogHandler = (x) => Debug.Log(x);
        ConnectionToken token = new ConnectionToken(tokenUID, null);

        ClientConfiguration clientConfiguration;

        if (!usingTLS)
        {
            clientConfiguration = new ClientConfiguration
            {
                ConnectionType = ConnectionType.RT_OVER_WS_UDP_UNSECURED
            }
        }
        ;
        else
        {
            clientConfiguration = new ClientConfiguration
            {
                ConnectionType = ConnectionType.RT_OVER_WSS_DTLS_TLS12
            }
        };

        _client = new Aws.GameLift.Realtime.Client(clientConfiguration);

        _client.DataReceived += OnDataReceived;

        int ListenPort = 8921;

        clientManager.MessageReceived($"[client] TLS: {usingTLS} with Port: {ListenPort}");
        _client.Connect(ipAddr, port, ListenPort, token);

        while (true)
        {
            if (_client.ConnectedAndReady)
            {
                clientManager.MessageReceived("[client] Connected to server");
                break;
            }
            yield return(null);
        }
    }
Example #13
0
    // common code whether we are connecting to a GameLift hosted server or
    // a local server
    private IEnumerator ConnectToServer(string ipAddr, int port, string tokenUID)
    {
        ClientLogger.LogHandler = (x) => Debug.Log(x);
        ConnectionToken token = new ConnectionToken(tokenUID, null);

        ClientConfiguration clientConfiguration = ClientConfiguration.Default();

        _client = new Aws.GameLift.Realtime.Client(clientConfiguration);
        _client.ConnectionOpen  += new EventHandler(OnOpenEvent);
        _client.ConnectionClose += new EventHandler(OnCloseEvent);
        _client.DataReceived    += new EventHandler <DataReceivedEventArgs>(OnDataReceived);
        _client.ConnectionError += new EventHandler <Aws.GameLift.Realtime.Event.ErrorEventArgs>(OnConnectionErrorEvent);

        int UDPListenPort = FindAvailableUDPPort(DEFAULT_UDP_PORT, DEFAULT_UDP_PORT + 20);

        if (UDPListenPort == -1)
        {
            Debug.Log("Unable to find an open UDP listen port");
            yield break;
        }
        else
        {
            Debug.Log($"UDP listening on port: {UDPListenPort}");
        }

        Debug.Log($"[client] Attempting to connect to server ip: {ipAddr} TCP port: {port} Player Session ID: {tokenUID}");
        _client.Connect(string.IsNullOrEmpty(ipAddr) ? DEFAULT_ENDPOINT : ipAddr, port, UDPListenPort, token);

        while (true)
        {
            if (_client.ConnectedAndReady)
            {
                IsConnectedToServer = true;
                Debug.Log("[client] Connected to server");
                break;
            }
            yield return(null);
        }
    }
        public IConnectionProvider GetProviderByFilename(string filename)
        {
            var token = new ConnectionToken(filename, "System.Data.SqlCeClient");

            return(_connectionProviderCache.GetOrAdd(token, LoadProviderByFilename));
        }
        public IConnectionProvider GetProviderByConnectionString(string connectionString)
        {
            var token = new ConnectionToken(connectionString, string.Empty);

            return(_connectionProviderCache.GetOrAdd(token, LoadProviderByConnectionString));
        }
 private bool RemoveConnection(ConnectionToken connection)
 {
     _syncHub.Clients.Client(connection.Id).RemoveObject(connection.ObjectKey);
     return(_connections.Remove(connection));
 }