90 lines
2.3 KiB
C
90 lines
2.3 KiB
C
#include <stdio.h>
|
|||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#include "dataserver_inner.h"
|
||
|
|
#include "list.h"
|
||
|
|
|
||
|
|
#define ds_inf(_fmt_,...) printf("I %s:%d "_fmt_"\n", __func__, __LINE__, ##__VA_ARGS__)
|
||
|
|
#define ds_err(_fmt_,...) printf("E %s:%d "_fmt_"\n", __func__, __LINE__, ##__VA_ARGS__)
|
||
|
|
|
||
|
|
static int server_stat(ds_connection_t *conn,
|
||
|
|
uint8_t *cmd, size_t size, uint8_t *resp, size_t resp_size)
|
||
|
|
{
|
||
|
|
int ret = 0;
|
||
|
|
int cnt = 0;
|
||
|
|
|
||
|
|
#define stat_printf(_fmt, ...) do {\
|
||
|
|
if ((resp_size - cnt) > 1) {\
|
||
|
|
ret = snprintf((char *)resp + cnt, resp_size - cnt, _fmt, ##__VA_ARGS__);\
|
||
|
|
if (ret <= 0) {\
|
||
|
|
return cnt + 1;\
|
||
|
|
} else {\
|
||
|
|
cnt += ret;\
|
||
|
|
}\
|
||
|
|
} else {\
|
||
|
|
ds_err("resp not enough");\
|
||
|
|
}\
|
||
|
|
} while (0)
|
||
|
|
|
||
|
|
stat_printf("ok");
|
||
|
|
cnt++;
|
||
|
|
stat_printf("conns:\n");
|
||
|
|
{
|
||
|
|
ds_server_t *server = (ds_server_t *)conn->server;
|
||
|
|
|
||
|
|
ds_connection_t *conn;
|
||
|
|
list_for_each_entry(conn, &server->conns, head) {
|
||
|
|
stat_printf("\t[%s]-[%s] fd[%d] active[%d] rb[%#zx/%#zx] flag[%#x]\n",
|
||
|
|
conn->device, conn->client, conn->fd, conn->active,
|
||
|
|
ds_rb_avail(&conn->rb), conn->rb.size, ds_rb_get_flag(&conn->rb));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return cnt + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
static ds_control_t controls[] = {
|
||
|
|
{.cmd = "stat2", .fn = server_stat, .desc = "stat2"},
|
||
|
|
};
|
||
|
|
static int num_controls = sizeof(controls) / sizeof(controls[0]);
|
||
|
|
|
||
|
|
int load(ds_connection_t *conn)
|
||
|
|
{
|
||
|
|
ds_inf("plugin loaded");
|
||
|
|
|
||
|
|
int i;
|
||
|
|
for (i = 0; i < num_controls; i++) {
|
||
|
|
int found = 0;
|
||
|
|
ds_control_t *ctl;
|
||
|
|
list_for_each_entry(ctl, &conn->controls, head) {
|
||
|
|
if (strcmp(ctl->cmd, controls[i].cmd) == 0) {
|
||
|
|
found = 1;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!found) {
|
||
|
|
list_add_tail(&controls[i].head, &conn->controls);
|
||
|
|
} else {
|
||
|
|
ds_err("cmd[%s] conflict, ignore the one in plugin",
|
||
|
|
controls[i].cmd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
int unload(ds_connection_t *conn)
|
||
|
|
{
|
||
|
|
int i;
|
||
|
|
for (i = 0; i < num_controls; i++) {
|
||
|
|
ds_control_t *ctl, *tmp;
|
||
|
|
list_for_each_entry_safe(ctl, tmp, &conn->controls, head) {
|
||
|
|
if (strcmp(ctl->cmd, controls[i].cmd) == 0)
|
||
|
|
list_del(&ctl->head);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
ds_inf("plugin unloaded");
|
||
|
|
return 0;
|
||
|
|
}
|