Bladeren bron

optimize code

hewei.it 4 jaren geleden
bovenliggende
commit
05e72e3d68

+ 1 - 1
evpp/TcpServer.h

@@ -154,10 +154,10 @@ public:
     uint32_t                max_connections;
 
 private:
-    EventLoopThreadPool     loop_threads;
     // fd => SocketChannelPtr
     std::map<int, SocketChannelPtr> channels; // GUAREDE_BY(mutex_)
     std::mutex                      mutex_;
+    EventLoopThreadPool             loop_threads;
 };
 
 }

+ 4 - 4
examples/httpd/httpd.cpp

@@ -7,8 +7,8 @@
 
 #include "router.h"
 
-http_server_t   g_http_server;
-HttpService     g_http_service;
+hv::HttpServer  g_http_server;
+hv::HttpService g_http_service;
 
 static void print_version();
 static void print_help();
@@ -272,7 +272,7 @@ int main(int argc, char** argv) {
 
     // http_server
     Router::Register(g_http_service);
-    g_http_server.service = &g_http_service;
-    ret = http_server_run(&g_http_server);
+    g_http_server.registerHttpService(&g_http_service);
+    g_http_server.run();
     return ret;
 }

+ 1 - 1
examples/httpd/router.cpp

@@ -6,7 +6,7 @@
 #include "hthread.h"
 #include "requests.h"
 
