hewei.it 4 年 前
コミット
e7458edd71
2 ファイル変更13 行追加5 行削除
  1. 4 0
      evpp/Channel.h
  2. 9 5
      http/WebSocketChannel.h

+ 4 - 0
evpp/Channel.h

@@ -181,11 +181,13 @@ public:
     }
 
     int startConnect(struct sockaddr* peeraddr) {
+        if (io_ == NULL) return -1;
         hio_set_peeraddr(io_, peeraddr, SOCKADDR_LEN(peeraddr));
         return startConnect();
     }
 
     int startConnect() {
+        if (io_ == NULL) return -1;
         status = CONNECTING;
         hio_setcb_connect(io_, on_connect);
         return hio_connect(io_);
@@ -196,12 +198,14 @@ public:
     }
 
     std::string localaddr() {
+        if (io_ == NULL) return "";
         struct sockaddr* addr = hio_localaddr(io_);
         char buf[SOCKADDR_STRLEN] = {0};
         return SOCKADDR_STR(addr, buf);
     }
 
     std::string peeraddr() {
+        if (io_ == NULL) return "";
         struct sockaddr* addr = hio_peeraddr(io_);
         char buf[SOCKADDR_STRLEN] = {0};
         return SOCKADDR_STR(addr, buf);

+ 9 - 5
http/WebSocketChannel.h

@@ -1,6 +1,8 @@
 #ifndef HV_WEBSOCKET_CHANNEL_H_
 #define HV_WEBSOCKET_CHANNEL_H_
 
+#include <mutex>
+
 #include "Channel.h"
 
 #include "wsdef.h"
@@ -31,15 +33,17 @@ public:
             *(int*)mask = rand();
         }
         int frame_size = ws_calc_frame_size(len, has_mask);
-        if (sendbuf.len < frame_size) {
-            sendbuf.resize(ceil2e(frame_size));
+        std::lock_guard<std::mutex> locker(mutex_);
+        if (sendbuf_.len < frame_size) {
+            sendbuf_.resize(ceil2e(frame_size));
         }
-        ws_build_frame(sendbuf.base, buf, len, mask, has_mask, opcode);
-        return write(sendbuf.base, frame_size);
+        ws_build_frame(sendbuf_.base, buf, len, mask, has_mask, opcode);
+        return write(sendbuf_.base, frame_size);
     }
 
 private:
-    Buffer sendbuf;
+    Buffer      sendbuf_;
+    std::mutex  mutex_;
 };
 
 }