From 87ff9f101749536ed89cb457402f33d479b12fe0 Mon Sep 17 00:00:00 2001 From: xx Date: Sat, 27 Apr 2024 18:13:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E4=BF=AE=E6=94=B9namespace?= =?UTF-8?q?=E9=99=8D=E7=BA=A7=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=94=AF=E6=8C=81?= =?UTF-8?q?C++14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ynet/ftp_client.h | 89 ++++---- include/ynet/http_agent.h | 45 ++-- include/ynet/http_cache.h | 44 ++-- include/ynet/http_cdn.h | 111 +++++----- include/ynet/http_center.h | 150 +++++++------- include/ynet/http_client_cache.h | 40 ++-- include/ynet/http_client_plus.h | 170 +++++++-------- include/ynet/http_controller.h | 82 ++++---- include/ynet/http_define.h | 341 ++++++++++++++++--------------- include/ynet/http_header.h | 78 +++---- include/ynet/http_host.h | 44 ++-- include/ynet/http_interceptor.h | 47 +++-- include/ynet/http_interface.h | 38 ++-- include/ynet/http_parser.h | 222 ++++++++++---------- include/ynet/http_reqpack.h | 192 ++++++++--------- include/ynet/http_request.h | 118 ++++++----- include/ynet/http_response.h | 58 +++--- include/ynet/http_router.h | 274 +++++++++++++------------ include/ynet/http_server.h | 122 +++++------ include/ynet/http_server_lst.h | 79 +++---- include/ynet/http_session.h | 108 +++++----- include/ynet/http_ssl.h | 78 +++---- include/ynet/http_website.h | 114 ++++++----- include/ynet/make_form.h | 30 +-- include/ynet/qps.hpp | 99 ++++----- include/ynet/tcp2tcp.h | 40 ++-- include/ynet/tcp_agent.h | 100 ++++----- include/ynet/tcp_agent_lst.h | 40 ++-- include/ynet/tcp_client.h | 70 ++++--- include/ynet/tcp_client_lst.h | 48 +++-- include/ynet/tcp_forward.h | 48 +++-- include/ynet/tcp_server.h | 130 ++++++------ include/ynet/tcp_server_lst.h | 40 ++-- include/ynet/ttu_client.h | 51 ++--- include/ynet/udp_node.h | 44 ++-- include/ynet/udp_node_lst.h | 36 ++-- include/ynet/util.h | 91 +++++---- include/ynet/utt_server.h | 39 ++-- include/yutil/codec.h | 72 ++++--- include/yutil/coroution.h | 2 + include/yutil/file.h | 249 +++++++++++----------- include/yutil/img.h | 9 +- include/yutil/mem.h | 14 +- include/yutil/process.h | 60 +++--- include/yutil/system.h | 66 +++--- include/yutil/time.h | 158 +++++++------- include/yutil/window.h | 100 ++++----- src/ymysql/src/mysql_plus.cpp | 9 +- src/yutil/src/coroution.cpp | 2 + 49 files changed, 2272 insertions(+), 2019 deletions(-) diff --git a/include/ynet/ftp_client.h b/include/ynet/ftp_client.h index 77cc668..6dad023 100644 --- a/include/ynet/ftp_client.h +++ b/include/ynet/ftp_client.h @@ -4,51 +4,58 @@ #include "yutil/thread.h" #include "ybase/error.h" #if USE_NET_FTP -namespace ylib::network::ftp +namespace ylib { - class client; - typedef void(*CALLBACK_FTPCLIENT_DOWNLOADING)(class ylib::network::ftp::client* client, uint32 all_size, uint32 down_size, uint64 param); - typedef void(*CALLBACK_FTPCLIENT_DOWNLOADED)(class ylib::network::ftp::client* client, bool result, uint64 param); - struct DownInfo + namespace network { - ~DownInfo() + namespace ftp { - if (file != nullptr) + class client; + typedef void(*CALLBACK_FTPCLIENT_DOWNLOADING)(class ylib::network::ftp::client* client, uint32 all_size, uint32 down_size, uint64 param); + typedef void(*CALLBACK_FTPCLIENT_DOWNLOADED)(class ylib::network::ftp::client* client, bool result, uint64 param); + struct DownInfo { - file->close(); - delete file; - } + ~DownInfo() + { + if (file != nullptr) + { + file->close(); + delete file; + } + } + ylib::file_io* file = nullptr; + uint32 size = 0; + void* fileptr = nullptr; + std::string local_path; + std::string remote_path; + uint64 param = 0; + CALLBACK_FTPCLIENT_DOWNLOADING downloading = nullptr; + CALLBACK_FTPCLIENT_DOWNLOADED downloaded = nullptr; + bool ok = false; + }; + class client :public ylib::error_base, private ylib::ithread + { + public: + client(); + ~client(); + bool connect(const std::string& ipaddress, const std::string& username, const std::string& password, ushort port); + void close(); + bool upload(const std::string& local_filepath, const std::string& remote_filepath); + bool download(const std::string& local_filepath, const std::string& remote_filepath, size_t param, CALLBACK_FTPCLIENT_DOWNLOADING downloading, CALLBACK_FTPCLIENT_DOWNLOADED downloaded); + void wait(); + bool download(const std::string& local_filepath, const std::string& remote_filepath); + bool create_dir(const std::string& remote_path); + bool delete_dir(const std::string& remote_path); + bool delete_file(const std::string& remote_path); + private: + void* m_ptr = nullptr; + DownInfo* m_download = nullptr; + CALLBACK_FTPCLIENT_DOWNLOADING m_cb_downloading = nullptr; + CALLBACK_FTPCLIENT_DOWNLOADED m_cb_downloaded = nullptr; + virtual bool run() override; + }; } - ylib::file_io* file = nullptr; - uint32 size = 0; - void* fileptr = nullptr; - std::string local_path; - std::string remote_path; - uint64 param = 0; - CALLBACK_FTPCLIENT_DOWNLOADING downloading = nullptr; - CALLBACK_FTPCLIENT_DOWNLOADED downloaded = nullptr; - bool ok = false; - }; - class client :public ylib::error_base, private ylib::ithread - { - public: - client(); - ~client(); - bool connect(const std::string& ipaddress, const std::string& username, const std::string& password, ushort port); - void close(); - bool upload(const std::string& local_filepath, const std::string& remote_filepath); - bool download(const std::string& local_filepath, const std::string& remote_filepath, size_t param, CALLBACK_FTPCLIENT_DOWNLOADING downloading, CALLBACK_FTPCLIENT_DOWNLOADED downloaded); - void wait(); - bool download(const std::string& local_filepath, const std::string& remote_filepath); - bool create_dir(const std::string& remote_path); - bool delete_dir(const std::string& remote_path); - bool delete_file(const std::string& remote_path); - private: - void* m_ptr = nullptr; - DownInfo* m_download = nullptr; - CALLBACK_FTPCLIENT_DOWNLOADING m_cb_downloading = nullptr; - CALLBACK_FTPCLIENT_DOWNLOADED m_cb_downloaded = nullptr; - virtual bool run() override; - }; + } + }; #endif \ No newline at end of file diff --git a/include/ynet/http_agent.h b/include/ynet/http_agent.h index 6afb5fa..014f31a 100644 --- a/include/ynet/http_agent.h +++ b/include/ynet/http_agent.h @@ -4,26 +4,33 @@ #if USE_NET_HTTP_WEBSITE class http_agent_listener; class http_agent_extra; -namespace ylib::network::http +namespace ylib { - class reqpack; - class server; - class agent:public ylib::error_base + namespace network { - public: - agent(); - ~agent(); - bool start(); - void stop(); - void disconnect(bool ssl,uint64 connid); - bool request(int32 wait_msec, reqpack* rp, network::http::proxy* proxy); - private: - void* get(); - private: - void* m_agent_ssl; - void* m_agent; - http_agent_listener* m_listener; - bool m_ssl; - }; + namespace http + { + class reqpack; + class server; + class agent :public ylib::error_base + { + public: + agent(); + ~agent(); + bool start(); + void stop(); + void disconnect(bool ssl, uint64 connid); + bool request(int32 wait_msec, reqpack* rp, network::http::proxy* proxy); + private: + void* get(); + private: + void* m_agent_ssl; + void* m_agent; + http_agent_listener* m_listener; + bool m_ssl; + }; + } + } + } #endif diff --git a/include/ynet/http_cache.h b/include/ynet/http_cache.h index a085b96..3334c0d 100644 --- a/include/ynet/http_cache.h +++ b/include/ynet/http_cache.h @@ -3,30 +3,36 @@ #include "http_reqpack.h" #include "http_interface.h" #include "yutil/map.hpp" -namespace ylib::network::http +namespace ylib { - class cache:public network::http::interface_,public ylib::error_base + namespace network { - public: - cache(); - ~cache(); - bool start(const ylib::json& config); - void stop(); - bool have_send(network::http::reqpack* rp); - bool enable(); - std::string make_key(const std::string& filepath); + namespace http + { + class cache :public network::http::interface_, public ylib::error_base + { + public: + cache(); + ~cache(); + bool start(const ylib::json& config); + void stop(); + bool have_send(network::http::reqpack* rp); + bool enable(); + std::string make_key(const std::string& filepath); - bool find_ext(const std::string& filepath); + bool find_ext(const std::string& filepath); - std::string make_cache_filepath(const std::string& filepath); - private: - bool m_enable; - std::string m_filepath; + std::string make_cache_filepath(const std::string& filepath); + private: + bool m_enable; + std::string m_filepath; - std::vector m_exts; + std::vector m_exts; - timestamp m_timeout_min; + timestamp m_timeout_min; - ylib::map m_keys; - }; + ylib::map m_keys; + }; + } + } } diff --git a/include/ynet/http_cdn.h b/include/ynet/http_cdn.h index 4284d77..e6451e8 100644 --- a/include/ynet/http_cdn.h +++ b/include/ynet/http_cdn.h @@ -5,62 +5,67 @@ #include "http_client_plus.h" #include "yutil/thread.h" -namespace ylib::network::http +namespace ylib { - class cnode:public ylib::error_base + namespace network { - public: - cnode(const ylib::json& config); - ~cnode(); - const std::string& host(); - int score(); - bool update(); - private: - network::http::client_plus m_client; - private: - // 名称 - std::string m_name; - // 域名 - std::string m_host; - // 管理密钥 - std::string m_key; - // 管理地址 - std::string m_mang_url; - // 回复耗时 - timestamp m_reply_wait_msec; - // 当前带宽上传 - uint64 m_send_bytes_sec; - // 当前带宽下载 - uint64 m_recv_bytes_sec; - // 更新时间 - timestamp m_update_sec; - // 通信状态 - bool m_net_status; - // 通信状态错误描述 - std::string m_net_errormsg; - // 最大带宽 - uint64 m_max_band; - }; + namespace http + { + class cnode :public ylib::error_base + { + public: + cnode(const ylib::json& config); + ~cnode(); + const std::string& host(); + int score(); + bool update(); + private: + network::http::client_plus m_client; + private: + // 名称 + std::string m_name; + // 域名 + std::string m_host; + // 管理密钥 + std::string m_key; + // 管理地址 + std::string m_mang_url; + // 回复耗时 + timestamp m_reply_wait_msec; + // 当前带宽上传 + uint64 m_send_bytes_sec; + // 当前带宽下载 + uint64 m_recv_bytes_sec; + // 更新时间 + timestamp m_update_sec; + // 通信状态 + bool m_net_status; + // 通信状态错误描述 + std::string m_net_errormsg; + // 最大带宽 + uint64 m_max_band; + }; - class cdn:public network::http::interface_,public ylib::error_base,private ylib::ithread - { - public: - cdn(); - ~cdn(); - bool start(const ylib::json& config); - std::string host(); - virtual bool run() override; - inline bool enable(){return m_enable;}; - inline bool manager(){return m_manager;}; - private: - bool m_enable; - bool m_manager; - std::vector m_nodes; + class cdn :public network::http::interface_, public ylib::error_base, private ylib::ithread + { + public: + cdn(); + ~cdn(); + bool start(const ylib::json& config); + std::string host(); + virtual bool run() override; + inline bool enable() { return m_enable; }; + inline bool manager() { return m_manager; }; + private: + bool m_enable; + bool m_manager; + std::vector m_nodes; - std::string m_cdn_node_key; - uint64 m_cdn_node_max_band; - ylib::BandInfo m_cdn_node_bandinfo; - }; - + std::string m_cdn_node_key; + uint64 m_cdn_node_max_band; + ylib::BandInfo m_cdn_node_bandinfo; + }; + } + } } diff --git a/include/ynet/http_center.h b/include/ynet/http_center.h index 2f95f14..ed7391e 100644 --- a/include/ynet/http_center.h +++ b/include/ynet/http_center.h @@ -7,81 +7,87 @@ #include "yutil/json.h" -namespace ylib::network::http +namespace ylib { - /*绑定域名*/ - struct domain_info + namespace network { - domain_info(){ - https = false; + namespace http + { + /*绑定域名*/ + struct domain_info + { + domain_info() { + https = false; + } + // 是否HTTPS + bool https; + // 域名 + std::string domain; + }; + + class server; + class router; + class session_mgr; + class website; + + /*************************************************************** + * Class:控制中心 + ***************************************************************/ + class center :public ylib::error_base + { + public: + + center(); + ~center(); + /****************************************************************** + * function:创建 + * param + * config : 配置项 + * return: + * 失败可通过 last_error() 返回错误信息。 + ******************************************************************/ + bool create(const ylib::json& config); + + bool start(); + + /****************************************************************** + * function:关闭 + ******************************************************************/ + void close(); + + /****************************************************************** + * Function:读配置 + * Param + * name : 名称 + * Return + * 配置内容 + ******************************************************************/ + std::string read_config(const std::string& name); + inline const ylib::json& config() { return m_json_config; } + network::http::server* server(ushort port); + + network::http::website* website(const std::string& host); + network::http::website* website_byname(const std::string& name); + private: + //所有监听端口 + std::vector listen_ports(); + //端口是否需要SSL + bool port_have_ssl(ushort port); + private: + // HTTP 服务 + std::vector m_server; + // 站点 + std::vector m_website; + // 配置缓存 + ylib::map m_config; + // JSON配置 + ylib::json m_json_config; + public: + uint64 m_temp[10]; + + }; } - // 是否HTTPS - bool https; - // 域名 - std::string domain; - }; - - class server; - class router; - class session_mgr; - class website; - - /*************************************************************** - * Class:控制中心 - ***************************************************************/ - class center:public ylib::error_base - { - public: - - center(); - ~center(); - /****************************************************************** - * function:创建 - * param - * config : 配置项 - * return: - * 失败可通过 last_error() 返回错误信息。 - ******************************************************************/ - bool create(const ylib::json& config); - - bool start(); - - /****************************************************************** - * function:关闭 - ******************************************************************/ - void close(); - - /****************************************************************** - * Function:读配置 - * Param - * name : 名称 - * Return - * 配置内容 - ******************************************************************/ - std::string read_config(const std::string& name); - inline const ylib::json& config() { return m_json_config; } - network::http::server* server(ushort port); - - network::http::website* website(const std::string& host); - network::http::website* website_byname(const std::string& name); - private: - //所有监听端口 - std::vector listen_ports(); - //端口是否需要SSL - bool port_have_ssl(ushort port); - private: - // HTTP 服务 - std::vector m_server; - // 站点 - std::vector m_website; - // 配置缓存 - ylib::map m_config; - // JSON配置 - ylib::json m_json_config; - public: - uint64 m_temp[10]; - - }; + } } #endif diff --git a/include/ynet/http_client_cache.h b/include/ynet/http_client_cache.h index 5e2d725..c0f1572 100644 --- a/include/ynet/http_client_cache.h +++ b/include/ynet/http_client_cache.h @@ -1,24 +1,30 @@ #pragma once #include "http_define.h" #if USE_NET_HTTP_CLIENT -namespace ylib::network::http +namespace ylib { - class client_plus; - //客户端缓存 - class client_cache + namespace network { - public: - client_cache(); - ~client_cache(); - void open(const std::string& dirpath); - bool read(network::http::client_plus* client, ylib::buffer& cache); - void set_header(network::http::client_plus* client, const std::string& url); - void close(); - void write(network::http::client_plus *client); - bool read(const std::string& url,ylib::buffer& cache); - private: - bool m_open; - std::string m_dirpath; - }; + namespace http + { + class client_plus; + //客户端缓存 + class client_cache + { + public: + client_cache(); + ~client_cache(); + void open(const std::string& dirpath); + bool read(network::http::client_plus* client, ylib::buffer& cache); + void set_header(network::http::client_plus* client, const std::string& url); + void close(); + void write(network::http::client_plus* client); + bool read(const std::string& url, ylib::buffer& cache); + private: + bool m_open; + std::string m_dirpath; + }; + } + } } #endif diff --git a/include/ynet/http_client_plus.h b/include/ynet/http_client_plus.h index cee1daf..57197ef 100644 --- a/include/ynet/http_client_plus.h +++ b/include/ynet/http_client_plus.h @@ -8,96 +8,102 @@ #include "make_form.h" class http_client_listener; -namespace ylib::network::http +namespace ylib { - class client_plus:public ylib::error_base + namespace network { - public: - client_plus(); - ~client_plus(); - void close(); - void set_timeout(uint32 connect_msec = 3000,uint32 recv_msec = 8000); - bool del(const std::string& url, const std::map& value = std::map()); - bool get(const std::string& url,const std::map& value = std::map(),bool wait = true); - bool post(const std::string& url,const std::map& value,bool to_utf8 = false); - bool post(const std::string& url, const ylib::json& value, bool to_utf8 = false); - bool post(const std::string& url, const ylib::buffer& value); - bool post(const std::string& url, const http::make_form& value); - bool head(const std::string& url); + namespace http + { + class client_plus :public ylib::error_base + { + public: + client_plus(); + ~client_plus(); + void close(); + void set_timeout(uint32 connect_msec = 3000, uint32 recv_msec = 8000); + bool del(const std::string& url, const std::map& value = std::map()); + bool get(const std::string& url, const std::map& value = std::map(), bool wait = true); + bool post(const std::string& url, const std::map& value, bool to_utf8 = false); + bool post(const std::string& url, const ylib::json& value, bool to_utf8 = false); + bool post(const std::string& url, const ylib::buffer& value); + bool post(const std::string& url, const http::make_form& value); + bool head(const std::string& url); - network::http::header_list& headers_request(); - network::http::header_list& headers_response(); - uint32 status(); - ylib::buffer& response(); - /*设置缓存*/ - void cache(client_cache* cache); - /*请求路径*/ - inline const std::string& url() { return m_url; } - /*cookie*/ - inline network::http::cookie& cookie() { return m_cookie; } - inline void cookie(const network::http::cookie& ck) { m_cookie = ck; } + network::http::header_list& headers_request(); + network::http::header_list& headers_response(); + uint32 status(); + ylib::buffer& response(); + /*设置缓存*/ + void cache(client_cache* cache); + /*请求路径*/ + inline const std::string& url() { return m_url; } + /*cookie*/ + inline network::http::cookie& cookie() { return m_cookie; } + inline void cookie(const network::http::cookie& ck) { m_cookie = ck; } - /*[回调] 正在下载*/ - void on_down_ing(const std::function& callback); - /*[回调] 下载结束*/ - void on_down_end(const std::function& callback); - /*[回调] 下载失败*/ - void on_down_failed(const std::function& callback); + /*[回调] 正在下载*/ + void on_down_ing(const std::function& callback); + /*[回调] 下载结束*/ + void on_down_end(const std::function& callback); + /*[回调] 下载失败*/ + void on_down_failed(const std::function& callback); - // 清理所有Cookie缓存 - static void clear_all_cookies(); + // 清理所有Cookie缓存 + static void clear_all_cookies(); - friend class http_client_listener; - public: - uint64 m_temp[10]; - private: - bool parseurl(const std::string& url); - bool connect(); - bool init(); - bool request(); - bool post(const std::string& url); - void* client(); - private: - // HP客户端 - void* m_client; - // HP客户端-SSL - void* m_client_ssl; - // HP监听器 - http_client_listener* m_listener; - // 是否已初始化 - bool m_init; - // 连接IP - std::string m_ipaddress; - // 连接端口 - ushort m_port; - // HTTPS - bool m_ssl; - // 请求链接 - std::string m_url; - // 请求路径 - std::string m_path; - // 请求方式 - network::http::method m_method; - // 请求数据 - ylib::buffer m_request_body; - // [header] 请求 - network::http::header_list m_headers_request; - // 超时时间 连接 - timestamp m_timeout_connect_msec; - // 超时时间 接收 - timestamp m_timeout_recv_msec; - // 缓存 - client_cache* m_cache; - // cookie - network::http::cookie m_cookie; - // 请求等待 - bool m_request_wait = true; + friend class http_client_listener; + public: + uint64 m_temp[10]; + private: + bool parseurl(const std::string& url); + bool connect(); + bool init(); + bool request(); + bool post(const std::string& url); + void* client(); + private: + // HP客户端 + void* m_client; + // HP客户端-SSL + void* m_client_ssl; + // HP监听器 + http_client_listener* m_listener; + // 是否已初始化 + bool m_init; + // 连接IP + std::string m_ipaddress; + // 连接端口 + ushort m_port; + // HTTPS + bool m_ssl; + // 请求链接 + std::string m_url; + // 请求路径 + std::string m_path; + // 请求方式 + network::http::method m_method; + // 请求数据 + ylib::buffer m_request_body; + // [header] 请求 + network::http::header_list m_headers_request; + // 超时时间 连接 + timestamp m_timeout_connect_msec; + // 超时时间 接收 + timestamp m_timeout_recv_msec; + // 缓存 + client_cache* m_cache; + // cookie + network::http::cookie m_cookie; + // 请求等待 + bool m_request_wait = true; #ifndef _WIN32 - public: + public: #endif - // 关闭 - bool m_close; + // 关闭 + bool m_close; - }; + }; + } + } } #endif diff --git a/include/ynet/http_controller.h b/include/ynet/http_controller.h index 4a0db4c..96762ab 100644 --- a/include/ynet/http_controller.h +++ b/include/ynet/http_controller.h @@ -51,48 +51,54 @@ using namespace ylib::network::http; #define qry_json_double(NAME) request()->parser()->json()[NAME].to(true) #define qry_json_short(NAME) request()->parser()->json()[NAME].to(true) #define qry_json_bool(NAME) request()->parser()->json()[NAME].to(true) -namespace ylib::network::http +namespace ylib { - class router; - class controller:public network::http::interface_ + namespace network { - public: - controller(); - ~controller(); - network::http::request* request(); - network::http::response* response(); - - - //min和max为<=或>= ,若填-1则不使用该条件 - std::string qry_string(const std::string& name, uint32 pmin = 0, uint32 pmax = _UINT_MAX); - int32 qry_int32(const std::string& name, int32 pmin = _INT_MIN, int32 pmax = _INT_MAX); - uint32 qry_uint32(const std::string& name, uint32 pmin = 0, uint32 pmax = _UINT_MAX); - int64 qry_int64(const std::string& name, int64 pmin = _INT64_MIN, int64 pmax = _INT64_MAX); - uint64 qry_uint64(const std::string& name, uint64 pmin = 0, uint64 pmax = _UINT64_MAX); - double qry_double(const std::string& name, double pmin = _DBL_MIN, double pmax = _DBL_MAX); - float qry_float(const std::string& name, float pmin = _FLT_MIN, float pmax = _FLT_MAX); - bool qry_empty(const std::string& name); - bool qry_bool(const std::string& name); - ylib::buffer qry_buffer(const std::string& name); - - // 请求参数 - bool request_param(const std::string& name,std::string& value); - - // 获取回复JSON - inline ylib::json& rpjson(){return response()->sjson["data"];} - inline ylib::json& rpcode() { return response()->sjson["code"]; } - //inline ylib::json& rp() { return response()->sjson["code"]; } - inline void rp(int code, const std::string& msg = "", const ylib::json& data = ylib::json()) + namespace http { - response()->sjson["code"] = code; - response()->sjson["msg"] = msg; - response()->sjson["data"] = data; - }; + class router; + class controller :public network::http::interface_ + { + public: + controller(); + ~controller(); + network::http::request* request(); + network::http::response* response(); - friend class router; - private: - network::http::reqpack* m_reqpack; - }; + //min和max为<=或>= ,若填-1则不使用该条件 + std::string qry_string(const std::string& name, uint32 pmin = 0, uint32 pmax = _UINT_MAX); + int32 qry_int32(const std::string& name, int32 pmin = _INT_MIN, int32 pmax = _INT_MAX); + uint32 qry_uint32(const std::string& name, uint32 pmin = 0, uint32 pmax = _UINT_MAX); + int64 qry_int64(const std::string& name, int64 pmin = _INT64_MIN, int64 pmax = _INT64_MAX); + uint64 qry_uint64(const std::string& name, uint64 pmin = 0, uint64 pmax = _UINT64_MAX); + double qry_double(const std::string& name, double pmin = _DBL_MIN, double pmax = _DBL_MAX); + float qry_float(const std::string& name, float pmin = _FLT_MIN, float pmax = _FLT_MAX); + bool qry_empty(const std::string& name); + bool qry_bool(const std::string& name); + ylib::buffer qry_buffer(const std::string& name); + + // 请求参数 + bool request_param(const std::string& name, std::string& value); + + // 获取回复JSON + inline ylib::json& rpjson() { return response()->sjson["data"]; } + inline ylib::json& rpcode() { return response()->sjson["code"]; } + //inline ylib::json& rp() { return response()->sjson["code"]; } + inline void rp(int code, const std::string& msg = "", const ylib::json& data = ylib::json()) + { + response()->sjson["code"] = code; + response()->sjson["msg"] = msg; + response()->sjson["data"] = data; + }; + + + friend class router; + private: + network::http::reqpack* m_reqpack = nullptr; + }; + } + } } #endif diff --git a/include/ynet/http_define.h b/include/ynet/http_define.h index c35ab23..8c10225 100644 --- a/include/ynet/http_define.h +++ b/include/ynet/http_define.h @@ -32,177 +32,182 @@ #define HTTP_ROUTER_PRINT 0 // http_interceptor 生产日志打印 #define HTTP_INTERCEPTOR_PRINT 0 -namespace ylib::network::http +namespace ylib { - class agent; - class website; - // 临时接收 - struct temp_recv + namespace network { - temp_recv() + namespace http { - agent_connid = 0; - agent_ssl = false; - } - void clear() - { - agent_connid = 0; - agent_ssl = false; - url.clear(); - ipaddress_port.clear(); - host.clear(); - } - // 接收数据 - ylib::buffer data; - // 代理ID - uint64 agent_connid; - // 代理SSL - bool agent_ssl; - // URL - std::string url; - // 代理连接IP及端口 - std::string ipaddress_port; - // Host - std::string host; - // 缓存路径 - ylib::file_io cache_file; - }; - // 代理 - struct proxy - { - proxy() - { - remote_port = 0; - ssl = false; - } - // 拦截地址正则 - std::regex src_express; - // 拦截地址字符串 - std::string src_str; - // 目标地址 - std::string dst; - // 请求URL - std::string remote_url; - // 请求地址 - std::string remote_ipaddress; - // 请求端口 - ushort remote_port; - // 主机 - std::string host; - // 附加协议头 - std::map request_headers; - std::map response_headers; - // SSL - bool ssl; + class agent; + class website; + // 临时接收 + struct temp_recv + { + temp_recv() + { + agent_connid = 0; + agent_ssl = false; + } + void clear() + { + agent_connid = 0; + agent_ssl = false; + url.clear(); + ipaddress_port.clear(); + host.clear(); + } + // 接收数据 + ylib::buffer data; + // 代理ID + uint64 agent_connid; + // 代理SSL + bool agent_ssl; + // URL + std::string url; + // 代理连接IP及端口 + std::string ipaddress_port; + // Host + std::string host; + // 缓存路径 + ylib::file_io cache_file; + }; + // 代理 + struct proxy + { + proxy() + { + remote_port = 0; + ssl = false; + } + // 拦截地址正则 + std::regex src_express; + // 拦截地址字符串 + std::string src_str; + // 目标地址 + std::string dst; + // 请求URL + std::string remote_url; + // 请求地址 + std::string remote_ipaddress; + // 请求端口 + ushort remote_port; + // 主机 + std::string host; + // 附加协议头 + std::map request_headers; + std::map response_headers; + // SSL + bool ssl; - }; - // httpserver代理后连接附加数据 - struct httpserver_proxy_extra - { - httpserver_proxy_extra() - { - agent = nullptr; - connid = 0; - index = 45854; - } - void* agent; - uint64 connid; - uint64 index; - }; - struct website_info - { - website_info() - { - gzip = false; - download_maxbaud = 0; - } - // 根目录 - std::string rootdir; - // GZIP - bool gzip; - // 默认编码方式 - std::string default_codec; - // 带宽限制 - uint32 download_maxbaud; - }; - /*表单信息*/ - struct form_info - { - form_info() - { - start = -1; - length = -1; - } - std::string disposition; - std::string content_type; - std::string name; - std::string filename; - int64 start; - int64 length; - ylib::buffer data; - }; - class controller; - //请求类型 - enum content_type - { - CT_FORM = 0, //表单: application/x-www-CT_FORM-urlencoded - CT_JSON = 1, //CT_JSON: application/CT_JSON - CT_TEXT = 2, //文本: text/plain - CT_NIL = 3, //无 - CT_ALL = 4 //所有 - }; - /*HTTP请求类型*/ - enum method - { - GET = 0, - POST = 1, - PUT = 2, - DEL = 3, - OTHER = 4, - HEAD = 5, - ALL = 100 - }; - inline std::string method_to_string(enum method m){ - switch(m){ - case GET: - return "GET"; - case POST: - return "POST"; - case PUT: - return "PUT"; - case DEL: - return "DEL"; - case OTHER: - return "OTHER"; - case ALL: - return "ALL"; - default: - return "unknown"; - } - return ""; - }; - /*控制器内返回结果*/ - enum response_type - { - RT_OK, //成功或已处理 - RT_500, //服务器内部错误 - RT_406, //服务器解析客户端数据失败或格式不正确拒绝解析 - RT_401 //需要鉴权,无权限 - }; - //控制器指针 - typedef response_type(network::http::controller::* HTTP_CTR_FUNCTION)(); - /*SSL验证类型*/ - enum ssl_verify_type - { - // 完全忽略验证证书的结果。当握手必须完成的话,就选中这个选项。其实真正有证书的人很少,尤其是在中国,那么如果 SSL 运用于一些免费的服务,比如 EMAIL 的时候,SERVER 端最好采用这个模式。 - SVT_VERIFY_NONE = 0x00, - // 希望验证对方的证书。这个是最一般的模式。对 CLIENT 来说,如果设置了这样的模式,验证SERVER的证书出了任何错误,SSL 握手都告吹。对 SERVER 来说,如果设置了这样的模式,CLIENT 倒不一定要把自己的证书交出去。如果 CLIENT 没有交出证书,SERVER 自己决定下一步怎么做。 - SVT_VERIFY_PEER = 0x01, - // 这是 SERVER 使用的一种模式,在这种模式下, SERVER 会向 CLIENT 要证书。如果 CLIENT 不给,SSL 握手告吹 - SVT_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02, - // 这是仅能使用在 SSL SESSION RENEGOTIATION 阶段的一种方式。如果不是用这个模式的话,那么在 RENEGOTIATION 的时候,CLIENT 都要把自己的证书送给 SERVER,然后做一番分析。这个过程很消耗 CPU 时间的,而这个模式则不需要 CLIENT 在 RENEGOTIATION 的时候重复送自己的证书了。 - SVT_VERIFY_CLIENT_ONCE = 0x03 + }; + // httpserver代理后连接附加数据 + struct httpserver_proxy_extra + { + httpserver_proxy_extra() + { + agent = nullptr; + connid = 0; + index = 45854; + } + void* agent; + uint64 connid; + uint64 index; + }; + struct website_info + { + website_info() + { + gzip = false; + download_maxbaud = 0; + } + // 根目录 + std::string rootdir; + // GZIP + bool gzip; + // 默认编码方式 + std::string default_codec; + // 带宽限制 + uint32 download_maxbaud; + }; + /*表单信息*/ + struct form_info + { + form_info() + { + start = -1; + length = -1; + } + std::string disposition; + std::string content_type; + std::string name; + std::string filename; + int64 start; + int64 length; + ylib::buffer data; + }; + class controller; + //请求类型 + enum content_type + { + CT_FORM = 0, //表单: application/x-www-CT_FORM-urlencoded + CT_JSON = 1, //CT_JSON: application/CT_JSON + CT_TEXT = 2, //文本: text/plain + CT_NIL = 3, //无 + CT_ALL = 4 //所有 + }; + /*HTTP请求类型*/ + enum method + { + GET = 0, + POST = 1, + PUT = 2, + DEL = 3, + OTHER = 4, + HEAD = 5, + ALL = 100 + }; + inline std::string method_to_string(enum method m) { + switch (m) { + case GET: + return "GET"; + case POST: + return "POST"; + case PUT: + return "PUT"; + case DEL: + return "DEL"; + case OTHER: + return "OTHER"; + case ALL: + return "ALL"; + default: + return "unknown"; + } + return ""; + }; + /*控制器内返回结果*/ + enum response_type + { + RT_OK, //成功或已处理 + RT_500, //服务器内部错误 + RT_406, //服务器解析客户端数据失败或格式不正确拒绝解析 + RT_401 //需要鉴权,无权限 + }; + //控制器指针 + typedef response_type(network::http::controller::* HTTP_CTR_FUNCTION)(); + /*SSL验证类型*/ + enum ssl_verify_type + { + // 完全忽略验证证书的结果。当握手必须完成的话,就选中这个选项。其实真正有证书的人很少,尤其是在中国,那么如果 SSL 运用于一些免费的服务,比如 EMAIL 的时候,SERVER 端最好采用这个模式。 + SVT_VERIFY_NONE = 0x00, + // 希望验证对方的证书。这个是最一般的模式。对 CLIENT 来说,如果设置了这样的模式,验证SERVER的证书出了任何错误,SSL 握手都告吹。对 SERVER 来说,如果设置了这样的模式,CLIENT 倒不一定要把自己的证书交出去。如果 CLIENT 没有交出证书,SERVER 自己决定下一步怎么做。 + SVT_VERIFY_PEER = 0x01, + // 这是 SERVER 使用的一种模式,在这种模式下, SERVER 会向 CLIENT 要证书。如果 CLIENT 不给,SSL 握手告吹 + SVT_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02, + // 这是仅能使用在 SSL SESSION RENEGOTIATION 阶段的一种方式。如果不是用这个模式的话,那么在 RENEGOTIATION 的时候,CLIENT 都要把自己的证书送给 SERVER,然后做一番分析。这个过程很消耗 CPU 时间的,而这个模式则不需要 CLIENT 在 RENEGOTIATION 的时候重复送自己的证书了。 + SVT_VERIFY_CLIENT_ONCE = 0x03 - }; - + }; + } + } } diff --git a/include/ynet/http_header.h b/include/ynet/http_header.h index 45e20b9..4f41ee7 100644 --- a/include/ynet/http_header.h +++ b/include/ynet/http_header.h @@ -2,47 +2,53 @@ #include "define.h" #if USE_NET_HTTP_UTIL #include -namespace ylib::network::http +namespace ylib { - - class header + namespace network { - public: - header(); - ~header(); - void clear(); - void name(const std::string& value); - std::string name() const; + namespace http + { - std::map param(); + class header + { + public: + header(); + ~header(); + void clear(); + void name(const std::string& value); + std::string name() const; - void parse(const std::string& body); - void push(const std::string& value); - void push(const std::string& key,const std::string& value); + std::map param(); - bool exist(const std::string& key); + void parse(const std::string& body); + void push(const std::string& value); + void push(const std::string& key, const std::string& value); - std::string to_string() const; - private: - std::string m_name; - std::map m_param; - std::vector m_values; - }; - class header_list - { - public: - header_list(); - ~header_list(); - bool exist(const std::string& name) const; - ylib::network::http::header get(const std::string& name); - void set(const std::string& name, const ylib::network::http::header& header); - void set(const std::string& name,const std::string& value); - void del(const std::string& name); - std::map to() const; - size_t size(); - void clear(); - private: - std::map m_headers; - }; + bool exist(const std::string& key); + + std::string to_string() const; + private: + std::string m_name; + std::map m_param; + std::vector m_values; + }; + class header_list + { + public: + header_list(); + ~header_list(); + bool exist(const std::string& name) const; + ylib::network::http::header get(const std::string& name); + void set(const std::string& name, const ylib::network::http::header& header); + void set(const std::string& name, const std::string& value); + void del(const std::string& name); + std::map to() const; + size_t size(); + void clear(); + private: + std::map m_headers; + }; + } + } } #endif diff --git a/include/ynet/http_host.h b/include/ynet/http_host.h index abdc778..fec8f7b 100644 --- a/include/ynet/http_host.h +++ b/include/ynet/http_host.h @@ -2,27 +2,33 @@ #include "define.h" #if USE_NET_HTTP_WEBSITE #include "ynet/http_interface.h" -namespace ylib::network::http +namespace ylib { - /****************************************************** - * class:域名器 - ******************************************************/ - class ssl; - class host :public ylib::error_base, public network::http::interface_ + namespace network { - public: - host(); - ~host(); - void init(const std::string& host,ushort port, network::http::ssl* ssl = nullptr); - inline network::http::ssl* ssl(){return m_ssl;} - inline bool equal(const std::string& host) { return m_host == host; } - inline const std::string& hostname() { return m_return_host; } - private: - std::string m_host; - std::string m_return_host; - ushort m_port; - network::http::ssl* m_ssl; - }; + namespace http + { + /****************************************************** + * class:域名器 + ******************************************************/ + class ssl; + class host :public ylib::error_base, public network::http::interface_ + { + public: + host(); + ~host(); + void init(const std::string& host, ushort port, network::http::ssl* ssl = nullptr); + inline network::http::ssl* ssl() { return m_ssl; } + inline bool equal(const std::string& host) { return m_host == host; } + inline const std::string& hostname() { return m_return_host; } + private: + std::string m_host; + std::string m_return_host; + ushort m_port; + network::http::ssl* m_ssl; + }; + } + } } #endif diff --git a/include/ynet/http_interceptor.h b/include/ynet/http_interceptor.h index 8006651..ead0bd6 100644 --- a/include/ynet/http_interceptor.h +++ b/include/ynet/http_interceptor.h @@ -6,30 +6,35 @@ #include "ynet/http_interface.h" #include "yutil/array.hpp" -namespace ylib::network::http +namespace ylib { - class reqpack; - - struct interceptor_info{ - std::regex express; - std::string express_string; - std::function callback; - }; - /****************************************************** - * class:拦截器 - ******************************************************/ - class interceptor :public ylib::error_base, public network::http::interface_ + namespace network { - public: - interceptor(); - ~interceptor(); - size_t add(const std::string& regex_express,std::function callback); - void remove(size_t index); - bool trigger(const std::string& url,network::http::reqpack* rp); - private: - ylib::nolock_array m_array; - }; + namespace http + { + class reqpack; + struct interceptor_info { + std::regex express; + std::string express_string; + std::function callback; + }; + /****************************************************** + * class:拦截器 + ******************************************************/ + class interceptor :public ylib::error_base, public network::http::interface_ + { + public: + interceptor(); + ~interceptor(); + size_t add(const std::string& regex_express, std::function callback); + void remove(size_t index); + bool trigger(const std::string& url, network::http::reqpack* rp); + private: + ylib::nolock_array m_array; + }; + } + } } #endif diff --git a/include/ynet/http_interface.h b/include/ynet/http_interface.h index 9e53ffc..cfc835f 100644 --- a/include/ynet/http_interface.h +++ b/include/ynet/http_interface.h @@ -3,23 +3,29 @@ #if USE_NET_HTTP_WEBSITE #include #include -namespace ylib::network::http +namespace ylib { - /************************************************************************* - * class:通用接口 - *************************************************************************/ - class website; - class center; - class interface_ + namespace network { - public: - inline void website(network::http::website* website) { m_website = website; } - inline network::http::website* website() { return m_website; } - inline void center(network::http::center* center) { m_center = center; } - inline network::http::center* center() { return m_center; } - private: - network::http::website* m_website; - network::http::center* m_center; - }; + namespace http + { + /************************************************************************* + * class:通用接口 + *************************************************************************/ + class website; + class center; + class interface_ + { + public: + inline void website(network::http::website* website) { m_website = website; } + inline network::http::website* website() { return m_website; } + inline void center(network::http::center* center) { m_center = center; } + inline network::http::center* center() { return m_center; } + private: + network::http::website* m_website; + network::http::center* m_center; + }; + } + } } #endif diff --git a/include/ynet/http_parser.h b/include/ynet/http_parser.h index 36eaae9..22bea5d 100644 --- a/include/ynet/http_parser.h +++ b/include/ynet/http_parser.h @@ -2,117 +2,123 @@ #include "http_define.h" #if USE_NET_HTTP_WEBSITE #include -namespace ylib::network::http +namespace ylib { - class parser; - /************************************************************************* - * class:FORM表单解析器 - *************************************************************************/ - class form_parser{ - public: - form_parser(); - ~form_parser(); - - - std::vector names(); - bool get(const std::string& name,form_info& info); - bool write_file(const std::string& name,const std::string& filepath); - - friend class parser; - private: - bool parse(ylib::buffer* data); - void parse_count(std::vector& starts,std::vector& lengths); - void parse_form(uint32 start,uint32 length); - char getchar(size_t index); - private: - ylib::buffer* m_data; - std::map m_infos; - }; - /************************************************************************* - * class:解析器 - *************************************************************************/ - class parser + namespace network { - public: - /****************************************************************** - * function:初始化 - * param - * - * url : 请求地址 - * method : 请求类型 - * data : 请求数据 - * content_type : 类型协议头 - ******************************************************************/ - bool init(const std::string& url,const network::http::method& method, const ylib::buffer& data,const std::string& content_type); - public: - parser(); - ~parser(); + namespace http + { + class parser; + /************************************************************************* + * class:FORM表单解析器 + *************************************************************************/ + class form_parser { + public: + form_parser(); + ~form_parser(); - const ylib::json& json(); - std::string text(); - /****************************************************************** - * function:取URL参数 - * param - * name : 名称 - * value : 数据 - ******************************************************************/ - bool url_param(const std::string& name,std::string& value); - bool url_param_exist(const std::string& name); - /****************************************************************** - * function:取URL请求参数名列表 - ******************************************************************/ - std::vector url_param_names(); - /****************************************************************** - * function:取BODY参数 - * param - * name : 名称 - * value : 数据 - ******************************************************************/ - bool body_param(const std::string& name,std::string& value); - bool body_param_exist(const std::string& name); - /****************************************************************** - * function:取BODY请求参数名列表 - ******************************************************************/ - std::vector body_param_names(); - /****************************************************************** - * function:取BODY请求参数名列表 - ******************************************************************/ - form_parser* form(); - private: - /****************************************************************** - * function:解析URL - ******************************************************************/ - void parse_url(const std::string& url,std::map& map); - /****************************************************************** - * function:解析JSON - ******************************************************************/ - void parse_json(); - /****************************************************************** - * function:解析FORM表单 - ******************************************************************/ - void parse_form(); - /****************************************************************** - * function:解析URL格式数据 - ******************************************************************/ - bool read_url_param(const std::map& map,const std::string& name,std::string& value); - private: - // URL参数 - std::map m_url_param; - // BODY参数 - std::map m_body_param; - // JSON参数 - ylib::json m_json_param; - // FORM解析器 - network::http::form_parser m_form; - // 请求地址 - std::string m_url; - // 请求数据 - ylib::buffer m_data; - // 请求内容类型 - std::string m_content_type; - // 请求类型 - network::http::method m_method; - }; + std::vector names(); + bool get(const std::string& name, form_info& info); + bool write_file(const std::string& name, const std::string& filepath); + + friend class parser; + private: + bool parse(ylib::buffer* data); + void parse_count(std::vector& starts, std::vector& lengths); + void parse_form(uint32 start, uint32 length); + char getchar(size_t index); + private: + ylib::buffer* m_data; + std::map m_infos; + }; + /************************************************************************* + * class:解析器 + *************************************************************************/ + class parser + { + public: + /****************************************************************** + * function:初始化 + * param + * + * url : 请求地址 + * method : 请求类型 + * data : 请求数据 + * content_type : 类型协议头 + ******************************************************************/ + bool init(const std::string& url, const network::http::method& method, const ylib::buffer& data, const std::string& content_type); + public: + parser(); + ~parser(); + + const ylib::json& json(); + std::string text(); + /****************************************************************** + * function:取URL参数 + * param + * name : 名称 + * value : 数据 + ******************************************************************/ + bool url_param(const std::string& name, std::string& value); + bool url_param_exist(const std::string& name); + /****************************************************************** + * function:取URL请求参数名列表 + ******************************************************************/ + std::vector url_param_names(); + /****************************************************************** + * function:取BODY参数 + * param + * name : 名称 + * value : 数据 + ******************************************************************/ + bool body_param(const std::string& name, std::string& value); + bool body_param_exist(const std::string& name); + /****************************************************************** + * function:取BODY请求参数名列表 + ******************************************************************/ + std::vector body_param_names(); + /****************************************************************** + * function:取BODY请求参数名列表 + ******************************************************************/ + form_parser* form(); + private: + /****************************************************************** + * function:解析URL + ******************************************************************/ + void parse_url(const std::string& url, std::map& map); + /****************************************************************** + * function:解析JSON + ******************************************************************/ + void parse_json(); + /****************************************************************** + * function:解析FORM表单 + ******************************************************************/ + void parse_form(); + /****************************************************************** + * function:解析URL格式数据 + ******************************************************************/ + bool read_url_param(const std::map& map, const std::string& name, std::string& value); + private: + // URL参数 + std::map m_url_param; + // BODY参数 + std::map m_body_param; + // JSON参数 + ylib::json m_json_param; + // FORM解析器 + network::http::form_parser m_form; + // 请求地址 + std::string m_url; + // 请求数据 + ylib::buffer m_data; + // 请求内容类型 + std::string m_content_type; + // 请求类型 + network::http::method m_method; + + }; + } + } } #endif diff --git a/include/ynet/http_reqpack.h b/include/ynet/http_reqpack.h index 8126634..9ae4ca0 100644 --- a/include/ynet/http_reqpack.h +++ b/include/ynet/http_reqpack.h @@ -4,104 +4,110 @@ #include "http_server.h" #include "yutil/strutils.h" #include "yutil/time.h" -namespace ylib::network::http +namespace ylib { - class server; - class response; - class request; - class website; - /************************************************************************* - * class:请求包 - *************************************************************************/ - class reqpack + namespace network { - public: + namespace http + { + class server; + class response; + class request; + class website; + /************************************************************************* + * class:请求包 + *************************************************************************/ + class reqpack + { + public: - reqpack(); - ~reqpack(); + reqpack(); + ~reqpack(); - void init(const std::string& url,const std::string& host,const ylib::buffer& data, uint64 connid, network::http::server* server); - void clear(); - network::http::request* request(); - network::http::response* response(); + void init(const std::string& url, const std::string& host, const ylib::buffer& data, uint64 connid, network::http::server* server); + void clear(); + network::http::request* request(); + network::http::response* response(); - inline const std::string& host() - { - /*获取基本请求信息*/ - return m_host; - } - network::http::method method(); - const std::string& filepath(); - void filepath(const std::string& path); - inline network::http::server* server() - { - return m_server; - } - inline ylib::buffer& data() - { - return m_data; - } - inline const std::string& url() - { - return m_url; - } - inline const std::string& referrer() - { - return m_referrer; - } - inline const uint64& connid() - { - return m_connid; - } - inline network::http::website* website() { - return m_website; - } - inline void website(network::http::website* website) { - m_website = website; - } - inline timestamp begin_msec(){ - return m_begin_msec; - } - inline std::string exec_msec(){ - return std::to_string(time::now_msec()-m_begin_msec); - } - inline std::string& remote(){ - if(m_remote_ipaddress.empty()){ - ushort port; - m_server->remote(connid(),m_remote_ipaddress,port); - } - return m_remote_ipaddress; - } - ylib::json& extra() { return m_extra; } - private: - // 请求主机 - std::string m_host; - // 请求方式 - network::http::method m_method; - // 请求路径 - std::string m_filepath; - // HPSERVER - network::http::server *m_server; - // 转发来路 - std::string m_referrer; - // 请求ID - uint64 m_connid; - // 站点 - network::http::website* m_website; - // 接收数据 - ylib::buffer m_data; - // URL - std::string m_url; - // 请求发起时间 - timestamp m_begin_msec; - // 远程IP - std::string m_remote_ipaddress; + inline const std::string& host() + { + /*获取基本请求信息*/ + return m_host; + } + network::http::method method(); + const std::string& filepath(); + void filepath(const std::string& path); + inline network::http::server* server() + { + return m_server; + } + inline ylib::buffer& data() + { + return m_data; + } + inline const std::string& url() + { + return m_url; + } + inline const std::string& referrer() + { + return m_referrer; + } + inline const uint64& connid() + { + return m_connid; + } + inline network::http::website* website() { + return m_website; + } + inline void website(network::http::website* website) { + m_website = website; + } + inline timestamp begin_msec() { + return m_begin_msec; + } + inline std::string exec_msec() { + return std::to_string(time::now_msec() - m_begin_msec); + } + inline std::string& remote() { + if (m_remote_ipaddress.empty()) { + ushort port; + m_server->remote(connid(), m_remote_ipaddress, port); + } + return m_remote_ipaddress; + } + ylib::json& extra() { return m_extra; } + private: + // 请求主机 + std::string m_host; + // 请求方式 + network::http::method m_method; + // 请求路径 + std::string m_filepath; + // HPSERVER + network::http::server* m_server; + // 转发来路 + std::string m_referrer; + // 请求ID + uint64 m_connid; + // 站点 + network::http::website* m_website; + // 接收数据 + ylib::buffer m_data; + // URL + std::string m_url; + // 请求发起时间 + timestamp m_begin_msec; + // 远程IP + std::string m_remote_ipaddress; - // 附加数据 - ylib::json m_extra; - private: - network::http::request* m_request; - network::http::response* m_response; - }; + // 附加数据 + ylib::json m_extra; + private: + network::http::request* m_request; + network::http::response* m_response; + }; + } + } } #endif diff --git a/include/ynet/http_request.h b/include/ynet/http_request.h index 8fb61bd..92cb4f0 100644 --- a/include/ynet/http_request.h +++ b/include/ynet/http_request.h @@ -6,65 +6,71 @@ #include "http_session.h" #include "http_parser.h" #include "http_interface.h" -namespace ylib::network::http +namespace ylib { - class reqpack; - class server; - /******************************************************************** - * class:Http请求解析类 - ********************************************************************/ - class request:public network::http::interface_ + namespace network { - public: - request(); - ~request(); + namespace http + { + class reqpack; + class server; + /******************************************************************** + * class:Http请求解析类 + ********************************************************************/ + class request :public network::http::interface_ + { + public: + request(); + ~request(); - /*************************************************************************** - * function:取协议头 - * param - * name : 名称 - * value : 内容 - ***************************************************************************/ - bool header(const std::string& name, std::string& value); + /*************************************************************************** + * function:取协议头 + * param + * name : 名称 + * value : 内容 + ***************************************************************************/ + bool header(const std::string& name, std::string& value); - /*************************************************************************** - * function:取请求类型 - ***************************************************************************/ - network::http::method method(); - /*************************************************************************** - * function:取请求路径 - ***************************************************************************/ - std::string filepath(); - /*************************************************************************** - * function:取请求主机 - ***************************************************************************/ - std::string host(); - /*************************************************************************** - * function:取Cookie - ***************************************************************************/ - bool cookie(const std::string& name, std::string& value); - /*************************************************************************** - * function:取Session - ***************************************************************************/ - network::http::session* session(const std::string& name); - /*************************************************************************** - * function:取reqpack - ***************************************************************************/ - network::http::reqpack* reqpack(); - /*************************************************************************** - * function:解析器 - ***************************************************************************/ - network::http::parser* parser(); - /*************************************************************************** - * function:Get Browserr Remote Ipaddress - ***************************************************************************/ - std::string remote_ipaddress(bool find_header = false,const std::string& inside_ipaddress = ""); - ushort remote_port(); - friend class reqpack; - private: - network::http::reqpack *m_reqpack; - network::http::session* m_session; - network::http::parser m_parser; - }; + /*************************************************************************** + * function:取请求类型 + ***************************************************************************/ + network::http::method method(); + /*************************************************************************** + * function:取请求路径 + ***************************************************************************/ + std::string filepath(); + /*************************************************************************** + * function:取请求主机 + ***************************************************************************/ + std::string host(); + /*************************************************************************** + * function:取Cookie + ***************************************************************************/ + bool cookie(const std::string& name, std::string& value); + /*************************************************************************** + * function:取Session + ***************************************************************************/ + network::http::session* session(const std::string& name); + /*************************************************************************** + * function:取reqpack + ***************************************************************************/ + network::http::reqpack* reqpack(); + /*************************************************************************** + * function:解析器 + ***************************************************************************/ + network::http::parser* parser(); + /*************************************************************************** + * function:Get Browserr Remote Ipaddress + ***************************************************************************/ + std::string remote_ipaddress(bool find_header = false, const std::string& inside_ipaddress = ""); + ushort remote_port(); + friend class reqpack; + private: + network::http::reqpack* m_reqpack; + network::http::session* m_session; + network::http::parser m_parser; + }; + } + } } #endif diff --git a/include/ynet/http_response.h b/include/ynet/http_response.h index 9a51952..ef84b2c 100644 --- a/include/ynet/http_response.h +++ b/include/ynet/http_response.h @@ -7,35 +7,41 @@ #include "http_header.h" #include "http_interface.h" -namespace ylib::network::http +namespace ylib { - class server; - class reqpack; - class response:public network::http::interface_ + namespace network { - public: - response(); - ~response(); - void init(reqpack* rp); - bool send(const char* buf, size_t buf_len, ushort stateNum = 200, const std::string& stateDesc = "OK"); - bool send(const ylib::buffer& value, ushort stateNum = 200, const std::string& stateDesc = "OK"); - bool send(const std::string& value, ushort stateNum = 200, const std::string& stateDesc = "OK"); - bool send(const ylib::json & json, ushort stateNum = 200, const std::string& stateDesc = "OK"); - bool send_file(const std::string& filepath, int32 downbaud = -1,bool rootdir = true); - std::map* headers(); - bool redirect(const std::string& filepath, bool MovedPermanently = false); - bool forward(const std::string& filepath); - public: - ylib::json sjson; - private: - bool filecache(const uint64& last_modify_time); - bool fileoffset(long filesize, long& start, long& len); - private: - std::map m_headers; - bool m_response; - network::http::reqpack *m_reqpack; + namespace http + { + class server; + class reqpack; + class response :public network::http::interface_ + { + public: + response(); + ~response(); + void init(reqpack* rp); + bool send(const char* buf, size_t buf_len, ushort stateNum = 200, const std::string& stateDesc = "OK"); + bool send(const ylib::buffer& value, ushort stateNum = 200, const std::string& stateDesc = "OK"); + bool send(const std::string& value, ushort stateNum = 200, const std::string& stateDesc = "OK"); + bool send(const ylib::json& json, ushort stateNum = 200, const std::string& stateDesc = "OK"); + bool send_file(const std::string& filepath, int32 downbaud = -1, bool rootdir = true); + std::map* headers(); + bool redirect(const std::string& filepath, bool MovedPermanently = false); + bool forward(const std::string& filepath); + public: + ylib::json sjson; + private: + bool filecache(const uint64& last_modify_time); + bool fileoffset(long filesize, long& start, long& len); + private: + std::map m_headers; + bool m_response; + network::http::reqpack* m_reqpack; - }; + }; + } + } } #endif diff --git a/include/ynet/http_router.h b/include/ynet/http_router.h index 28e7345..ea89e30 100644 --- a/include/ynet/http_router.h +++ b/include/ynet/http_router.h @@ -9,146 +9,152 @@ #include "yutil/queue.hpp" #include "yutil/thread.h" class IHPThreadPool; -namespace ylib::network::http +namespace ylib { - class reqpack; - class http_server_lst; - class request; - class response; - class interceptor; - /************************************* - * struct:订阅信息 - *************************************/ - struct subscribe_info{ - subscribe_info() - { - method = network::http::ALL; - controller = false; - controller_function = nullptr; - } - // 请求路径 - std::regex express; - // 请求类型 - network::http::method method; - // 回调函数 - std::function callback; - // 控制器。为控制器则启动下面两个属性 - bool controller; - // 创建控制器类指针 - std::function create_controller_callback; - // 控制器函数 - HTTP_CTR_FUNCTION controller_function; - }; - /************************************************************************* - * class:路由中专服务 - *************************************************************************/ - class router:public ylib::error_base,public network::http::interface_,private ylib::ithread + namespace network { - public: - /*线程参数信息*/ - struct thread_param_info + namespace http { - void clear(){} - network::http::router* router; - network::http::reqpack* reqpack; - }; + class reqpack; + class http_server_lst; + class request; + class response; + class interceptor; + /************************************* + * struct:订阅信息 + *************************************/ + struct subscribe_info { + subscribe_info() + { + method = network::http::ALL; + controller = false; + controller_function = nullptr; + } + // 请求路径 + std::regex express; + // 请求类型 + network::http::method method; + // 回调函数 + std::function callback; + // 控制器。为控制器则启动下面两个属性 + bool controller; + // 创建控制器类指针 + std::function create_controller_callback; + // 控制器函数 + HTTP_CTR_FUNCTION controller_function; + }; + /************************************************************************* + * class:路由中专服务 + *************************************************************************/ + class router :public ylib::error_base, public network::http::interface_, private ylib::ithread + { + public: + /*线程参数信息*/ + struct thread_param_info + { + void clear() {} + network::http::router* router; + network::http::reqpack* reqpack; + }; - public: - router(); - ~router(); - /****************************************************************** - * function:启动 - * param - * config : 配置项 - * return: - * 失败可通过 last_error() 返回错误信息。 - ******************************************************************/ - bool start(const ylib::json& config); - /****************************************************************** - * function:关闭 - ******************************************************************/ - void close(); - /****************************************************************** - * function:拦截器 - ******************************************************************/ - network::http::interceptor* interceptor(); - /****************************************************************** - * function:订阅 - * desc:浏览器请求会首先触发订阅,订阅未找到符合项则触发 other 传入函数。 - * param - * path : 路径 - * method : 请求类型 - * callback : 触发回调 - * return: - * 同一地址不允许订阅两次 - ******************************************************************/ - void subscribe( - const std::string& path, - network::http::method method, - std::function callback - ); + public: + router(); + ~router(); + /****************************************************************** + * function:启动 + * param + * config : 配置项 + * return: + * 失败可通过 last_error() 返回错误信息。 + ******************************************************************/ + bool start(const ylib::json& config); + /****************************************************************** + * function:关闭 + ******************************************************************/ + void close(); + /****************************************************************** + * function:拦截器 + ******************************************************************/ + network::http::interceptor* interceptor(); + /****************************************************************** + * function:订阅 + * desc:浏览器请求会首先触发订阅,订阅未找到符合项则触发 other 传入函数。 + * param + * path : 路径 + * method : 请求类型 + * callback : 触发回调 + * return: + * 同一地址不允许订阅两次 + ******************************************************************/ + void subscribe( + const std::string& path, + network::http::method method, + std::function callback + ); #define SUBSCRIBE(ROUTER,CONTROLLER,FUNCTION,PATH,METHOD) ROUTER->subscribe([]()->void*{return new CONTROLLER;},(ylib::network::http::HTTP_CTR_FUNCTION)&CONTROLLER::FUNCTION,PATH,METHOD) - void subscribe( - std::function create_controller_callback, - network::http::HTTP_CTR_FUNCTION function, - std::string path, - network::http::method method - ); + void subscribe( + std::function create_controller_callback, + network::http::HTTP_CTR_FUNCTION function, + std::string path, + network::http::method method + ); - /****************************************************************** - * function:其它 - * desc:未订阅请求触发该回调 - * param - * callback : 触发回调 - ******************************************************************/ - void other(std::function callback); - /****************************************************************** - * function:数据接收后回调 - ******************************************************************/ - void on_recved(std::function callback); - /****************************************************************** - * function:数据发送前回调 - * desc:不支持大文件断点传输方式 - ******************************************************************/ - void on_sendbefore(std::function callback); - /****************************************************************** - * function:线程回调[禁止调用] - * desc:put提交后投递到线程池,线程池开始执行调用该回调。 - * param - * param : 线程参数 - ******************************************************************/ - void __thread_callback(reqpack* rq); - friend class http_server_lst; - friend class response; - /****************************************************************** - * function:队列数 - ******************************************************************/ - size_t queue_size(); - private: - // 添加任务 - void push(reqpack* rp); - // 是否为代理任务 - bool is_proxy(reqpack* rp); - // 是否为CDN服务 - bool is_cdn(reqpack* rp); - private: - // 订阅列表 - ylib::nolock_array m_subscribe; - // 线程池 - IHPThreadPool* m_threadpool; - // [回调] 未订阅请求 - std::function m_callback_other; - // [回调] 接收后 - std::function m_callback_recved; - // [回调] 发送前 - std::function m_callback_sendbefore; - // 拦截器 - network::http::interceptor* m_interceptor; - private: - ylib::queue m_handle_queue; - // 通过 ithread 继承 - virtual bool run() override; - }; + /****************************************************************** + * function:其它 + * desc:未订阅请求触发该回调 + * param + * callback : 触发回调 + ******************************************************************/ + void other(std::function callback); + /****************************************************************** + * function:数据接收后回调 + ******************************************************************/ + void on_recved(std::function callback); + /****************************************************************** + * function:数据发送前回调 + * desc:不支持大文件断点传输方式 + ******************************************************************/ + void on_sendbefore(std::function callback); + /****************************************************************** + * function:线程回调[禁止调用] + * desc:put提交后投递到线程池,线程池开始执行调用该回调。 + * param + * param : 线程参数 + ******************************************************************/ + void __thread_callback(reqpack* rq); + friend class http_server_lst; + friend class response; + /****************************************************************** + * function:队列数 + ******************************************************************/ + size_t queue_size(); + private: + // 添加任务 + void push(reqpack* rp); + // 是否为代理任务 + bool is_proxy(reqpack* rp); + // 是否为CDN服务 + bool is_cdn(reqpack* rp); + private: + // 订阅列表 + ylib::nolock_array m_subscribe; + // 线程池 + IHPThreadPool* m_threadpool; + // [回调] 未订阅请求 + std::function m_callback_other; + // [回调] 接收后 + std::function m_callback_recved; + // [回调] 发送前 + std::function m_callback_sendbefore; + // 拦截器 + network::http::interceptor* m_interceptor; + private: + ylib::queue m_handle_queue; + // 通过 ithread 继承 + virtual bool run() override; + }; + } + } } #endif diff --git a/include/ynet/http_server.h b/include/ynet/http_server.h index f0da03f..b918f98 100644 --- a/include/ynet/http_server.h +++ b/include/ynet/http_server.h @@ -8,71 +8,77 @@ #include #include "http_interface.h" #include "qps.hpp" -namespace ylib::network::http +namespace ylib { - class http_server_lst; - class host; - /************************************************************************* - * class:HTTP服务器 - *************************************************************************/ - class server:public ylib::error_base,public network::http::interface_ + namespace network { - public: - server(); - ~server(); - bool create(bool https, ushort port); - /****************************************************************** - * Function:启动 - * Param: - * ipaddress : 监听地址 - * port : 监听端口 - ******************************************************************/ - bool start(); - /****************************************************************** - * Function:关闭 - ******************************************************************/ - bool close(); - /****************************************************************** - * Function:取远程信息 - * Param: - * connid : 连接ID - * ipaddress : 远程地址 - * port : 远程端口 - ******************************************************************/ - bool remote(uint64 connid, std::string& ipaddress, ushort& port); + namespace http + { + class http_server_lst; + class host; + /************************************************************************* + * class:HTTP服务器 + *************************************************************************/ + class server :public ylib::error_base, public network::http::interface_ + { + public: + server(); + ~server(); + bool create(bool https, ushort port); + /****************************************************************** + * Function:启动 + * Param: + * ipaddress : 监听地址 + * port : 监听端口 + ******************************************************************/ + bool start(); + /****************************************************************** + * Function:关闭 + ******************************************************************/ + bool close(); + /****************************************************************** + * Function:取远程信息 + * Param: + * connid : 连接ID + * ipaddress : 远程地址 + * port : 远程端口 + ******************************************************************/ + bool remote(uint64 connid, std::string& ipaddress, ushort& port); - /****************************************************************** - * Function:HP服务指针 - ******************************************************************/ - void* hpserver(); - /****************************************************************** - * Function:每秒请求数 - ******************************************************************/ - network::qps* qps(); - inline const ushort port() { return m_port; } + /****************************************************************** + * Function:HP服务指针 + ******************************************************************/ + void* hpserver(); + /****************************************************************** + * Function:每秒请求数 + ******************************************************************/ + network::qps* qps(); + inline const ushort port() { return m_port; } #if USE_NET_HTTP_AGENT == 1 - inline network::http::agent* agent(){return m_agent;} + inline network::http::agent* agent() { return m_agent; } #endif - public: - bool m_init_ssl; - private: - friend class http_server_lst; - private: - // HP HttpServer 指针 - void* m_server; - // HP Listener 指针 - network::http::http_server_lst* m_listener; - // https - bool m_https; - // 端口 - ushort m_port; - // QPS - network::qps m_qps; + public: + bool m_init_ssl; + private: + friend class http_server_lst; + private: + // HP HttpServer 指针 + void* m_server; + // HP Listener 指针 + network::http::http_server_lst* m_listener; + // https + bool m_https; + // 端口 + ushort m_port; + // QPS + network::qps m_qps; #if USE_NET_HTTP_AGENT == 1 - // 代理指针 - network::http::agent *m_agent; + // 代理指针 + network::http::agent* m_agent; #endif - }; + }; + } + } } diff --git a/include/ynet/http_server_lst.h b/include/ynet/http_server_lst.h index 8929aa6..43a69fc 100644 --- a/include/ynet/http_server_lst.h +++ b/include/ynet/http_server_lst.h @@ -3,47 +3,52 @@ #if USE_NET_HTTP_WEBSITE #include "HPSocket/HPSocket.h" #include -namespace ylib::network::http +namespace ylib { - - class server; - class agent; - class http_server_lst : public IHttpServerListener + namespace network { - public: + namespace http + { + class server; + class agent; + class http_server_lst : public IHttpServerListener + { + public: - public: - http_server_lst(server* server); - ~http_server_lst(); - friend class server; - private: - EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen); - EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient); - EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID); - EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, int iLength); - virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; - EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); - EnHandleResult OnShutdown(ITcpServer* pSender); - EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode); + public: + http_server_lst(server* server); + ~http_server_lst(); + friend class server; + private: + EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen); + EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient); + EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID); + EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, int iLength); + virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; + EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); + EnHandleResult OnShutdown(ITcpServer* pSender); + EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode); - EnHttpParseResult OnMessageBegin(IHttpServer* pSender, CONNID dwConnID); - EnHttpParseResult OnRequestLine(IHttpServer* pSender, CONNID dwConnID, LPCSTR lpszMethod, LPCSTR lpszUrl); - EnHttpParseResult OnStatusLine(IHttpServer* pSender, CONNID dwConnID, USHORT usStatusCode, LPCSTR lpszDesc); - EnHttpParseResult OnHeader(IHttpServer* pSender, CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue); - EnHttpParseResult OnHeadersComplete(IHttpServer* pSender, CONNID dwConnID); - EnHttpParseResult OnChunkHeader(IHttpServer* pSender, CONNID dwConnID, int iLength); - EnHttpParseResult OnChunkComplete(IHttpServer* pSender, CONNID dwConnID); - EnHttpParseResult OnUpgrade(IHttpServer* pSender, CONNID dwConnID, EnHttpUpgradeType enUpgradeType); - EnHttpParseResult OnBody(IHttpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); - EnHttpParseResult OnMessageComplete(IHttpServer* pSender, CONNID dwConnID); - EnHttpParseResult OnParseError(IHttpServer* pSender, CONNID dwConnID, int iErrorCode, LPCSTR lpszErrorDesc); + EnHttpParseResult OnMessageBegin(IHttpServer* pSender, CONNID dwConnID); + EnHttpParseResult OnRequestLine(IHttpServer* pSender, CONNID dwConnID, LPCSTR lpszMethod, LPCSTR lpszUrl); + EnHttpParseResult OnStatusLine(IHttpServer* pSender, CONNID dwConnID, USHORT usStatusCode, LPCSTR lpszDesc); + EnHttpParseResult OnHeader(IHttpServer* pSender, CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue); + EnHttpParseResult OnHeadersComplete(IHttpServer* pSender, CONNID dwConnID); + EnHttpParseResult OnChunkHeader(IHttpServer* pSender, CONNID dwConnID, int iLength); + EnHttpParseResult OnChunkComplete(IHttpServer* pSender, CONNID dwConnID); + EnHttpParseResult OnUpgrade(IHttpServer* pSender, CONNID dwConnID, EnHttpUpgradeType enUpgradeType); + EnHttpParseResult OnBody(IHttpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); + EnHttpParseResult OnMessageComplete(IHttpServer* pSender, CONNID dwConnID); + EnHttpParseResult OnParseError(IHttpServer* pSender, CONNID dwConnID, int iErrorCode, LPCSTR lpszErrorDesc); - EnHandleResult OnWSMessageHeader(IHttpServer* pSender, CONNID dwConnID, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4], ULONGLONG ullBodyLen); - EnHandleResult OnWSMessageBody(IHttpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); - EnHandleResult OnWSMessageComplete(IHttpServer* pSender, CONNID dwConnID); - private: - // HPSERVER 指针 - network::http::server* m_server; - }; + EnHandleResult OnWSMessageHeader(IHttpServer* pSender, CONNID dwConnID, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE lpszMask[4], ULONGLONG ullBodyLen); + EnHandleResult OnWSMessageBody(IHttpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); + EnHandleResult OnWSMessageComplete(IHttpServer* pSender, CONNID dwConnID); + private: + // HPSERVER 指针 + network::http::server* m_server; + }; + } + } } #endif diff --git a/include/ynet/http_session.h b/include/ynet/http_session.h index 1027cc9..f04663d 100644 --- a/include/ynet/http_session.h +++ b/include/ynet/http_session.h @@ -7,61 +7,67 @@ #include "http_interface.h" #include "yutil/counter.hpp" #include "yutil/cache.h" -namespace ylib::network::http +namespace ylib { - class server; - struct session_config + namespace network { - session_config() + namespace http { - update_sec = 0; - timeout = 0; + class server; + struct session_config + { + session_config() + { + update_sec = 0; + timeout = 0; + } + // 更新时间 + uint64 update_sec; + // 过期时间 + uint32 timeout; + // 数据 + ylib::json data; + }; + class session_mgr; + class session :public ylib::json + { + public: + session(); + ~session(); + void init(const std::string& session_name); + void destory(); + std::string session_id(); + void update(); + friend class session_mgr; + friend class request; + private: + std::string m_session_name; + session_mgr* m_mgr; + }; + class session_mgr :public ylib::error_base, public network::http::interface_ + { + public: + session_mgr(); + ~session_mgr(); + bool start(const std::string& cache_path, uint32 timeout_sec); + void close(); + std::string create(int32 timeout_sec); + bool add(const std::string& session_name, int32 timeout_sec); + bool exist(const std::string& session_name); + void set(const std::string& session_name, http::session& session); + void del(const std::string& session_name); + bool get(const std::string& session_name, http::session& session); + void update(const std::string& session_name, int32 timeout_sec); + void clear(); + ylib::cache* cache(); + friend class session; + private: + ylib::counter m_index; + ylib::cache* m_cache; + uint32 m_timeout_sec; + }; } - // 更新时间 - uint64 update_sec; - // 过期时间 - uint32 timeout; - // 数据 - ylib::json data; - }; - class session_mgr; - class session:public ylib::json - { - public: - session(); - ~session(); - void init(const std::string& session_name); - void destory(); - std::string session_id(); - void update(); - friend class session_mgr; - friend class request; - private: - std::string m_session_name; - session_mgr* m_mgr; - }; - class session_mgr:public ylib::error_base, public network::http::interface_ - { - public: - session_mgr(); - ~session_mgr(); - bool start(const std::string& cache_path,uint32 timeout_sec); - void close(); - std::string create(int32 timeout_sec); - bool add(const std::string &session_name, int32 timeout_sec); - bool exist(const std::string &session_name); - void set(const std::string &session_name,http::session& session); - void del(const std::string &session_name); - bool get(const std::string &session_name,http::session& session); - void update(const std::string &session_name, int32 timeout_sec); - void clear(); - ylib::cache* cache(); - friend class session; - private: - ylib::counter m_index; - ylib::cache *m_cache; - uint32 m_timeout_sec; - }; + } } diff --git a/include/ynet/http_ssl.h b/include/ynet/http_ssl.h index f8050a5..23cc846 100644 --- a/include/ynet/http_ssl.h +++ b/include/ynet/http_ssl.h @@ -4,43 +4,49 @@ #include #include "http_interface.h" #include -namespace ylib::network::http +namespace ylib { - class server; - /****************************************************** - * class:域名器 - ******************************************************/ - class ssl :public ylib::error_base, public network::http::interface_ + namespace network { - public: - /********************************************************************************************** - * function:构造函数 - * param - * server : HTTP服务 - * verify_type : 验证方式 - * pem_cert : 证书内容 - * pem_key : 证书密钥 - * pem_ca : CA证书 - * password : 证书密码,没有留空 - **********************************************************************************************/ - ssl(network::http::server* server,ssl_verify_type verify_type,const std::string& pem_cert,const std::string&pem_key, const std::string& pem_ca = "", const std::string& password = ""); - ~ssl(); - /********************************************************************************************** - * function:注册证书 - **********************************************************************************************/ - bool regist(); - /********************************************************************************************** - * function:绑定 - **********************************************************************************************/ - bool bind(const std::string& host); - private: - ssl_verify_type m_verify_type; - std::string m_pem_cert; - std::string m_pem_key; - std::string m_pem_ca; - std::string m_pem_password; - network::http::server* m_server; - int32 m_index; - }; + namespace http + { + class server; + /****************************************************** + * class:域名器 + ******************************************************/ + class ssl :public ylib::error_base, public network::http::interface_ + { + public: + /********************************************************************************************** + * function:构造函数 + * param + * server : HTTP服务 + * verify_type : 验证方式 + * pem_cert : 证书内容 + * pem_key : 证书密钥 + * pem_ca : CA证书 + * password : 证书密码,没有留空 + **********************************************************************************************/ + ssl(network::http::server* server, ssl_verify_type verify_type, const std::string& pem_cert, const std::string& pem_key, const std::string& pem_ca = "", const std::string& password = ""); + ~ssl(); + /********************************************************************************************** + * function:注册证书 + **********************************************************************************************/ + bool regist(); + /********************************************************************************************** + * function:绑定 + **********************************************************************************************/ + bool bind(const std::string& host); + private: + ssl_verify_type m_verify_type; + std::string m_pem_cert; + std::string m_pem_key; + std::string m_pem_ca; + std::string m_pem_password; + network::http::server* m_server; + int32 m_index; + }; + } + } } #endif diff --git a/include/ynet/http_website.h b/include/ynet/http_website.h index d3db15a..13613d5 100644 --- a/include/ynet/http_website.h +++ b/include/ynet/http_website.h @@ -6,65 +6,71 @@ #include #include "http_interface.h" #include "yutil/array.hpp" -namespace ylib::network::http +namespace ylib { - class server; - class router; - class session_mgr; - class host; - class agent; - class cache; - class cdn; - class website :public ylib::error_base, public network::http::interface_ + namespace network { - public: + namespace http + { + class server; + class router; + class session_mgr; + class host; + class agent; + class cache; + class cdn; + class website :public ylib::error_base, public network::http::interface_ + { + public: - public: - website(); - ~website(); - /****************************************************************** - * function:启动 - * param - * config : 配置项 - * return: - * 失败可通过 last_error() 返回错误信息。 - ******************************************************************/ - bool start(const ylib::json& config); - /****************************************************************** - * function:关闭 - ******************************************************************/ - void close(); + public: + website(); + ~website(); + /****************************************************************** + * function:启动 + * param + * config : 配置项 + * return: + * 失败可通过 last_error() 返回错误信息。 + ******************************************************************/ + bool start(const ylib::json& config); + /****************************************************************** + * function:关闭 + ******************************************************************/ + void close(); - network::http::router* router(); - inline network::http::cache* cache(){return m_cache;} - inline network::http::cdn* cdn(){return m_cdn;} + network::http::router* router(); + inline network::http::cache* cache() { return m_cache; } + inline network::http::cdn* cdn() { return m_cdn; } - network::http::session_mgr* session(); - bool host(const std::string& host); - ylib::nolock_array* proxy(); - const network::http::website_info* info(); - inline const std::string& name(){return m_name;} -/* inline network::http::agent* agent() { return m_agent; }*/ - private: - // 文件缓存 - network::http::cache* m_cache; - // SESSION缓存 - network::http::session_mgr* m_session; - // router路由 服务 - network::http::router* m_router; - // CDN服务 - network::http::cdn* m_cdn; - // 配置信息 - struct website_info m_info; - // HOST - std::vector m_hosts; - // HTTPS - bool m_https; - // 反向代理 - ylib::nolock_array m_proxy; - // 名称 - std::string m_name; - }; + network::http::session_mgr* session(); + bool host(const std::string& host); + ylib::nolock_array* proxy(); + const network::http::website_info* info(); + inline const std::string& name() { return m_name; } + /* inline network::http::agent* agent() { return m_agent; }*/ + private: + // 文件缓存 + network::http::cache* m_cache; + // SESSION缓存 + network::http::session_mgr* m_session; + // router路由 服务 + network::http::router* m_router; + // CDN服务 + network::http::cdn* m_cdn; + // 配置信息 + struct website_info m_info; + // HOST + std::vector m_hosts; + // HTTPS + bool m_https; + // 反向代理 + ylib::nolock_array m_proxy; + // 名称 + std::string m_name; + }; + } + } } #endif diff --git a/include/ynet/make_form.h b/include/ynet/make_form.h index 955dc00..18dc4cb 100644 --- a/include/ynet/make_form.h +++ b/include/ynet/make_form.h @@ -3,19 +3,25 @@ #if USE_NET_HTTP_UTIL #include "yutil/vector.hpp" -namespace ylib::network::http +namespace ylib { - class make_form + namespace network { - public: - make_form(); - ~make_form(); - bool add(const std::string& name, const std::string& value); - bool add(const std::string& name, const std::string& filename, const std::string& content_type, const ylib::buffer& data); - bool make(ylib::buffer& data,std::string& boundary) const; - private: - ylib::vector m_list; - ylib::buffer m_data; - }; + namespace http + { + class make_form + { + public: + make_form(); + ~make_form(); + bool add(const std::string& name, const std::string& value); + bool add(const std::string& name, const std::string& filename, const std::string& content_type, const ylib::buffer& data); + bool make(ylib::buffer& data, std::string& boundary) const; + private: + ylib::vector m_list; + ylib::buffer m_data; + }; + } + } } #endif diff --git a/include/ynet/qps.hpp b/include/ynet/qps.hpp index c263ca3..a3dfcd8 100644 --- a/include/ynet/qps.hpp +++ b/include/ynet/qps.hpp @@ -3,57 +3,60 @@ #include "ybase/define.h" #include "yutil/system.h" -namespace ylib::network +namespace ylib { - class qps + namespace network { - public: - qps() + class qps { + public: + qps() + { - m_record = false; - } - void request(const uint64& size,bool no_count = false) - { - if(m_record == false) - return; - std::unique_lock guard(m_mutex); - if (no_count == false) - request_sec_count++; - request_sec_size += size; + m_record = false; + } + void request(const uint64& size, bool no_count = false) + { + if (m_record == false) + return; + std::unique_lock guard(m_mutex); + if (no_count == false) + request_sec_count++; + request_sec_size += size; - } - void response(const uint64& size, bool no_count = false) - { - if(m_record == false) - return; - std::unique_lock guard(m_mutex); - if (no_count == false) - response_sec_count++; - response_sec_size += size; - } - void record(uint32 wait_msec = 1000) - { - request_sec_count = 0; - request_sec_size = 0; - response_sec_count = 0; - response_sec_size = 0; - m_record = true; - system::sleep_msec(wait_msec); - m_record = false; - } - public: - // 每秒请求数 - uint64 request_sec_count; - // 每秒请求大小 - uint64 request_sec_size; - // 每秒回复数 - uint64 response_sec_count; - // 每秒回复大小 - uint64 response_sec_size; - private: - std::mutex m_mutex; - // record - bool m_record; - }; + } + void response(const uint64& size, bool no_count = false) + { + if (m_record == false) + return; + std::unique_lock guard(m_mutex); + if (no_count == false) + response_sec_count++; + response_sec_size += size; + } + void record(uint32 wait_msec = 1000) + { + request_sec_count = 0; + request_sec_size = 0; + response_sec_count = 0; + response_sec_size = 0; + m_record = true; + system::sleep_msec(wait_msec); + m_record = false; + } + public: + // 每秒请求数 + uint64 request_sec_count; + // 每秒请求大小 + uint64 request_sec_size; + // 每秒回复数 + uint64 response_sec_count; + // 每秒回复大小 + uint64 response_sec_size; + private: + std::mutex m_mutex; + // record + bool m_record; + }; + } } diff --git a/include/ynet/tcp2tcp.h b/include/ynet/tcp2tcp.h index 62dc75b..49d02df 100644 --- a/include/ynet/tcp2tcp.h +++ b/include/ynet/tcp2tcp.h @@ -5,23 +5,29 @@ #include "ybase/error.h" #include "yutil/map.hpp" -namespace ylib::network::tcp +namespace ylib { - class agent; - class server; - class tcp2tcp :public ylib::error_base + namespace network { - public: - tcp2tcp(); - ~tcp2tcp(); - bool start(const std::string& listen_address,ushort listen_port, bool agent_async_connect = false/*是否异步*/, ylib::receive_model agent_model = PUSH_DEFAULT, ylib::receive_model server_model = PUSH_DEFAULT); - void stop(); - network::tcp::server* server() { return m_server; } - network::tcp::agent* agent() { return m_agent; } - private: - // TCP服务 - network::tcp::server* m_server = nullptr; - // TCP代理 - network::tcp::agent* m_agent = nullptr; - }; + namespace tcp + { + class agent; + class server; + class tcp2tcp :public ylib::error_base + { + public: + tcp2tcp(); + ~tcp2tcp(); + bool start(const std::string& listen_address, ushort listen_port, bool agent_async_connect = false/*是否异步*/, ylib::receive_model agent_model = PUSH_DEFAULT, ylib::receive_model server_model = PUSH_DEFAULT); + void stop(); + network::tcp::server* server() { return m_server; } + network::tcp::agent* agent() { return m_agent; } + private: + // TCP服务 + network::tcp::server* m_server = nullptr; + // TCP代理 + network::tcp::agent* m_agent = nullptr; + }; + } + } } \ No newline at end of file diff --git a/include/ynet/tcp_agent.h b/include/ynet/tcp_agent.h index 3b92436..f24e9a6 100644 --- a/include/ynet/tcp_agent.h +++ b/include/ynet/tcp_agent.h @@ -6,54 +6,60 @@ #include "yutil/map.hpp" #include "ynet/http_define.h" class ITcpAgent; -namespace ylib::network::tcp +namespace ylib { - class agent_lst; - class agent:public ylib::error_base - { - public: - agent(); - ~agent(); - // 启动,是否采用异步 - bool start(bool async = false, ylib::receive_model model = ylib::PUSH_DEFAULT); - void stop(); - bool connect(const std::string& address,const ushort& port,uint64& connid,void* extra = nullptr); - bool disConnect(uint64 connid); - bool isConnect(uint64 connid); - bool send(uint64 connid,const char* data,uint32 len); - bool send(uint64 connid, const ylib::buffer& data); - void* extra_ptr(uint64 connid); - bool extra_ptr(uint64 connid,void* extra); - template - T* extra(uint64 connid) + namespace network + { + namespace tcp { - return (T*)extra_ptr(connid); - } - ITcpAgent* getHP() { - return m_agent; - } - void on_handshake(std::function callback) { m_on_handshake_callback = callback; } - void on_send(std::function callback) { m_on_send_callback = callback; } - void on_receive(std::function callback) { m_on_receive_callback = callback; } - void on_receive(std::function callback) { m_on_receive_notdata_callback = callback; } - void on_close(std::function callback) { m_on_close_callback = callback; } - void on_shutdown(std::function callback) { m_on_shutdown_callback = callback; } - void on_prepareconnect(std::function callback) { m_on_prepareconnect_callback = callback; } - void on_connect(std::function callback) { m_on_connect_callback = callback; } - friend class agent_lst; - public: - std::function m_on_receive_callback; - private: - std::function m_on_handshake_callback; - std::function m_on_send_callback; - std::function m_on_receive_notdata_callback; - std::function m_on_close_callback; - std::function m_on_shutdown_callback; - std::function m_on_prepareconnect_callback; - std::function m_on_connect_callback; + class agent_lst; + class agent :public ylib::error_base + { + public: + agent(); + ~agent(); + // 启动,是否采用异步 + bool start(bool async = false, ylib::receive_model model = ylib::PUSH_DEFAULT); + void stop(); + bool connect(const std::string& address, const ushort& port, uint64& connid, void* extra = nullptr); + bool disConnect(uint64 connid); + bool isConnect(uint64 connid); + bool send(uint64 connid, const char* data, uint32 len); + bool send(uint64 connid, const ylib::buffer& data); + void* extra_ptr(uint64 connid); + bool extra_ptr(uint64 connid, void* extra); + template + T* extra(uint64 connid) + { + return (T*)extra_ptr(connid); + } + ITcpAgent* getHP() { + return m_agent; + } + void on_handshake(std::function callback) { m_on_handshake_callback = callback; } + void on_send(std::function callback) { m_on_send_callback = callback; } + void on_receive(std::function callback) { m_on_receive_callback = callback; } + void on_receive(std::function callback) { m_on_receive_notdata_callback = callback; } + void on_close(std::function callback) { m_on_close_callback = callback; } + void on_shutdown(std::function callback) { m_on_shutdown_callback = callback; } + void on_prepareconnect(std::function callback) { m_on_prepareconnect_callback = callback; } + void on_connect(std::function callback) { m_on_connect_callback = callback; } + friend class agent_lst; + public: + std::function m_on_receive_callback; + private: + std::function m_on_handshake_callback; + std::function m_on_send_callback; + std::function m_on_receive_notdata_callback; + std::function m_on_close_callback; + std::function m_on_shutdown_callback; + std::function m_on_prepareconnect_callback; + std::function m_on_connect_callback; - ITcpAgent* m_agent = nullptr; - network::tcp::agent_lst *m_listener = nullptr; - ylib::receive_model m_model = ylib::PUSH_DEFAULT; - }; + ITcpAgent* m_agent = nullptr; + network::tcp::agent_lst* m_listener = nullptr; + ylib::receive_model m_model = ylib::PUSH_DEFAULT; + }; + } + } } \ No newline at end of file diff --git a/include/ynet/tcp_agent_lst.h b/include/ynet/tcp_agent_lst.h index deb3651..1656a27 100644 --- a/include/ynet/tcp_agent_lst.h +++ b/include/ynet/tcp_agent_lst.h @@ -3,24 +3,30 @@ #ifdef USE_NET_TCP_AGENT #include #include "HPSocket/HPSocket.h" -namespace ylib::network::tcp +namespace ylib { - class agent; - class agent_lst :public ITcpAgentListener + namespace network { - public: - agent_lst(ylib::network::tcp::agent *agent); - ~agent_lst(); - EnHandleResult OnHandShake(ITcpAgent* pSender, CONNID dwConnID) override; - EnHandleResult OnSend(ITcpAgent* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; - EnHandleResult OnReceive(ITcpAgent* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; - EnHandleResult OnReceive(ITcpAgent* pSender, CONNID dwConnID, int iLength) override; - EnHandleResult OnClose(ITcpAgent* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override; - EnHandleResult OnShutdown(ITcpAgent* pSender) override; - EnHandleResult OnPrepareConnect(ITcpAgent* pSender, CONNID dwConnID, SOCKET socket) override; - EnHandleResult OnConnect(ITcpAgent* pSender, CONNID dwConnID) override; - private: - ylib::network::tcp::agent* m_agent = nullptr; - }; + namespace tcp + { + class agent; + class agent_lst :public ITcpAgentListener + { + public: + agent_lst(ylib::network::tcp::agent* agent); + ~agent_lst(); + EnHandleResult OnHandShake(ITcpAgent* pSender, CONNID dwConnID) override; + EnHandleResult OnSend(ITcpAgent* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; + EnHandleResult OnReceive(ITcpAgent* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; + EnHandleResult OnReceive(ITcpAgent* pSender, CONNID dwConnID, int iLength) override; + EnHandleResult OnClose(ITcpAgent* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override; + EnHandleResult OnShutdown(ITcpAgent* pSender) override; + EnHandleResult OnPrepareConnect(ITcpAgent* pSender, CONNID dwConnID, SOCKET socket) override; + EnHandleResult OnConnect(ITcpAgent* pSender, CONNID dwConnID) override; + private: + ylib::network::tcp::agent* m_agent = nullptr; + }; + } + } } #endif diff --git a/include/ynet/tcp_client.h b/include/ynet/tcp_client.h index 62f05b0..0752e34 100644 --- a/include/ynet/tcp_client.h +++ b/include/ynet/tcp_client.h @@ -6,43 +6,49 @@ #include "ybase/buffer.h" #include "ybase/error.h" class ITcpClient; -namespace ylib::network::tcp +namespace ylib { - class client_lst; - class client:public ylib::error_base + namespace network { - public: - client(ylib::receive_model model = ylib::PUSH_DEFAULT); - ~client(); - // 通过 IClient 继承 - bool start(); - bool start(const ylib::AddressPort& remote_ap); - bool disconn(); - bool close(uint32 wait_msec = 0); - bool connect(const ylib::AddressPort& remote_ap, int32 wait_msec = -1); - bool send(const char* buff, uint32 len); - inline const ITcpClient* getHP(){return m_client;} - friend class client_lst; - public: - void on_recv(std::function callback){m_callback_onrecv = callback;} - void on_accept(std::function callback){m_callback_onaccept = callback;} - void on_close(std::function callback){m_callback_onclose = callback;} - void on_filter(std::function callback){m_callback_onfilter = callback;} - private: - std::function m_callback_onaccept; - std::function m_callback_onclose; - std::function m_callback_onrecv; - std::function m_callback_onfilter; + namespace tcp + { + class client_lst; + class client :public ylib::error_base + { + public: + client(ylib::receive_model model = ylib::PUSH_DEFAULT); + ~client(); + // 通过 IClient 继承 + bool start(); + bool start(const ylib::AddressPort& remote_ap); + bool disconn(); + bool close(uint32 wait_msec = 0); + bool connect(const ylib::AddressPort& remote_ap, int32 wait_msec = -1); + bool send(const char* buff, uint32 len); + inline const ITcpClient* getHP() { return m_client; } + friend class client_lst; + public: + void on_recv(std::function callback) { m_callback_onrecv = callback; } + void on_accept(std::function callback) { m_callback_onaccept = callback; } + void on_close(std::function callback) { m_callback_onclose = callback; } + void on_filter(std::function callback) { m_callback_onfilter = callback; } + private: + std::function m_callback_onaccept; + std::function m_callback_onclose; + std::function m_callback_onrecv; + std::function m_callback_onfilter; - ylib::AddressPort m_remote_ap; + ylib::AddressPort m_remote_ap; - // 是否PULL模型 - bool m_ispull = false; + // 是否PULL模型 + bool m_ispull = false; - ITcpClient *m_client = nullptr; - client_lst *m_client_listener = nullptr; + ITcpClient* m_client = nullptr; + client_lst* m_client_listener = nullptr; - ylib::receive_model m_model = ylib::PUSH_DEFAULT; - }; + ylib::receive_model m_model = ylib::PUSH_DEFAULT; + }; + } + } } #endif diff --git a/include/ynet/tcp_client_lst.h b/include/ynet/tcp_client_lst.h index 593bc74..91a41ef 100644 --- a/include/ynet/tcp_client_lst.h +++ b/include/ynet/tcp_client_lst.h @@ -3,28 +3,34 @@ #ifdef USE_NET_TCP_CLIENT #include #include "HPSocket/HPSocket.h" -namespace ylib::network::tcp +namespace ylib { - /*接收数据的线程池调用线程,用于再调用用户提供的指针,主要是考虑到 m_sportMode 模式才会使用到该函数*/ - void __HP_CALL callback_thread_recv_tcpclient(PVOID param); - class client; - class client_lst :public CTcpClientListener + namespace network { - public: - client_lst(client* client); - ~client_lst(); - friend class client; - private: - virtual EnHandleResult OnPrepareConnect(ITcpClient* pSender, CONNID dwConnID, SOCKET socket) override; - virtual EnHandleResult OnConnect(ITcpClient* pSender, CONNID dwConnID) override; - virtual EnHandleResult OnHandShake(ITcpClient* pSender, CONNID dwConnID) override; - virtual EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; - virtual EnHandleResult OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; - virtual EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override; - virtual EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, int iLength) override; - private: - int m_state; - ylib::network::tcp::client* m_client; - }; + namespace tcp + { + /*接收数据的线程池调用线程,用于再调用用户提供的指针,主要是考虑到 m_sportMode 模式才会使用到该函数*/ + void __HP_CALL callback_thread_recv_tcpclient(PVOID param); + class client; + class client_lst :public CTcpClientListener + { + public: + client_lst(client* client); + ~client_lst(); + friend class client; + private: + virtual EnHandleResult OnPrepareConnect(ITcpClient* pSender, CONNID dwConnID, SOCKET socket) override; + virtual EnHandleResult OnConnect(ITcpClient* pSender, CONNID dwConnID) override; + virtual EnHandleResult OnHandShake(ITcpClient* pSender, CONNID dwConnID) override; + virtual EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; + virtual EnHandleResult OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override; + virtual EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override; + virtual EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, int iLength) override; + private: + int m_state; + ylib::network::tcp::client* m_client; + }; + } + } } #endif diff --git a/include/ynet/tcp_forward.h b/include/ynet/tcp_forward.h index c5d8b46..0b7b69c 100644 --- a/include/ynet/tcp_forward.h +++ b/include/ynet/tcp_forward.h @@ -1,29 +1,35 @@ #pragma once #include "ynet/tcp2tcp.h" #include "yutil/map.hpp" -namespace ylib::network::tcp +namespace ylib { - class forward + namespace network { - public: - struct Packages : public std::vector + namespace tcp { + class forward + { + public: + struct Packages : public std::vector + { - }; - public: - forward(); - ~forward(); - bool start(const std::string& listen_address, ushort listen_port); - void stop(); - void setAP(const ylib::AddressPort& remote) { m_remote = remote; } - void setMaxConntionCount(uint32 max) { m_max_connection_count = max; } - uint64 connectionCount(); - void disIpAddress(const std::string& ipaddress); - private: - ylib::AddressPort m_remote; - network::tcp::tcp2tcp m_t2t; - uint32 m_max_connection_count = 0; - // 待发送 - ylib::map m_wait_send_data; - }; + }; + public: + forward(); + ~forward(); + bool start(const std::string& listen_address, ushort listen_port); + void stop(); + void setAP(const ylib::AddressPort& remote) { m_remote = remote; } + void setMaxConntionCount(uint32 max) { m_max_connection_count = max; } + uint64 connectionCount(); + void disIpAddress(const std::string& ipaddress); + private: + ylib::AddressPort m_remote; + network::tcp::tcp2tcp m_t2t; + uint32 m_max_connection_count = 0; + // 待发送 + ylib::map m_wait_send_data; + }; + } + } } \ No newline at end of file diff --git a/include/ynet/tcp_server.h b/include/ynet/tcp_server.h index cb14473..82b5998 100644 --- a/include/ynet/tcp_server.h +++ b/include/ynet/tcp_server.h @@ -4,72 +4,78 @@ #include "ybase/error.h" #ifdef USE_NET_TCP_SERVER class ITcpServer; -namespace ylib::network::tcp +namespace ylib { - class server_lst; - /**************************************************************** - * Class: TCP服务器 - * Desc:使用HPSocket开源库。 - ****************************************************************/ - class server :public ylib::error_base - { - public: - server(); - ~server(); - // 通过 IServer 继承 - bool start(const ylib::AddressPort& bind_ap, ylib::receive_model model = ylib::PUSH_DEFAULT); - void close(); - bool send(uint64 connid, const char* data, uint32 len); - bool send(uint64 connid,const ylib::buffer& data); - bool started(); - bool disConnect(uint64 connid); - bool isConnect(uint64 connid); - template - T* extra(uint64 connid) + namespace network + { + namespace tcp { - return (T*)extra_ptr(connid); - } - void* extra_ptr(uint64 connid); - bool extra_ptr(uint64 connid, void* extra); - inline ITcpServer* getHP() { return m_server; } - /**************************************************************** - * Fun:[事件]连接 + class server_lst; + /**************************************************************** + * Class: TCP服务器 + * Desc:使用HPSocket开源库。 ****************************************************************/ - void on_accept(std::function callback) { m_callback_accept = callback; } - /**************************************************************** - * Fun:[事件]关闭 - ****************************************************************/ - void on_close(std::function callback) { m_callback_close = callback; } - /**************************************************************** - * Fun:[事件]接收 - ****************************************************************/ - void on_recv(std::function callback) { m_callback_recv = callback; } - /**************************************************************** - * Fun:[事件]握手 - ****************************************************************/ - void on_handshake(std::function callback) { m_callback_handshake = callback; } - /**************************************************************** - * Fun:[事件]已发送 - ****************************************************************/ - void on_send(std::function callback) { m_callback_send = callback; } - /**************************************************************** - * Fun:[事件]过滤接收 - ****************************************************************/ - void on_recv(std::function callback) { m_callback_filter = callback; } - friend class server_lst; - public: - std::function m_callback_recv; - private: - std::function m_callback_accept; - std::function m_callback_close; - std::function m_callback_send; - std::function m_callback_handshake; - std::function m_callback_filter; - ylib::receive_model m_model = ylib::PUSH_DEFAULT; + class server :public ylib::error_base + { + public: + server(); + ~server(); + // 通过 IServer 继承 + bool start(const ylib::AddressPort& bind_ap, ylib::receive_model model = ylib::PUSH_DEFAULT); + void close(); + bool send(uint64 connid, const char* data, uint32 len); + bool send(uint64 connid, const ylib::buffer& data); + bool started(); + bool disConnect(uint64 connid); + bool isConnect(uint64 connid); + template + T* extra(uint64 connid) + { + return (T*)extra_ptr(connid); + } + void* extra_ptr(uint64 connid); + bool extra_ptr(uint64 connid, void* extra); + inline ITcpServer* getHP() { return m_server; } + /**************************************************************** + * Fun:[事件]连接 + ****************************************************************/ + void on_accept(std::function callback) { m_callback_accept = callback; } + /**************************************************************** + * Fun:[事件]关闭 + ****************************************************************/ + void on_close(std::function callback) { m_callback_close = callback; } + /**************************************************************** + * Fun:[事件]接收 + ****************************************************************/ + void on_recv(std::function callback) { m_callback_recv = callback; } + /**************************************************************** + * Fun:[事件]握手 + ****************************************************************/ + void on_handshake(std::function callback) { m_callback_handshake = callback; } + /**************************************************************** + * Fun:[事件]已发送 + ****************************************************************/ + void on_send(std::function callback) { m_callback_send = callback; } + /**************************************************************** + * Fun:[事件]过滤接收 + ****************************************************************/ + void on_recv(std::function callback) { m_callback_filter = callback; } + friend class server_lst; + public: + std::function m_callback_recv; + private: + std::function m_callback_accept; + std::function m_callback_close; + std::function m_callback_send; + std::function m_callback_handshake; + std::function m_callback_filter; + ylib::receive_model m_model = ylib::PUSH_DEFAULT; - ITcpServer* m_server = nullptr; - ylib::network::tcp::server_lst* m_server_listener = nullptr; - }; + ITcpServer* m_server = nullptr; + ylib::network::tcp::server_lst* m_server_listener = nullptr; + }; + } + } } #endif \ No newline at end of file diff --git a/include/ynet/tcp_server_lst.h b/include/ynet/tcp_server_lst.h index 5e30b87..15b111f 100644 --- a/include/ynet/tcp_server_lst.h +++ b/include/ynet/tcp_server_lst.h @@ -2,24 +2,30 @@ #include "ynet/define.h" #ifdef USE_NET_TCP_SERVER #include "HPSocket/HPSocket.h" -namespace ylib::network::tcp +namespace ylib { - class server; - class server_lst :public CTcpServerListener + namespace network { - public: - server_lst(ylib::network::tcp::server* server); - ~server_lst(); - virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen); - virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); - virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); - virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode); - virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient); - virtual EnHandleResult OnShutdown(ITcpServer* pSender); - virtual EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID); - virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, int iLength); - public: - ylib::network::tcp::server* m_server; - }; + namespace tcp + { + class server; + class server_lst :public CTcpServerListener + { + public: + server_lst(ylib::network::tcp::server* server); + ~server_lst(); + virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen); + virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); + virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength); + virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode); + virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient); + virtual EnHandleResult OnShutdown(ITcpServer* pSender); + virtual EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID); + virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, int iLength); + public: + ylib::network::tcp::server* m_server; + }; + } + } } #endif diff --git a/include/ynet/ttu_client.h b/include/ynet/ttu_client.h index e2395a1..0d24373 100644 --- a/include/ynet/ttu_client.h +++ b/include/ynet/ttu_client.h @@ -5,33 +5,36 @@ #include "yutil/map.hpp" namespace ylib::network::tcp { class server; } class UdpClientListener; -namespace ylib::network +namespace ylib { - class ttu_client + namespace network { - public: - struct Packages : public std::vector + class ttu_client { + public: + struct Packages : public std::vector + { + }; + public: + ttu_client(); + ~ttu_client(); + bool start(const ylib::AddressPort& listen); + void close(); + // 设置远程地址 + void setRemoteAP(const ylib::AddressPort& remote_ap) { m_remote_ap = remote_ap; } + void setMacConnectionCount(int count = -1) { m_max_connection_count = count; } + + size_t getConnectionCount(); + + friend class UdpClientListener; + public: + ylib::AddressPort m_remote_ap; + ylib::network::tcp::server* m_server = nullptr; + // 待发送 + ylib::map m_wait_send_data; + // 最大连接数 + int m_max_connection_count = -1; }; - public: - ttu_client(); - ~ttu_client(); - bool start(const ylib::AddressPort& listen); - void close(); - // 设置远程地址 - void setRemoteAP(const ylib::AddressPort& remote_ap) { m_remote_ap = remote_ap; } - void setMacConnectionCount(int count = -1) { m_max_connection_count = count; } - - size_t getConnectionCount(); - - friend class UdpClientListener; - public: - ylib::AddressPort m_remote_ap; - ylib::network::tcp::server* m_server = nullptr; - // 待发送 - ylib::map m_wait_send_data; - // 最大连接数 - int m_max_connection_count = -1; - }; + } } \ No newline at end of file diff --git a/include/ynet/udp_node.h b/include/ynet/udp_node.h index ef2983f..df80011 100644 --- a/include/ynet/udp_node.h +++ b/include/ynet/udp_node.h @@ -4,26 +4,32 @@ #include #include "ybase/error.h" class IUdpNode; -namespace ylib::network::udp +namespace ylib { - class node_lst; - class node :public ylib::error_base + namespace network { - public: - node(); - ~node(); - // 通过 IServer 继承 - bool start(const ylib::AddressPort& bind_ap); - bool close(); - bool send(const std::string& remote_ipaddress, ushort remote_port, const char* pData, uint32 len); - void on_recv(std::function callback) { m_callback_recv = callback; } - const ylib::AddressPort& local() { return m_local_ap; } - friend class node_lst; - private: - std::function m_callback_recv; - ylib::AddressPort m_local_ap; - IUdpNode* m_node = nullptr; - ylib::network::udp::node_lst* m_node_listener = nullptr; - }; + namespace udp + { + class node_lst; + class node :public ylib::error_base + { + public: + node(); + ~node(); + // 通过 IServer 继承 + bool start(const ylib::AddressPort& bind_ap); + bool close(); + bool send(const std::string& remote_ipaddress, ushort remote_port, const char* pData, uint32 len); + void on_recv(std::function callback) { m_callback_recv = callback; } + const ylib::AddressPort& local() { return m_local_ap; } + friend class node_lst; + private: + std::function m_callback_recv; + ylib::AddressPort m_local_ap; + IUdpNode* m_node = nullptr; + ylib::network::udp::node_lst* m_node_listener = nullptr; + }; + } + } } #endif \ No newline at end of file diff --git a/include/ynet/udp_node_lst.h b/include/ynet/udp_node_lst.h index 9babcd2..f9f840b 100644 --- a/include/ynet/udp_node_lst.h +++ b/include/ynet/udp_node_lst.h @@ -3,22 +3,28 @@ #if USE_NET_UDP_NODE #include "HPSocket/HPSocket.h" #include -namespace ylib::network::udp +namespace ylib { - class node; - class node_lst :public CUdpNodeListener + namespace network { - public: - node_lst(ylib::network::udp::node* node); - ~node_lst(); - private: - virtual EnHandleResult OnPrepareListen(IUdpNode* pSender, SOCKET soListen); - virtual EnHandleResult OnSend(IUdpNode* pSender, LPCTSTR lpszRemoteAddress, USHORT usRemotePort, const BYTE* pData, int iLength); - virtual EnHandleResult OnReceive(IUdpNode* pSender, LPCTSTR lpszRemoteAddress, USHORT usRemotePort, const BYTE* pData, int iLength); - virtual EnHandleResult OnError(IUdpNode* pSender, EnSocketOperation enOperation, int iErrorCode, LPCTSTR lpszRemoteAddress, USHORT usRemotePort, const BYTE* pBuffer, int iLength); - virtual EnHandleResult OnShutdown(IUdpNode* pSender); - private: - ylib::network::udp::node* m_node = nullptr; - }; + namespace udp + { + class node; + class node_lst :public CUdpNodeListener + { + public: + node_lst(ylib::network::udp::node* node); + ~node_lst(); + private: + virtual EnHandleResult OnPrepareListen(IUdpNode* pSender, SOCKET soListen); + virtual EnHandleResult OnSend(IUdpNode* pSender, LPCTSTR lpszRemoteAddress, USHORT usRemotePort, const BYTE* pData, int iLength); + virtual EnHandleResult OnReceive(IUdpNode* pSender, LPCTSTR lpszRemoteAddress, USHORT usRemotePort, const BYTE* pData, int iLength); + virtual EnHandleResult OnError(IUdpNode* pSender, EnSocketOperation enOperation, int iErrorCode, LPCTSTR lpszRemoteAddress, USHORT usRemotePort, const BYTE* pBuffer, int iLength); + virtual EnHandleResult OnShutdown(IUdpNode* pSender); + private: + ylib::network::udp::node* m_node = nullptr; + }; + } + } } #endif \ No newline at end of file diff --git a/include/ynet/util.h b/include/ynet/util.h index 063e79d..b6423bd 100644 --- a/include/ynet/util.h +++ b/include/ynet/util.h @@ -3,53 +3,56 @@ #if USE_NET_HTTP_UTIL #include #include -namespace ylib::network +namespace ylib { - //取文档类型 - void content_type(const std::string& extName, std::string& type); - //拆分URL - bool parse_url(const std::string& url, std::string& httpType, std::string& host, std::string& ipaddress, ushort& port, std::string& urlField); - //拆分URL - bool parse_url_host(const std::string& url, std::string& host); - //INT IP 转 字符串IP - void to_string(uint32 int_ip, std::string& ipaddress); - struct TcpConf + namespace network { - TcpConf() + //取文档类型 + void content_type(const std::string& extName, std::string& type); + //拆分URL + bool parse_url(const std::string& url, std::string& httpType, std::string& host, std::string& ipaddress, ushort& port, std::string& urlField); + //拆分URL + bool parse_url_host(const std::string& url, std::string& host); + //INT IP 转 字符串IP + void to_string(uint32 int_ip, std::string& ipaddress); + struct TcpConf { - local_port = 0; - remote_port = 0; - __local_ipaddress = 0; - __remote_ipaddress = 0; - } - void local_ipaddress(std::string& ipaddress) const - { - to_string(__local_ipaddress, ipaddress); - } - void remote_ipaddress(std::string& ipaddress) const - { - to_string(__remote_ipaddress, ipaddress); - } - uint32 __local_ipaddress; - uint32 __remote_ipaddress; - uint32 local_port; - uint32 remote_port; - }; + TcpConf() + { + local_port = 0; + remote_port = 0; + __local_ipaddress = 0; + __remote_ipaddress = 0; + } + void local_ipaddress(std::string& ipaddress) const + { + to_string(__local_ipaddress, ipaddress); + } + void remote_ipaddress(std::string& ipaddress) const + { + to_string(__remote_ipaddress, ipaddress); + } + uint32 __local_ipaddress; + uint32 __remote_ipaddress; + uint32 local_port; + uint32 remote_port; + }; - //URL转IP地址 - std::string to_ip(const std::string& url); - // 端口是否占用 - bool is_occupy(uint32 port); - /* - 取大小名称 - B/KB/MB/GB - */ - std::string size_name(double size, uint32 fixe = 0); - // 是否为IPV4 - bool is_ipv4(const std::string& value); - // 是否为IPV6 - bool is_ipv6(const std::string& value); - // 是否为域名 - bool is_domain(const std::string& value); + //URL转IP地址 + std::string to_ip(const std::string& url); + // 端口是否占用 + bool is_occupy(uint32 port); + /* + 取大小名称 + B/KB/MB/GB + */ + std::string size_name(double size, uint32 fixe = 0); + // 是否为IPV4 + bool is_ipv4(const std::string& value); + // 是否为IPV6 + bool is_ipv6(const std::string& value); + // 是否为域名 + bool is_domain(const std::string& value); + } } #endif diff --git a/include/ynet/utt_server.h b/include/ynet/utt_server.h index 50d86ad..842dfc3 100644 --- a/include/ynet/utt_server.h +++ b/include/ynet/utt_server.h @@ -5,24 +5,27 @@ class ITcpAgent; class TcpAgentListener; class UdpServerListener; -namespace ylib::network +namespace ylib { - class utt_server + namespace network { - public: - struct Packages : public std::vector {}; - public: - utt_server(); - ~utt_server(); - bool start(const ylib::AddressPort& bind_ap, const ylib::AddressPort& remote_ap); - void stop(); - public: - ylib::AddressPort m_remote_ap; - void* m_udp_server = nullptr; - UdpServerListener* m_udp_server_listener = nullptr; - ITcpAgent* m_agent = nullptr; - TcpAgentListener* m_agent_listener = nullptr; - // 待发送 - ylib::map m_wait_send_data; - }; + class utt_server + { + public: + struct Packages : public std::vector {}; + public: + utt_server(); + ~utt_server(); + bool start(const ylib::AddressPort& bind_ap, const ylib::AddressPort& remote_ap); + void stop(); + public: + ylib::AddressPort m_remote_ap; + void* m_udp_server = nullptr; + UdpServerListener* m_udp_server_listener = nullptr; + ITcpAgent* m_agent = nullptr; + TcpAgentListener* m_agent_listener = nullptr; + // 待发送 + ylib::map m_wait_send_data; + }; + } } \ No newline at end of file diff --git a/include/yutil/codec.h b/include/yutil/codec.h index b1f6fb0..a85d5c6 100644 --- a/include/yutil/codec.h +++ b/include/yutil/codec.h @@ -2,44 +2,48 @@ #include #include "ybase/buffer.h" -namespace ylib::codec +namespace ylib { - std::string md5(const ylib::buffer& data); - std::string to_utf8(const std::string& gbk); - std::string to_gbk(const std::string& utf8); - - - namespace gzip + namespace codec { - ylib::buffer en(const ylib::buffer& data); - ylib::buffer de(const ylib::buffer& data); - } + std::string md5(const ylib::buffer& data); + std::string to_utf8(const std::string& gbk); + std::string to_gbk(const std::string& utf8); - namespace base64 { - std::string en(const ylib::buffer& data); - ylib::buffer de(const std::string& data); - } - namespace url { - std::string en(const ylib::buffer& data); - ylib::buffer de(const std::string& data); - } + + namespace gzip + { + ylib::buffer en(const ylib::buffer& data); + ylib::buffer de(const ylib::buffer& data); + } + + namespace base64 { + std::string en(const ylib::buffer& data); + ylib::buffer de(const std::string& data); + } + namespace url { + std::string en(const ylib::buffer& data); + ylib::buffer de(const std::string& data); + } #if 1 - namespace des3 { - ylib::buffer en(const ylib::buffer& data, const std::string& key); - ylib::buffer de(const ylib::buffer& data, const std::string& key); - } + namespace des3 { + ylib::buffer en(const ylib::buffer& data, const std::string& key); + ylib::buffer de(const ylib::buffer& data, const std::string& key); + } #endif - namespace aes { - enum class variant { - AES128 = 16, - AES192 = 24, - AES256 = 32 - }; - enum class mode { - CBC, - ECB - }; - ylib::buffer en(const ylib::buffer& data, const std::string& key,ylib::codec::aes::variant variant,ylib::codec::aes::mode mode); - ylib::buffer de(const ylib::buffer& ciphertext, const std::string& key, variant var, mode mod); + namespace aes { + enum class variant { + AES128 = 16, + AES192 = 24, + AES256 = 32 + }; + enum class mode { + CBC, + ECB + }; + ylib::buffer en(const ylib::buffer& data, const std::string& key, ylib::codec::aes::variant variant, ylib::codec::aes::mode mode); + ylib::buffer de(const ylib::buffer& ciphertext, const std::string& key, variant var, mode mod); + } } + } diff --git a/include/yutil/coroution.h b/include/yutil/coroution.h index cea6075..a0a0a4b 100644 --- a/include/yutil/coroution.h +++ b/include/yutil/coroution.h @@ -1,4 +1,5 @@ #pragma once +#if 0 #include #include #include @@ -77,3 +78,4 @@ namespace ylib::co co_thread_pool* m_pool = nullptr; }; } +#endif \ No newline at end of file diff --git a/include/yutil/file.h b/include/yutil/file.h index fcd6693..e9fdb91 100644 --- a/include/yutil/file.h +++ b/include/yutil/file.h @@ -9,139 +9,140 @@ #include "ybase/error.h" #include "ybase/buffer.h" -namespace ylib::file +namespace ylib { - /** - * @brief 删除文件 - * @param filepath - * @return - */ - bool remove(const std::string& filepath); - /** - * @brief 删除目录 - * @param dirpath - * @param recycle - * @return - */ - bool remove_dir(const std::string& dirpath + namespace file + { + /** + * @brief 删除文件 + * @param filepath + * @return + */ + bool remove(const std::string& filepath); + /** + * @brief 删除目录 + * @param dirpath + * @param recycle + * @return + */ + bool remove_dir(const std::string& dirpath #if _WIN32 - ,bool recycle = false + , bool recycle = false #endif - ); - /** - * @brief 创建目录 - * @param dirpath - * @return - */ - bool create_dir(const std::string& dirpath,bool create_parent_dir = true); + ); + /** + * @brief 创建目录 + * @param dirpath + * @return + */ + bool create_dir(const std::string& dirpath, bool create_parent_dir = true); - /** - * @brief 读取文件 - * @param filepath - * @return - */ - ylib::buffer read(const std::string& filepath); - bool read(const std::string& filepath,ylib::buffer& data); - /** - * @brief 写到文件 - * @param filepath - * @param data - * @return - */ - bool write(const std::string& filepath,const ylib::buffer& data); - bool write(const std::string& filepath,const char* data,size_t len); + /** + * @brief 读取文件 + * @param filepath + * @return + */ + ylib::buffer read(const std::string& filepath); + bool read(const std::string& filepath, ylib::buffer& data); + /** + * @brief 写到文件 + * @param filepath + * @param data + * @return + */ + bool write(const std::string& filepath, const ylib::buffer& data); + bool write(const std::string& filepath, const char* data, size_t len); #ifdef _WIN32 - /** - * @brief 列出目录文件 - * @param rootPath - * @param list - * @return - */ - bool list(const std::string& rootPath, std::map& list); + /** + * @brief 列出目录文件 + * @param rootPath + * @param list + * @return + */ + bool list(const std::string& rootPath, std::map& list); #endif - /** - * @brief 取扩展名 - * @param path - * @return - */ - std::string ext(const std::string& path); - - /** - * @brief 是否存在文件 - * @param filepath - * @return - */ - bool exist(const std::string& filepath); - /** - * @brief 是否存在目录 - * @param filepath - * @return - */ - bool exist_dir(const std::string& dirpath); + /** + * @brief 取扩展名 + * @param path + * @return + */ + std::string ext(const std::string& path); - /** - * @brief 取文件大小 - * @param filepath - * @return - * 成功>=0 失败=-1 - */ - int64 size(const std::string& filepath); + /** + * @brief 是否存在文件 + * @param filepath + * @return + */ + bool exist(const std::string& filepath); + /** + * @brief 是否存在目录 + * @param filepath + * @return + */ + bool exist_dir(const std::string& dirpath); - /** - * @brief 取上级目录 - * @param path - * @return - */ - std::string parent_dir(const std::string& path); + /** + * @brief 取文件大小 + * @param filepath + * @return + * 成功>=0 失败=-1 + */ + int64 size(const std::string& filepath); - /** - * @brief 取文件名 - * @param path - * @param have_ext - * @return - */ - std::string filename(const std::string& path,bool have_ext = true); - /** - * @brief 复制文件 - */ - bool copy(const std::string& src, const std::string& dst); - /** - * @brief 复制目录 - */ - void copy_dir(const std::string& src, const std::string& dst); - // 遍历目录 - std::map traverse(const std::string& dirpath,const std::string& regex_pattern = "(.*\\.txt$)"); + /** + * @brief 取上级目录 + * @param path + * @return + */ + std::string parent_dir(const std::string& path); - // 生成一个临时文件路径 - std::string temp_filepath(); - - // 格式化目录 - std::string format_separator(const std::string& filepath); + /** + * @brief 取文件名 + * @param path + * @param have_ext + * @return + */ + std::string filename(const std::string& path, bool have_ext = true); + /** + * @brief 复制文件 + */ + bool copy(const std::string& src, const std::string& dst); + /** + * @brief 复制目录 + */ + void copy_dir(const std::string& src, const std::string& dst); + // 遍历目录 + std::map traverse(const std::string& dirpath, const std::string& regex_pattern = "(.*\\.txt$)"); -} -namespace ylib { - class file_io:public ylib::error_base - { - public: - file_io(); - ~file_io(); - bool open(const std::string& filepath,bool only_read = false,bool auto_create = true); - void close(); - bool appead(const char* data, int64 len); - bool appead(const ylib::buffer& data); - bool write(const char* data, int64 len); - bool write(const ylib::buffer& data); - ylib::buffer read(int64 size); - bool read(int64 size,ylib::buffer& data); - void jump(int64 offset,std::ios_base::seekdir way = std::ios::cur); - std::streampos cur(); - bool clear(); - std::streamsize size(); - bool is_open(); - inline const std::string& filepath(){return m_filepath;} - private: - std::string m_filepath; - std::fstream *m_stream = nullptr; - bool m_only_read = false; - }; + // 生成一个临时文件路径 + std::string temp_filepath(); + + // 格式化目录 + std::string format_separator(const std::string& filepath); + + } + class file_io :public ylib::error_base + { + public: + file_io(); + ~file_io(); + bool open(const std::string& filepath, bool only_read = false, bool auto_create = true); + void close(); + bool appead(const char* data, int64 len); + bool appead(const ylib::buffer& data); + bool write(const char* data, int64 len); + bool write(const ylib::buffer& data); + ylib::buffer read(int64 size); + bool read(int64 size, ylib::buffer& data); + void jump(int64 offset, std::ios_base::seekdir way = std::ios::cur); + std::streampos cur(); + bool clear(); + std::streamsize size(); + bool is_open(); + inline const std::string& filepath() { return m_filepath; } + private: + std::string m_filepath; + std::fstream* m_stream = nullptr; + bool m_only_read = false; + }; } diff --git a/include/yutil/img.h b/include/yutil/img.h index 98d034c..1f2590a 100644 --- a/include/yutil/img.h +++ b/include/yutil/img.h @@ -2,9 +2,12 @@ #include "ybase/define.h" #include "ybase/buffer.h" -namespace ylib::img +namespace ylib { - //生成验证码 - ylib::buffer make_code(uint32 width,uint32 height,uint32 code_length, std::string& captcha); + namespace img + { + //生成验证码 + ylib::buffer make_code(uint32 width,uint32 height,uint32 code_length, std::string& captcha); + } } diff --git a/include/yutil/mem.h b/include/yutil/mem.h index e29092e..bb2fc51 100644 --- a/include/yutil/mem.h +++ b/include/yutil/mem.h @@ -3,10 +3,14 @@ #include #include "ybase/define.h" -namespace ylib::mem +namespace ylib { - const static uint32 __big_size = 1024 * 1024 * 10; - void* malloc(size_t size); - void free(void* src); - void* realloc(void* src,size_t length); + namespace mem + { + const static uint32 __big_size = 1024 * 1024 * 10; + void* malloc(size_t size); + void free(void* src); + void* realloc(void* src, size_t length); + } + } diff --git a/include/yutil/process.h b/include/yutil/process.h index 5c980aa..5d8024e 100644 --- a/include/yutil/process.h +++ b/include/yutil/process.h @@ -4,36 +4,40 @@ #include #include #include "ybase/define.h" -namespace ylib::process +namespace ylib { -#ifdef _WIN32 - // 取进程路径 - std::string getpath(uint32 process_id); - /*进程信息*/ - struct proc_info + namespace process { - std::string path() +#ifdef _WIN32 + // 取进程路径 + std::string getpath(uint32 process_id); + /*进程信息*/ + struct proc_info { - if(__filepath.empty()) - return ylib::process::getpath(pid); - return __filepath; - } - std::string __filepath; - std::string name; - uint32 parent_pid = 0; - uint32 pid = 0; - }; - // 创建进程 - bool create(const std::string& filepath, const std::string& working_directory = "", const std::vector& args = {}, bool wait_close = false, bool show_window = true,size_t* pid = nullptr); - // 销毁进程 - bool destory(uint32 process_id); - // 系统进程列表 - std::list list(); - // 是否存在 - bool exist(const std::string& filepath); - // 检测多开 - bool already_running(const std::string& name); - // 设置为启动 - bool running(const std::string& name); + std::string path() + { + if (__filepath.empty()) + return ylib::process::getpath(pid); + return __filepath; + } + std::string __filepath; + std::string name; + uint32 parent_pid = 0; + uint32 pid = 0; + }; + // 创建进程 + bool create(const std::string& filepath, const std::string& working_directory = "", const std::vector& args = {}, bool wait_close = false, bool show_window = true, size_t* pid = nullptr); + // 销毁进程 + bool destory(uint32 process_id); + // 系统进程列表 + std::list list(); + // 是否存在 + bool exist(const std::string& filepath); + // 检测多开 + bool already_running(const std::string& name); + // 设置为启动 + bool running(const std::string& name); #endif + } + } diff --git a/include/yutil/system.h b/include/yutil/system.h index 16be4fd..3dfadb0 100644 --- a/include/yutil/system.h +++ b/include/yutil/system.h @@ -1,40 +1,44 @@ #pragma once #include #include "ybase/define.h" -namespace ylib::system +namespace ylib { - //睡眠 - void sleep_msec(uint32 msec); - //取随机数 - int64 random(int64 min, int64 max); - //取运行目录 - std::string current_dir(); - //取运行全路径 - std::string current_filepath(); - //取临时目录 - std::string temp_path(); - //桌面路径 - std::string desktop_path(); - //取当前用户目录 - std::string currentuser_path(); - //系统通知 - void notification(const std::string& title, const std::string& message); - //mac地址 - std::vector mac(); + namespace system + { + //睡眠 + void sleep_msec(uint32 msec); + //取随机数 + int64 random(int64 min, int64 max); + //取运行目录 + std::string current_dir(); + //取运行全路径 + std::string current_filepath(); + //取临时目录 + std::string temp_path(); + //桌面路径 + std::string desktop_path(); + //取当前用户目录 + std::string currentuser_path(); + //系统通知 + void notification(const std::string& title, const std::string& message); + //mac地址 + std::vector mac(); #ifdef _WIN32 - //打开浏览器 - void open_browser(const std::string& url); - //磁盘列表 - std::vector disk_list(); - //取磁盘信息 - ylib::DiskCapacity disk_capacity(char disk_name); - //取容量最大磁盘号 - char disk_max_capacity(); - //系统版本 - std::string version(); - //取系统最后错误描述 - std::string last_error(); + //打开浏览器 + void open_browser(const std::string& url); + //磁盘列表 + std::vector disk_list(); + //取磁盘信息 + ylib::DiskCapacity disk_capacity(char disk_name); + //取容量最大磁盘号 + char disk_max_capacity(); + //系统版本 + std::string version(); + //取系统最后错误描述 + std::string last_error(); #endif + } + } diff --git a/include/yutil/time.h b/include/yutil/time.h index a755a46..346e3a4 100644 --- a/include/yutil/time.h +++ b/include/yutil/time.h @@ -3,94 +3,98 @@ #include #include "ybase/define.h" -namespace ylib::time +namespace ylib { - struct datetime + namespace time { - datetime() { - year = 0; - month = 0; - day = 0; - hour = 0; - minute = 0; - second = 0; - } - short year; - char month; - char day; - char hour; - char minute; - char second; - }; + struct datetime + { + datetime() { + year = 0; + month = 0; + day = 0; + hour = 0; + minute = 0; + second = 0; + } + short year; + char month; + char day; + char hour; + char minute; + char second; + }; - //网络时间 - timestamp network_msec(); + //网络时间 + timestamp network_msec(); - //标准字符串时间转时间戳 - timestamp to_ts(const std::string& timestr, const std::string& formart = "%d-%d-%d %d:%d:%d"); - timestamp to_ts(struct ylib::time::datetime& datetime); - //当前时间戳 - uint32 now_sec(); - timestamp now_msec(); - timestamp now_usec(); - /* + //标准字符串时间转时间戳 + timestamp to_ts(const std::string& timestr, const std::string& formart = "%d-%d-%d %d:%d:%d"); + timestamp to_ts(struct ylib::time::datetime& datetime); + //当前时间戳 + uint32 now_sec(); + timestamp now_msec(); + timestamp now_usec(); + /* - %a 星期几的简写 - %A 星期几的全称 - %b 月分的简写 - %B 月份的全称 - %c 标准的日期的时间串 - %C 年份的后两位数字 - %d 十进制表示的每月的第几天 - %D 月/天/年 - %e 在两字符域中,十进制表示的每月的第几天 - %F 年-月-日 - %g 年份的后两位数字,使用基于周的年 - %G 年分,使用基于周的年 - %h 简写的月份名 - %H 24小时制的小时 - %I 12小时制的小时 - %j 十进制表示的每年的第几天 - %m 十进制表示的月份 - %M 十时制表示的分钟数 - %n 新行符 - %p 本地的AM或PM的等价显示 - %r 12小时的时间 - %R 显示小时和分钟:hh:mm - %S 十进制的秒数 - %t 水平制表符 - %T 显示时分秒:hh:mm:ss - %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0) - %U 第年的第几周,把星期日做为第一天(值从0到53) - %V 每年的第几周,使用基于周的年 - %w 十进制表示的星期几(值从0到6,星期天为0) - %W 每年的第几周,把星期一做为第一天(值从0到53) - %x 标准的日期串 - %X 标准的时间串 - %y 不带世纪的十进制年份(值从0到99) - %Y 带世纪部分的十进制年份 - %z,%Z 时区名称,如果不能得到时区名称则返回空字符。 - %% 百分号 - 取时间字符串*/ - std::string now_time(const std::string& format = "%Y-%m-%d %H:%M:%S"); + %a 星期几的简写 + %A 星期几的全称 + %b 月分的简写 + %B 月份的全称 + %c 标准的日期的时间串 + %C 年份的后两位数字 + %d 十进制表示的每月的第几天 + %D 月/天/年 + %e 在两字符域中,十进制表示的每月的第几天 + %F 年-月-日 + %g 年份的后两位数字,使用基于周的年 + %G 年分,使用基于周的年 + %h 简写的月份名 + %H 24小时制的小时 + %I 12小时制的小时 + %j 十进制表示的每年的第几天 + %m 十进制表示的月份 + %M 十时制表示的分钟数 + %n 新行符 + %p 本地的AM或PM的等价显示 + %r 12小时的时间 + %R 显示小时和分钟:hh:mm + %S 十进制的秒数 + %t 水平制表符 + %T 显示时分秒:hh:mm:ss + %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0) + %U 第年的第几周,把星期日做为第一天(值从0到53) + %V 每年的第几周,使用基于周的年 + %w 十进制表示的星期几(值从0到6,星期天为0) + %W 每年的第几周,把星期一做为第一天(值从0到53) + %x 标准的日期串 + %X 标准的时间串 + %y 不带世纪的十进制年份(值从0到99) + %Y 带世纪部分的十进制年份 + %z,%Z 时区名称,如果不能得到时区名称则返回空字符。 + %% 百分号 + 取时间字符串*/ + std::string now_time(const std::string& format = "%Y-%m-%d %H:%M:%S"); - std::string format(timestamp time,const std::string& format = "%Y-%m-%d %H:%M:%S"); + std::string format(timestamp time, const std::string& format = "%Y-%m-%d %H:%M:%S"); - void format(timestamp time, struct ylib::time::datetime& datetime); + void format(timestamp time, struct ylib::time::datetime& datetime); - ylib::time::datetime now_time2(); + ylib::time::datetime now_time2(); - //获取当前时区 - int32 now_zone(); - //获取当前GMT时间 - void now_gmt(std::string& gmt); - //时间戳转GMT时间 - void to_gmt(const timestamp ×tamp,std::string& gmt); + //获取当前时区 + int32 now_zone(); + //获取当前GMT时间 + void now_gmt(std::string& gmt); + //时间戳转GMT时间 + void to_gmt(const timestamp& timestamp, std::string& gmt); - //取当前凌晨时间 - timestamp now_zero_sec(); - std::string now_zero_time(); + //取当前凌晨时间 + timestamp now_zero_sec(); + std::string now_zero_time(); + } + } diff --git a/include/yutil/window.h b/include/yutil/window.h index 67edc62..7b4af78 100644 --- a/include/yutil/window.h +++ b/include/yutil/window.h @@ -4,55 +4,59 @@ #include "ybase/define.h" -namespace ylib::window{ - //取父句柄 - size_t parent(size_t handle); - //取窗口类名 - std::string class_name(size_t handle); - //取窗口标题 - std::string title_name(size_t handle); - //置窗口标题 - bool title_name(size_t handle,const std::string& name); - // 查找 - size_t find(const std::string& class_name, const std::string& title_name = ""); - std::vector find(size_t parent = 0, const std::string& class_name = "", const std::string& title_name = "",const std::string& filepath = ""); - // 取矩形 - ylib::Rect rect(size_t handle); - // 设置父句柄 - void set_parent(size_t handle,size_t parent); - // 取进程路径 - std::string path(size_t handle); - // 关闭 - void close(size_t handle); - // 内容 - void content(size_t handle,const std::string& value); - std::string content(size_t handle); - namespace menu +namespace ylib{ + namespace window { - bool click(size_t handle, size_t menu_child_handle, uint32 menu_item_idx); - size_t get(size_t handle); - size_t child(size_t menu_handle, uint32 menu_childs_idx); - } - namespace checkbox - { - bool checked(size_t handle); - bool checked(size_t handle, bool checked); - } - namespace tab - { - void current(size_t handle, uint32 idx); - } - namespace button - { - bool click(size_t handle); - bool click(size_t parent, const std::string& class_name, const std::string& title_name = ""); - } - namespace list - { - void insert(size_t handle, uint32 index, const std::string& value); - } + //取父句柄 + size_t parent(size_t handle); + //取窗口类名 + std::string class_name(size_t handle); + //取窗口标题 + std::string title_name(size_t handle); + //置窗口标题 + bool title_name(size_t handle, const std::string& name); + // 查找 + size_t find(const std::string& class_name, const std::string& title_name = ""); + std::vector find(size_t parent = 0, const std::string& class_name = "", const std::string& title_name = "", const std::string& filepath = ""); + // 取矩形 + ylib::Rect rect(size_t handle); + // 设置父句柄 + void set_parent(size_t handle, size_t parent); + // 取进程路径 + std::string path(size_t handle); + // 关闭 + void close(size_t handle); + // 内容 + void content(size_t handle, const std::string& value); + std::string content(size_t handle); + namespace menu + { + bool click(size_t handle, size_t menu_child_handle, uint32 menu_item_idx); + size_t get(size_t handle); + size_t child(size_t menu_handle, uint32 menu_childs_idx); + } + namespace checkbox + { + bool checked(size_t handle); + bool checked(size_t handle, bool checked); + } + namespace tab + { + void current(size_t handle, uint32 idx); + } + namespace button + { + bool click(size_t handle); + bool click(size_t parent, const std::string& class_name, const std::string& title_name = ""); + } + namespace list + { + void insert(size_t handle, uint32 index, const std::string& value); + } - void rect_sort(std::vector& list, uint32 type, bool ps = true); - bool enable(size_t win_handle, bool enable); + void rect_sort(std::vector& list, uint32 type, bool ps = true); + bool enable(size_t win_handle, bool enable); + } + } #endif \ No newline at end of file diff --git a/src/ymysql/src/mysql_plus.cpp b/src/ymysql/src/mysql_plus.cpp index e2b6312..e675350 100644 --- a/src/ymysql/src/mysql_plus.cpp +++ b/src/ymysql/src/mysql_plus.cpp @@ -278,7 +278,14 @@ void ylib::mysql::prepare_statement::set_datetime(uint32 index, const std::strin { CHECK_SQL_PPST; PRINT_DEBUG_SET_NSTRING; - PREPARE_STATEMENT->setDateTime(index,value.c_str()); + if (value.empty()) + { + PREPARE_STATEMENT->setNull(index, sql::DataType::DATE); + } + else + { + PREPARE_STATEMENT->setDateTime(index, value.c_str()); + } } void ylib::mysql::prepare_statement::set_double(uint32 index, double value) diff --git a/src/yutil/src/coroution.cpp b/src/yutil/src/coroution.cpp index e4b6397..c0c6a7d 100644 --- a/src/yutil/src/coroution.cpp +++ b/src/yutil/src/coroution.cpp @@ -1,3 +1,4 @@ +#if 0 #include "yutil/coroution.h" #include "HPSocket/HPSocket.h" #include "yutil/system.h" @@ -129,3 +130,4 @@ void ylib::co::scheduler::exec_queue() } } +#endif \ No newline at end of file