ithewei преди 4 години
родител
ревизия
1ce991a24a
променени са 11 файла, в които са добавени 68 реда и са изтрити 36 реда
  1. 26 22
      event/nio.c
  2. 0 4
      evpp/Channel.h
  3. 22 3
      evpp/TcpClient.h
  4. 1 1
      evpp/TcpClient_test.cpp
  5. 5 2
      evpp/TcpServer.h
  6. 5 0
      evpp/UdpClient.h
  7. 2 1
      examples/http_server_test.cpp
  8. 1 0
      examples/httpd/httpd.cpp
  9. 2 1
      examples/tcp_echo_server.c
  10. 2 1
      examples/websocket_server_test.cpp
  11. 2 1
      http/client/http_client.cpp

+ 26 - 22
event/nio.c

@@ -158,19 +158,20 @@ static void ssl_client_handshake(hio_t* io) {
 
 static void nio_accept(hio_t* io) {
     // printd("nio_accept listenfd=%d\n", io->fd);
+    int connfd = 0, err = 0;
     socklen_t addrlen;
 accept:
     addrlen = sizeof(sockaddr_u);
-    int connfd = accept(io->fd, io->peeraddr, &addrlen);
+    connfd = accept(io->fd, io->peeraddr, &addrlen);
     hio_t* connio = NULL;
     if (connfd < 0) {
-        if (socket_errno() == EAGAIN) {
+        err = socket_errno();
+        if (err == EAGAIN) {
             //goto accept_done;
             return;
-        }
-        else {
-            io->error = socket_errno();
+        } else {
             perror("accept");
+            io->error = err;
             goto accept_error;
         }
     }
@@ -216,7 +217,7 @@ static void nio_connect(hio_t* io) {
     int ret = getpeername(io->fd, io->peeraddr, &addrlen);
     if (ret < 0) {
         io->error = socket_errno();
-        printd("connect failed: %s: %d\n", strerror(socket_errno()), socket_errno());
+        printd("connect failed: %s: %d\n", strerror(io->error), io->error);
         goto connect_failed;
     }
     else {
@@ -305,20 +306,23 @@ static int __nio_write(hio_t* io, const void* buf, int len) {
 static void nio_read(hio_t* io) {
     // printd("nio_read fd=%d\n", io->fd);
     void* buf;
-    int len, nread;
+    int len = 0, nread = 0, err = 0;
 read:
     buf = io->readbuf.base + io->readbuf.offset;
     len = io->readbuf.len - io->readbuf.offset;
     nread = __nio_read(io, buf, len);
     // printd("read retval=%d\n", nread);
     if (nread < 0) {
-        if (socket_errno() == EAGAIN) {
-            //goto read_done;
+        err = socket_errno();
+        if (err == EAGAIN) {
+            // goto read_done;
             return;
-        }
-        else {
-            io->error = socket_errno();
+        } else if (err == EMSGSIZE) {
+            // ignore
+            return;
+        } else {
             // perror("read");
+            io->error = err;
             goto read_error;
         }
     }
@@ -337,7 +341,7 @@ disconnect:
 
 static void nio_write(hio_t* io) {
     // printd("nio_write fd=%d\n", io->fd);
-    int nwrite = 0;
+    int nwrite = 0, err = 0;
     hrecursive_mutex_lock(&io->write_mutex);
 write:
     if (write_queue_empty(&io->write_queue)) {
@@ -354,14 +358,14 @@ write:
     nwrite = __nio_write(io, buf, len);
     // printd("write retval=%d\n", nwrite);
     if (nwrite < 0) {
-        if (socket_errno() == EAGAIN) {
+        err = socket_errno();
+        if (err == EAGAIN) {
             //goto write_done;
             hrecursive_mutex_unlock(&io->write_mutex);
             return;
-        }
-        else {
-            io->error = socket_errno();
+        } else {
             // perror("write");
+            io->error = err;
             goto write_error;
         }
     }
@@ -469,21 +473,21 @@ int hio_write (hio_t* io, const void* buf, size_t len) {
         hloge("hio_write called but fd[%d] already closed!", io->fd);
         return -1;
     }
-    int nwrite = 0;
+    int nwrite = 0, err = 0;
     hrecursive_mutex_lock(&io->write_mutex);
     if (write_queue_empty(&io->write_queue)) {
 try_write:
         nwrite = __nio_write(io, buf, len);
         // printd("write retval=%d\n", nwrite);
         if (nwrite < 0) {
-            if (socket_errno() == EAGAIN) {
+            err = socket_errno();
+            if (err == EAGAIN) {
                 nwrite = 0;
                 hlogw("try_write failed, enqueue!");
                 goto enqueue;
-            }
-            else {
+            } else {
                 // perror("write");
-                io->error = socket_errno();
+                io->error = err;
                 goto write_error;
             }
         }

+ 0 - 4
evpp/Channel.h

@@ -237,10 +237,6 @@ public:
         return SOCKADDR_STR(addr, buf);
     }
 
-    int send(const std::string& str) {
-        return write(str);
-    }
-
 private:
     static void on_connect(hio_t* io) {
         SocketChannel* channel = (SocketChannel*)hio_context(io);

+ 22 - 3
evpp/TcpClient.h

@@ -157,16 +157,35 @@ public:
         loop_thread.stop(wait_threads_stopped);
     }
 
-    int withTLS(const char* cert_file = NULL, const char* key_file = NULL) {
-        tls = true;
+    int send(const void* data, int size) {
+        if (channel == NULL) return 0;
+        return channel->write(data, size);
+    }
+
+    int send(Buffer* buf) {
+        if (channel == NULL) return 0;
+        return channel->write(buf);
+    }
+
+    int send(const std::string& str) {
+        if (channel == NULL) return 0;
+        return channel->write(str);
+    }
+
+    int withTLS(const char* cert_file = NULL, const char* key_file = NULL, bool verify_peer = false) {
         if (cert_file) {
             hssl_ctx_init_param_t param;
             memset(&param, 0, sizeof(param));
             param.crt_file = cert_file;
             param.key_file = key_file;
+            param.verify_peer = verify_peer ? 1 : 0;
             param.endpoint = HSSL_CLIENT;
-            return hssl_ctx_init(&param) == NULL ? -1 : 0;
+            if (hssl_ctx_init(&param) == NULL) {
+                fprintf(stderr, "hssl_ctx_init failed!\n");
+                return -1;
+            }
         }
+        tls = true;
         return 0;
     }
 

+ 1 - 1
evpp/TcpClient_test.cpp

@@ -35,7 +35,7 @@ int main(int argc, char* argv[]) {
                     char str[DATETIME_FMT_BUFLEN] = {0};
                     datetime_t dt = datetime_now();
                     datetime_fmt(&dt, str);
-                    channel->send(str);
+                    channel->write(str);
                 } else {
                     killTimer(timerID);
                 }

+ 5 - 2
evpp/TcpServer.h

@@ -57,15 +57,18 @@ public:
     }
 
     int withTLS(const char* cert_file, const char* key_file) {
-        tls = true;
         if (cert_file) {
             hssl_ctx_init_param_t param;
             memset(&param, 0, sizeof(param));
             param.crt_file = cert_file;
             param.key_file = key_file;
             param.endpoint = HSSL_SERVER;
-            return hssl_ctx_init(&param) == NULL ? -1 : 0;
+            if (hssl_ctx_init(&param) == NULL) {
+                fprintf(stderr, "hssl_ctx_init failed!\n");
+                return -1;
+            }
         }
+        tls = true;
         return 0;
     }
 

+ 5 - 0
evpp/UdpClient.h

@@ -52,6 +52,11 @@ public:
         loop_thread.stop(wait_threads_stopped);
     }
 
+    int sendto(const void* data, int size) {
+        if (channel == NULL) return 0;
+        return channel->write(data, size);
+    }
+
     int sendto(Buffer* buf) {
         if (channel == NULL) return 0;
         return channel->write(buf);

+ 2 - 1
examples/http_server_test.cpp

@@ -66,8 +66,9 @@ int main(int argc, char** argv) {
     memset(&param, 0, sizeof(param));
     param.crt_file = "cert/server.crt";
     param.key_file = "cert/server.key";
+    param.endpoint = HSSL_SERVER;
     if (hssl_ctx_init(&param) == NULL) {
-        fprintf(stderr, "SSL certificate verify failed!\n");
+        fprintf(stderr, "hssl_ctx_init failed!\n");
         return -20;
     }
 #endif

+ 1 - 0
examples/httpd/httpd.cpp

@@ -159,6 +159,7 @@ int parse_confile(const char* confile) {
         param.crt_file = crt_file.c_str();
         param.key_file = key_file.c_str();
         param.ca_file = ca_file.c_str();
+        param.endpoint = HSSL_SERVER;
         if (hssl_ctx_init(&param) == NULL) {
             hloge("SSL certificate verify failed!");
             exit(0);

+ 2 - 1
examples/tcp_echo_server.c

@@ -66,8 +66,9 @@ int main(int argc, char** argv) {
         memset(&param, 0, sizeof(param));
         param.crt_file = "cert/server.crt";
         param.key_file = "cert/server.key";
+        param.endpoint = HSSL_SERVER;
         if (hssl_ctx_init(&param) == NULL) {
-            fprintf(stderr, "SSL certificate verify failed!\n");
+            fprintf(stderr, "hssl_ctx_init failed!\n");
             return -30;
         }
     }

+ 2 - 1
examples/websocket_server_test.cpp

@@ -84,8 +84,9 @@ int main(int argc, char** argv) {
     memset(&param, 0, sizeof(param));
     param.crt_file = "cert/server.crt";
     param.key_file = "cert/server.key";
+    param.endpoint = HSSL_SERVER;
     if (hssl_ctx_init(&param) == NULL) {
-        fprintf(stderr, "SSL certificate verify failed!\n");
+        fprintf(stderr, "hssl_ctx_init failed!\n");
         return -20;
     }
 #endif

+ 2 - 1
http/client/http_client.cpp

@@ -364,7 +364,8 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
         // hssl_set_sni_hostname(cli->ssl, req->host.c_str());
         int ret = hssl_connect(cli->ssl);
         if (ret != 0) {
-            fprintf(stderr, "SSL handshake failed: %d\n", ret);
+            fprintf(stderr, "ssl handshake failed: %d\n", ret);
+            hloge("ssl handshake failed: %d", ret);
             hssl_free(cli->ssl);
             cli->ssl = NULL;
             closesocket(connfd);