request_client
简介
基于本pkg,可以快速开发对接各类公共服务器的pkg,例如pkg.social_client,pkg.auth_client等等。
信息
/pkg/request_client.gs一般作为组件使用,继承该组件用于创建连接各类公共服务器的客户端对象,例如/pkg/social_client/SocialClient.gs、/pkg/auth_client/AuthClient.gs等。
构造函数para参数字段的说明
| 字段 | 说明 | 是否必有 | 
|---|---|---|
| host或者ip | 主机地址 | Y | 
| port | 主机端口 | Y | 
| using_websocket | 是否使用websocket协议 | N;缺省值为false | 
| port_recv_size | 网络接收缓冲区大小,使用gs协议时起效 | N;缺省值为0 | 
| max_send_size | 网络允许发送的数据包大小,使用gs协议时起效 | N;缺省值为0 | 
| max_pending_count | 请求队列允许积压的阈值,超过该值时发送请求功能将暂时变得不可能直至恢复 | N;缺省值为0 | 
| keep_alive | 心跳间隔 | N;缺省值为60 | 
| auth_connection_callback | 连接主机成功后执行的登录主机验证的回调函数 | N;一般是在回调函数里发送验证消息 | 
| cookieless_msg_func | 本连接接收主机发送的无cookie消息的处理函数 | N;缺省值为nil,丢弃主机发送的无cookie消息 | 
| cookieless_msg_queue | 本连接接收主机发送的无cookie消息的队列 | N;缺省值为nil,丢弃主机发送的无cookie消息 | 
| proxy_connection_callback | 用于生成代理连接的生成回调 | N;缺省值为nil,不使用代理连接 | 
| request_timeout_seconds | 默认请求超时时间 | N;缺省值为30 | 
组件接口
request_client.gs
| 函数原型 | 函数作用 | 
|---|---|
| void enable_debug(bool enable) | 开启/关闭调试信息(显示收发消息) | 
| bool send(string cmd, map args = ) | 向目标主机发送指令(进队列),不等待指令结果 | 
| mixed send_request(string cmd, map args = , int timeout_seconds = 15) | 向目标主机发送指令(进队列),并且获取指令结果 | 
| bool send_priority(string cmd, map args = ) | 优先向目标主机发送指令(不进队列),不等待指令结果 | 
| mixed send_request_priority(string cmd, map args = , int timeout_seconds = 15) | 优先向主机发送指令(不进队列),并且获取指令结果 | 
| void set_auth_connection_callback(function fn) | 设置连接建立后验证登录的会调函数 | 
| bool is_connected() | 是否已经连接主机(包括已经连接并且通过身份认证) | 
send_scheduler.gs
发送调度器
目的是减少协程数量
| 函数原型 | 函数作用 | 
|---|---|
| void add_client(object client_ob) | 添加客户端对象 | 
| void remove_client(object client_ob) | 移除客户端对象 | 
样例
// SocialClient.gs
// created by linlc may/20/2021
//
#pragma parallel
import gs.lang.*;
import gs.util.*;
component pkg.request_client;
// 设置
readonly map _config := {};
void create(map para = nil)
{
    if (is_static_object())
        return;
    _config := para;
    assert(_config["account"], "Missing field in para: account");
    assert(_config["server_id"], "Missing field in para: server_id");
    assert(_config["auth_account_para"], "Missing field in para: auth_account_para");
    set_auth_connection_callback((: auth_connection_callback :));
}
// 验证自己
public bool auth_account(map auth_args)
{
    map args = {
        "account" : _config["account"],
        "server_id" : _config["server_id"],
    } << auth_args;
    mixed auth_result = send_request("cmd_auth_account", args);
    if (! auth_result || auth_result["error"])
        return false;
    return true;
}
public bool ping()
{
    map args = {
    };
    map result = send_request("cmd_ping", args, 10);
    if (! result || result["error"])
        return false;
    return true;
}
public bool register_as_auth_agent(map agent_info)
{
    map args = {
        "server_id" : _config["server_id"],
        "agent_info" : agent_info,
    };
    mixed register_result = send_request("cmd_auth_agent_register", args);
    if (! register_result || register_result["error"])
        return false;
    return true;
}
public string get_name()
{
    return _config["account"];
}
public mixed get_server_id()
{
    return _config["server_id"];
}
mixed auth_connection_callback(object c)
{
    map auth_para = {
        "account" : _config["account"],
        "server_id" : _config["server_id"],
    } + _config["auth_account_para"];
    mixed auth_result = c.send_request_priority("cmd_auth_account", auth_para);
    if (! auth_result || auth_result["error"])
        return false;
    if (_config["act_as_agent"])
    {
        map register_args = {
            "server_id" : _config["server_id"],
            "agent_info" : _config["act_as_agent"],
        };
        mixed register_result = c.send_request_priority(conn, "cmd_auth_agent_register", register_args);
        if (! register_result || register_result["error"])
            return false;
    }
    return true;
}
// social_client.gs
// created by linlc oct/30/2020
// social_server连接客户端
#pragma parallel
import gs.lang.*;
import gs.util.*;
public object create_client(map para)
{
    object ob = new_object(__DIR__ "SocialClient.gs", this_domain(), para);
    return ob;
}