GS2-Lock SDK for Game Engine API Reference

Specifications of models and API references for GS2-Lock SDK for Game Engine

Model

EzMutex

Mutex

The mutex provided by GS2 is a type of re-entrant lock.

When acquiring a lock, a transaction ID is specified, and the lock can be acquired again only if the same transaction ID is specified. Since it has a reference counter, the same number of unlock operations are required when releasing it.

TypeConditionRequiredDefaultValue LimitsDescription
mutexIdstring
*
~ 1024 charsMutex GRN
* Set automatically by the server
propertyIdstring
~ 1024 charsProperty ID
An identifier for the resource to be locked, determining which resource the lock is protecting.
When multiple processes need exclusive access to the same resource, they should specify the same property ID.
transactionIdstring
~ 256 charsTransaction ID
An identifier for the transaction acquiring the lock. This ID is used to implement re-entrant locking behavior:
if a lock request specifies the same transaction ID as the current lock holder, the lock is successfully acquired again and the reference count is incremented.
A lock request with a different transaction ID will be rejected while the lock is held.
ttlAtlongThe absolute time 1 hour after the current timeDatetime of ttl
Unix time, milliseconds

Methods

get

Get Mutex status

Request

TypeConditionRequiredDefaultValue LimitsDescription
namespaceNamestring
~ 128 charsNamespace name
Namespace-specific name. Specified using alphanumeric characters, hyphens (-), underscores (_), and periods (.).
propertyIdstring
~ 1024 charsProperty ID
An identifier for the resource to be locked, determining which resource the lock is protecting.
When multiple processes need exclusive access to the same resource, they should specify the same property ID.
gameSessionGameSession
GameSession

Result

TypeDescription
itemEzMutexMutex

Implementation Example

    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    var item = await domain.ModelAsync();
    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    var future = domain.ModelFuture();
    yield return future;
    var item = future.Result;
    const auto Domain = Gs2->Lock->Namespace(
        "namespace-0001" // namespaceName
    )->Me(
        GameSession
    )->Mutex(
        "property-0001" // propertyId
    );
    const auto Future = Domain->Model();
    Future->StartSynchronousTask();
    if (Future->GetTask().IsError())
    {
        return false;
    }
Value change event handling
    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    
    // Start event handling
    var callbackId = domain.Subscribe(
        value => {
            // Called when the value changes
            // The "value" is passed the value after the change.
        }
    );

    // Stop event handling
    domain.Unsubscribe(callbackId);
    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    
    // Start event handling
    var callbackId = domain.Subscribe(
        value => {
            // Called when the value changes
            // The "value" is passed the value after the change.
        }
    );

    // Stop event handling
    domain.Unsubscribe(callbackId);
    const auto Domain = Gs2->Lock->Namespace(
        "namespace-0001" // namespaceName
    )->Me(
        GameSession
    )->Mutex(
        "property-0001" // propertyId
    );
    
    // Start event handling
    const auto CallbackId = Domain->Subscribe(
        [](TSharedPtr<Gs2::Lock::Model::FMutex> value) {
            // Called when the value changes
            // The "value" is passed the value after the change.
        }
    );

    // Stop event handling
    Domain->Unsubscribe(CallbackId);

lock

Obtaining a lock

Locks the resource with the property ID for the number of seconds specified by ttl. The transaction ID must be specified when locking. Acquisition of a lock on the same property ID by a different transaction ID will fail. If the lock acquisition request is from the same transaction, the reference counter is increased.

Request

TypeConditionRequiredDefaultValue LimitsDescription
namespaceNamestring
~ 128 charsNamespace name
Namespace-specific name. Specified using alphanumeric characters, hyphens (-), underscores (_), and periods (.).
propertyIdstring
~ 1024 charsProperty ID
An identifier for the resource to be locked, determining which resource the lock is protecting.
When multiple processes need exclusive access to the same resource, they should specify the same property ID.
gameSessionGameSession
GameSession
transactionIdstring
~ 256 charsTransaction ID
An identifier for the transaction acquiring the lock. This ID is used to implement re-entrant locking behavior:
if a lock request specifies the same transaction ID as the current lock holder, the lock is successfully acquired again and the reference count is incremented.
A lock request with a different transaction ID will be rejected while the lock is held.
ttllong
0 ~ 9223372036854775805Duration of lock acquisition (seconds)

Result

TypeDescription
itemEzMutexMutex

Implementation Example

    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    var result = await domain.LockAsync(
        transactionId: "transaction-0001",
        ttl: 100000L
    );
    var item = await result.ModelAsync();
    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    var future = domain.LockFuture(
        transactionId: "transaction-0001",
        ttl: 100000L
    );
    yield return future;
    if (future.Error != null)
    {
        onError.Invoke(future.Error, null);
        yield break;
    }
    var future2 = future.Result.ModelFuture();
    yield return future2;
    if (future2.Error != null)
    {
        onError.Invoke(future2.Error, null);
        yield break;
    }
    var result = future2.Result;
    const auto Domain = Gs2->Lock->Namespace(
        "namespace-0001" // namespaceName
    )->Me(
        GameSession
    )->Mutex(
        "property-0001" // propertyId
    );
    const auto Future = Domain->Lock(
        "transaction-0001", // transactionId
        100000L // ttl
    );
    Future->StartSynchronousTask();
    if (Future->GetTask().IsError())
    {
        return false;
    }

    // obtain changed values / result values
    const auto Future2 = Future->GetTask().Result()->Model();
    Future2->StartSynchronousTask();
    if (Future2->GetTask().IsError())
    {
        return Future2->GetTask().Error();
    }
    const auto Result = Future2->GetTask().Result();

unlock

Releasing a lock

The lock must be released from the same transaction ID. If the lock is re-entered when the lock is acquired, the lock is released the same number of times, and the actual release occurs when the reference counter reaches zero.

Request

TypeConditionRequiredDefaultValue LimitsDescription
namespaceNamestring
~ 128 charsNamespace name
Namespace-specific name. Specified using alphanumeric characters, hyphens (-), underscores (_), and periods (.).
propertyIdstring
~ 1024 charsProperty ID
An identifier for the resource to be locked, determining which resource the lock is protecting.
When multiple processes need exclusive access to the same resource, they should specify the same property ID.
gameSessionGameSession
GameSession
transactionIdstring
~ 256 charsTransaction ID
An identifier for the transaction acquiring the lock. This ID is used to implement re-entrant locking behavior:
if a lock request specifies the same transaction ID as the current lock holder, the lock is successfully acquired again and the reference count is incremented.
A lock request with a different transaction ID will be rejected while the lock is held.

Result

TypeDescription
itemEzMutexMutex

Implementation Example

    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    var result = await domain.UnlockAsync(
        transactionId: "transaction-0001"
    );
    var domain = gs2.Lock.Namespace(
        namespaceName: "namespace-0001"
    ).Me(
        gameSession: GameSession
    ).Mutex(
        propertyId: "property-0001"
    );
    var future = domain.UnlockFuture(
        transactionId: "transaction-0001"
    );
    yield return future;
    if (future.Error != null)
    {
        onError.Invoke(future.Error, null);
        yield break;
    }
    const auto Domain = Gs2->Lock->Namespace(
        "namespace-0001" // namespaceName
    )->Me(
        GameSession
    )->Mutex(
        "property-0001" // propertyId
    );
    const auto Future = Domain->Unlock(
        "transaction-0001" // transactionId
    );
    Future->StartSynchronousTask();
    if (Future->GetTask().IsError())
    {
        return false;
    }
    const auto Result = Future->GetTask().Result();