瀏覽代碼

Add some errno

ithewei 3 年之前
父節點
當前提交
2d1240040b
共有 5 個文件被更改,包括 22 次插入7 次删除
  1. 5 0
      base/herr.h
  2. 3 1
      event/hevent.c
  3. 9 2
      event/nio.c
  4. 3 3
      http/client/http_client.cpp
  5. 2 1
      mqtt/mqtt_client.c

+ 5 - 0
base/herr.h

@@ -46,6 +46,11 @@
     F(1032, READ_FILE,      "Read file failed") \
     F(1033, WRITE_FILE,     "Write file failed")\
     \
+    F(1040, SSL,            "SSL/TLS error")        \
+    F(1041, NEW_SSL_CTX,    "New SSL_CTX failed")   \
+    F(1042, NEW_SSL,        "New SSL failed")       \
+    F(1043, SSL_HANDSHAKE,  "SSL handshake failed") \
+    \
     F(1100, TASK_TIMEOUT,       "Task timeout")     \
     F(1101, TASK_QUEUE_FULL,    "Task queue full")  \
     F(1102, TASK_QUEUE_EMPTY,   "Task queue empty") \

+ 3 - 1
event/hevent.c

@@ -2,6 +2,7 @@
 #include "hsocket.h"
 #include "hatomic.h"
 #include "hlog.h"
+#include "herr.h"
 
 #include "unpack.h"
 
@@ -479,7 +480,7 @@ int hio_set_ssl_ctx(hio_t* io, hssl_ctx_t ssl_ctx) {
 
 int hio_new_ssl_ctx(hio_t* io, hssl_ctx_opt_t* opt) {
     hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
-    if (ssl_ctx == NULL) return HSSL_ERROR;
+    if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
     io->alloced_ssl_ctx = 1;
     return hio_set_ssl_ctx(io, ssl_ctx);
 }
@@ -689,6 +690,7 @@ void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
 void hio_alloc_readbuf(hio_t* io, int len) {
     if (len > MAX_READ_BUFSIZE) {
         hloge("read bufsize > %u, close it!", (unsigned int)MAX_READ_BUFSIZE);
+        io->error = ERR_OVER_LIMIT;
         hio_close_async(io);
         return;
     }

+ 9 - 2
event/nio.c

@@ -4,6 +4,7 @@
 #include "hsocket.h"
 #include "hssl.h"
 #include "hlog.h"
+#include "herr.h"
 #include "hthread.h"
 
 static void __connect_timeout_cb(htimer_t* timer) {
@@ -82,6 +83,7 @@ static void ssl_server_handshake(hio_t* io) {
     }
     else {
         hloge("ssl handshake failed: %d", ret);
+        io->error = ERR_SSL_HANDSHAKE;
         hio_close(io);
     }
 }
@@ -104,6 +106,7 @@ static void ssl_client_handshake(hio_t* io) {
     }
     else {
         hloge("ssl handshake failed: %d", ret);
+        io->error = ERR_SSL_HANDSHAKE;
         hio_close(io);
     }
 }
@@ -149,12 +152,12 @@ static void nio_accept(hio_t* io) {
                     io->alloced_ssl_ctx = 1;
                 }
                 if (ssl_ctx == NULL) {
-                    io->error = HSSL_ERROR;
+                    io->error = ERR_NEW_SSL_CTX;
                     goto accept_error;
                 }
                 hssl_t ssl = hssl_new(ssl_ctx, connfd);
                 if (ssl == NULL) {
-                    io->error = HSSL_ERROR;
+                    io->error = ERR_NEW_SSL;
                     goto accept_error;
                 }
                 connio->ssl = ssl;
@@ -199,10 +202,12 @@ static void nio_connect(hio_t* io) {
                     io->alloced_ssl_ctx = 1;
                 }
                 if (ssl_ctx == NULL) {
+                    io->error = ERR_NEW_SSL_CTX;
                     goto connect_error;
                 }
                 hssl_t ssl = hssl_new(ssl_ctx, io->fd);
                 if (ssl == NULL) {
+                    io->error = ERR_NEW_SSL;
                     goto connect_error;
                 }
                 io->ssl = ssl;
@@ -417,6 +422,7 @@ int hio_connect(hio_t* io) {
     if (ret < 0 && socket_errno() != EINPROGRESS) {
 #endif
         perror("connect");
+        io->error = socket_errno();
         hio_close(io);
         return ret;
     }
@@ -489,6 +495,7 @@ enqueue:
         if (io->write_bufsize + len - nwrite > MAX_WRITE_BUFSIZE) {
             if (io->write_bufsize > MAX_WRITE_BUFSIZE) {
                 hloge("write bufsize > %u, close it!", (unsigned int)MAX_WRITE_BUFSIZE);
+                io->error = ERR_OVER_LIMIT;
                 goto write_error;
             }
         }

+ 3 - 3
http/client/http_client.cpp

@@ -119,7 +119,7 @@ int http_client_set_ssl_ctx(http_client_t* cli, hssl_ctx_t ssl_ctx) {
 int http_client_new_ssl_ctx(http_client_t* cli, hssl_ctx_opt_t* opt) {
     opt->endpoint = HSSL_CLIENT;
     hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
-    if (ssl_ctx == NULL) return HSSL_ERROR;
+    if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
     cli->alloced_ssl_ctx = true;
     return http_client_set_ssl_ctx(cli, ssl_ctx);
 }
@@ -452,12 +452,12 @@ static int http_client_connect(http_client_t* cli, const char* host, int port, i
         }
         if (ssl_ctx == NULL) {
             closesocket(connfd);
-            return HSSL_ERROR;
+            return NABS(ERR_NEW_SSL_CTX);
         }
         cli->ssl = hssl_new(ssl_ctx, connfd);
         if (cli->ssl == NULL) {
             closesocket(connfd);
-            return HSSL_ERROR;
+            return NABS(ERR_NEW_SSL);
         }
         if (!is_ipaddr(host)) {
             hssl_set_sni_hostname(cli->ssl, host);

+ 2 - 1
mqtt/mqtt_client.c

@@ -1,6 +1,7 @@
 #include "mqtt_client.h"
 #include "hbase.h"
 #include "hlog.h"
+#include "herr.h"
 #include "hendian.h"
 
 static unsigned short mqtt_next_mid() {
@@ -429,7 +430,7 @@ int mqtt_client_set_ssl_ctx(mqtt_client_t* cli, hssl_ctx_t ssl_ctx) {
 int mqtt_client_new_ssl_ctx(mqtt_client_t* cli, hssl_ctx_opt_t* opt) {
     opt->endpoint = HSSL_CLIENT;
     hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
-    if (ssl_ctx == NULL) return HSSL_ERROR;
+    if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
     cli->alloced_ssl_ctx = true;
     return mqtt_client_set_ssl_ctx(cli, ssl_ctx);
 }