Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
Fixed code to new API. Not tested yet, but compiles successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilm committed Sep 21, 2014
1 parent 14ac232 commit af22d7e
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 123 deletions.
17 changes: 10 additions & 7 deletions code/cgi/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -7,8 +8,8 @@ uv_loop_t *loop;
uv_process_t child_req;
uv_process_options_t options;

void cleanup_handles(uv_process_t *req, int exit_status, int term_signal) {
fprintf(stderr, "Process exited with status %d, signal %d\n", exit_status, term_signal);
void cleanup_handles(uv_process_t *req, int64_t exit_status, int term_signal) {
fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
uv_close((uv_handle_t*) req->data, NULL);
uv_close((uv_handle_t*) req, NULL);
}
Expand Down Expand Up @@ -38,8 +39,9 @@ void invoke_cgi_script(uv_tcp_t *client) {
options.args = args;

child_req.data = (void*) client;
if (uv_spawn(loop, &child_req, options)) {
fprintf(stderr, "%s\n", uv_strerror(uv_last_error(loop)));
int r;
if ((r = uv_spawn(loop, &child_req, &options))) {
fprintf(stderr, "%s\n", uv_strerror(r));
return;
}
}
Expand All @@ -66,11 +68,12 @@ int main() {
uv_tcp_t server;
uv_tcp_init(loop, &server);

struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
uv_tcp_bind(&server, bind_addr);
struct sockaddr_in bind_addr;
uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0);
int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
if (r) {
fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
fprintf(stderr, "Listen error %s\n", uv_err_name(r));
return 1;
}
return uv_run(loop, UV_RUN_DEFAULT);
Expand Down
5 changes: 3 additions & 2 deletions code/detach/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ int main() {
options.args = args;
options.flags = UV_PROCESS_DETACHED;

if (uv_spawn(loop, &child_req, options)) {
fprintf(stderr, "%s\n", uv_strerror(uv_last_error(loop)));
int r;
if ((r = uv_spawn(loop, &child_req, &options))) {
fprintf(stderr, "%s\n", uv_strerror(r));
return 1;
}
fprintf(stderr, "Launched sleep with PID %d\n", child_req.pid);
Expand Down
29 changes: 15 additions & 14 deletions code/dns/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@

uv_loop_t *loop;

uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
return uv_buf_init((char*) malloc(suggested_size), suggested_size);
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}

void on_read(uv_stream_t *client, ssize_t nread, uv_buf_t buf) {
if (nread == -1) {
if (uv_last_error(loop).code != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(uv_last_error(loop)));
void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, NULL);
free(client);
return;
}

char *data = (char*) malloc(sizeof(char) * (nread+1));
data[nread] = '\0';
strncpy(data, buf.base, nread);
strncpy(data, buf->base, nread);

fprintf(stderr, "%s", data);
free(data);
free(buf.base);
free(buf->base);
}

void on_connect(uv_connect_t *req, int status) {
if (status == -1) {
fprintf(stderr, "connect failed error %s\n", uv_err_name(uv_last_error(loop)));
if (status < 0) {
fprintf(stderr, "connect failed error %s\n", uv_err_name(status));
free(req);
return;
}
Expand All @@ -39,8 +40,8 @@ void on_connect(uv_connect_t *req, int status) {
}

void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res) {
if (status == -1) {
fprintf(stderr, "getaddrinfo callback error %s\n", uv_err_name(uv_last_error(loop)));
if (status < 0) {
fprintf(stderr, "getaddrinfo callback error %s\n", uv_err_name(status));
return;
}

Expand All @@ -53,7 +54,7 @@ void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res) {
uv_tcp_init(loop, socket);

connect_req->data = (void*) socket;
uv_tcp_connect(connect_req, socket, *(struct sockaddr_in*) res->ai_addr, on_connect);
uv_tcp_connect(connect_req, socket, (const struct sockaddr*) res->ai_addr, on_connect);

uv_freeaddrinfo(res);
}
Expand All @@ -72,7 +73,7 @@ int main() {
int r = uv_getaddrinfo(loop, &resolver, on_resolved, "irc.freenode.net", "6667", &hints);

if (r) {
fprintf(stderr, "getaddrinfo call error %s\n", uv_err_name(uv_last_error(loop)));
fprintf(stderr, "getaddrinfo call error %s\n", uv_err_name(r));
return 1;
}
return uv_run(loop, UV_RUN_DEFAULT);
Expand Down
2 changes: 1 addition & 1 deletion code/idle-basic/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

int64_t counter = 0;

void wait_for_a_while(uv_idle_t* handle, int status) {
void wait_for_a_while(uv_idle_t* handle) {
counter++;

if (counter >= 10e6)
Expand Down
13 changes: 8 additions & 5 deletions code/idle-compute/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uv_fs_t stdin_watcher;
uv_idle_t idler;
char buffer[1024];

void crunch_away(uv_idle_t* handle, int status) {
void crunch_away(uv_idle_t* handle) {
// Compute extra-terrestrial life
// fold proteins
// computer another digit of PI
Expand All @@ -21,11 +21,13 @@ void on_type(uv_fs_t *req) {
if (stdin_watcher.result > 0) {
buffer[stdin_watcher.result] = '\0';
printf("Typed %s\n", buffer);
uv_fs_read(loop, &stdin_watcher, 1, buffer, 1024, -1, on_type);

uv_buf_t buf = uv_buf_init(buffer, 1024);
uv_fs_read(loop, &stdin_watcher, 1, &buf, 1, 0, on_type);
uv_idle_start(&idler, crunch_away);
}
else {
fprintf(stderr, "error opening file: %d\n", req->errorno);
else if (stdin_watcher.result < 0) {
fprintf(stderr, "error opening file: %s\n", uv_err_name(req->result));
}
}

Expand All @@ -34,7 +36,8 @@ int main() {

uv_idle_init(loop, &idler);

uv_fs_read(loop, &stdin_watcher, 1, buffer, 1024, -1, on_type);
uv_buf_t buf = uv_buf_init(buffer, 1024);
uv_fs_read(loop, &stdin_watcher, 1, &buf, 1, 0, on_type);
uv_idle_start(&idler, crunch_away);
return uv_run(loop, UV_RUN_DEFAULT);
}
22 changes: 13 additions & 9 deletions code/multi-echo-server/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -17,13 +18,14 @@ int child_worker_count;
uv_buf_t dummy_buf;
char worker_path[500];

void close_process_handle(uv_process_t *req, int exit_status, int term_signal) {
fprintf(stderr, "Process exited with status %d, signal %d\n", exit_status, term_signal);
void close_process_handle(uv_process_t *req, int64_t exit_status, int term_signal) {
fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
uv_close((uv_handle_t*) req, NULL);
}

uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
return uv_buf_init((char*) calloc(suggested_size, 1), suggested_size);
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}

void on_new_connection(uv_stream_t *server, int status) {
Expand Down Expand Up @@ -87,7 +89,7 @@ void setup_workers() {
worker->options.file = args[0];
worker->options.args = args;

uv_spawn(loop, &worker->req, worker->options);
uv_spawn(loop, &worker->req, &worker->options);
fprintf(stderr, "Started worker %d\n", worker->req.pid);
}
}
Expand All @@ -100,10 +102,12 @@ int main() {
uv_tcp_t server;
uv_tcp_init(loop, &server);

struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
uv_tcp_bind(&server, bind_addr);
if (uv_listen((uv_stream_t*) &server, 128, on_new_connection)) {
fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
struct sockaddr_in bind_addr;
uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0);
int r;
if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
fprintf(stderr, "Listen error %s\n", uv_err_name(r));
return 2;
}
return uv_run(loop, UV_RUN_DEFAULT);
Expand Down
49 changes: 30 additions & 19 deletions code/multi-echo-server/worker.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -7,42 +8,52 @@
uv_loop_t *loop;
uv_pipe_t queue;

uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
return uv_buf_init((char*) calloc(suggested_size, 1), suggested_size);
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}

void echo_write(uv_write_t *req, int status) {
if (status == -1) {
fprintf(stderr, "Write error %s\n", uv_err_name(uv_last_error(loop)));
if (status) {
fprintf(stderr, "Write error %s\n", uv_err_name(status));
}
char *base = (char*) req->data;
free(base);
free(req);
}

void echo_read(uv_stream_t *client, ssize_t nread, uv_buf_t buf) {
if (nread == -1) {
if (uv_last_error(loop).code != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(uv_last_error(loop)));
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, NULL);
return;
}

uv_write_t *req = (uv_write_t *) malloc(sizeof(uv_write_t));
req->data = (void*) buf.base;
buf.len = nread;
uv_write(req, client, &buf, 1, echo_write);
uv_buf_t wrbuf = uv_buf_init(buf->base, nread);
uv_write(req, client, &wrbuf, 1, echo_write);
free(buf->base);
}

void on_new_connection(uv_pipe_t *q, ssize_t nread, uv_buf_t buf, uv_handle_type pending) {
if (pending == UV_UNKNOWN_HANDLE) {
// error!
void on_new_connection(uv_stream_t *q, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) q, NULL);
return;
}

uv_pipe_t *pipe = (uv_pipe_t*) q;
if (!uv_pipe_pending_count(pipe)) {
fprintf(stderr, "No pending count\n");
return;
}

uv_handle_type pending = uv_pipe_pending_type(pipe);
assert(pending == UV_TCP);

uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
uv_tcp_init(loop, client, 0);
if (uv_accept((uv_stream_t*) q, (uv_stream_t*) client) == 0) {
uv_tcp_init(loop, client);
if (uv_accept(q, (uv_stream_t*) client) == 0) {
fprintf(stderr, "Worker %d: Accepted fd %d\n", getpid(), client->io_watcher.fd);
uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
}
Expand All @@ -56,6 +67,6 @@ int main() {

uv_pipe_init(loop, &queue, 1);
uv_pipe_open(&queue, 0);
uv_read2_start((uv_stream_t*)&queue, alloc_buffer, on_new_connection);
uv_read_start((uv_stream_t*)&queue, alloc_buffer, on_new_connection);
return uv_run(loop, UV_RUN_DEFAULT);
}
7 changes: 5 additions & 2 deletions code/onchange/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uv_loop_t *loop;
const char *command;

void run_command(uv_fs_event_t *handle, const char *filename, int events, int status) {
fprintf(stderr, "Change detected in %s: ", handle->filename);
fprintf(stderr, "Change detected in %s: ", handle->path);
if (events == UV_RENAME)
fprintf(stderr, "renamed");
if (events == UV_CHANGE)
Expand All @@ -28,7 +28,10 @@ int main(int argc, char **argv) {

while (argc-- > 2) {
fprintf(stderr, "Adding watch on %s\n", argv[argc]);
uv_fs_event_init(loop, (uv_fs_event_t*) malloc(sizeof(uv_fs_event_t)), argv[argc], run_command, 0);
uv_fs_event_t *fs_event_req = malloc(sizeof(uv_fs_event_t));
uv_fs_event_init(loop, fs_event_req);
// The recursive flag watches subdirectories too.
uv_fs_event_start(fs_event_req, run_command, argv[argc], UV_FS_EVENT_RECURSIVE);
}

return uv_run(loop, UV_RUN_DEFAULT);
Expand Down
34 changes: 17 additions & 17 deletions code/pipe-echo-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@

uv_loop_t *loop;

uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
return uv_buf_init((char*) calloc(suggested_size, 1), suggested_size);
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}

void echo_write(uv_write_t *req, int status) {
if (status == -1) {
fprintf(stderr, "Write error %s\n", uv_err_name(uv_last_error(loop)));
if (status < 0) {
fprintf(stderr, "Write error %s\n", uv_err_name(status));
}
char *base = (char*) req->data;
free(base);
free(req);
}

void echo_read(uv_stream_t *client, ssize_t nread, uv_buf_t buf) {
if (nread == -1) {
if (uv_last_error(loop).code != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(uv_last_error(loop)));
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, NULL);
return;
}

uv_write_t *req = (uv_write_t *) malloc(sizeof(uv_write_t));
req->data = (void*) buf.base;
buf.len = nread;
uv_write(req, client, &buf, 1, echo_write);
uv_buf_t wrbuf = uv_buf_init(buf->base, nread);
uv_write(req, client, &wrbuf, 1, echo_write);
free(buf->base);
}

void on_new_connection(uv_stream_t *server, int status) {
Expand Down Expand Up @@ -62,12 +61,13 @@ int main() {

signal(SIGINT, remove_sock);

if (uv_pipe_bind(&server, "echo.sock")) {
fprintf(stderr, "Bind error %s\n", uv_err_name(uv_last_error(loop)));
int r;
if ((r = uv_pipe_bind(&server, "echo.sock"))) {
fprintf(stderr, "Bind error %s\n", uv_err_name(r));
return 1;
}
if (uv_listen((uv_stream_t*) &server, 128, on_new_connection)) {
fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
fprintf(stderr, "Listen error %s\n", uv_err_name(r));
return 2;
}
return uv_run(loop, UV_RUN_DEFAULT);
Expand Down
Loading

0 comments on commit af22d7e

Please sign in to comment.