ithewei 4 سال پیش
والد
کامیت
918a718a44
7فایلهای تغییر یافته به همراه107 افزوده شده و 26 حذف شده
  1. 3 2
      .github/workflows/CI.yml
  2. 1 0
      Makefile
  3. 6 1
      event/hevent.c
  4. 11 4
      event/hevent.h
  5. 27 19
      event/hloop.h
  6. 1 0
      scripts/unittest.sh
  7. 58 0
      unittest/sizeof_test.cpp

+ 3 - 2
.github/workflows/CI.yml

@@ -20,8 +20,9 @@ jobs:
           sudo apt update
           sudo apt install libssl-dev libnghttp2-dev
           ./configure --with-openssl --with-nghttp2
-          make libhv examples unittest evpp
+          make libhv evpp
           make check
+          make run-unittest
 
   build-macos:
     name: build-macos
@@ -31,7 +32,7 @@ jobs:
       - name: build
         run: |
           ./configure
-          make libhv examples unittest evpp
+          make libhv evpp examples unittest
 
   build-windows:
     name: build-windows

+ 1 - 0
Makefile

@@ -185,6 +185,7 @@ unittest: prepare
 	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/synchronized_test unittest/synchronized_test.cpp -pthread
 	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/threadpool_test   unittest/threadpool_test.cpp  -pthread
 	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/objectpool_test   unittest/objectpool_test.cpp  -pthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Issl -Ievent -Ievpp -Icpputil -Ihttp -Ihttp/client -Ihttp/server -o bin/sizeof_test unittest/sizeof_test.cpp
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -o bin/nslookup          unittest/nslookup_test.c      protocol/dns.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -o bin/ping              unittest/ping_test.c          protocol/icmp.c base/hsocket.c base/htime.c -DPRINT_DEBUG
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -o bin/ftp               unittest/ftp_test.c           protocol/ftp.c  base/hsocket.c

+ 6 - 1
event/hevent.c

