Slave server class.

Note: API in this class is not thread-safe and intended to be called only from the main worker thread with the one exception: work().

Constructor

new (metav:MetaServer, idv:Int)

Fields

read only cacheConnection:CacheConnection

Connection to cache server.

read only cacheManager:CacheManager

Data blocks cache manager.

read only clientTimer:ClientTimer

Client timer for recurring tasks.

read only clients:List<ClientInfo>

Clients list. Do not add or remove clients directly.

read only clientsByID:Map<Int, ClientInfo>

Clients map. Do not add or remove clients directly.

read only config:Config

Server configuration file access.

read only configCompression:Int

Cached configuration variable. Enables message compression.

read only configCompressionLevel:Int

Cached configuration variable. Message compression level (1-9).

read only configLogTime:Bool

Cached configuration variable. If set to true, will log time spent on each client request.

read only coreModuleParams:_CoreModuleParams

Core module parameters.

coreTableModule:TableModuleCore

Static tables module.

coreVarsModule:VarsModuleCore

Game variables module.

read only id:Int

Local server ID.

read only localeManager:LocaleManager

Localization manager.

read only name:String

Local server name.

read only profiler:Profiler

Server profiler.

read only serverType:String

Server type. Sent to cache server on server login.

slaveID:Int

Unique server ID on cache server. This variable is set on server login.

read only temp:Temp

Temp directory reader/writer.

read only timer:Timer

Timer for running recurring tasks.

read only url:URLRequester

Threaded HTTP request manager.

Methods

inline addModule (m:Dynamic):Void

Adds initialized module to list.

Parameters:

m

Server module.

inline addNoLoginRequests (list:Array<String>):Void

Client request types that are given as a parameter will not require client login to work.

Parameters:

list

Client request types in the <module>.<name> format.

inline cacheRequest (params:Dynamic):Dynamic

Sends request to cache server and waits for response.

Parameters:

params

Request parameters. Should have the _type field that sets the request type in the <module>.<method> format.

Returns:

Cache server response parameters.

inline disconnect (c:ClientInfo):Void

Immediately disconnects a client and triggers disconnect hooks.

Parameters:

c

Client.

getClientByConnectionID (id:Int):ClientInfo

Returns client by his connection id.

Parameters:

id

Connection ID.

Returns:

Client. Returns null, if no such client is online.

getClientInternal (id:Int, ?onlineOnly:Bool):ClientInfo

Returns a client from a list of clients. Can create and populate the client instance with createClientFromCache() if no such client is online.

Note: You can declare getClient() method on top of that to return the proper client class.

public inline function getClient(id: Int, ?onlineOnly: Bool): MyClient
  {
    return cast getClientInternal(id, onlineOnly);
  }

Parameters:

id

Client ID.

onlineOnly

If true, will not try to create a new client when needed. If false or null, will create a new client instance when client is not logged on and return it. The new client instance is not added to client list.

getModule (name:String):Dynamic

Returns server module by its name.

Parameters:

name

Module name.

Returns:

Module. Null, if no such module found.

getSubscribedMethods (tag:String):Null<List<Dynamic>>

Returns a list of subscribed methods for a given event type. Module subscription API is described in detail in Subscriptions article.

Parameters:

tag

Event type.

Returns:

List of methods subscribed to this event type.

getSubscribedModules (tag:String):Null<List<Module<ClientInfo, Server>>>

Returns a list of subscribed modules for a given event type. Module subscription API is described in detail in Subscriptions article.

Parameters:

tag

Event type.

Returns:

List of modules subscribed to this event type.

dynamic initModules ():Void

Initializes project server modules. This method is called from initServer(). Intended to call addModule() or loadModules().

loadModules (list:Array<Dynamic>):Void

Loads a list of server modules. Should be called in initModules().

Parameters:

list

List of module classes to load.

inline log (type:String, s:String, ?logEx:Bool):Void

Adds a typed message to server log.

Parameters:

type

Message type.

s

Message to log.

logEx

If true, also logs call stack.

migrateClient (client:ClientInfo, serverID:Int, params:Dynamic, func:String):Void

Starts migrating this client instance from this server to a server with a given ID belonging to the same metaserver.

If the client has already logged in, this process will logout the client from the old server freeing all of the user blocks and calling all of the logout hooks without notifying him and then login him into the new server fully reinitializing the client instance (getting user blocks, etc). The client will not receive a "user.login" notification and the client socket will be left open. All client variables will be lost during the transition.

If the client has not yet logged in, this process will move the client to the new server and execute the callback.

Note: Be careful with client migration calls since the logout and consequent relogin use a lot of resources. Do not do multiple migration calls for the same client in a very quick succession. That might result in dropped client messages.

