Skip to content

Commit

Permalink
indent examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikwidlund committed Dec 27, 2020
1 parent 80a2011 commit d5e3ac7
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ server:
strip server

indent:
clang-format -i src/reactor/*.c test/*.c
clang-format -i src/reactor/*.c test/*.c example/*.c
34 changes: 17 additions & 17 deletions example/benchmark_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <reactor.h>

char r[] =
"GET /path HTTP/1.0\r\n"
"Host: test.com\r\n"
"User-Agent: benchmark\r\n"
"Accept: */*\r\n"
"\r\n";
"GET /path HTTP/1.0\r\n"
"Host: test.com\r\n"
"User-Agent: benchmark\r\n"
"Accept: */*\r\n"
"\r\n";

int main()
{
Expand All @@ -28,22 +28,22 @@ int main()
len = strlen(input);

t1 = utility_tsc();
for (i = 0; i < iterations; i ++)
{
n = http_request_read(&request, (segment) {input, len});
assert(n == len);
}
for (i = 0; i < iterations; i++)
{
n = http_request_read(&request, (segment) {input, len});
assert(n == len);
}
t2 = utility_tsc();
(void) fprintf(stderr, "< %f\n", (double) (t2 - t1) / iterations);

t1 = utility_tsc();
for (i = 0; i < iterations; i ++)
{
http_response_ok(&response, segment_string("text/plain"), segment_string("Hello, World!"));
n = http_response_size(&response);
assert(n < sizeof output);
http_response_write(&response, (segment) {output, n});
}
for (i = 0; i < iterations; i++)
{
http_response_ok(&response, segment_string("text/plain"), segment_string("Hello, World!"));
n = http_response_size(&response);
assert(n < sizeof output);
http_response_write(&response, (segment) {output, n});
}
t2 = utility_tsc();
(void) fprintf(stderr, "> %f\n", (double) (t2 - t1) / iterations);
}
59 changes: 29 additions & 30 deletions example/httpd_lowlevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,38 @@ static core_status client_event(core_event *event)
ssize_t n;

switch (event->type)
{
case STREAM_READ:
s = stream_read(client);
do
{
case STREAM_READ:
s = stream_read(client);
do
{
n = http_request_read(&request, segment_offset(s, offset));
if (n <= 0)
break;
if (segment_equal(request.method, segment_string("GET")) && segment_equal(request.target, segment_string("/plaintext")))
plaintext(client);
else
not_found(client);
offset += n;
}
while (offset < s.size);
n = http_request_read(&request, segment_offset(s, offset));
if (n <= 0)
break;
if (segment_equal(request.method, segment_string("GET")) && segment_equal(request.target, segment_string("/plaintext")))
plaintext(client);
else
not_found(client);
offset += n;
} while (offset < s.size);

if (n == -1)
{
stream_destruct(client);
free(client);
return CORE_ABORT;
}

stream_flush(client);
stream_consume(client, offset);
return CORE_OK;
case STREAM_FLUSH:
return CORE_OK;
default:
if (n == -1)
{
stream_destruct(client);
free(client);
return CORE_ABORT;
}

stream_flush(client);
stream_consume(client, offset);
return CORE_OK;
case STREAM_FLUSH:
return CORE_OK;
default:
stream_destruct(client);
free(client);
return CORE_ABORT;
}
}

static core_status server_event(core_event *event)
Expand Down Expand Up @@ -99,10 +98,10 @@ int main()
s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (s == -1)
err(1, "socket");
(void) setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (int[]){1}, sizeof(int));
(void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (int[]){1}, sizeof(int));
(void) setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (int[]) {1}, sizeof(int));
(void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (int[]) {1}, sizeof(int));

