Compile with -ansi

This commit is contained in:
Ryan Dahl 2011-03-29 16:40:27 -07:00
parent 0fe92b75bc
commit 311fbe3f8f
6 changed files with 47 additions and 56 deletions

View File

@ -1,11 +1,11 @@
test/echo-server: test/echo-server.c ol.a 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 ol.a: ol-unix.o ev/ev.o
$(AR) rcs 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 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 ev/ev.o: ev/config.h ev/ev.c
$(MAKE) -C ev $(MAKE) -C ev

View File

@ -179,7 +179,7 @@ struct ev_loop;
#endif #endif
/* EV_INLINE is used for functions in header files */ /* 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 # define EV_INLINE static inline
#else #else
# define EV_INLINE static # define EV_INLINE static

View File

@ -4,6 +4,12 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <string.h> /* strnlen */ #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); void ol_tcp_io(EV_P_ ev_io* watcher, int revents);
@ -123,9 +129,9 @@ void ol_server_io(EV_P_ ev_io* watcher, int revents) {
if (fd < 0) { if (fd < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
return; // No problem. return; /* No problem. */
} else if (errno == EMFILE) { } else if (errno == EMFILE) {
// TODO special trick. unlock reserved socket, accept, close. /* TODO special trick. unlock reserved socket, accept, close. */
return; return;
} else { } else {
ol_close_error(handle, ol_err_new(handle, errno)); ol_close_error(handle, ol_err_new(handle, errno));
@ -211,7 +217,7 @@ void ol_tcp_connect(ol_handle* handle, ol_req* req) {
ev_set_cb(&handle->_.read_watcher, ol_tcp_io); ev_set_cb(&handle->_.read_watcher, ol_tcp_io);
/* Successful connection */ /* Successful connection */
ol_connect_cb connect_cb = req->connect_cb; ol_connect_cb connect_cb = req->cb;
if (connect_cb) { if (connect_cb) {
if (req->_.local) { if (req->_.local) {
connect_cb(NULL, ol_err_new(handle, 0)); connect_cb(NULL, ol_err_new(handle, 0));
@ -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) { int ol_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt) {
// stub /* stub */
assert(0); assert(0);
return 0; return 0;
} }
@ -319,18 +325,18 @@ int ol_write2(ol_handle* handle, const char* msg) {
void ol_req_append(ol_handle* handle, ol_req *req) { 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) { int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {
assert(handle->_.fd >= 0); 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. */ /* 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) { if (!req) {
return ol_err_new(handle, ENOMEM); return ol_err_new(handle, ENOMEM);
} }
@ -341,19 +347,21 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {
} else { } else {
/* Attempt to read immediately */ /* 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) { if (nread < 0) {
} }
ssize_t buftotal; ssize_t buftotal;
for (int i = 0; i < bufcnt; i++) { int i;
buftotal += bufs.len; for (i = 0; i < bufcnt; i++) {
buftotal += bufs[i].len;
if (buftotal == nread) { if (buftotal == nread) {
/* We read everything */ /* We read everything */
if (req_in.read_cb) { ol_read_cb cb = req_in->cb;
req_in.read_cb(handle, req_in, nread, 0); if (cb) {
cb(req_in, nread, 0);
} }
} }
@ -365,12 +373,10 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {
if (!req) { if (!req) {
return ol_err_new(handle, ENOMEM); return ol_err_new(handle, ENOMEM);
} }
// blah
} }
ngx_queue_insert_tail(); /* ngx_queue_insert_tail(); */
return 0; return 0;
} }
@ -378,6 +384,6 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) {
void ol_free(ol_handle* handle) { void ol_free(ol_handle* handle) {
free(handle); free(handle);
// lists? /* lists? */
return; return;
} }

View File

@ -20,21 +20,11 @@ typedef struct {
} ol_buf; } 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 { typedef struct {
int local; int local;
ol_req_cb connect_cb; ol_req_cb connect_cb;
ngx_queue_t read_reqs;
} ol_req_private; } ol_req_private;
@ -52,8 +42,7 @@ typedef struct {
ev_io write_watcher; ev_io write_watcher;
ngx_queue_t write_queue; ngx_queue_t write_queue;
ngx_queue_t all_handles; ngx_queue_t read_reqs;
} ol_handle_private; } ol_handle_private;

36
ol.h
View File

@ -5,7 +5,7 @@
typedef int ol_err; // FIXME typedef int ol_err; /* FIXME */
typedef struct ol_req_s ol_req; typedef struct ol_req_s ol_req;
typedef struct ol_handle_s ol_handle; typedef struct ol_handle_s ol_handle;
@ -36,11 +36,11 @@ typedef enum {
struct ol_handle_s { struct ol_handle_s {
// read-only /* read-only */
ol_handle_type type; ol_handle_type type;
// private /* private */
ol_handle_private _; ol_handle_private _;
// public /* public */
ol_accept_cb accept_cb; ol_accept_cb accept_cb;
ol_close_cb close_cb; ol_close_cb close_cb;
void* data; void* data;
@ -57,18 +57,13 @@ typedef enum {
struct ol_req_s { struct ol_req_s {
// read-only /* read-only */
ol_req_type type; ol_req_type type;
ol_handle* handle; ol_handle* handle;
// private /* private */
ol_req_private _; ol_req_private _;
// public /* public */
union { void* cb;
ol_read_cb read_cb;
ol_write_cb write_cb;
ol_connect_cb connect_cb;
ol_shutdown_cb shutdown_cb;
};
void *data; void *data;
}; };
@ -78,28 +73,29 @@ int ol_run();
ol_handle* ol_handle_new(ol_close_cb close_cb, void* data); 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_bind(ol_handle* handle, struct sockaddr* addr);
int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb); 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_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_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_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt);
int ol_write2(ol_handle* handle, const char* msg); int ol_write2(ol_handle* handle, const char* msg);
int ol_shutdown(ol_handle* handle, ol_req *req); int ol_shutdown(ol_handle* handle, ol_req *req);
// Request handle to be closed. close_cb will be made /* Request handle to be closed. close_cb will be made */
// synchronously during this call. /* synchronously during this call. */
int ol_close(ol_handle* handle); int ol_close(ol_handle* handle);
// Must be called for all handles after close_cb. Handles that arrive /* Must be called for all handles after close_cb. Handles that arrive
// via the accept_cb must use ol_free(). * via the accept_cb must use ol_free().
*/
void ol_free(ol_handle* handle); void ol_free(ol_handle* handle);
// Utility /* Utility */
struct sockaddr_in ol_ip4_addr(char *ip, int port); struct sockaddr_in ol_ip4_addr(char *ip, int port);
#endif /* OL_H */ #endif /* OL_H */

View File

@ -37,7 +37,7 @@ void after_read(ol_req* req, size_t nread, ol_err err) {
} else { } else {
peer_t *peer = (peer_t*) req->data; peer_t *peer = (peer_t*) req->data;
peer->buf.len = nread; peer->buf.len = nread;
peer->req.write_cb = after_write; peer->req.cb = after_write;
ol_write(peer->handle, &peer->req, &peer->buf, 1); ol_write(peer->handle, &peer->req, &peer->buf, 1);
} }
} }
@ -46,7 +46,7 @@ void after_read(ol_req* req, size_t nread, ol_err err) {
void try_read(peer_t* peer) { void try_read(peer_t* peer) {
peer->buf.len = BUFSIZE; peer->buf.len = BUFSIZE;
peer->req.read_cb = after_read; peer->req.cb = after_read;
ol_read(peer->handle, &peer->req, &peer->buf, 1); ol_read(peer->handle, &peer->req, &peer->buf, 1);
} }
@ -76,7 +76,7 @@ void on_accept(ol_handle* server, ol_handle* new_client) {
r = ol_write2(new_client, "Hello\n"); r = ol_write2(new_client, "Hello\n");
if (r < 0) { if (r < 0) {
// error /* error */
assert(0); assert(0);
} }
} }