Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Compile with -ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Mar 29, 2011
1 parent 0fe92b7 commit 311fbe3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,11 +1,11 @@
test/echo-server: test/echo-server.c ol.a
$(CC) -g -o test/echo-server test/echo-server.c ol.a -lm
$(CC) -ansi -g -o test/echo-server test/echo-server.c ol.a -lm

ol.a: ol-unix.o ev/ev.o
$(AR) rcs ol.a ol-unix.o ev/ev.o

ol-unix.o: ol-unix.c ol.h ol-unix.h
$(CC) -g -c ol-unix.c -o ol-unix.o -lm
$(CC) -ansi -g -c ol-unix.c -o ol-unix.o -lm

ev/ev.o: ev/config.h ev/ev.c
$(MAKE) -C ev
Expand Down
2 changes: 1 addition & 1 deletion ev/ev.h
Expand Up @@ -179,7 +179,7 @@ struct ev_loop;
#endif

/* EV_INLINE is used for functions in header files */
#if __STDC_VERSION__ >= 199901L || __GNUC__ >= 3
#if __STDC_VERSION__ >= 199901L && __GNUC__ >= 3
# define EV_INLINE static inline
#else
# define EV_INLINE static
Expand Down
40 changes: 23 additions & 17 deletions ol-unix.c
Expand Up @@ -4,6 +4,12 @@
#include <errno.h>
#include <assert.h>
#include <string.h> /* strnlen */
#include <unistd.h>
#include <fcntl.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


