[ccan] ccan: the psuedo "networking" module.

Allan Ference f.fallen45 at gmail.com
Sun Dec 9 17:34:26 EST 2012


Hey Rusty,

What do you think about the following code design?  I've not even compiled
it but i'm dropping it here for suggestions.

#ifndef _SOCKET_H
#define _SOCKET_H

#include <csnippets/list.h>
#include <csnippets/pollevent.h>

#include <time.h>
#include <pthread.h>

struct sk_buff {
/** The data read from a recv() syscall  */
void *data;
/** length of @data.  */
size_t size;
};

struct listener {
int fd;                         /* Socket file descriptor.  */

struct list_head children;      /* List of connections (struct conn) */
unsigned int num_connections;   /* Current active connections */

void (*on_accept) (struct listener *self, struct conn *conn);
};

struct conn {
int fd; /* The socket file descriptor */
char host[1025]; /* The hostname of this connection */
char port[32]; /* The port we're connected to */
char remote[1025]; /* Who did we connect to?  Or who did we come from?  */
time_t last_active; /* The timestamp of last activity.  Useful for PING
PONG. */

/* "write buffer" this is changed whenever data has been been sent.
 * If the data was successfully sent over the connection, ops.write will be
 * called.  */
struct sk_buff wbuff;
struct list_node node;     /* next and previous conn */
};
struct io_service;

#define __ct_typecheck(__type, __desired_type)       \
((void)sizeof(char[1 - 2 *
!__builtin_types_compatible_p(__typeof__((type)),
__typeof__((__desired_type)))]))

typedef void (*conn_callback) (void *, void *);
#define conn_register_cb(conn,  ccb, cb) \
__ct_typecheck(conn, struct conn);  __ct_typecheck(cb, conn_callback); \
_conn_register_cb((conn), (ccb), (cb))

typedef enum {
CB_READ,
CB_WRITE,
CB_DISCONNECT,
CB_CONNECT
} conn_callback_t;

typedef void (*listener_callback) (void *, void *);
#define listener_register_callback(listener, cb) \
__ct_typecheck(listener, struct listener); __ct_typecheck(cb, void (*)
(struct listener *, struct conn *)); \
_listener_register_callback((listener), (cb))

struct listener *listener_new(struct io_service *io, const char *address,
const char *service,
unsigned long max_conns);
int _listener_register_callback(struct listener *listener,
listener_callback cb);

int listener_accept(struct listener *li, struct conn **to);
int listener_accept_next(struct listener *li, struct conn **to,
listener_callback cb);

struct conn *conn_new(struct io_service *io, const char *hostname, const
char *port);
int _conn_register_callback(struct conn *conn, const conn_callback_t ccb,
conn_callback cb);

ssize_t conn_write(struct conn *conn, const void *data, size_t dlen);
ssize_t conn_write_next(struct conn *conn, const void *data, size_t dlen,
conn_callback cb);
ssize_t conn_read(struct conn *conn, const void *data, size_t dlen);
ssize_t conn_read_next(struct conn *conn, const void *data, size_t dlen,
conn_callback cb);

int service_poll(struct io_service *service);
int service_remove_listener(struct io_service *service, struct listener
*listener);
int service_remove_conn(struct io_service *service, struct conn *conn);

#endif    /* _SOCKET_H */

Thanks,
Allan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20121209/ce5ecc9f/attachment-0001.html>


More information about the ccan mailing list