Last active
June 4, 2021 10:46
-
-
Save cpq/8df18ed692f7a4d7a461a662ba87d380 to your computer and use it in GitHub Desktop.
CNIP API suggestion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The stack has two API: low level and high level | |
// Low level: used to hook it up to a hardware, to send/receive MAC frames | |
// High level: used to implement network apps, like HTTP/MQTT client/servers. | |
// Low level | |
// A device code does something like this: | |
// | |
// void cnip_mac_out(char *buf, size_t len) { | |
// bitbang_to_ethernet(buf, len); // Push outgoing frame to the network | |
// } | |
// | |
// for (;;) { | |
// char frame[1500]; | |
// size_t n = receive_frame(frame, sizeof(frame)); | |
// cnip_mac_in(frame, n); // Process incoming frame | |
// cnip_poll(current_time); // Process timers, and call user high level handlers | |
// } | |
void cnip_mac_in(char *buf, size_t len); // Implemented by CNIP | |
void cnip_mac_out(char *buf, size_t le); // Must be implemented by user | |
void cnip_poll(time_t now); // Must be called periodically or on event | |
// High level API - for creating network applications/services | |
// Example usage: | |
// void my_fn(struct cn_connection *c, int ev, void *ev_data) { | |
// switch (ev) { | |
// case EV_READ: | |
// serve_http(c, c->recv_buffer); | |
// break; | |
// } | |
// } | |
// | |
// cn_tcp_listen(0, 80, my_fn, &minecraft_server_data); | |
struct cn_connection { | |
... stuff like local/remote addresses, incoming and outgoing IO buffers | |
}; | |
// User-defined callback function, called on various events | |
enum { EV_ERROR, EV_CONNECT, EV_ACCEPT, EV_READ, EV_WRITE, EV_CLOSE }; | |
typedef void (*cnip_fn_t)(struct cn_connection *, int ev, void *ev_data); | |
struct cn_connection *cnip_tcp_listen(uint32_t ip, uint16_t port, cnip_fn_t fn, void *fn_data); | |
struct cn_connection *cnip_tcp_connect(uint32_t ip, uint16_t port, cnip_fn_t fn, void *fn_data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment