10000 net, refactor: support timeout in Socket.connect. · fibjs/fibjs@66e8ea3 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 66e8ea3

Browse files
committed
net, refactor: support timeout in Socket.connect.
1 parent 36d0d32 commit 66e8ea3

File tree

16 files changed

+52
-41
lines changed

16 files changed

+52
-41
lines changed

fibjs/include/Socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Socket : public Socket_base {
6161
virtual result_t get_localPort(int32_t& retVal);
6262
virtual result_t get_timeout(int32_t& retVal);
6363
virtual result_t set_timeout(int32_t newVal);
64-
virtual result_t connect(exlib::string host, int32_t port, AsyncEvent* ac);
64+
virtual result_t connect(exlib::string host, int32_t port, int32_t timeout, AsyncEvent* ac);
6565
virtual result_t bind(exlib::string addr, int32_t port, bool allowIPv4);
6666
virtual result_t bind(int32_t port, bool allowIPv4);
6767
virtual result_t listen(int32_t backlog);

fibjs/include/UVSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UVSocket : public UVStream_tmpl<Socket_base> {
3434
virtual result_t get_remotePort(int32_t& retVal);
3535
virtual result_t get_localAddress(exlib::string& retVal);
3636
virtual result_t get_localPort(int32_t& retVal);
37-
virtual result_t connect(exlib::string host, int32_t port, AsyncEvent* ac);
37+
virtual result_t connect(exlib::string host, int32_t port, int32_t timeout, AsyncEvent* ac);
3838
virtual result_t bind(exlib::string addr, int32_t port, bool allowIPv4);
3939
virtual result_t bind(int32_t port, bool allowIPv4);
4040
virtual result_t listen(int32_t backlog);

fibjs/include/UVStream.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ class UVStream_tmpl : public T {
3030
}
3131
}
3232

33+
UVTimeout(UVStream_tmpl* _this, int32_t timeout)
34+
: m_this(_this)
35+
, m_timeout(timeout)
36+
{
B41A 37+
if (m_timeout > 0) {
38+
uv_timer_init(s_uv_loop, this);
39+
uv_timer_start(this, on_timeout, m_timeout, 0);
40+
}
41+
}
42+
3343
virtual ~UVTimeout()
3444
{
3545
}

fibjs/include/ifs/Socket.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Socket_base : public Stream_base {
3333
virtual result_t get_localPort(int32_t& retVal) = 0;
3434
virtual result_t get_timeout(int32_t& retVal) = 0;
3535
virtual result_t set_timeout(int32_t newVal) = 0;
36-
virtual result_t connect(exlib::string host, int32_t port, AsyncEvent* ac) = 0;
36+
virtual result_t connect(exlib::string host, int32_t port, int32_t timeout, AsyncEvent* ac) = 0;
3737
virtual result_t bind(int32_t port, bool allowIPv4) = 0;
3838
virtual result_t bind(exlib::string addr, int32_t port, bool allowIPv4) = 0;
3939
virtual result_t listen(int32_t backlog) = 0;
@@ -62,7 +62,7 @@ class Socket_base : public Stream_base {
6262
static void s_send(const v8::FunctionCallbackInfo<v8::Value>& args);
6363

6464
public:
65-
ASYNC_MEMBER2(Socket_base, connect, exlib::string, int32_t);
65+
ASYNC_MEMBER3(Socket_base, connect, exlib::string, int32_t, int32_t);
6666
ASYNC_MEMBERVALUE1(Socket_base, accept, obj_ptr<Socket_base>);
6767
ASYNC_MEMBERVALUE2(Socket_base, recv, int32_t, obj_ptr<Buffer_base>);
6868
ASYNC_MEMBER1(Socket_base, send, Buffer_base*);
@@ -217,15 +217,16 @@ inline void Socket_base::s_connect(const v8::FunctionCallbackInfo<v8::Value>& ar
217217
ASYNC_METHOD_INSTANCE(Socket_base);
218218
METHOD_ENTER();
219219

220-
ASYNC_METHOD_OVER(2, 1);
220+
ASYNC_METHOD_OVER(3, 2);
221221

222222
ARG(exlib::string, 0);
223-
OPT_ARG(int32_t, 1, 0);
223+
ARG(int32_t, 1);
224+
OPT_ARG(int32_t, 2, 0);
224225

225226
if (!cb.IsEmpty())
226-
hr = pInst->acb_connect(v0, v1, cb, args);
227+
hr = pInst->acb_connect(v0, v1, v2, cb, args);
227228
else
228-
hr = pInst->ac_connect(v0, v1);
229+
hr = pInst->ac_connect(v0, v1, v2);
229230

230231
METHOD_VOID();
231232
}

fibjs/src/db/redis/Redis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ result_t Redis::connect(const char* host, int32_t port, AsyncEvent* ac)
6161

6262
m_subMode = 0;
6363

64-
return m_sock->connect(host, port, ac);
64+
return m_sock->connect(host, port, 0, ac);
6565
}
6666

6767
#define REDIS_MAX_LINE 1024

fibjs/src/http/HttpClient.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ result_t HttpClient::request(exlib::string method, obj_ptr<Url>& u, SeekableStre
661661
ON_STATE(asyncRequest, socks_hello)
662662
{
663663
obj_ptr<Buffer_base> buf = new Buffer("\5\1\0", 3);
664+
665+
((Socket_base*)(Stream_base*)m_conn)->set_timeout(m_hc->m_timeout);
664666
return m_conn->write(buf, next(socks_hello_response));
665667
}
666668

@@ -745,6 +747,7 @@ result_t HttpClient::request(exlib::string method, obj_ptr<Url>& u, SeekableStre
745747

746748
ON_STATE(asyncRequest, ssl_connect)
747749
{
750+
((Socket_base*)(Stream_base*)m_conn)->set_timeout(m_hc->m_timeout);
748751
return m_hc->request(m_conn, m_reqConn, m_retVal, next(ssl_handshake));
749752
}
750753

@@ -795,6 +798,9 @@ result_t HttpClient::request(exlib::string method, obj_ptr<Url>& u, SeekableStre
795798

796799
ON_STATE(asyncRequest, connected)
797800
{
801+
if (!m_ssl)
802+
((Socket_base*)(Stream_base*)m_conn)->set_timeout(m_hc->m_timeout);
803+
798804
return m_hc->request(m_conn, m_req, m_response_body, m_retVal, next(requested));
799805
}
800806

fibjs/src/net/Socket.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ result_t Socket::listen(int32_t backlog)
275275
return 0;
276276
}
277277

278-
result_t Socket::connect(exlib::string host, int32_t port, AsyncEvent* ac)
278+
result_t Socket::connect(exlib::string host, int32_t port, int32_t timeout, AsyncEvent* ac)
279279
{
280280
#ifdef _WIN32
281281
if (!m_bBind) {
@@ -286,8 +286,8 @@ result_t Socket::connect(exlib::string host, int32_t port, AsyncEvent* ac)
286286
#endif
287287

288288
obj_ptr<Timer> timer;
289-
if (ac->isAsync() && m_timeout > 0) {
290-
timer = new IOTimer(m_timeout, this);
289+
if (ac->isAsync() && timeout > 0) {
290+
timer = new IOTimer(tim 10000 eout, this);
291291
timer->sleep();
292292
}
293293

fibjs/src/net/Socket_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace socket {
3737

3838
assert(!Runtime::check());
3939

40-
result_t hr = ((Socket_base*)sock)->cc_connect(host, port);
40+
result_t hr = ((Socket_base*)sock)->cc_connect(host, port, 0);
4141
if (hr < 0) {
4242
Runtime::setError(hr);
4343
return 0;

fibjs/src/net/UVSocket.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ result_t UVSocket::listen(int32_t backlog)
187187
});
188188
}
189189

190-
result_t UVSocket::connect(exlib::string host, int32_t port, AsyncEvent* ac)
190+
result_t UVSocket::connect(exlib::string host, int32_t port, int32_t timeout, AsyncEvent* ac)
191191
{
192192
class AsyncConnect : public uv_connect_t,
193193
public UVTimeout {
194194
public:
195-
AsyncConnect(UVSocket* pThis, AsyncEvent* ac)
196-
: UVTimeout(pThis)
195+
AsyncConnect(UVSocket* pThis, int32_t timeout, AsyncEvent* ac)
196+
: UVTimeout(pThis, timeout)
197197
, m_ac(ac)
198198
{
199199
}
@@ -215,7 +215,7 @@ result_t UVSocket::connect(exlib::string host, int32_t port, AsyncEvent* ac)
215215

216216
if (m_family == net_base::C_AF_UNIX) {
217217
return uv_async([&] {
218-
uv_pipe_connect(new AsyncConnect(this, ac), &m_pipe, host.c_str(), AsyncConnect::callback);
218+
uv_pipe_connect(new AsyncConnect(this, timeout, ac), &m_pipe, host.c_str(), AsyncConnect::callback);
219219
return 0;
220220
});
221221
} else {
@@ -234,7 +234,7 @@ result_t UVSocket::connect(exlib::string host, int32_t port, AsyncEvent* ac)
234234
}
235235

236236
return uv_async([&] {
237-
return uv_tcp_connect(new AsyncConnect(this, ac), &m_tcp, (sockaddr*)&addr_info, AsyncConnect::callback);
237+
return uv_tcp_connect(new AsyncConnect(this, timeout, ac), &m_tcp, (sockaddr*)&addr_info, AsyncConnect::callback);
238238
});
239239
}
240240
}

fibjs/src/net/net.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,17 @@ result_t net_base::connect(exlib::string url, int32_t timeout, obj_ptr<Stream_ba
179179
if (hr < 0)
180180
return hr;
181181

182-
socket->set_timeout(timeout);
183-
184182
retVal = socket;
185-
return socket->connect(u->m_hostname, nPort, ac);
183+
return socket->connect(u->m_hostname, nPort, timeout, ac);
186184
} else {
187185
obj_ptr<Socket_base> socket;
188186

189187
result_t hr = Socket_base::_new(net_base::C_AF_UNIX, socket);
190188
if (hr < 0)
191189
return hr;
192190

193-
socket->set_timeout(timeout);
194-
195191
retVal = socket;
196-
return socket->connect(url.substr(5), 0, ac);
192+
return socket->connect(url.substr(5), 0, timeout, ac);
197193
}
198194
}
199195

fibjs/src/ssl/ssl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ result_t ssl_base::connect(exlib::string url, int32_t verification, X509Cert_bas
6464
ON_STATE(asyncConnect, connect)
6565
{
6666
Socket_base::_new(m_ipv6 ? net_base::C_AF_INET6 : net_base::C_AF_INET, m_sock);
67-
68-
m_sock->set_timeout(m_timeout);
69-
return m_sock->connect(m_host, m_port, next(handshake));
67+
return m_sock->connect(m_host, m_port, m_timeout, next(handshake));
7068
}
7169

7270
ON_STATE(asyncConnect, handshake)

idl/zh-cn/Socket.idl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ interface Socket : Stream
3232

3333
/*! @brief 建立一个 tcp 连接
3434
@param host 指定对方地址或主机名,也可以指向 unix socket 和 Windows pipe 路径
35-
@param port 指定对方端口,连接 unix socket 和 Windows pipe 时,忽略此参数
35+
@param port 指定对方端口,连接 unix socket 和 Windows pipe 时,此参数需要为 0
36+
@param timeout 指定超时时间,单位是毫秒,默认为 0
3637
*/
37-
connect(String host, Integer port = 0) async;
38+
connect(String host, Integer port, Integer timeout = 0) async;
3839

3940
/*! @brief 将当前 Socket 绑定至本地所有地址的指定端口
4041
@param port 指定绑定的端口

idl/zh-cn/net.idl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module net
5151

5252
/*! @brief 创建一个 Socket 或 SslSocket 对象并建立连接
5353
@param url 指定连接的协议,可以是:tcp://host:port 或者 ssl://host:port,也可以是:unix:/usr/local/proc1 或者 pipe://./pipe/proc1,连接 pipe 时需要用 `/` 替换 `\`
54-
@param timeout 指定超时时间,单位是毫秒,默认为0
54+
@param timeout 指定超时时间,单位是毫秒,默认为 0
5555
@return 返回连接成功的 Socket 或者 SslSocket 对象
5656
*/
5757
static Stream connect(String url, Integer timeout = 0) async;
@@ -61,7 +61,7 @@ module net
6161

6262
/*! @brief 创建一个 Smtp 对象并建立连接,参见 Smtp
6363
@param url 指定连接的协议,可以是:tcp://host:port 或者 ssl://host:port
64-
@param timeout 指定超时时间,单位是毫秒,默认为0
64+
@param timeout 指定超时时间,单位是毫秒,默认为 0
6565
@return 返回连接成功的 Smtp 对象
6666
*/
6767
static Smtp openSmtp(String url, Integer timeout = 0) async;

npm/types/dts/interface/Socket.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ declare class Class_Socket extends Class_Stream {
5151
/**
5252
* @description 建立一个 tcp 连接
5353
* @param host 指定对方地址或主机名,也可以指向 unix socket 和 Windows pipe 路径
54-
* @param port 指定对方端口,连接 unix socket 和 Windows pipe 时,忽略此参数
54+
* @param port 指定对方端口,连接 unix socket 和 Windows pipe 时,此参数需要为 0
55+
* @param timeout 指定超时时间,单位是毫秒,默认为 0
5556
*
5657
*/
57-
connect(host: string, port?: number): void;
58+
connect(host: string, port: number, timeout?: number): void;
5859

59-
connect(host: string, port?: number, callback?: (err: Error | undefined | null)=>any): void;
60+
connect(host: string, port: number, timeout?: number, callback?: (err: Error | undefined | null)=>any): void;
6061

6162
/**
6263
* @description 将当前 Socket 绑定至本地所有地址的指定端口

npm/types/dts/module/net.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ declare module 'net' {
8585
/**
8686
* @description 创建一个 Socket 或 SslSocket 对象并建立连接
8787
* @param url 指定连接的协议,可以是:tcp://host:port 或者 ssl://host:port,也可以是:unix:/usr/local/proc1 或者 pipe://./pipe/proc1,连接 pipe 时需要用 `/` 替换 `\`
88-
* @param timeout 指定超时时间,单位是毫秒,默认为0
88+
* @param timeout 指定超时时间,单位是毫秒,默认为 0
8989
* @return 返回连接成功的 Socket 或者 SslSocket 对象
9090
*
9191
*/
@@ -101,7 +101,7 @@ declare module 'net' {
101101
/**
102102
* @description 创建一个 Smtp 对象并建立连接,参见 Smtp
103103
* @param url 指定连接的协议,可以是:tcp://host:port 或者 ssl://host:port
104-
* @param timeout 指定超时时间,单位是毫秒,默认为0
104+
* @param timeout 指定超时时间,单位是毫秒,默认为 0
105105
* @return 返回连接成功的 Smtp 对象
106106
*
107107
*/

test/net_test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,12 @@ function test_net(eng, use_uv) {
444444

445445
var c1 = new net.Socket();
446446

447-
c1.timeout = 300;
448-
449447
test_util.gc();
450448

451449
c1.connect('127.0.0.1', _port);
452450

453451
var t1 = new Date();
452+
c1.timeout = 300;
454453
assert.throws(() => {
455454
c1.recv();
456455
});
@@ -461,10 +460,9 @@ function test_net(eng, use_uv) {
461460
assert.lessThan(t2 - t1, 1000);
462461

463462
var c2 = new net.Socket();
464-
c2.timeout = 300;
465463
var t1 = new Date();
466464
assert.throws(() => {
467-
c2.connect('192.166.166.166', 8086 + base_port);
465+
c2.connect('192.166.166.166', 8086 + base_port, 300);
468466
});
469467
var t2 = new Date();
470468

@@ -661,7 +659,7 @@ function test_net(eng, use_uv) {
661659

662660
function conn_socket() {
663661
var s1 = new net.Socket(net.AF_UNIX);
664-
s1.connect(_path);
662+
s1.connect(_path, 0);
665663
s1.send(new Buffer("GET / HTTP/1.0"));
666664
assert.equal("GET / HTTP/1.0", s1.recv());
667665
s1.close();

0 commit comments

Comments
 (0)
0