16.2. Sending server data

The protocols also support sending data from the client to the server in a similar way. Usually this is used for the client to request data from the server.

To send data to the server, use the $world->sendserverdata function. This function takes one argument, which must be a hash reference pointing to a hash similar to %::server_data. The entries in the hash specify what is sent to the server: the protocol, the package/message (when supported by the protocol) and the data to send, which is encoded to the appropriate format.

For example, this is an example of sending a request over GMCP:

Example 16.1. A GMCP request
$world->sendserverdata({ protocol => 'GMCP',
                         package => 'Core',
                         subpackage => 'Supports',
                         message => 'Set',
                         data => [ "Char 1", "Char.Skills 1", "Char.Items 1" ]

16.2.1. MSDP data

It is possible to send data using the MSDP protocol with $world->sendserverdata, but it might not do what the server expects.

MSDP has two ways of sending arrays: one is an explicit array delimited by MSDP_ARRAY_OPEN and MSDP_ARRAY_CLOSE, and another where multiple values are sent for the same variable (in the specification, this way is only demonstrated in data sent by the client). In my opinion that second way should not exist, as it is unnecessary and possibly confusing. When receiving data, both formats are converted to a Perl list. When sending data with $world->sendserverdata, lists are always sent as an explicit array.

Servers, however, seem to want multi-valued variables, so there is a function specific to MSDP, shown in the example below:

Example 16.2. A MSDP request
$world->sendmsdpcommand('SEND', 'HEALTH', 'HEALTH_MAX', 'MANA', 'MANA_MAX');

The first argument is the name of the variable, the other arguments are values to be sent. This gets converted to the following message:

Example 16.3. Message sent via MSDP