proxy_client
简介
做为客户端接入proxy server
- 一般是玩家客户端(大部分情况下为非gs实现)才需要接入proxy server;
这个pkg主要是client gs使用,用来实现一些测试功能以及演示如何接入proxy server;
组件接口
ProxyClient.gs
接入proxy server的客户端连接对象
ProxyClient对象在创建时可以指定对象的所有者owner;owner对象将处理ProxyClient对象收到的消息(调用owner的perform_recv_in_co方法);
当owner对象未指定时(为nil),ProxyClient对象收到的消息将直接丢弃;
函数原型 | 函数作用 |
---|---|
bool connect(string host, int port, map connect_para = ) | 连接proxy server |
void send(string cmd, ...) | 向proxy server发送消息 |
proxy_client.gs
函数原型 | 函数作用 |
---|---|
object create_client(map para) | 创建一个到proxy server的客户端连接 |
样例
// 处理和proxy server连接的对象
import gs.lang.*;
import gs.util.*;
import pkg.proxy_client;
readonly object _client_ob := nil;
readonly queue _q := nil;
readonly coroutine _co := nil;
void create(map para = {})
{
if (is_static_object())
return;
_q := queue.create("");
_co := coroutine.create(nil, (: co_entry :));
// 创建一个到proxy server的连接对象
map proxy_client_para = {
"owner" : this,
};
_client_ob := proxy_client.create_client(proxy_client_para);
}
void destruct()
{
if (is_static_object())
return;
_q.send_dup(-1);
_client_ob?.close();
}
// 连接proxy server
public bool connect_proxy(string host, int port)
{
map connect_para = {
"port_recv_size" : 1024 * 128,
};
return _client_ob.connect(host, port, connect_para);
}
// 发送消息给proxy server,并等待结果
public mixed send_request(string cmd, map args, int timeout_seconds = 10)
{
queue rq = queue.create("", 1);
defer rq.close();
args["cookie"] = rq.handle_id();
_client_ob.send(cmd, args);
mixed result = rq.receive(timeout_seconds);
return result;
}
// 处理ProxyClient对象收到的消息
public parallel void perform_recv_in_co(mixed args)
{
let string cmd, map resp = args;
mixed response_cookie = resp["cookie"];
if (! response_cookie)
{
_q.send_dup(args);
return;
}
queue q = handle.find(response_cookie);
if (q)
q.send_dup(resp);
}
void co_entry()
{
defer {
_q.close();
};
while (true)
{
mixed msg = _q.receive();
if (msg == -1)
return;
printf("RECV: %M\n", msg);
}
}