fnet.c

Simple C networking library
git clone git://git.finwo.net/lib/fnet.c
Log | Files | Refs | README

fnet.h (2441B)


      1 #ifndef __INCLUDE_FINWO_FNET_H__
      2 #define __INCLUDE_FINWO_FNET_H__
      3 
      4 #include <stdint.h>
      5 
      6 #include "tidwall/buf.h"
      7 
      8 #define FNET_FLAG            uint8_t
      9 #define FNET_FLAG_RECONNECT  1
     10 
     11 #define FNET_PROTOCOL  uint8_t
     12 #define FNET_PROTO_TCP 0
     13 
     14 #define FNET_RETURNCODE                  int
     15 #define FNET_RETURNCODE_OK               0
     16 #define FNET_RETURNCODE_ERROR            -1
     17 #define FNET_RETURNCODE_MISSING_ARGUMENT -2
     18 #define FNET_RETURNCODE_NOT_IMPLEMENTED  -3
     19 #define FNET_RETURNCODE_ERRNO            -4
     20 #define FNET_RETURNCODE_UNPROCESSABLE    -5
     21 #define FNET_RETURNCODE_ALREADY_ACTIVE   -6
     22 
     23 #define FNET_STATUS               uint8_t
     24 #define FNET_STATUS_INITIALIZING   0
     25 #define FNET_STATUS_CONNECTING     1 // Client-only status
     26 #define FNET_STATUS_CONNECTED      2 // Connection ready
     27 #define FNET_STATUS_LISTENING      4 // Listen ready
     28 #define FNET_STATUS_READY          6 // Any ready
     29 #define FNET_STATUS_ACCEPTED       8 // Whether the connection is an accepted one
     30 #define FNET_STATUS_ERROR         16
     31 #define FNET_STATUS_CLOSED        32
     32 
     33 #define FNET_EVENT         int
     34 #define FNET_EVENT_LISTEN  1
     35 #define FNET_EVENT_CONNECT 2
     36 #define FNET_EVENT_DATA    3
     37 #define FNET_EVENT_TICK    4
     38 #define FNET_EVENT_CLOSE   5
     39 
     40 #define FNET_CALLBACK(NAME) void (*(NAME))(struct fnet_ev *event)
     41 
     42 struct fnet_ev {
     43   struct fnet_t *connection;
     44   FNET_EVENT     type;
     45   struct buf    *buffer;
     46   void          *udata;
     47 };
     48 
     49 struct fnet_t {
     50   FNET_PROTOCOL proto;
     51   FNET_STATUS   status;
     52   FNET_CALLBACK(onListen);
     53   FNET_CALLBACK(onConnect);
     54   FNET_CALLBACK(onData);
     55   FNET_CALLBACK(onTick);
     56   FNET_CALLBACK(onClose);
     57   void *udata;
     58 };
     59 
     60 struct fnet_options_t {
     61   FNET_PROTOCOL proto;
     62   FNET_FLAG     flags;
     63   FNET_CALLBACK(onListen);
     64   FNET_CALLBACK(onConnect);
     65   FNET_CALLBACK(onData);
     66   FNET_CALLBACK(onTick);
     67   FNET_CALLBACK(onClose);
     68   void *udata;
     69 };
     70 
     71 struct fnet_t * fnet_listen(const char *address, uint16_t port, const struct fnet_options_t *options);
     72 struct fnet_t * fnet_connect(const char *address, uint16_t port, const struct fnet_options_t *options);
     73 
     74 FNET_RETURNCODE fnet_process(const struct fnet_t *connection);
     75 FNET_RETURNCODE fnet_write(const struct fnet_t *connection, struct buf *buf);
     76 FNET_RETURNCODE fnet_close(const struct fnet_t *connection);
     77 FNET_RETURNCODE fnet_free(struct fnet_t *connection);
     78 
     79 void            fnet_thread();
     80 FNET_RETURNCODE fnet_main();
     81 FNET_RETURNCODE fnet_shutdown();
     82 
     83 #endif // __INCLUDE_FINWO_FNET_H__