1
0
ithewei 4 жил өмнө
parent
commit
22dd4abe67

+ 4 - 1
base/hssl.c

@@ -2,9 +2,12 @@
 
 #include "hplatform.h"
 
-static hssl_ctx_t s_ssl_ctx = 0;
+static hssl_ctx_t s_ssl_ctx = NULL;
 
 hssl_ctx_t hssl_ctx_instance() {
+    if (s_ssl_ctx == NULL) {
+        s_ssl_ctx = hssl_ctx_init(NULL);
+    }
     return s_ssl_ctx;
 }
 

+ 7 - 4
evpp/Channel.h

@@ -81,14 +81,17 @@ public:
         return hio_read_stop(io_);
     }
 
-    int write(Buffer* buf) {
+    int write(const void* data, int size) {
         if (!isOpened()) return 0;
-        return hio_write(io_, buf->data(), buf->size());
+        return hio_write(io_, data, size);
+    }
+
+    int write(Buffer* buf) {
+        return write(buf->data(), buf->size());
     }
 
     int write(const std::string& str) {
-        if (!isOpened()) return 0;
-        return hio_write(io_, str.data(), str.size());
+        return write(str.data(), str.size());
     }
 
     int close() {

+ 23 - 14
evpp/TcpClient.h

@@ -39,15 +39,18 @@ struct ReconnectInfo {
     }
 };
 
-class TcpClient {
+template<class TSocketChannel = SocketChannel>
+class TcpClientTmpl {
 public:
-    TcpClient() {
+    typedef std::shared_ptr<TSocketChannel> TSocketChannelPtr;
+
+    TcpClientTmpl() {
         tls = false;
         connect_timeout = 5000;
         enable_reconnect = false;
     }
 
-    ~TcpClient() {
+    virtual ~TcpClientTmpl() {
     }
 
     EventLoopPtr loop() {
@@ -75,7 +78,7 @@ public:
         hio_t* io = hio_get(loop_thread.hloop(), connfd);
         assert(io != NULL);
         hio_set_peeraddr(io, peeraddr, SOCKADDR_LEN(peeraddr));
-        channel.reset(new SocketChannel(io));
+        channel.reset(new TSocketChannel(io));
         return connfd;
     }
 
@@ -140,7 +143,7 @@ public:
     }
 
     void start(bool wait_threads_started = true) {
-        loop_thread.start(wait_threads_started, std::bind(&TcpClient::startConnect, this));
+        loop_thread.start(wait_threads_started, std::bind(&TcpClientTmpl::startConnect, this));
     }
     void stop(bool wait_threads_stopped = true) {
         loop_thread.stop(wait_threads_stopped);
@@ -148,11 +151,15 @@ public:
 
     int withTLS(const char* cert_file = NULL, const char* key_file = NULL) {
         tls = true;
-        hssl_ctx_init_param_t param;
-        memset(&param, 0, sizeof(param));
-        param.crt_file = cert_file;
-        param.key_file = key_file;
-        return hssl_ctx_init(&param) == NULL ? -1 : 0;
+        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 = 1;
+            return hssl_ctx_init(&param) == NULL ? -1 : 0;
+        }
+        return 0;
     }
 
     void setConnectTimeout(int ms) {
@@ -165,7 +172,7 @@ public:
     }
 
 public:
-    SocketChannelPtr        channel;
+    TSocketChannelPtr       channel;
 
     sockaddr_u              peeraddr;
     bool                    tls;
@@ -174,13 +181,15 @@ public:
     ReconnectInfo           reconnect_info;
 
     // Callback
-    ConnectionCallback      onConnection;
-    MessageCallback         onMessage;
-    WriteCompleteCallback   onWriteComplete;
+    std::function<void(const TSocketChannelPtr&)>           onConnection;
+    std::function<void(const TSocketChannelPtr&, Buffer*)>  onMessage;
+    std::function<void(const TSocketChannelPtr&, Buffer*)>  onWriteComplete;
 private:
     EventLoopThread         loop_thread;
 };
 
+typedef TcpClientTmpl<SocketChannel> TcpClient;
+
 }
 
 #endif // HV_TCP_CLIENT_HPP_

+ 10 - 6
evpp/TcpServer.h

@@ -20,7 +20,7 @@ public:
         connection_num = 0;
     }
 
-    ~TcpServer() {
+    virtual ~TcpServer() {
     }
 
     //@retval >=0 listenfd, <0 error
@@ -58,11 +58,15 @@ public:
 
     int withTLS(const char* cert_file, const char* key_file) {
         tls = true;
-        hssl_ctx_init_param_t param;
-        memset(&param, 0, sizeof(param));
-        param.crt_file = cert_file;
-        param.key_file = key_file;
-        return hssl_ctx_init(&param) == NULL ? -1 : 0;
+        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 = 0;
+            return hssl_ctx_init(&param) == NULL ? -1 : 0;
+        }
+        return 0;
     }
 
     // channel

+ 1 - 1
evpp/UdpClient.h

@@ -14,7 +14,7 @@ public:
     UdpClient() {
     }
 
-    ~UdpClient() {
+    virtual ~UdpClient() {
     }
 
     EventLoopPtr loop() {

+ 1 - 1
evpp/UdpServer.h

@@ -14,7 +14,7 @@ public:
     UdpServer() {
     }
 
-    ~UdpServer() {
+    virtual ~UdpServer() {
     }
 
     EventLoopPtr loop() {

+ 0 - 5
examples/udp_echo_server.c

@@ -11,10 +11,6 @@
 #include "hloop.h"
 #include "hsocket.h"
 
-static void on_close(hio_t* io) {
-    printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-}
-
 static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
     printf("on_recvfrom fd=%d readbytes=%d\n", hio_fd(io), readbytes);
     char localaddrstr[SOCKADDR_STRLEN] = {0};
@@ -40,7 +36,6 @@ int main(int argc, char** argv) {
     if (io == NULL) {
         return -20;
     }
-    hio_setcb_close(io, on_close);
     hio_setcb_read(io, on_recvfrom);
     hio_read(io);
     hloop_run(loop);

+ 0 - 3
http/client/http_client.cpp

@@ -323,9 +323,6 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
     tcp_nodelay(connfd, 1);
 
     if (req->https) {
-        if (hssl_ctx_instance() == NULL) {
-            hssl_ctx_init(NULL);
-        }
         hssl_ctx_t ssl_ctx = hssl_ctx_instance();
         if (ssl_ctx == NULL) {
             closesocket(connfd);