@@ -125,10 +125,15 @@ void hio_ready(hio_t* io) {
     io->upstream_io = NULL;
     // unpack
     io->unpack_setting = NULL;
+    // ssl
+    io->ssl = NULL;
     // private:
+#if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
     io->event_index[0] = io->event_index[1] = -1;
+#endif
+#ifdef EVENT_IOCP
     io->hovlp = NULL;
-    io->ssl = NULL;
+#endif
 
     // io_type
     fill_io_type(io);

+ 11 - 4
event/hevent.h

@@ -2,6 +2,8 @@
 #define HV_EVENT_H_
 
 #include "hloop.h"
+#include "iowatcher.h"
+
 #include "hbuf.h"
 #include "hmutex.h"
 
@@ -134,14 +136,19 @@ struct hio_s {
     hio_send_heartbeat_fn heartbeat_fn;
     htimer_t*   heartbeat_timer;
     // upstream
-    struct hio_s*   upstream_io;
+    struct hio_s*       upstream_io;    // for hio_setup_upstream
     // unpack
-    unpack_setting_t*   unpack_setting;
+    unpack_setting_t*   unpack_setting; // for hio_set_unpack
+    // ssl
+    void*       ssl; // for hio_enable_ssl / hio_set_ssl
+    void*       ctx; // for hio_context / hio_set_context
 // private:
+#if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
     int         event_index[2]; // for poll,kqueue
+#endif
+#ifdef EVENT_IOCP
     void*       hovlp;          // for iocp/overlapio
-    void*       ssl;            // for SSL
-    void*       ctx;
+#endif
 };
 /*
  * hio lifeline:

+ 27 - 19
event/hloop.h

@@ -430,25 +430,33 @@ typedef enum {
 typedef struct unpack_setting_s {
     unpack_mode_e   mode;
     unsigned int    package_max_length;
-    // UNPACK_BY_FIXED_LENGTH
-    unsigned int    fixed_length;
-    // UNPACK_BY_DELIMITER
-    unsigned char   delimiter[PACKAGE_MAX_DELIMITER_BYTES];
-    unsigned short  delimiter_bytes;
-    // UNPACK_BY_LENGTH_FIELD
-    /* package_len = head_len + body_len + length_adjustment
-     *
-     * if (length_field_coding == ENCODE_BY_VARINT) head_len = body_offset + varint_bytes - length_field_bytes;
-     * else head_len = body_offset;
-     *
-     * body_len calc by length_field
-     *
-     */
-    unsigned short  body_offset;
-    unsigned short  length_field_offset;
-    unsigned short  length_field_bytes;
-    unpack_coding_e length_field_coding;
-    int             length_adjustment;
+    union {
+        // UNPACK_BY_FIXED_LENGTH
+        struct {
+            unsigned int    fixed_length;
+        };
+        // UNPACK_BY_DELIMITER
+        struct {
+            unsigned char   delimiter[PACKAGE_MAX_DELIMITER_BYTES];
+            unsigned short  delimiter_bytes;
+        };
+        // UNPACK_BY_LENGTH_FIELD
+        /* package_len = head_len + body_len + length_adjustment
+         *
+         * if (length_field_coding == ENCODE_BY_VARINT) head_len = body_offset + varint_bytes - length_field_bytes;
+         * else head_len = body_offset;
+         *
+         * body_len calc by length_field
+         *
+         */
+        struct {
+            unsigned short  body_offset;
+            unsigned short  length_field_offset;
+            unsigned short  length_field_bytes;
+            unpack_coding_e length_field_coding;
+            int             length_adjustment;
+        };
+    };
 #ifdef __cplusplus
     unpack_setting_s() {
         // Recommended setting:

+ 1 - 0
scripts/unittest.sh

@@ -24,3 +24,4 @@ bin/hpath_test
 bin/socketpair_test
 # bin/threadpool_test
 # bin/objectpool_test
+bin/sizeof_test

+ 58 - 0
unittest/sizeof_test.cpp

@@ -0,0 +1,58 @@
+#include <stdio.h>
+
+#include "hloop.h"
+#include "hevent.h"
+
+#include "EventLoop.h"
+#include "EventLoopThread.h"
+#include "EventLoopThreadPool.h"
+#include "Channel.h"
+#include "TcpClient.h"
+#include "TcpServer.h"
+#include "UdpClient.h"
+#include "UdpServer.h"
+
+#include "HttpMessage.h"
+#include "Http1Parser.h"
+#include "HttpContext.h"
+#include "HttpServer.h"
+
+#include "WebSocketChannel.h"
+#include "WebSocketParser.h"
+#include "WebSocketServer.h"
+#include "WebSocketClient.h"
+
+using namespace hv;
+
+int main() {
+    // event
+    printf("sizeof(struct hloop_s)=%lu\n", sizeof(struct hloop_s));
+    printf("sizeof(struct hevent_s)=%lu\n", sizeof(struct hevent_s));
+    printf("sizeof(struct hidle_s)=%lu\n", sizeof(struct hidle_s));
+    printf("sizeof(struct htimer_s)=%lu\n", sizeof(struct htimer_s));
+    printf("sizeof(struct htimeout_s)=%lu\n", sizeof(struct htimeout_s));
+    printf("sizeof(struct hperiod_s)=%lu\n", sizeof(struct hperiod_s));
+    printf("sizeof(struct hio_s)=%lu\n", sizeof(struct hio_s));
+    // evpp
+    printf("sizeof(class EventLoop)=%lu\n", sizeof(EventLoop));
+    printf("sizeof(class EventLoopThread)=%lu\n", sizeof(EventLoopThread));
+    printf("sizeof(class EventLoopThreadPool)=%lu\n", sizeof(EventLoopThreadPool));
+    printf("sizeof(class Channel)=%lu\n", sizeof(Channel));
+    printf("sizeof(class SocketChannel)=%lu\n", sizeof(SocketChannel));
+    printf("sizeof(class TcpClient)=%lu\n", sizeof(TcpClient));
+    printf("sizeof(class TcpServer)=%lu\n", sizeof(TcpServer));
+    printf("sizeof(class UdpClient)=%lu\n", sizeof(UdpClient));
+    printf("sizeof(class UdpServer)=%lu\n", sizeof(UdpServer));
+    // http
+    printf("sizeof(class HttpRequest)=%lu\n", sizeof(HttpRequest));
+    printf("sizeof(class HttpResponse)=%lu\n", sizeof(HttpResponse));
+    printf("sizeof(class Http1Parser)=%lu\n", sizeof(Http1Parser));
+    printf("sizeof(class HttpContext)=%lu\n", sizeof(HttpContext));
+    printf("sizeof(class HttpServer)=%lu\n", sizeof(HttpServer));
+    // websocket
+    printf("sizeof(class WebSocketChannel)=%lu\n", sizeof(WebSocketChannel));
+    printf("sizeof(class WebSocketParser)=%lu\n", sizeof(WebSocketParser));
+    printf("sizeof(class WebSocketClient)=%lu\n", sizeof(WebSocketClient));
+    printf("sizeof(class WebSocketServer)=%lu\n", sizeof(WebSocketServer));
+    return 0;
+}