Przeglądaj źródła

Add hv_rand, hv_random_string

ithewei 3 lat temu
rodzic
commit
053d849923

+ 31 - 0
base/hbase.c

@@ -6,6 +6,10 @@
 
 #include "hatomic.h"
 
+#ifndef RAND_MAX
+#define RAND_MAX 2147483647
+#endif
+
 static hatomic_t s_alloc_cnt = HATOMIC_VAR_INIT(0);
 static hatomic_t s_free_cnt = HATOMIC_VAR_INIT(0);
 
@@ -330,3 +334,30 @@ char* get_executable_file(char* buf, int size) {
 char* get_run_dir(char* buf, int size) {
     return getcwd(buf, size);
 }
+
+int hv_rand(int min, int max) {
+    static int s_seed = 0;
+    assert(max > min);
+
+    if (s_seed == 0) {
+        s_seed = time(NULL);
+        srand(s_seed);
+    }
+
+    int _rand = rand();
+    _rand = min + (int) ((double) ((double) (max) - (min) + 1.0) * ((_rand) / ((RAND_MAX) + 1.0)));
+    return _rand;
+}
+
+void hv_random_string(char *buf, int len) {
+    static char s_characters[] = {
+        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
+        'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+        'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+    };
+    int i = 0;
+    for (; i < len; i++) {
+        buf[i] = s_characters[hv_rand(0, sizeof(s_characters) - 1)];
+    }
+    buf[i] = '\0';
+}

+ 4 - 0
base/hbase.h

@@ -105,6 +105,10 @@ HV_EXPORT char* get_executable_dir(char* buf, int size);
 HV_EXPORT char* get_executable_file(char* buf, int size);
 HV_EXPORT char* get_run_dir(char* buf, int size);
 
+// random
+HV_EXPORT int   hv_rand(int min, int max);
+HV_EXPORT void  hv_random_string(char *buf, int len);
+
 END_EXTERN_C
 
 #endif // HV_BASE_H_

+ 4 - 0
docs/API.md

@@ -124,6 +124,8 @@
 - get_executable_dir
 - get_executable_file
 - get_run_dir
+- hv_rand
+- hv_random_string
 
 ### hversion.h
 - hv_version
@@ -620,6 +622,8 @@
 - mqtt_client_get_last_error
 - mqtt_client_set_ssl_ctx
 - mqtt_client_new_ssl_ctx
+- mqtt_client_set_reconnect
+- mqtt_client_reconnect
 - mqtt_client_connect
 - mqtt_client_disconnect
 - mqtt_client_publish

+ 1 - 6
examples/mqtt/mqtt_sub.c

@@ -56,7 +56,7 @@ static void on_mqtt(mqtt_client_t* cli, int type) {
         printf("mqtt connack!\n");
     {
         const char* topic = (const char*)mqtt_client_get_userdata(cli);
-        int mid = mqtt_client_subscribe(cli, topic, 2);
+        int mid = mqtt_client_subscribe(cli, topic, 0);
         printf("mqtt subscribe mid=%d\n", mid);
     }
         break;
@@ -74,11 +74,6 @@ static int mqtt_subscribe(const char* host, int port, const char* topic) {
     mqtt_client_t* cli = mqtt_client_new(NULL);
     if (cli == NULL) return -1;
 
-    // client_id
-    char client_id[64];
-    snprintf(client_id, sizeof(client_id), "mqtt_sub_%ld", hv_getpid());
-    printf("client_id: %s\n", client_id);
-    mqtt_client_set_id(cli, client_id);
 #if TEST_AUTH
     mqtt_client_set_auth(cli, "test", "123456");
 #endif

+ 1 - 2
examples/tcp_chat_server.c

@@ -151,8 +151,7 @@ int main(int argc, char** argv) {
 
     s_chatroom.loop = loop;
     s_chatroom.listenio = listenio;
-    srand(time(NULL));
-    s_chatroom.roomid = rand() % 1000000;
+    s_chatroom.roomid = hv_rand(100000, 999999);
     list_init(&s_chatroom.conns);
 
     hloop_run(loop);

+ 15 - 5
mqtt/mqtt_client.c

@@ -57,8 +57,12 @@ static int mqtt_client_login(mqtt_client_t* cli) {
 
     if (*cli->client_id) {
         cid_len = strlen(cli->client_id);
-        len += cid_len;
+    } else {
+        cid_len = 20;
+        hv_random_string(cli->client_id, cid_len);
+        hlogi("MQTT client_id: %.*s", (int)cid_len, cli->client_id);
     }
+    len += cid_len;
     if (cid_len == 0) cli->clean_session = 1;
     if (cli->clean_session) {
         conn_flags |= MQTT_CONN_CLEAN_SESSION;
@@ -101,10 +105,15 @@ static int mqtt_client_login(mqtt_client_t* cli) {
     unsigned char* p = buf;
     int headlen = mqtt_head_pack(&head, p);
     p += headlen;
-    // TODO: just implement MQTT_PROTOCOL_V311
-    PUSH16(p, 4);
-    PUSH_N(p, "MQTT", 4);
-    PUSH8(p, MQTT_PROTOCOL_V311);
+    // TODO: Not implement MQTT_PROTOCOL_V5
+    if (cli->protocol_version == MQTT_PROTOCOL_V31) {
+        PUSH16(p, 6);
+        PUSH_N(p, MQTT_PROTOCOL_NAME_v31, 6);
+    } else {
+        PUSH16(p, 4);
+        PUSH_N(p, MQTT_PROTOCOL_NAME, 4);
+    }
+    PUSH8(p, cli->protocol_version);
     PUSH8(p, conn_flags);
     PUSH16(p, cli->keepalive);
     PUSH16(p, cid_len);
@@ -319,6 +328,7 @@ mqtt_client_t* mqtt_client_new(hloop_t* loop) {
     HV_ALLOC_SIZEOF(cli);
     if (cli == NULL) return NULL;
     cli->loop = loop;
+    cli->protocol_version = MQTT_PROTOCOL_V311;
     cli->keepalive = DEFAULT_MQTT_KEEPALIVE;
     hmutex_init(&cli->mutex_);
     return cli;

+ 6 - 5
mqtt/mqtt_client.h

@@ -22,10 +22,11 @@ struct mqtt_client_s {
     reconn_setting_t* reconn_setting;
     // login: flags + keepalive + client_id + will + username + password
     // flags
-    unsigned short clean_session:   1;
-    unsigned short ssl: 1; // Read Only
-    unsigned short alloced_ssl_ctx: 1; // intern
-    unsigned short keepalive;
+    unsigned char   protocol_version; // Default MQTT_PROTOCOL_V311
+    unsigned char   clean_session:   1;
+    unsigned char   ssl: 1; // Read Only
+    unsigned char   alloced_ssl_ctx: 1; // intern
+    unsigned short  keepalive;
     char client_id[64];
     // will
     mqtt_message_t* will;
@@ -112,7 +113,7 @@ HV_EXPORT int mqtt_client_publish(mqtt_client_t* cli,
 
 // subscribe
 HV_EXPORT int mqtt_client_subscribe(mqtt_client_t* cli,
-        const char* topic, int qos);
+        const char* topic, int qos DEFAULT(0));
 
 // unsubscribe
 HV_EXPORT int mqtt_client_unsubscribe(mqtt_client_t* cli,

+ 4 - 1
mqtt/mqtt_protocol.h

@@ -5,10 +5,13 @@
 
 #define DEFAULT_MQTT_PORT   1883
 
-#define MQTT_PROTOCOL_V31   3 // Deprecated
+#define MQTT_PROTOCOL_V31   3
 #define MQTT_PROTOCOL_V311  4
 #define MQTT_PROTOCOL_V5    5 // Not yet supproted
 
+#define MQTT_PROTOCOL_NAME      "MQTT"
+#define MQTT_PROTOCOL_NAME_v31  "MQIsdp"
+
 /*
  * MQTT connect
  * 2 + 4 protocol_name + 1 protocol_version + 1 conn_flags + 2 keepalive + 2 + [client_id] +