Note: The client message handling order of the messages that accumulated during the migration process is not guaranteed. If the migration process is started from the client message handling method (called from Module.call()), the client response from that method will be ignored due to the migration.

Parameters:

client

Client instance.

serverID

Local slave server ID.

params

Callback arguments objects. Can be null.

func

Callback function name to run on destination server after the migration is completed. Should be in a standard "module.method" format. Accepts two arguments: new client instance and client parameters. Can be null.

nextID (t:String):Int

Returns next available row ID from sequence for given database table.

Parameters:

t

Database table name.

Returns:

Next available row ID.

notify (id:Int, msg:Dynamic):String

Sends message through cache server to a game server that this client is logged on. If the client is on the same server, the message stays local. If the client is offline, the message is sent to first game server and the client will be initialized from database and marked as offline. You can add validation parameters to message as described in _NotifyValidateParams. Server notifications are described in detail in Notifications article.

Usage cheatsheet:

// client 1 on server 1 gives money to client 2 on server 2
server.notify(id, { // client 2 ID
  // method to call (called on server 2):
  // UserModule.giveMoney2(c2: Client, msg: { from: Int, amount: Int })
  _type: 'user.giveMoney2',

  // client 2 ID
  _id: id,

  // client 2 connection ID (will be used instead of client ID if set)
  _connectionID: connectionID,

  _validate: {
    // validation function (called on server 2):
    // UserModule.giveMoneyValidate(c2: Client, msg: { from: Int, amount: Int })
    // should return `{ errorCode: 'ok', ... }` on success
    func: 'user.giveMoneyValidate',

    // success handler (called on server 1):
    // UserModule.giveMoney3(c: Client, msg: { from: Int, amount: Int, ... })
    // msg has original call parameters + parameters returned by giveMoney2()
    // can make a response to client 1
    onSuccess: 'user.giveMoney3',

    // failure handler (called on server 1):
    // UserModule.giveMoneyFail(c: Client, msg: { from: Int, amount: Int })
    // can make a response to client 1
    onFail: 'user.giveMoneyFail',

    // client 1 ID
    id: fromID

    // client 1 connection ID (will be used instead of client ID if set)
    connectionID: fromConnectionID
    },

  // notification parameters
  from: fromID,
  amount: amount
  });

Parameters:

id

Client ID.

msg

Message parameters. Can have _validate field with validation parameters. If the parameters object has "_id" field, the notification handler will be passed ClientInfo object with this client ID as an argument.

Returns:

Operation error code. noSlavesOnline - no game slave server. ok - operation successful.

dynamic onJoinNotify (client:ClientInfo):Void

Event hook. Notifies all clients that new client has joined with user.join messages by default. The message have a single name parameter with the new client name.

Parameters:

client

Client.

inline pending (q:String):Void

Sends SQL query to cache server for execution and immediately returns.

Parameters:

q

SQL query string.

inline print (s:String):Void

Prints a string to stdout.

Parameters:

s

String to output.

inline query (q:String):List<Dynamic>

Runs SQL query through cache server in database. When the server runs in uniserver mode it will use local database connection instead.

Parameters:

q

SQL query string.

Returns:

A list of results if the query is of SELECT type. Null otherwise.

inline queryTrace (q:String):List<Dynamic>

Runs SQL query through cache server in database and logs it in trace log. When the server runs in uniserver mode it will use local database connection instead.

Parameters:

q

SQL query string.

Returns:

A list of results if the query is of SELECT type. Null otherwise.

inline quote (s:String):String

Escapes a string for using in SQL query as a row field.

Parameters:

s

String.

Returns:

Escaped string.

sendTo (id:Int, msg:Dynamic):String

Sends message to a client that is logged on a different game server.

Note: If client is on the same server, sends message directly.

Parameters:

id

Client ID.

msg

Message parameters. Message type must be given in a _type field.

Returns:

Operation error code. userNotOnline - user is not online. ok - operation successful.

inline stats (s:String):Void

Adds a stats message to stats log.

Parameters:

s

Message to log.

subscribeMethod (tag:String, method:Dynamic):Void

Subscribes method to an event. Module subscription API is described in detail in Subscriptions article.

Parameters:

tag

Event type.

method

Method.

subscribeModule (tag:String, module:Dynamic):Void

Subscribes module to an event. Module subscription API is described in detail in Subscriptions article.

Parameters:

tag

Event type.

module

Server module.

inline work (id:String, f:Void ‑> Void, ?logMsg:String):Void

Sends task to worker thread. The method given as an argument will be called in main worker thread. This method is thread-safe.

Parameters:

id

Task string ID. Used with extended profiling.

f

Method to call.

logMsg

Optional log message.

inline workExt (o:_WorkParams):Void

Sends task to worker thread. The method given as an argument will be called in main worker thread. This method is thread-safe.

Parameters:

id

Worker task parameters object.