max number of clients added

pull/16/head
Martin Ger 2017-11-05 15:36:06 +01:00
rodzic ce86663aed
commit 23c54e7cd7
12 zmienionych plików z 40 dodań i 14 usunięć

Wyświetl plik

@ -12,7 +12,7 @@ bool MQTT_server_start(uint16_t portno, uint16_t max_subscriptions, uint16_t max
typedef void (*MqttDataCallback)(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh);
typedef bool (*MqttAuthCallback)(const char* username, const char *password, struct espconn *pesp_conn);
typedef bool (*MqttConnectCallback)(struct espconn *pesp_conn);
typedef bool (*MqttConnectCallback)(struct espconn *pesp_conn, uint16_t client_count);
void MQTT_server_onData(MqttDataCallback dataCb);
void MQTT_server_onAuth(MqttAuthCallback authCb);

Wyświetl plik

@ -68,6 +68,7 @@ MQTT broker related command:
- set broker_access _mode_: controls the networks that allow MQTT broker access (0: no access, 1: only internal, 2: only external, 3: both (default))
- set broker_subscriptions _max_: sets the max number of subscription the broker can store (default: 30)
- set broker_retained_messages _max_: sets the max number of retained messages the broker can store (default: 30)
- set broker_clients _clients_max_: sets the max number of concurrent client connections (default: 8)
- save_retained: saves the current state of all retained topics (max. 4096 Bytes in sum) to flash, so they will persist a reboot
- delete_retained: deletes the state of all retained topics in RAM and flash
- set broker_autoretain [0|1]: selects, whether the broker should do a "save_retained" automatically each time it receives a new retained message (default off). With this option on the broker can be resetted at any time without loosing state. However, this is slow and too many writes may damage flash mem.
@ -269,14 +270,14 @@ typedef bool (*MqttAuthCallback)(const char* username, const char *password, str
void MQTT_server_onAuth(MqttAuthCallback authCb);
typedef bool (*MqttConnectCallback)(struct espconn *pesp_conn);
typedef bool (*MqttConnectCallback)(struct espconn *pesp_conn, uint16_t client_count);
void MQTT_server_onConnect(MqttConnectCallback connectCb);
```
If an *MqttAuthCallback* function is registered with MQTT_server_onAuth(), it is called on each connect request. Based on username, password, and optionally the connection info (e.g. the IP address) the function has to return *true* for authenticated or *false* for rejected. If a request provides no username and/or password these parameter strings are empty. If no *MqttAuthCallback* function is set, each request will be admitted.
The *MqttConnectCallback* function does a similar check for the connection, but it is called right after the connect request before any internal status is allocated. This is done in order to reject requests from unautorized clients in an early stage.
The *MqttConnectCallback* function does a similar check for the connection, but it is called right after the connect request before the MQTT connect request is processed. This is done in order to reject requests from unautorized clients in an early stage. The number of currently connected clients (incl. the current one) is given in the *client_count* paramater. With this info you can reject too many concurrent connections.
# Limitations on the number of clients
To adjust memory consumption of one MQTT connection and thus the max number of concurrent connections you can redefine MQTT_BUF_SIZE and QUEUE_BUFFER_SIZE in "user_config.h". MQTT_BUF_SIZE is the max. size of pending inbound messages for one connection (and thus also the max. size of a single MQTT message) and QUEUE_BUFFER_SIZE is the max. size of all pending outbound messages for one connection. Currently these parameters are set to 1024 resp. 2048 bytes, resulting in a memory consumption of about 4 KB per connection and a max number of connections of about 8-9 (depending on the memory usage of the rest of the program). When you reduce buffer sizes, e.g. to 512 and 1024 bytes, a single connection requires only about 2.5 KB resulting in up to 13 possible concurrent MQTT connections. In any case you have to increase the number of TCP connections (default 5) first by calling "espconn_tcp_set_max_con(n)" with n, the max. number of concurrent TCP connections, less or equal to 15.

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -1,2 +1,2 @@
7d439d81815850d151040d962e550f17cc914e19 0x00000.bin
06c6e4b1e89a1cc0ddaf8c91265e9eac35253a02 0x10000.bin
007d430ba40457630d310b60c45fad3f1b027a77 0x00000.bin
3cf10a8b1e8c9b77b4db3b2f189f940de2dfe434 0x10000.bin

Wyświetl plik

@ -15,7 +15,7 @@
#define LOCAL_MQTT_CLIENT ((void*)-1)
typedef bool (*MqttAuthCallback)(const char* username, const char *password, struct espconn *pesp_conn);
typedef bool (*MqttConnectCallback)(struct espconn *pesp_conn);
typedef bool (*MqttConnectCallback)(struct espconn *pesp_conn, uint16_t client_count);
typedef struct _MQTT_ClientCon {
struct espconn *pCon;
@ -39,6 +39,7 @@ typedef struct _MQTT_ClientCon {
} MQTT_ClientCon;
extern MQTT_ClientCon *clientcon_list;
uint16_t MQTT_CountClientCon();
bool MQTT_server_start(uint16_t portno, uint16_t max_subscriptions, uint16_t max_retained_topics);
void MQTT_server_onConnect(MqttConnectCallback connectCb);

Wyświetl plik

@ -154,6 +154,13 @@ bool ICACHE_FLASH_ATTR MQTT_InitClientCon(MQTT_ClientCon * mqttClientCon) {
return true;
}
uint16_t ICACHE_FLASH_ATTR MQTT_CountClientCon() {
MQTT_ClientCon *p;
uint16_t count = 0;
for (p = clientcon_list; p != NULL; p = p->next, count++);
return count;
}
bool ICACHE_FLASH_ATTR MQTT_DeleteClientCon(MQTT_ClientCon * mqttClientCon) {
MQTT_INFO("MQTT:DeleteClientCon\r\n");
@ -815,7 +822,7 @@ static void ICACHE_FLASH_ATTR MQTT_ClientCon_connected_cb(void *arg) {
os_timer_setfn(&mqttClientCon->mqttTimer, (os_timer_func_t *) mqtt_server_timer, mqttClientCon);
os_timer_arm(&mqttClientCon->mqttTimer, 1000, 1);
if (local_connect_cb != NULL && local_connect_cb(pespconn) == false) {
if (local_connect_cb != NULL && local_connect_cb(pespconn, MQTT_CountClientCon()) == false) {
mqttClientCon->connState = TCP_DISCONNECT;
system_os_post(MQTT_SERVER_TASK_PRIO, 0, (os_param_t) mqttClientCon);
return;

Wyświetl plik

@ -39,6 +39,7 @@ void config_load_default(sysconfig_p config) {
config->max_subscriptions = 30;
config->max_retained_messages = 30;
config->max_clients = 8;
config->auto_retained = 0;
os_sprintf(config->mqtt_broker_user, "%s", "none");
config->mqtt_broker_password[0] = 0;

Wyświetl plik

@ -49,8 +49,9 @@ typedef struct
uint16_t config_port; // Port on which the concole listenes (0 if no access)
uint8_t config_access; // Controls the interfaces that allow config access (default LOCAL_ACCESS | REMOTE_ACCESS)
uint16_t max_subscriptions; // Upper limit of subscribed topics
uint16_t max_retained_messages; // Upper limit of stored retained messages
uint16_t max_subscriptions; // Upper limit on subscribed topics
uint16_t max_retained_messages; // Upper limit on stored retained messages
uint16_t max_clients; // Upper limit on concurrently connected clients
uint8_t auto_retained; // Automatically save retained messages to flash (default: off)
uint8_t mqtt_broker_user[32]; // Username for client login, "none" if empty
uint8_t mqtt_broker_password[32]; // Password for client login

Wyświetl plik

@ -931,7 +931,7 @@ int ICACHE_FLASH_ATTR parse_action(int next_token, bool doit) {
}
#ifdef HTTPC
else if (is_token(next_token, "http_get")) {
len_check(2);
len_check(1);
char *url_data;
int url_len;

Wyświetl plik

@ -456,7 +456,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
to_console(response);
os_sprintf(response, "set [network|dns|ip|netmask|gw|config_port|config_access] <val>\r\n");
to_console(response);
os_sprintf(response, "set [broker_user|broker_password|broker_access] <val>\r\n");
os_sprintf(response, "set [broker_user|broker_password|broker_access|broker_clients] <val>\r\n");
to_console(response);
os_sprintf(response, "set [broker_subscriptions|broker_retained_messages|broker_autoretain] <val>\r\n");
to_console(response);
@ -529,7 +529,11 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
os_sprintf(response, "MQTT broker max. subscription: %d\r\nMQTT broker max. retained messages: %d%s\r\n",
config.max_subscriptions, config.max_retained_messages, config.auto_retained?" (auto saved)":"");
to_console(response);
to_console(response);
os_sprintf(response, "MQTT broker max. clients: %d\r\n", config.max_clients);
to_console(response);
if (os_strcmp(config.mqtt_broker_user, "none") != 0) {
os_sprintf(response,
"MQTT broker username: %s\r\nMQTT broker password: %s\r\n",
@ -616,7 +620,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
MQTT_ClientCon *clientcon;
int ccnt = 0;
os_sprintf(response, "Current clients:\r\n");
os_sprintf(response, "Current clients: %d\r\n", MQTT_CountClientCon());
to_console(response);
for (clientcon = clientcon_list; clientcon != NULL; clientcon = clientcon->next, ccnt++) {
os_sprintf(response, "%s%s", clientcon->connect_info.client_id, clientcon->next != NULL ? ", " : "");
@ -1144,6 +1148,12 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
goto command_handled;
}
if (strcmp(tokens[1], "broker_clients") == 0) {
config.max_clients = atoi(tokens[2]);
os_sprintf(response, "Broker max clients set\r\n");
goto command_handled;
}
if (strcmp(tokens[1], "broker_user") == 0) {
os_strncpy(config.mqtt_broker_user, tokens[2], 32);
config.mqtt_broker_user[31] = '\0';
@ -1656,7 +1666,7 @@ bool ICACHE_FLASH_ATTR mqtt_broker_auth(const char* username, const char *passwo
}
bool ICACHE_FLASH_ATTR mqtt_broker_connect(struct espconn *pesp_conn) {
bool ICACHE_FLASH_ATTR mqtt_broker_connect(struct espconn *pesp_conn, uint16_t client_count) {
//os_printf("connect from " IPSTR "\r\n", IP2STR((ip_addr_t *)&(pesp_conn->proto.tcp->remote_ip)));
if (!check_connection_access(pesp_conn, config.mqtt_broker_access)) {
@ -1665,6 +1675,11 @@ bool ICACHE_FLASH_ATTR mqtt_broker_connect(struct espconn *pesp_conn) {
return false;
}
if (client_count > config.max_clients) {
os_printf("Client disconnected - too many concurrent clients\r\n");
return false;
}
return true;
}