e = bind(s, (struct sockaddr *) (struct sockaddr_in[]){{.sin_family = AF_INET, .sin_port = htons(8080)}},
e = bind(s, (struct sockaddr *) (struct sockaddr_in[]) {{.sin_family = AF_INET, .sin_port = htons(8080)}},
sizeof(struct sockaddr_in));
if (e == -1)
err(1, "bind");
Expand Down
24 changes: 12 additions & 12 deletions example/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ static core_status callback(core_event *event)
struct inotify_event *e = (struct inotify_event *) event->data;

switch (event->type)
{
case NOTIFY_EVENT:
(void) fprintf(stdout, "%04x %s\n", e->mask, e->len ? e->name : "");
return CORE_OK;
default:
notify_destruct(notify);
return CORE_ABORT;
}
{
case NOTIFY_EVENT:
(void) fprintf(stdout, "%04x %s\n", e->mask, e->len ? e->name : "");
return CORE_OK;
default:
notify_destruct(notify);
return CORE_ABORT;
}
}

int main(int argc, char **argv)
Expand All @@ -29,10 +29,10 @@ int main(int argc, char **argv)
notify notify;

if (argc != 2)
{
(void) fprintf(stderr, "usage: %s PATH\n", __progname);
exit(1);
}
{
(void) fprintf(stderr, "usage: %s PATH\n", __progname);
exit(1);
}

core_construct(NULL);
notify_construct(&notify, callback, &notify);
Expand Down
18 changes: 9 additions & 9 deletions example/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ static core_status handler(core_event *event)
server_context *context = (server_context *) event->data;

switch (event->type)
{
case SERVER_REQUEST:
server_ok(context, segment_string("text/plain"), segment_string("Hello, World!"));
return CORE_OK;
default:
warn("error");
server_destruct(server);
return CORE_ABORT;
}
{
case SERVER_REQUEST:
server_ok(context, segment_string("text/plain"), segment_string("Hello, World!"));
return CORE_OK;
default:
warn("error");
server_destruct(server);
return CORE_ABORT;
}
}

int main()
Expand Down
32 changes: 16 additions & 16 deletions example/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ void process(stream *stream)
segment line;

while (1)
{
line = stream_read_line(stream);
if (!line.size)
break;
(void) printf("%.*s", (int) line.size, (char *) line.base);
stream_consume(stream, line.size);
}
{
line = stream_read_line(stream);
if (!line.size)
break;
(void) printf("%.*s", (int) line.size, (char *) line.base);
stream_consume(stream, line.size);
}
}

static core_status callback(core_event *event)
{
switch (event->type)
{
case STREAM_READ:
process(event->state);
return CORE_OK;
default:
process(event->state);
stream_destruct(event->state);
return CORE_ABORT;
}
{
case STREAM_READ:
process(event->state);
return CORE_OK;
default:
process(event->state);
stream_destruct(event->state);
return CORE_ABORT;
}
}

int main()
Expand Down
38 changes: 19 additions & 19 deletions example/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

struct state
{
timer timer;
int count;
timer timer;
int count;
uint64_t tsc;
};

Expand All @@ -20,25 +20,25 @@ static core_status callback(core_event *event)
int64_t t;

switch (event->type)
{
case TIMER_ALARM:
if (state->tsc == 0)
state->tsc = utility_tsc();
else
{
case TIMER_ALARM:
if (state->tsc == 0)
state->tsc = utility_tsc();
else
{
t = utility_tsc();
fprintf(stderr, "%.02fGhz\n", (double) (t - state->tsc) / 1000000000.);
state->tsc = t;
state->count --;
}

if (state->count)
return CORE_OK;
/* fall through */
default:
timer_destruct(&state->timer);
return CORE_ABORT;
t = utility_tsc();
fprintf(stderr, "%.02fGhz\n", (double) (t - state->tsc) / 1000000000.);
state->tsc = t;
state->count--;
}

if (state->count)
return CORE_OK;
/* fall through */
default:
timer_destruct(&state->timer);
return CORE_ABORT;
}
}

int main()
Expand Down

0 comments on commit d5e3ac7

Please sign in to comment.