Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
FutureKosmos committed Jul 20, 2024
1 parent cc4c7d3 commit c106ba0
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 235 deletions.
22 changes: 11 additions & 11 deletions docs/API-Specification.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,21 @@ <h3 id="cdk-list">cdk-list </h3>
* This function initializes a red-black tree with the specified comparison function.
*
* Note: CDK provides the following built-in functions for keycmp:
extern int default_keycmp_i8(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i16(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i32(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i64(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u8(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u16(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u32(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u64(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_str(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i8(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_i16(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_i32(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_i64(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u8(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u16(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u32(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u64(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_str(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
*
* @param tree Pointer to the red-black tree structure
* @param keycmp Comparison function for the keys in the tree
* @return N/A
*/</span>
<span class="token keyword keyword-extern">extern</span> <span class="token keyword keyword-void">void</span> <span class="token function">cdk_rbtree_init</span><span class="token punctuation">(</span><span class="token class-name">cdk_rbtree_t</span><span class="token operator">*</span> tree<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span><span class="token punctuation">(</span><span class="token operator">*</span>keycmp<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token class-name">cdk_rbtree_node_key_t</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token class-name">cdk_rbtree_node_key_t</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword keyword-extern">extern</span> <span class="token keyword keyword-void">void</span> <span class="token function">cdk_rbtree_init</span><span class="token punctuation">(</span><span class="token class-name">cdk_rbtree_t</span><span class="token operator">*</span> tree<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span><span class="token punctuation">(</span><span class="token operator">*</span>keycmp<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token class-name">cdk_rbtree_key_t</span><span class="token operator">*</span><span class="token punctuation">,</span> <span class="token class-name">cdk_rbtree_key_t</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token comment">/**
* @brief Insert a node into the red-black tree
*
Expand All @@ -233,7 +233,7 @@ <h3 id="cdk-list">cdk-list </h3>
* @param key Key of the node to be found
* @return Pointer to the found node, or NULL if not found
*/</span>
<span class="token keyword keyword-extern">extern</span> <span class="token class-name">cdk_rbtree_node_t</span><span class="token operator">*</span> <span class="token function">cdk_rbtree_find</span><span class="token punctuation">(</span><span class="token class-name">cdk_rbtree_t</span><span class="token operator">*</span> tree<span class="token punctuation">,</span> <span class="token class-name">cdk_rbtree_node_key_t</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword keyword-extern">extern</span> <span class="token class-name">cdk_rbtree_node_t</span><span class="token operator">*</span> <span class="token function">cdk_rbtree_find</span><span class="token punctuation">(</span><span class="token class-name">cdk_rbtree_t</span><span class="token operator">*</span> tree<span class="token punctuation">,</span> <span class="token class-name">cdk_rbtree_key_t</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><pre data-role="codeBlock" data-info="c" class="language-c c"><code><span class="token comment">/**
* @brief Erase a node from the red-black tree
*
Expand Down
22 changes: 11 additions & 11 deletions docs/API-Specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,21 @@ extern bool cdk_queue_empty(cdk_queue_t* q);
* This function initializes a red-black tree with the specified comparison function.
*
* Note: CDK provides the following built-in functions for keycmp:
extern int default_keycmp_i8(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i16(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i32(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i64(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u8(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u16(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u32(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_u64(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_str(cdk_rbtree_node_key_t* k1, cdk_rbtree_node_key_t* k2);
extern int default_keycmp_i8(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_i16(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_i32(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_i64(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u8(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u16(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u32(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_u64(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
extern int default_keycmp_str(cdk_rbtree_key_t* k1, cdk_rbtree_key_t* k2);
*
* @param tree Pointer to the red-black tree structure
* @param keycmp Comparison function for the keys in the tree
* @return N/A
*/
extern void cdk_rbtree_init(cdk_rbtree_t* tree, int(*keycmp)(cdk_rbtree_node_key_t*, cdk_rbtree_node_key_t*));
extern void cdk_rbtree_init(cdk_rbtree_t* tree, int(*keycmp)(cdk_rbtree_key_t*, cdk_rbtree_key_t*));
```
```c
/**
Expand All @@ -250,7 +250,7 @@ extern void cdk_rbtree_insert(cdk_rbtree_t* tree, cdk_rbtree_node_t* node);
* @param key Key of the node to be found
* @return Pointer to the found node, or NULL if not found
*/
extern cdk_rbtree_node_t* cdk_rbtree_find(cdk_rbtree_t* tree, cdk_rbtree_node_key_t key);
extern cdk_rbtree_node_t* cdk_rbtree_find(cdk_rbtree_t* tree, cdk_rbtree_key_t key);
```
```c
/**
Expand Down
100 changes: 48 additions & 52 deletions examples/example-async-tcp-client.c
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
#include "cdk.h"

typedef enum net_msg_type_e{
KEEPALIVE_MSG_TYPE,
PING_MSG_TYPE,
PONG_MSG_TYPE,
}net_msg_type_t;
typedef enum msg_type_e{
TYPE_KEEPALIVE,
TYPE_PING,
TYPE_PONG,
} msg_type_t;

typedef struct net_msg_hdr_s {
typedef struct msg_hdr_s {
uint32_t size;
net_msg_type_t type;
}net_msg_hdr_t;
msg_type_t type;
} msg_hdr_t;

typedef struct net_msg_s {
net_msg_hdr_t hdr;
typedef struct msg_s {
msg_hdr_t hdr;
char data[];
}net_msg_t;
} msg_t;

char ping[16] = "PING";
char keepalive[16] = "KEEPALIVE";
char PING[16] = "PING";
char KEEPALIVE[16] = "KEEPALIVE";

static void handle_connect(cdk_channel_t* channel) {
static void _connect_cb(cdk_channel_t* channel) {
cdk_logi("[%d]has connected to remote...\n", (int)channel->fd);
net_msg_t* msg = malloc(sizeof(net_msg_t) + sizeof(ping));
if (msg) {
msg->hdr.size = htonl((uint32_t)(sizeof(ping)));
msg->hdr.type = htonl(PING_MSG_TYPE);
memcpy(msg->data, ping, sizeof(ping));
cdk_net_send(channel, msg, sizeof(net_msg_t) + sizeof(ping));

free(msg);
msg = NULL;
msg_t* smsg = malloc(sizeof(msg_t) + sizeof(PING));
if (smsg) {
smsg->hdr.size = htonl((uint32_t)(sizeof(PING)));
smsg->hdr.type = htonl(TYPE_PING);
memcpy(smsg->data, PING, sizeof(PING));
cdk_net_send(channel, smsg, sizeof(msg_t) + sizeof(PING));
free(smsg);
smsg = NULL;
}
}

static void handle_read(cdk_channel_t* channel, void* buf, size_t len) {
net_msg_t* rmsg = buf;
static void _read_cb(cdk_channel_t* channel, void* buf, size_t len) {
msg_t* rmsg = buf;
cdk_address_t addrinfo;
cdk_net_extract_address(channel->fd, &addrinfo, true);
cdk_logi("recv %s from %s:%d. type: %d, len: %d.\n", rmsg->data, addrinfo.addr, addrinfo.port, ntohl(rmsg->hdr.type), ntohl(rmsg->hdr.size));

net_msg_t* msg = malloc(sizeof(net_msg_t) + sizeof(ping));
if (msg) {
msg->hdr.size = htonl((uint32_t)(sizeof(ping)));
msg->hdr.type = htonl(PING_MSG_TYPE);
memcpy(msg->data, ping, sizeof(ping));
cdk_net_send(channel, msg, sizeof(net_msg_t) + sizeof(ping));

free(msg);
msg = NULL;
msg_t* smsg = malloc(sizeof(msg_t) + sizeof(PING));
if (smsg) {
smsg->hdr.size = htonl((uint32_t)(sizeof(PING)));
smsg->hdr.type = htonl(TYPE_PING);
memcpy(smsg->data, PING, sizeof(PING));
cdk_net_send(channel, smsg, sizeof(msg_t) + sizeof(PING));
free(smsg);
smsg = NULL;
}
}

static void handle_close(cdk_channel_t* channel, const char* error) {
static void _close_cb(cdk_channel_t* channel, const char* error) {
cdk_loge("connection closed, reason: %s\n", error);
}

static void handle_heartbeat(cdk_channel_t* channel) {
net_msg_t* msg = malloc(sizeof(net_msg_t) + sizeof(keepalive));
if (msg) {
msg->hdr.size = htonl((uint32_t)(sizeof(keepalive)));
msg->hdr.type = htonl(KEEPALIVE_MSG_TYPE);
memcpy(msg->data, keepalive, sizeof(keepalive));
cdk_net_send(channel, msg, sizeof(net_msg_t) + sizeof(keepalive));

free(msg);
msg = NULL;
static void _heartbeat_cb(cdk_channel_t* channel) {
msg_t* smsg = malloc(sizeof(msg_t) + sizeof(KEEPALIVE));
if (smsg) {
smsg->hdr.size = htonl((uint32_t)(sizeof(KEEPALIVE)));
smsg->hdr.type = htonl(TYPE_KEEPALIVE);
memcpy(smsg->data, KEEPALIVE, sizeof(KEEPALIVE));
cdk_net_send(channel, smsg, sizeof(msg_t) + sizeof(KEEPALIVE));
free(smsg);
smsg = NULL;
}
}

Expand All @@ -79,9 +76,6 @@ int main(void) {
.verifypeer = true
}
};
cdk_net_startup(&conf);
cdk_logger_create(NULL, false);

cdk_unpacker_t unpacker = {
.type = TYPE_LENGTHFIELD,
.lengthfield.adj = 0,
Expand All @@ -91,15 +85,17 @@ int main(void) {
.lengthfield.size = 4
};
cdk_handler_t handler = {
.tcp.on_connect = handle_connect,
.tcp.on_read = handle_read,
.tcp.on_close = handle_close,
.tcp.on_heartbeat = handle_heartbeat,
.tcp.on_connect = _connect_cb,
.tcp.on_read = _read_cb,
.tcp.on_close = _close_cb,
.tcp.on_heartbeat = _heartbeat_cb,
.tcp.wr_timeout = 10000,
.tcp.conn_timeout = 5000,
.tcp.hb_interval = 5000,
.tcp.unpacker = &unpacker
};
cdk_net_startup(&conf);
cdk_logger_create(NULL, false);
cdk_net_dial("tcp", "127.0.0.1", "9999", &handler);
cdk_net_cleanup();
cdk_logger_destroy();
Expand Down
79 changes: 38 additions & 41 deletions examples/example-async-tcp-server.c
Original file line number Diff line number Diff line change
@@ -1,62 +1,60 @@
#include "cdk.h"

typedef enum net_msg_type_e {
KEEPALIVE_MSG_TYPE,
PING_MSG_TYPE,
PONG_MSG_TYPE,
}net_msg_type_t;
typedef enum msg_type_e {
TYPE_KEEPALIVE,
TYPE_PING,
TYPE_PONG,
} msg_type_t;

typedef struct net_msg_hdr_s {
typedef struct msg_hdr_s {
uint32_t size;
net_msg_type_t type;
}net_msg_hdr_t;
msg_type_t type;
} msg_hdr_t;

typedef struct net_msg_s {
net_msg_hdr_t hdr;
typedef struct msg_s {
msg_hdr_t hdr;
char data[];
}net_msg_t;
} msg_t;

char pong[16] = "PONG";
char keepalive[16] = "KEEPALIVE";
char PONG[16] = "PONG";
char KEEPALIVE[16] = "KEEPALIVE";

static void handle_accept(cdk_channel_t* channel) {
static void _accept_cb(cdk_channel_t* channel) {
cdk_logi("tid[%d], [%d]new connection coming...\n", (int)cdk_utils_systemtid(), (int)channel->fd);
}

static void handle_read(cdk_channel_t* channel, void* buf, size_t len) {
net_msg_t* rmsg = buf;
static void _read_cb(cdk_channel_t* channel, void* buf, size_t len) {
msg_t* rmsg = buf;
cdk_address_t addrinfo;
cdk_net_extract_address(channel->fd, &addrinfo, true);
cdk_logi("recv %s from %s:%d. type: %d, len: %d.\n", rmsg->data, addrinfo.addr, addrinfo.port, ntohl(rmsg->hdr.type), ntohl(rmsg->hdr.size));

if (ntohl(rmsg->hdr.type) == PING_MSG_TYPE) {
net_msg_t* smsg = malloc(sizeof(net_msg_t) + sizeof(pong));
if (ntohl(rmsg->hdr.type) == TYPE_PING) {
msg_t* smsg = malloc(sizeof(msg_t) + sizeof(PONG));
if (smsg) {
smsg->hdr.size = htonl((uint32_t)(sizeof(pong)));
smsg->hdr.type = htonl(PONG_MSG_TYPE);
memcpy(smsg->data, pong, sizeof(pong));
cdk_net_send(channel, smsg, sizeof(net_msg_t) + sizeof(pong));

smsg->hdr.size = htonl((uint32_t)(sizeof(PONG)));
smsg->hdr.type = htonl(TYPE_PONG);
memcpy(smsg->data, PONG, sizeof(PONG));
cdk_net_send(channel, smsg, sizeof(msg_t) + sizeof(PONG));
free(smsg);
smsg = NULL;
}
}
}

static void handle_close(cdk_channel_t* channel, const char* error) {
static void _close_cb(cdk_channel_t* channel, const char* error) {
cdk_loge("connection closed, reason: %s\n", error);
}

static void handle_heartbeat(cdk_channel_t* channel) {
net_msg_t* msg = malloc(sizeof(net_msg_t) + sizeof(keepalive));
if (msg) {
msg->hdr.size = htonl((uint32_t)(sizeof(keepalive)));
msg->hdr.type = htonl(KEEPALIVE_MSG_TYPE);
memcpy(msg->data, keepalive, sizeof(keepalive));
cdk_net_send(channel, msg, sizeof(net_msg_t) + sizeof(keepalive));

free(msg);
msg = NULL;
static void _heartbeat_cb(cdk_channel_t* channel) {
msg_t* smsg = malloc(sizeof(msg_t) + sizeof(KEEPALIVE));
if (smsg) {
smsg->hdr.size = htonl((uint32_t)(sizeof(KEEPALIVE)));
smsg->hdr.type = htonl(TYPE_KEEPALIVE);
memcpy(smsg->data, KEEPALIVE, sizeof(KEEPALIVE));
cdk_net_send(channel, smsg, sizeof(msg_t) + sizeof(KEEPALIVE));
free(smsg);
smsg = NULL;
}
}

Expand All @@ -71,9 +69,6 @@ int main(void) {
.verifypeer = false
}
};
cdk_net_startup(&conf);
cdk_logger_create(NULL, false);

cdk_unpacker_t unpacker = {
.type = TYPE_LENGTHFIELD,
.lengthfield.adj = 0,
Expand All @@ -83,14 +78,16 @@ int main(void) {
.lengthfield.size = 4
};
cdk_handler_t handler = {
.tcp.on_accept = handle_accept,
.tcp.on_read = handle_read,
.tcp.on_close = handle_close,
.tcp.on_heartbeat = handle_heartbeat,
.tcp.on_accept = _accept_cb,
.tcp.on_read = _read_cb,
.tcp.on_close = _close_cb,
.tcp.on_heartbeat = _heartbeat_cb,
.tcp.rd_timeout = 10000,
.tcp.hb_interval = 5000,
.tcp.unpacker = &unpacker
};
cdk_net_startup(&conf);
cdk_logger_create(NULL, false);
cdk_net_listen("tcp", "0.0.0.0", "9999", &handler);
cdk_net_cleanup();
cdk_logger_destroy();
Expand Down
Loading

0 comments on commit c106ba0

Please sign in to comment.