Skip to content

Commit 57db1c2

Browse files
committed
update dependency - bug fix parse_url
- `c-raii` added better `getopt` command line handling, update `fetch.c` example with so.
1 parent 2c0dc8b commit 57db1c2

5 files changed

Lines changed: 54 additions & 28 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
3030
find_package(raii QUIET)
3131
if(NOT raii_FOUND)
3232
FetchContent_Declare(raii
33-
URL https://github.com/zelang-dev/c-raii/archive/refs/tags/2.2.0.zip
34-
URL_MD5 d6909031ab3bd47cdbd81a95c7eb4458
33+
URL https://github.com/zelang-dev/c-raii/archive/refs/tags/2.2.1.zip
34+
URL_MD5 01aca7e52b6287ee50ec3e8060c3968c
3535
)
3636
FetchContent_MakeAvailable(raii)
3737
endif()

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,19 @@ which call this function as an coroutine! */
339339
C_API int uv_main(int, char **);
340340

341341
C_API uv_loop_t *asio_loop(void);
342+
343+
/* Same as `sleepfor`, but uses `uv_timer_start` to explicitly give up the current `coroutine`
344+
for at least `ms` milliseconds. Other tasks continue to run during this time. */
342345
C_API u32 delay(u32 ms);
343346

347+
/**
348+
* Creates an `this` instance for `data` object by `asio_types`
349+
* on current `coroutine` `user_data`.
350+
*
351+
* WILL `panic` ~logic_error~, if `user_data` not empty.
352+
*/
353+
C_API void uv_this(void_t *data, asio_types type);
354+
344355
C_API string fs_readfile(string_t path);
345356
C_API int fs_writefile(string_t path, string_t text);
346357

docs/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,19 @@ which call this function as an coroutine! */
339339
C_API int uv_main(int, char **);
340340

341341
C_API uv_loop_t *asio_loop(void);
342+
343+
/* Same as `sleepfor`, but uses `uv_timer_start` to explicitly give up the current `coroutine`
344+
for at least `ms` milliseconds. Other tasks continue to run during this time. */
342345
C_API u32 delay(u32 ms);
343346

347+
/**
348+
* Creates an `this` instance for `data` object by `asio_types`
349+
* on current `coroutine` `user_data`.
350+
*
351+
* WILL `panic` ~logic_error~, if `user_data` not empty.
352+
*/
353+
C_API void uv_this(void_t *data, asio_types type);
354+
344355
C_API string fs_readfile(string_t path);
345356
C_API int fs_writefile(string_t path, string_t text);
346357

examples/fetch.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,37 @@
22

33
int uv_main(int argc, char **argv) {
44
yield();
5-
if (argc <= 1) {
6-
cerr("Usage: fetch url"CLR_LN);
7-
return 1;
8-
}
9-
10-
string data = nullptr;
11-
int chunks = 0;
12-
url_t *url = parse_url(argv[1]);
13-
if (!is_empty(url)) {
14-
dnsinfo_t *dns = get_addrinfo(url->host, url->scheme, 3,
15-
kv(ai_flags, AF_UNSPEC),
16-
kv(ai_socktype, SOCK_STREAM),
17-
kv(ai_protocol, IPPROTO_TCP)
18-
);
5+
cli_message_set("\turl - website\n", false);
6+
if (is_cli_getopt(nullptr, true)) {
7+
string data = nullptr;
8+
int chunks = 0;
9+
url_t *url = parse_url(cli_getopt());
10+
if (!is_empty(url)) {
11+
dnsinfo_t *dns = get_addrinfo(url->host, url->scheme, 3,
12+
kv(ai_flags, AF_UNSPEC),
13+
kv(ai_socktype, SOCK_STREAM),
14+
kv(ai_protocol, IPPROTO_TCP)
15+
);
1916

20-
use_ca_certificate("cert.pem");
21-
uv_stream_t *client = stream_secure(addrinfo_ip(dns), url->host, url->port);
22-
if (!is_empty(client) && stream_write(client, "GET /"CRLF)) {
23-
cout(CLR_LN);
24-
while (stream_peek(client) != UV_EOF) {
25-
if (!is_empty(data = stream_read(client)))
26-
cout("%s", data);
27-
else
28-
break;
17+
use_ca_certificate("cert.pem");
18+
uv_stream_t *client = stream_secure(addrinfo_ip(dns), url->host, url->port);
19+
if (!is_empty(client) && stream_write(client, "GET /"CRLF)) {
20+
cout(CLR_LN);
21+
while (stream_peek(client) != UV_EOF) {
22+
if (!is_empty(data = stream_read(client)))
23+
cout(data);
24+
else
25+
break;
2926

30-
chunks++;
27+
chunks++;
28+
}
3129
}
30+
31+
cout("\n\nReceived: %d chunks.\n", chunks);
32+
} else {
33+
return is_cli_getopt("help", false);
3234
}
3335
}
3436

35-
cout("\n\nReceived: %d chunks.\n", chunks);
3637
return coro_err_code();
3738
}

include/asio.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ C_API bool is_nameinfo(void_t);
573573
/* This library provides its own ~main~,
574574
which call this function as an coroutine! */
575575
C_API int uv_main(int, char **);
576+
577+
/* Same as `sleepfor`, but uses `uv_timer_start` to explicitly give up the current `coroutine`
578+
for at least `ms` milliseconds. Other tasks continue to run during this time. */
576579
C_API u32 delay(u32 ms);
577580

578581
typedef struct {
@@ -588,7 +591,7 @@ C_API uv_tls_t *get_handle_tls_socket(void_t);
588591
C_API sockaddr_t *sockaddr(string_t host, int port);
589592

590593
/**
591-
* Creates an `this` instance for `data` `asio_types`
594+
* Creates an `this` instance for `data` object by `asio_types`
592595
* on current `coroutine` `user_data`.
593596
*
594597
* WILL `panic` ~logic_error~, if `user_data` not empty.

0 commit comments

Comments
 (0)