ithewei 4 gadi atpakaļ
vecāks
revīzija
f8fcda74ed
5 mainītis faili ar 31 papildinājumiem un 58 dzēšanām
  1. 4 0
      CMakeLists.txt
  2. 10 10
      examples/nc.c
  3. 1 1
      http/Http2Parser.cpp
  4. 4 0
      http/HttpMessage.h
  5. 12 47
      http/grpcdef.h

+ 4 - 0
CMakeLists.txt

@@ -166,6 +166,10 @@ if(WITH_EVPP)
             set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
         endif()
     endif()
+
+    if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND CMAKE_COMPILER_IS_GNUCC)
+        set(LIBS ${LIBS} stdc++)
+    endif()
 endif()
 
 list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})

+ 10 - 10
examples/nc.c

@@ -38,14 +38,14 @@ hio_t*      sockio = NULL;
 
 int verbose = 0;
 
-void send_heartbeat(hio_t* io) {
+static void send_heartbeat(hio_t* io) {
     static char buf[] = "PING\r\n";
     // printf("send_heartbeat %s", buf);
     hio_write(io, buf, 6);
 }
 
-void on_recv(hio_t* io, void* buf, int readbytes) {
-    //printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
+static void on_recv(hio_t* io, void* buf, int readbytes) {
+    // printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
     if (verbose) {
         char localaddrstr[SOCKADDR_STRLEN] = {0};
         char peeraddrstr[SOCKADDR_STRLEN] = {0};
@@ -69,9 +69,9 @@ void on_recv(hio_t* io, void* buf, int readbytes) {
     fflush(stdout);
 }
 
-void on_stdin(hio_t* io, void* buf, int readbytes) {
-    //printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
-    //printf("> %s\n", buf);
+static void on_stdin(hio_t* io, void* buf, int readbytes) {
+    // printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
+    // printf("> %s\n", buf);
 
     char* str = (char*)buf;
 
@@ -115,13 +115,13 @@ void on_stdin(hio_t* io, void* buf, int readbytes) {
     hio_write(sockio, buf, readbytes);
 }
 
-void on_close(hio_t* io) {
-    //printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
+static void on_close(hio_t* io) {
+    // printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
     hio_del(stdinio, HV_READ);
 }
 
-void on_connect(hio_t* io) {
-    //printf("on_connect fd=%d\n", hio_fd(io));
+static void on_connect(hio_t* io) {
+    // printf("on_connect fd=%d\n", hio_fd(io));
     if (verbose) {
         char localaddrstr[SOCKADDR_STRLEN] = {0};
         char peeraddrstr[SOCKADDR_STRLEN] = {0};

+ 1 - 1
http/Http2Parser.cpp

@@ -362,7 +362,7 @@ int on_data_chunk_recv_callback(nghttp2_session *session,
         // grpc_message_hd
         if (len >= GRPC_MESSAGE_HDLEN) {
             grpc_message_hd msghd;
-            grpc_message_hd_unpack(data, &msghd);
+            grpc_message_hd_unpack(&msghd, data);
             printd("grpc_message_hd: flags=%d length=%d\n", msghd.flags, msghd.length);
             data += GRPC_MESSAGE_HDLEN;
             len -= GRPC_MESSAGE_HDLEN;

+ 4 - 0
http/HttpMessage.h

@@ -238,6 +238,10 @@ public:
         return content_type;
     }
 
+    void AddCookie(const HttpCookie& cookie) {
+        cookies.push_back(cookie);
+    }
+
     int String(const std::string& str) {
         content_type = TEXT_PLAIN;
         body = str;

+ 12 - 47
http/grpcdef.h

@@ -12,34 +12,36 @@ extern "C" {
 
 typedef struct {
     unsigned char   flags;
-    int             length;
+    unsigned int    length;
 } grpc_message_hd;
 
 typedef struct {
     unsigned char   flags;
-    int             length;
+    unsigned int    length;
     unsigned char*  message;
 } grpc_message;
 
 static inline void grpc_message_hd_pack(const grpc_message_hd* hd, unsigned char* buf) {
-    // hton
-    int length = hd->length;
     unsigned char* p = buf;
+    // flags
     *p++ = hd->flags;
+    // hton length
+    unsigned int length = hd->length;
     *p++ = (length >> 24) & 0xFF;
     *p++ = (length >> 16) & 0xFF;
     *p++ = (length >>  8) & 0xFF;
     *p++ =  length        & 0xFF;
 }
 
-static inline void grpc_message_hd_unpack(const unsigned char* buf, grpc_message_hd* hd) {
-    // ntoh
+static inline void grpc_message_hd_unpack(grpc_message_hd* hd, const unsigned char* buf) {
     const unsigned char* p = buf;
+    // flags
     hd->flags = *p++;
-    hd->length  = *p++ << 24;
-    hd->length += *p++ << 16;
-    hd->length += *p++ << 8;
-    hd->length += *p++;
+    // ntoh length
+    hd->length  = ((unsigned int)*p++) << 24;
+    hd->length |= ((unsigned int)*p++) << 16;
+    hd->length |= ((unsigned int)*p++) << 8;
+    hd->length |= *p++;
 }
 
 // protobuf
@@ -80,43 +82,6 @@ typedef enum {
 #define PROTOBUF_FILED_NUMBER(tag)                  ((tag) >> 3)
 #define PROTOBUF_WIRE_TYPE(tag)                     ((tag) & 0x07)
 
-// varint little-endian
-// MSB
-static inline int varint_encode(long long value, unsigned char* buf) {
-    unsigned char ch;
-    unsigned char *p = buf;
-    int bytes = 0;
-    do {
-        ch = value & 0x7F;
-        value >>= 7;
-        *p++ = value == 0 ? ch : (ch | 0x80);
-        ++bytes;
-    } while (value);
-    return bytes;
-}
-
-// @param[IN|OUT] len: in=>buflen, out=>varint bytesize
-static inline long long varint_decode(const unsigned char* buf, int* len) {
-    long long ret = 0;
-    int bytes = 0;
-    int bits = 0;
-    const unsigned char *p = buf;
-    unsigned char ch;
-    do {
-        if (len && *len && bytes == *len) {
-            break;
-        }
-        ch = *p & 0x7F;
-        ret |= (ch << bits);
-        bits += 7;
-        ++bytes;
-        if (!(*p & 0x80)) break;
-        ++p;
-    } while(bytes < 10);
-    *len = bytes;
-    return ret;
-}
-
 #ifdef __cplusplus
 }
 #endif