-void Router::Register(HttpService& router) {
+void Router::Register(hv::HttpService& router) {
     // preprocessor => Handler => postprocessor
     router.preprocessor = Handler::preprocessor;
     router.postprocessor = Handler::postprocessor;

+ 1 - 1
examples/httpd/router.h

@@ -5,7 +5,7 @@
 
 class Router {
 public:
-    static void Register(HttpService& router);
+    static void Register(hv::HttpService& router);
 };
 
 #endif // HV_HTTPD_ROUTER_H

+ 39 - 6
http/HttpMessage.h

@@ -207,11 +207,23 @@ public:
     bool IsChunked();
     bool IsKeepAlive();
 
+    // headers
+    void SetHeader(const char* key, const std::string& value) {
+        headers[key] = value;
+    }
     std::string GetHeader(const char* key, const std::string& defvalue = "") {
         auto iter = headers.find(key);
         return iter == headers.end() ? defvalue : iter->second;
     }
 
+    // body
+    void SetBody(const std::string& body) {
+        this->body = body;
+    }
+    const std::string& Body() {
+        return this->body;
+    }
+
     // headers -> string
     void DumpHeaders(std::string& str);
     // structured content -> body
@@ -337,16 +349,27 @@ public:
 
     virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
 
+    // method
+    void SetMethod(const char* method) {
+        this->method = http_method_enum(method);
+    }
+    const char* Method() {
+        return http_method_str(method);
+    }
+
+    // url
+    void SetUrl(const char* url) {
+        this->url = url;
+    }
+    const std::string& Url() {
+        return url;
+    }
     // structed url -> url
     void DumpUrl();
     // url -> structed url
     void ParseUrl();
 
-    std::string Host() {
-        auto iter = headers.find("Host");
-        return iter == headers.end() ? host : iter->second;
-    }
-
+    // /path
     std::string Path() {
         const char* s = path.c_str();
         const char* e = s;
@@ -354,11 +377,21 @@ public:
         return std::string(s, e);
     }
 
+    // ?query_params
+    void SetParam(const char* key, const std::string& value) {
+        query_params[key] = value;
+    }
     std::string GetParam(const char* key, const std::string& defvalue = "") {
         auto iter = query_params.find(key);
         return iter == query_params.end() ? defvalue : iter->second;
     }
 
+    // Host:
+    std::string Host() {
+        auto iter = headers.find("Host");
+        return iter == headers.end() ? host : iter->second;
+    }
+
     // Range: bytes=0-4095
     void SetRange(long from = 0, long to = -1) {
         headers["Range"] = hv::asprintf("bytes=%ld-%ld", from, to);
@@ -373,7 +406,7 @@ public:
         return false;
     }
 
-    // Cookie
+    // Cookie:
     void SetCookie(const HttpCookie& cookie) {
         headers["Cookie"] = cookie.dump();
     }

+ 1 - 2
http/server/HttpContext.h

@@ -5,10 +5,9 @@
 #include "HttpMessage.h"
 #include "HttpResponseWriter.h"
 
-struct HttpService;
-
 namespace hv {
 
+struct HttpService;
 struct HV_EXPORT HttpContext {
     HttpService*            service;
     HttpRequestPtr          request;

+ 60 - 10
http/server/HttpServer.h

@@ -3,8 +3,13 @@
 
 #include "hexport.h"
 #include "HttpService.h"
-
+// #include "WebSocketServer.h"
+namespace hv {
 struct WebSocketService;
+}
+using hv::HttpService;
+using hv::WebSocketService;
+
 typedef struct http_server_s {
     char host[64];
     int port; // http_port
@@ -39,28 +44,73 @@ typedef struct http_server_s {
 #endif
 } http_server_t;
 
+// @param wait: Whether to occupy current thread
+HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
+
+// NOTE: stop all loops and join all threads
+HV_EXPORT int http_server_stop(http_server_t* server);
+
 /*
 #include "HttpServer.h"
+using namespace hv;
 
 int main() {
     HttpService service;
-    service.base_url = "/api/v1";
     service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
         resp->body = "pong";
         return 200;
     });
 
-    http_server_t server;
-    server.port = 8080;
-    server.worker_processes = 4;
-    server.service = &service;
-    http_server_run(&server);
+    HttpServer server;
+    server.registerHttpService(&service);
+    server.setPort(8080);
+    server.setThreadNum(4);
+    server.run();
     return 0;
 }
 */
-HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
 
-// NOTE: stop all loops and join all threads
-HV_EXPORT int http_server_stop(http_server_t* server);
+namespace hv {
+
+class HttpServer : public http_server_t {
+public:
+    HttpServer() : http_server_t() {}
+    ~HttpServer() { stop(); }
+
+    void registerHttpService(HttpService* service) {
+        this->service = service;
+    }
+
+    void setHost(const char* host = "0.0.0.0") {
+        if (host) strcpy(this->host, host);
+    }
+
+    void setPort(int port = 0, int ssl_port = 0) {
+        if (port != 0) this->port = port;
+        if (ssl_port != 0) this->https_port = ssl_port;
+    }
+
+    void setProcessNum(int num) {
+        this->worker_processes = num;
+    }
+
+    void setThreadNum(int num) {
+        this->worker_threads = num;
+    }
+
+    int run(bool wait = true) {
+        return http_server_run(this, wait);
+    }
+
+    int start() {
+        return run(false);
+    }
+
+    int stop() {
+        return http_server_stop(this);
+    }
+};
+
+}
 
 #endif // HV_HTTP_SERVER_H_

+ 4 - 0
http/server/HttpService.cpp

@@ -2,6 +2,8 @@
 
 #include "hbase.h" // import strendswith
 
+namespace hv {
+
 void HttpService::AddApi(const char* path, http_method method, http_sync_handler sync_handler, http_async_handler async_handler, http_handler handler) {
     std::shared_ptr<http_method_handlers> method_handlers = NULL;
     auto iter = api_handlers.find(path);
@@ -138,3 +140,5 @@ int HttpService::GetApi(HttpRequest* req, http_sync_handler* sync_handler, http_
     if (handler) *handler = NULL;
     return HTTP_STATUS_NOT_FOUND;
 }
+
+}

+ 4 - 0
http/server/HttpService.h

@@ -51,6 +51,8 @@ typedef std::list<http_method_handler>                                  http_met
 // path => http_method_handlers
 typedef std::map<std::string, std::shared_ptr<http_method_handlers>>    http_api_handlers;
 
+namespace hv {
+
 struct HV_EXPORT HttpService {
     // preprocessor -> processor -> postprocessor
     http_sync_handler   preprocessor;
@@ -224,4 +226,6 @@ struct HV_EXPORT HttpService {
     }
 };
 
+}
+
 #endif // HV_HTTP_SERVICE_H_

+ 14 - 4
http/server/WebSocketServer.h

@@ -8,6 +8,12 @@
 #include "HttpServer.h"
 #include "WebSocketChannel.h"
 
+#define websocket_server_t      http_server_t
+#define websocket_server_run    http_server_run
+#define websocket_server_stop   http_server_stop
+
+namespace hv {
+
 struct WebSocketService {
     std::function<void(const WebSocketChannelPtr&, const std::string&)> onopen;
     std::function<void(const WebSocketChannelPtr&, const std::string&)> onmessage;
@@ -18,10 +24,14 @@ struct WebSocketService {
         ping_interval = 10000; // ms
     }
 };
-#define WebSocketServerCallbacks WebSocketService // deprecated
 
-#define websocket_server_t      http_server_t
-#define websocket_server_run    http_server_run
-#define websocket_server_stop   http_server_stop
+class WebSocketServer : public HttpServer {
+public:
+    void registerWebSocketService(WebSocketService* service) {
+        this->ws = service;
+    }
+};
+
+}
 
 #endif // HV_WEBSOCKET_SERVER_H_