void ol_tcp_io(EV_P_ ev_io* watcher, int revents);
Expand Down Expand Up @@ -123,9 +129,9 @@ void ol_server_io(EV_P_ ev_io* watcher, int revents) {

if (fd < 0) {
if (errno == EAGAIN) {
return; // No problem.
return; /* No problem. */
} else if (errno == EMFILE) {
// TODO special trick. unlock reserved socket, accept, close.
/* TODO special trick. unlock reserved socket, accept, close. */
return;
} else {
ol_close_error(handle, ol_err_new(handle, errno));
Expand Down Expand Up @@ -211,7 +217,7 @@ void ol_tcp_connect(ol_handle* handle, ol_req* req) {
ev_set_cb(&handle->_.read_watcher, ol_tcp_io);

/* Successful connection */
ol_connect_cb connect_cb = req->connect_cb;
ol_connect_cb connect_cb = req->cb;
if (connect_cb) {
if (req->_.local) {
connect_cb(NULL, ol_err_new(handle, 0));
Expand Down Expand Up @@ -303,7 +309,7 @@ int ol_connect(ol_handle* handle, ol_req *req_in, struct sockaddr* addr) {
}

int ol_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt) {
// stub
/* stub */
assert(0);
return 0;
}
Expand All @@ -319,18 +325,18 @@ int ol_write2(ol_handle* handle, const char* msg) {


void ol_req_append(ol_handle* handle, ol_req *req) {
ngx_queue_insert_tail(&handle->read_reqs, &req->read_queue);
ngx_queue_insert_tail(&handle->_.read_reqs, &req->_.read_reqs);
}


int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {
assert(handle->_.fd >= 0);

if (!ngx_queue_empty(&handle->read_reqs)) {
if (!ngx_queue_empty(&handle->_.read_reqs)) {
/* There are already pending read_reqs. We must get in line. */
assert(ev_is_active(&handle->read_watcher));
assert(ev_is_active(&handle->_.read_watcher));

ol_req *req = ol_req_maybe_alloc(handle, req_in);
ol_req* req = ol_req_maybe_alloc(handle, req_in);
if (!req) {
return ol_err_new(handle, ENOMEM);
}
Expand All @@ -341,19 +347,21 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {

} else {
/* Attempt to read immediately */
ssize_t nread = readv(handle->_.fd, (struct iovec) bufs, bufcnt);
ssize_t nread = readv(handle->_.fd, (struct iovec*) bufs, bufcnt);

if (nread < 0) {

}

ssize_t buftotal;
for (int i = 0; i < bufcnt; i++) {
buftotal += bufs.len;
int i;
for (i = 0; i < bufcnt; i++) {
buftotal += bufs[i].len;
if (buftotal == nread) {
/* We read everything */
if (req_in.read_cb) {
req_in.read_cb(handle, req_in, nread, 0);
ol_read_cb cb = req_in->cb;
if (cb) {
cb(req_in, nread, 0);
}
}

Expand All @@ -365,19 +373,17 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {
if (!req) {
return ol_err_new(handle, ENOMEM);
}

// blah
}


ngx_queue_insert_tail();
/* ngx_queue_insert_tail(); */

return 0;
}


void ol_free(ol_handle* handle) {
free(handle);
// lists?
/* lists? */
return;
}
15 changes: 2 additions & 13 deletions ol-unix.h
Expand Up @@ -20,21 +20,11 @@ typedef struct {
} ol_buf;


typedef struct {
ol_buf* bufs;
int bufcnt;
int current_index;
size_t written;
ol_write_cb write_cb;
ol_handle* handle;
ngx_queue_t write_queue;
} ol_bucket;



typedef struct {
int local;
ol_req_cb connect_cb;
ngx_queue_t read_reqs;
} ol_req_private;


Expand All @@ -52,8 +42,7 @@ typedef struct {
ev_io write_watcher;

ngx_queue_t write_queue;
ngx_queue_t all_handles;

ngx_queue_t read_reqs;

} ol_handle_private;

Expand Down
36 changes: 16 additions & 20 deletions ol.h
Expand Up @@ -5,7 +5,7 @@



typedef int ol_err; // FIXME
typedef int ol_err; /* FIXME */

typedef struct ol_req_s ol_req;
typedef struct ol_handle_s ol_handle;
Expand Down Expand Up @@ -36,11 +36,11 @@ typedef enum {


struct ol_handle_s {
// read-only
/* read-only */
ol_handle_type type;
// private
/* private */
ol_handle_private _;
// public
/* public */
ol_accept_cb accept_cb;
ol_close_cb close_cb;
void* data;
Expand All @@ -57,18 +57,13 @@ typedef enum {


struct ol_req_s {
// read-only
/* read-only */
ol_req_type type;
ol_handle* handle;
// private
/* private */
ol_req_private _;
// public
union {
ol_read_cb read_cb;
ol_write_cb write_cb;
ol_connect_cb connect_cb;
ol_shutdown_cb shutdown_cb;
};
/* public */
void* cb;
void *data;
};

Expand All @@ -78,28 +73,29 @@ int ol_run();

ol_handle* ol_handle_new(ol_close_cb close_cb, void* data);

// TCP server methods.
/* TCP server methods. */
int ol_bind(ol_handle* handle, struct sockaddr* addr);
int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb);

// TCP socket methods.
/* TCP socket methods. */
int ol_connect(ol_handle* handle, ol_req *req, struct sockaddr* addr);
int ol_read(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt);
int ol_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt);
int ol_write2(ol_handle* handle, const char* msg);
int ol_shutdown(ol_handle* handle, ol_req *req);

// Request handle to be closed. close_cb will be made
// synchronously during this call.
/* Request handle to be closed. close_cb will be made */
/* synchronously during this call. */
int ol_close(ol_handle* handle);

// Must be called for all handles after close_cb. Handles that arrive
// via the accept_cb must use ol_free().
/* Must be called for all handles after close_cb. Handles that arrive
* via the accept_cb must use ol_free().
*/
void ol_free(ol_handle* handle);



// Utility
/* Utility */
struct sockaddr_in ol_ip4_addr(char *ip, int port);

#endif /* OL_H */
6 changes: 3 additions & 3 deletions test/echo-server.c
Expand Up @@ -37,7 +37,7 @@ void after_read(ol_req* req, size_t nread, ol_err err) {
} else {
peer_t *peer = (peer_t*) req->data;
peer->buf.len = nread;
peer->req.write_cb = after_write;
peer->req.cb = after_write;
ol_write(peer->handle, &peer->req, &peer->buf, 1);
}
}
Expand All @@ -46,7 +46,7 @@ void after_read(ol_req* req, size_t nread, ol_err err) {

void try_read(peer_t* peer) {
peer->buf.len = BUFSIZE;
peer->req.read_cb = after_read;
peer->req.cb = after_read;
ol_read(peer->handle, &peer->req, &peer->buf, 1);
}

Expand Down Expand Up @@ -76,7 +76,7 @@ void on_accept(ol_handle* server, ol_handle* new_client) {

r = ol_write2(new_client, "Hello\n");
if (r < 0) {
// error
/* error */
assert(0);
}
}
Expand Down

0 comments on commit 311fbe3

Please sign in to comment.