TCPClient Client transport type used with userData.GetClientTransport()
const TCPClient = 1
UDPClient Client transport type used with userData.GetClientTransport()
const UDPClient = 2
UnknownClient invalid network transport type that should never show up...
const UnknownClient = 4
WSClient Client transport type used with userData.GetClientTransport()
const WSClient = 3
func CopyUser(user *User) map[string]interface{}
CopyUser Copies a user struct for transferring user client data to another server.
Parameters
userData - User to be copied as a map.
func DecodeUserCopy(b []byte) map[string]interface{}
DecodeUserCopy decodes encoded user by EncodeUserCopy
func DecodeUserData(encoded string) (map[string]interface{}, error)
DecodeUserData decodes encoded user property data to a map
func DiscardUser(reliableClientAddr string, sid string)
DiscardUser Deletes a user data. Either reliableClientAddr or SID to be used.
[NOTE] Uses mutex lock internally. [NOTE] Newer version of the clients use SID instead of the client address.
Parameters
reliableClientAddr - Client address of the user to be discarded. sid - User SID of the user to be discarded.
func EncodeUserCopy(user *User) []byte
EncodeUserCopy encodes the user into a byte array to be decoded by DecodeUserCopy.
func EncodeUserData(user *User) (string, error)
EncodeUserData encodes the user property data to base64 string.
func ExistsByID(sid string) bool
ExistsByID returns true if the user by the given SID exists.
[NOTE] This does NOT update user TTL. [NOTE] Uses mutex lock internally.
func GetAllUserIDs() []string
GetAllUserIDs returns all user SIDs that are connected to the server process.
[IMPORTANT] This maybe extremely expensive to use as it loops all existing users on the server. [IMPORTANT] Limiting the usage of this function for tests and debugging is recommended.
func GetCCU() int
GetCCU returns the number of concurrent users of the server.
[NOTE] Uses mutex lock internally..
func New(data map[string]interface{}, initTTL int64) (map[string]interface{}, error)
New [INTERNAL USE ONLY] handle mesh network command sent from HTTP
[NOTE] Uses mutex lock internally. [IMPORTANT] The return value of map[string]interface{} is always nil with or without error returned.
If initTTL is greater than 0, it will be used as the initial TTL (in seconds)
If initTTL is 0 or less, config TTL will be used as the initial TTL (in seconds)
func OnClientAddressChange(callback func(userData *User))
OnClientAddressChange assigns a callback on change client global address.
Public IP address is checked every time we receive TCP/UDP packets and if the address is changed, this callback will be triggered.
[IMPORTANT] All callbacks are invoked synchronously and if a callback blocks, it will block the other operation.
Parameters
callback - Callback to be invoked when a global IP address is changed on client side. func(userData *User) userData - User data of the user that made the new connection with the server.
func OnDiscard(callback func(sid string, userData *User))
OnDiscard Registers a callback on user data deletion.
[IMPORTANT] This function is not goroutine safe and it must not be used in runtime. assign callbacks on the start of your process only. [IMPORTANT] The callbacks are invoked while a mutex lock is held, which means using mutex lock in the callbacks may result in unexpected blocking.
Parameters
callback - Callback to be invoked when the target user is deleted. func(sid string, userData *User) sid - SID of the discarded user. userData - User data of the discarded user.
func OnNew(callback func(userData *User))
OnNew Registers a callback on user.New()
[IMPORTANT] All callbacks are invoked synchronously and if a callback blocks, it will block the other operation.
Parameters
callback - Callback to be invoked when a new user is created. func(userData *User) userData - User data of new user.
func OnNewConnection(callback func(userData *User))
OnNewConnection assigns a callback on new user connection.
New connection is made and the user client actually establishes a valid connection with the server.
[IMPORTANT] All callbacks are invoked synchronously and if a callback blocks, it will block the other operation.
Parameters
callback - Callback to be invoked when a new user is created. func(userData *User) userData - User data of the user that made the new connection with the server.
func OnSIDUpdate(callback func(previousSID string, newSID string, previousUserData *User, newUserData *User))
OnSIDUpdate assigns a callback to be invoked when the user changes its SID.
[IMPORTANT] The callbacks are invoked while a mutex lock is held, which means using mutex lock in the callbacks may result in unexpected blocking. [NOTE] The callbacks are invoked when the user attempts to create a new user data with the existing user ID with different SID. If the user attempts to create a new user data without calling Disconnect and the user data is not expired yet, User module replaces the user data and SID.
func RemoveOnNewConnection(callback func(userData *User))
RemoveOnNewConnection removes an assigned callback on new user connection.
func SetTCPState(sid string, state *tcp.State)
SetTCPState [INTERNAL USE ONLY] Sets TCP user state to the user data and updates TTL
[NOTE] Uses mutex lock internally.
Parameters
sid - User SID of the target user to updated. state - TCP state to be assigned to the user.
func SetUDPStateAndRUDPConn(sid string, state *udp.State)
SetUDPStateAndRUDPConn [INTERNAL USE ONLY] Sets UDP user state to the user data and updates TTL
[NOTE] Uses mutex lock internally.
Parameters
sid - User SID of the target user to updated. state - UDP state to be assigned to the user.
func SetUserByID(sid string, user *User, ttl int64) bool
SetUserByID Sets a user data by SID.
It returns false if it fails to set the user.
[NOTE] Uses mutex lock internally.
Parameters
sid - User SID to store in the server memory. userData - User to be stored. ttl - TTL of the stored user in seconds. This will be extended by using Extend etc.
func Setup(ttl int64)
Setup sets up user package without network protocol association.
ttl int64 - TTL of the user data in seconds.
func SetupAsConnector(ttl int64)
SetupAsConnector Sets up as Connector server
ttl int64 - TTL of the health check in seconds.
func SetupAsTCPServer()
SetupAsTCPServer Sets up as TCP server.
[IMPORTANT] You must invoke this function before invoking diarkis.Start() to setup as TCP server. [IMPORTANT] You may not setup both TCP and UDP.
func SetupAsUDPServer()
SetupAsUDPServer Sets up as UDP server
[IMPORTANT] You must invoke this function before invoking diarkis.Start() to setup as UDP server. [IMPORTANT] You may not setup both TCP and UDP.
func UpdateUserByID(sid string) bool
UpdateUserByID Updates TTL of user data in the vault.
It returns false if the update fails.
[NOTE] Uses mutex lock internally.
Parameters
sid - User SID of the target user to updated.
User represents user client and its property data, network connection.
type User struct { // unique ID to identify the user by ID string SID string SIDBytes []byte IsMoving bool // Key value storage: Do NOT use this map directly. // Use Get, Set, Update, and "GetAs..." instead. Data map[string]interface{} // Deprecated and no longer used Payload []byte TCPState *tcp.State UDPState *udp.State RUDPConn *udp.Connection UDPClientLocalAddrList []string // UDPState is always a new one.. UDPClientKey string // encryption keys EncryptionKey []byte EncryptionIV []byte EncryptionMacKey []byte // reconnect flag Reconnect bool sync.RWMutex // contains filtered or unexported fields }
func CreateBlankUser(sid, uid string) *User
CreateBlankUser creates an incomplete user to be used as a dummy.
Usage Example: Create a dummy user to create an empty room and/or group without actual client.
sid - Blank user SID. uid - Blank user ID.
func CreateNewUser(data map[string]interface{}, initTTL int64) (*User, int64, error)
CreateNewUser creates a new user data.
This function does NOT store the new user in the vault memory.
Input parameter data map[string]interface{} must contain the following properties:
Error Cases
+----------------------------+------------------------------------------------------------------+ | Error | Reason | +----------------------------+------------------------------------------------------------------+ | Failed to generate UUID v4 | crypto/rand.Ready failed. | | User already exists | User with the same SID or UID already exists on the same server. | +----------------------------+------------------------------------------------------------------+ [IMPORTANT] Uniqueness of the user by its SID and UID is guaranteed only on the same server.
Parameters
sid - SID of the new user to be created. The format is UUID v4. key - Encryption key of the user. iv - Encryption IV of the user. macKey - Encryption mac key of the user. uid - User ID. data - Optional property: Encoded user property data.
func GetUserByID(sid string) *User
GetUserByID returns a user by its SID.
[IMPORTANT] It returns nil if the user is not found by the given SID. [NOTE] This does NOT update user TTL. [NOTE] Uses mutex lock internally.
Parameters
sid - User SID of the target user.
func GetUserBySID(sid string) *User
GetUserBySID is an alias of GetUserByID and it returns a user by its SID.
[IMPORTANT] It returns nil if the user is not found by the given SID. [NOTE] Uses mutex lock internally.
Parameters
sid - User SID of the target user.
func GetUserByUID(uid string) *User
GetUserByUID returns a user by its user ID (UID).
[IMPORTANT] It returns nil if the user is not found by the given UID. [NOTE] Uses mutex lock internally.
Parameters
uid - User ID of the target user.
func (user *User) Delete(key string) bool
Delete removes the key and its value and returns true if the deletion was successful.
[NOTE] Uses mutex lock internally. [NOTE] If the key does not exist, it will simple do nothing and returns false.
func (user *User) Disconnect()
Disconnect disconnects the client from the server
func (user *User) Get(key string) interface{}
Get returns a value associated with the key (SID) given
[NOTE] Uses mutex lock internally.
User data must be handled by the user and nobody else, so no race condition...
[IMPORTANT] If you need to store a structured data, please use json label or convert to map instead. [IMPORTANT] If the value is a map, the data type will become map[string]interface{} when the user moves to the other server. [IMPORTANT] If the value is a slice, the data type will become []interface{} when the user moves to the other server. [IMPORTANT] If the value is numeric, the data type will become float64 when the user moves to the other server.
Example:
numInterface := userData.GetAsInt("num") // this maybe float64, but we want to make sure it is read as an int
Parameters
key string - User property key of the value to retrieve.
func (u *User) GetAsBool(k string) (bool, bool)
GetAsBool returns user data by its key as a bool.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [NOTE] Uses mutex lock internally.
func (u *User) GetAsBoolArray(k string) ([]bool, bool)
GetAsBoolArray returns user data by its key as an array of bool.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [NOTE] Uses mutex lock internally.
func (u *User) GetAsBytes(k string) ([]byte, bool)
GetAsBytes returns user data by its key as a string.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the value held is a base64 encoded string, it will be returned as a byte array as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsFloat32(k string) (float32, bool)
GetAsFloat32 returns user data by its key as a float32.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsFloat64(k string) (float64, bool)
GetAsFloat64 returns user data by its key as a float64.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsFloat64Array(k string) ([]float64, bool)
GetAsFloat64Array returns user data by its key as an array of float64.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [NOTE] Uses mutex lock internally.
func (u *User) GetAsInt(k string) (int, bool)
GetAsInt returns user data by its key as a int.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsInt16(k string) (int16, bool)
GetAsInt16 returns user data by its key as a int16.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsInt16Array(k string) ([]int16, bool)
GetAsInt16Array returns user data by its key as an array of int16.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsInt32(k string) (int32, bool)
GetAsInt32 returns user data by its key as a int32.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsInt32Array(k string) ([]int32, bool)
GetAsInt32Array returns user data by its key as an array of int32.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsInt64(k string) (int64, bool)
GetAsInt64 returns user data by its key as a int64.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsInt64Array(k string) ([]int64, bool)
GetAsInt64Array returns user data by its key as an array of int64.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsInt8(k string) (int8, bool)
GetAsInt8 returns user data by its key as a int8.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsInt8Array(k string) ([]int8, bool)
GetAsInt8Array returns user data by its key as an array of int8.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsIntArray(k string) ([]int, bool)
GetAsIntArray returns user data by its key as an array of int.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsString(k string) (string, bool)
GetAsString returns user data by its key as a string.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [NOTE] Uses mutex lock internally.
func (u *User) GetAsStringArray(k string) ([]string, bool)
GetAsStringArray returns user data by its key as an array of string.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [NOTE] Uses mutex lock internally.
func (u *User) GetAsUint(k string) (uint, bool)
GetAsUint returns user data by its key as a uint.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsUint16(k string) (uint16, bool)
GetAsUint16 returns user data by its key as a uint16.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsUint16Array(k string) ([]uint16, bool)
GetAsUint16Array returns user data by its key as an array of uint16.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsUint32(k string) (uint32, bool)
GetAsUint32 returns user data by its key as a uint32.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsUint32Array(k string) ([]uint32, bool)
GetAsUint32Array returns user data by its key as an array of uint32.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsUint64(k string) (uint64, bool)
GetAsUint64 returns user data by its key as a uint32.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsUint64Array(k string) ([]uint64, bool)
GetAsUint64Array returns user data by its key as an array of uint64.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (u *User) GetAsUint8(k string) (uint8, bool)
GetAsUint8 returns user data by its key as a uint8.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be true, if the value is invalid or missing. [IMPORTANT] If the held value is float64, it will be converted to the correct type and returned as a valid value.
[NOTE] Uses mutex lock internally.
func (u *User) GetAsUint8Array(k string) ([]uint8, bool)
GetAsUint8Array returns user data by its key as an array of uint8.
[IMPORTANT] The second returned value indicates if the value exists or not. The second value will be false, if the value data type is not invalid or missing. Valid means that the value is of the correct data type. [IMPORTANT] If one or more elements in the array are not the specified data type, the second return value will be false. [IMPORTANT] If one or more elements in the array are to be float64, it is an exception that will be auto-converted to the specified numerical data type and the second return value will be true. [NOTE] Uses mutex lock internally.
func (user *User) GetClientAddr() string
GetClientAddr returns the user client address.
[IMPORTANT] If the UDP server has a configuration enableP2P: false, The returned value will be "0.0.0.0:0".
func (user *User) GetClientKey() string
GetClientKey returns clientKey.
Returns an empty string if client key is not set by environment variable DIARKIS_CLIENT_KEY.
func (user *User) GetClientTransport() int
GetClientTransport returns enum to indicate which network transport the user client is using:
Possible values:
TCPClient int = 1 UDPClient int = 2 WSClient int = 3 UnknownClient int = 4 // this should never happen...
func (user *User) GetLatency() int64
GetLatency returns server-to-client latency in milliseconds of the user client.
func (user *User) IsTCP() bool
IsTCP returns true if the user client is TCP
func (user *User) IsUDP() bool
IsUDP returns true if the user client is UDP
func (user *User) OnClientAddressChange(callback func())
OnClientAddressChange [INTERNAL USE ONLY]
callback - Callback to be invoked when a public IP address for the client is changed
func (user *User) OnNewConnection(callback func())
OnNewConnection [INTERNAL USE ONLY]
callback - Callback to be invoked when a new user connection is established with the client.
func (user *User) Push(message []byte, reliable bool)
Push use ServerPush instead
func (user *User) RemoveOnNewConnection()
RemoveOnNewConnection [INTERNAL USE ONLY]
func (user *User) Respond(message []byte, status uint8, reliable bool)
Respond use ServerRespond instead
func (user *User) Send(ver uint8, cmd uint16, message []byte, status uint8, reliable bool)
Send Sends a packet from the server to the target user client with a response status
func (user *User) ServerPush(ver uint8, cmd uint16, message []byte, reliable bool)
ServerPush Sends a push packet from the server to the target user client.
ver - Server push message command version. cmd - Server push message command ID. message - Message byte array to be sent as a push. reliable - If true, UDP will become RUDP.
func (user *User) ServerRespond(message []byte, ver uint8, cmd uint16, status uint8, reliable bool)
ServerRespond sends a packet as a response w/ ver and cmd of your choice
message - Message byte array to be sent as a response. ver - Server response command version. cmd - Server response command ID. status - Server response status. Status consts are available in server package. server.Ok (success), server.Bad (error caused by the user), server.Err (error caused by the server). reliable - If true, UDP will become RUDP.
func (user *User) Set(key string, value interface{})
Set stores a value along with the key (SID): the value will remain stored until user object is discarded
[NOTE] Uses mutex lock internally.
User data must be handled by the user and nobody else, so no race condition....
[IMPORTANT] value does NOT support struct. If you need to store a structured data, please use map instead. [IMPORTANT] If you store complex data types such as maps etc all value type will be stored as interface{}. key string - Key of the user property to be set. value interface{} - Value of the user property to be set.
func (user *User) SetLatency(clientTime int64)
SetLatency [INTERNAL USE ONLY]
func (user *User) Update(key string, op func(data interface{}) interface{})
Update allows a custom operation function to "update" user data by its key
[NOTE] Uses mutex lock internally. [IMPORTANT] value does NOT support struct. If you need to store a structured data, please use map instead. [IMPORTANT] If you store complex data types such as maps etc all value type will be stored as interface{}.
Parameters
key string - Key of the user property to be updated. op func(targetPropertyValue interface{}) - Operation function to handle the property update.