重新修改namespace降级,用于支持C++14
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::string> m_exts;
|
||||
std::vector<std::string> m_exts;
|
||||
|
||||
timestamp m_timeout_min;
|
||||
timestamp m_timeout_min;
|
||||
|
||||
ylib::map<std::string, std::string> m_keys;
|
||||
};
|
||||
ylib::map<std::string, std::string> m_keys;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<network::http::cnode*> 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<network::http::cnode*> 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;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ushort> listen_ports();
|
||||
//端口是否需要SSL
|
||||
bool port_have_ssl(ushort port);
|
||||
private:
|
||||
// HTTP 服务
|
||||
std::vector<network::http::server*> m_server;
|
||||
// 站点
|
||||
std::vector<network::http::website*> m_website;
|
||||
// 配置缓存
|
||||
ylib::map<std::string, std::string> 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<ushort> listen_ports();
|
||||
//端口是否需要SSL
|
||||
bool port_have_ssl(ushort port);
|
||||
private:
|
||||
// HTTP 服务
|
||||
std::vector<network::http::server*> m_server;
|
||||
// 站点
|
||||
std::vector<network::http::website*> m_website;
|
||||
// 配置缓存
|
||||
ylib::map<std::string,std::string> m_config;
|
||||
// JSON配置
|
||||
ylib::json m_json_config;
|
||||
public:
|
||||
uint64 m_temp[10];
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::string, std::string>& value = std::map<std::string, std::string>());
|
||||
bool get(const std::string& url,const std::map<std::string,std::string>& value = std::map<std::string,std::string>(),bool wait = true);
|
||||
bool post(const std::string& url,const std::map<std::string,std::string>& 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<std::string, std::string>& value = std::map<std::string, std::string>());
|
||||
bool get(const std::string& url, const std::map<std::string, std::string>& value = std::map<std::string, std::string>(), bool wait = true);
|
||||
bool post(const std::string& url, const std::map<std::string, std::string>& 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<bool(void* data, uint32 downsize, uint64 alldownsize,uint64 allsize, network::http::client_plus& client)>& callback);
|
||||
/*[回调] 下载结束*/
|
||||
void on_down_end(const std::function<void(network::http::client_plus& client)>& callback);
|
||||
/*[回调] 下载失败*/
|
||||
void on_down_failed(const std::function<void(network::http::client_plus& client)>& callback);
|
||||
/*[回调] 正在下载*/
|
||||
void on_down_ing(const std::function<bool(void* data, uint32 downsize, uint64 alldownsize, uint64 allsize, network::http::client_plus& client)>& callback);
|
||||
/*[回调] 下载结束*/
|
||||
void on_down_end(const std::function<void(network::http::client_plus& client)>& callback);
|
||||
/*[回调] 下载失败*/
|
||||
void on_down_failed(const std::function<void(network::http::client_plus& client)>& 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
|
||||
|
||||
@@ -51,48 +51,54 @@ using namespace ylib::network::http;
|
||||
#define qry_json_double(NAME) request()->parser()->json()[NAME].to<double>(true)
|
||||
#define qry_json_short(NAME) request()->parser()->json()[NAME].to<short>(true)
|
||||
#define qry_json_bool(NAME) request()->parser()->json()[NAME].to<bool>(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
|
||||
|
||||
@@ -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<std::string, std::string> request_headers;
|
||||
std::map<std::string, std::string> 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<std::string, std::string> request_headers;
|
||||
std::map<std::string, std::string> 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
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,47 +2,53 @@
|
||||
#include "define.h"
|
||||
#if USE_NET_HTTP_UTIL
|
||||
#include <map>
|
||||
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<std::string,std::string> 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<std::string, std::string> 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<std::string, std::string> m_param;
|
||||
std::vector<std::string> 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<std::string, std::string> to() const;
|
||||
size_t size();
|
||||
void clear();
|
||||
private:
|
||||
std::map<std::string, header> m_headers;
|
||||
};
|
||||
bool exist(const std::string& key);
|
||||
|
||||
std::string to_string() const;
|
||||
private:
|
||||
std::string m_name;
|
||||
std::map<std::string, std::string> m_param;
|
||||
std::vector<std::string> 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<std::string, std::string> to() const;
|
||||
size_t size();
|
||||
void clear();
|
||||
private:
|
||||
std::map<std::string, header> m_headers;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<bool(network::http::reqpack* rp)> 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<bool(network::http::reqpack* rp)> callback);
|
||||
void remove(size_t index);
|
||||
bool trigger(const std::string& url,network::http::reqpack* rp);
|
||||
private:
|
||||
ylib::nolock_array<interceptor_info*> m_array;
|
||||
};
|
||||
namespace http
|
||||
{
|
||||
class reqpack;
|
||||
|
||||
struct interceptor_info {
|
||||
std::regex express;
|
||||
std::string express_string;
|
||||
std::function<bool(network::http::reqpack* rp)> 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<bool(network::http::reqpack* rp)> callback);
|
||||
void remove(size_t index);
|
||||
bool trigger(const std::string& url, network::http::reqpack* rp);
|
||||
private:
|
||||
ylib::nolock_array<interceptor_info*> m_array;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,23 +3,29 @@
|
||||
#if USE_NET_HTTP_WEBSITE
|
||||
#include <functional>
|
||||
#include <map>
|
||||
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
|
||||
|
||||
@@ -2,117 +2,123 @@
|
||||
#include "http_define.h"
|
||||
#if USE_NET_HTTP_WEBSITE
|
||||
#include <map>
|
||||
namespace ylib::network::http
|
||||
namespace ylib
|
||||
{
|
||||
class parser;
|
||||
/*************************************************************************
|
||||
* class:FORM表单解析器
|
||||
*************************************************************************/
|
||||
class form_parser{
|
||||
public:
|
||||
form_parser();
|
||||
~form_parser();
|
||||
|
||||
|
||||
std::vector<std::string> 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<uint32>& starts,std::vector<uint32>& lengths);
|
||||
void parse_form(uint32 start,uint32 length);
|
||||
char getchar(size_t index);
|
||||
private:
|
||||
ylib::buffer* m_data;
|
||||
std::map<std::string,form_info> 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<std::string> 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<std::string> body_param_names();
|
||||
/******************************************************************
|
||||
* function:取BODY请求参数名列表
|
||||
******************************************************************/
|
||||
form_parser* form();
|
||||
private:
|
||||
/******************************************************************
|
||||
* function:解析URL
|
||||
******************************************************************/
|
||||
void parse_url(const std::string& url,std::map<std::string,std::string>& map);
|
||||
/******************************************************************
|
||||
* function:解析JSON
|
||||
******************************************************************/
|
||||
void parse_json();
|
||||
/******************************************************************
|
||||
* function:解析FORM表单
|
||||
******************************************************************/
|
||||
void parse_form();
|
||||
/******************************************************************
|
||||
* function:解析URL格式数据
|
||||
******************************************************************/
|
||||
bool read_url_param(const std::map<std::string,std::string>& map,const std::string& name,std::string& value);
|
||||
private:
|
||||
// URL参数
|
||||
std::map<std::string,std::string> m_url_param;
|
||||
// BODY参数
|
||||
std::map<std::string,std::string> 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<std::string> 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<uint32>& starts, std::vector<uint32>& lengths);
|
||||
void parse_form(uint32 start, uint32 length);
|
||||
char getchar(size_t index);
|
||||
private:
|
||||
ylib::buffer* m_data;
|
||||
std::map<std::string, form_info> 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<std::string> 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<std::string> body_param_names();
|
||||
/******************************************************************
|
||||
* function:取BODY请求参数名列表
|
||||
******************************************************************/
|
||||
form_parser* form();
|
||||
private:
|
||||
/******************************************************************
|
||||
* function:解析URL
|
||||
******************************************************************/
|
||||
void parse_url(const std::string& url, std::map<std::string, std::string>& map);
|
||||
/******************************************************************
|
||||
* function:解析JSON
|
||||
******************************************************************/
|
||||
void parse_json();
|
||||
/******************************************************************
|
||||
* function:解析FORM表单
|
||||
******************************************************************/
|
||||
void parse_form();
|
||||
/******************************************************************
|
||||
* function:解析URL格式数据
|
||||
******************************************************************/
|
||||
bool read_url_param(const std::map<std::string, std::string>& map, const std::string& name, std::string& value);
|
||||
private:
|
||||
// URL参数
|
||||
std::map<std::string, std::string> m_url_param;
|
||||
// BODY参数
|
||||
std::map<std::string, std::string> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::string,std::string>* 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<std::string,std::string> 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<std::string, std::string>* 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<std::string, std::string> m_headers;
|
||||
bool m_response;
|
||||
network::http::reqpack* m_reqpack;
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<void(network::http::request*,network::http::response*)> callback;
|
||||
// 控制器。为控制器则启动下面两个属性
|
||||
bool controller;
|
||||
// 创建控制器类指针
|
||||
std::function<void* ()> 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<void(network::http::request*, network::http::response*)> callback;
|
||||
// 控制器。为控制器则启动下面两个属性
|
||||
bool controller;
|
||||
// 创建控制器类指针
|
||||
std::function<void* ()> 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<void(network::http::request*,network::http::response*)> 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<void(network::http::request*, network::http::response*)> 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<void* ()> create_controller_callback,
|
||||
network::http::HTTP_CTR_FUNCTION function,
|
||||
std::string path,
|
||||
network::http::method method
|
||||
);
|
||||
void subscribe(
|
||||
std::function<void* ()> create_controller_callback,
|
||||
network::http::HTTP_CTR_FUNCTION function,
|
||||
std::string path,
|
||||
network::http::method method
|
||||
);
|
||||
|
||||
/******************************************************************
|
||||
* function:其它
|
||||
* desc:未订阅请求触发该回调
|
||||
* param
|
||||
* callback : 触发回调
|
||||
******************************************************************/
|
||||
void other(std::function<void(network::http::request*,network::http::response*)> callback);
|
||||
/******************************************************************
|
||||
* function:数据接收后回调
|
||||
******************************************************************/
|
||||
void on_recved(std::function<void(const ylib::buffer& begin, ylib::buffer* end)> callback);
|
||||
/******************************************************************
|
||||
* function:数据发送前回调
|
||||
* desc:不支持大文件断点传输方式
|
||||
******************************************************************/
|
||||
void on_sendbefore(std::function<void(const ylib::buffer& begin, ylib::buffer* end)> 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<network::http::subscribe_info*> m_subscribe;
|
||||
// 线程池
|
||||
IHPThreadPool* m_threadpool;
|
||||
// [回调] 未订阅请求
|
||||
std::function<void(network::http::request*, network::http::response*)> m_callback_other;
|
||||
// [回调] 接收后
|
||||
std::function<void(const ylib::buffer& begin, ylib::buffer* end)> m_callback_recved;
|
||||
// [回调] 发送前
|
||||
std::function<void(const ylib::buffer& begin,ylib::buffer*end)> m_callback_sendbefore;
|
||||
// 拦截器
|
||||
network::http::interceptor* m_interceptor;
|
||||
private:
|
||||
ylib::queue<network::http::reqpack*> m_handle_queue;
|
||||
// 通过 ithread 继承
|
||||
virtual bool run() override;
|
||||
};
|
||||
/******************************************************************
|
||||
* function:其它
|
||||
* desc:未订阅请求触发该回调
|
||||
* param
|
||||
* callback : 触发回调
|
||||
******************************************************************/
|
||||
void other(std::function<void(network::http::request*, network::http::response*)> callback);
|
||||
/******************************************************************
|
||||
* function:数据接收后回调
|
||||
******************************************************************/
|
||||
void on_recved(std::function<void(const ylib::buffer& begin, ylib::buffer* end)> callback);
|
||||
/******************************************************************
|
||||
* function:数据发送前回调
|
||||
* desc:不支持大文件断点传输方式
|
||||
******************************************************************/
|
||||
void on_sendbefore(std::function<void(const ylib::buffer& begin, ylib::buffer* end)> 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<network::http::subscribe_info*> m_subscribe;
|
||||
// 线程池
|
||||
IHPThreadPool* m_threadpool;
|
||||
// [回调] 未订阅请求
|
||||
std::function<void(network::http::request*, network::http::response*)> m_callback_other;
|
||||
// [回调] 接收后
|
||||
std::function<void(const ylib::buffer& begin, ylib::buffer* end)> m_callback_recved;
|
||||
// [回调] 发送前
|
||||
std::function<void(const ylib::buffer& begin, ylib::buffer* end)> m_callback_sendbefore;
|
||||
// 拦截器
|
||||
network::http::interceptor* m_interceptor;
|
||||
private:
|
||||
ylib::queue<network::http::reqpack*> m_handle_queue;
|
||||
// 通过 ithread 继承
|
||||
virtual bool run() override;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -8,71 +8,77 @@
|
||||
#include <map>
|
||||
#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
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,47 +3,52 @@
|
||||
#if USE_NET_HTTP_WEBSITE
|
||||
#include "HPSocket/HPSocket.h"
|
||||
#include <string>
|
||||
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
|
||||
|
||||
@@ -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<size_t> 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<size_t> m_index;
|
||||
ylib::cache *m_cache;
|
||||
uint32 m_timeout_sec;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,43 +4,49 @@
|
||||
#include <functional>
|
||||
#include "http_interface.h"
|
||||
#include <map>
|
||||
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
|
||||
|
||||
@@ -6,65 +6,71 @@
|
||||
#include <regex>
|
||||
#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<network::http::proxy*>* 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<network::http::host*> m_hosts;
|
||||
// HTTPS
|
||||
bool m_https;
|
||||
// 反向代理
|
||||
ylib::nolock_array<network::http::proxy*> m_proxy;
|
||||
// 名称
|
||||
std::string m_name;
|
||||
};
|
||||
network::http::session_mgr* session();
|
||||
bool host(const std::string& host);
|
||||
ylib::nolock_array<network::http::proxy*>* 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<network::http::host*> m_hosts;
|
||||
// HTTPS
|
||||
bool m_https;
|
||||
// 反向代理
|
||||
ylib::nolock_array<network::http::proxy*> m_proxy;
|
||||
// 名称
|
||||
std::string m_name;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<form_info*> 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<form_info*> m_list;
|
||||
ylib::buffer m_data;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<typename T>
|
||||
T* extra(uint64 connid)
|
||||
namespace network
|
||||
{
|
||||
namespace tcp
|
||||
{
|
||||
return (T*)extra_ptr(connid);
|
||||
}
|
||||
ITcpAgent* getHP() {
|
||||
return m_agent;
|
||||
}
|
||||
void on_handshake(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_handshake_callback = callback; }
|
||||
void on_send(std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> callback) { m_on_send_callback = callback; }
|
||||
void on_receive(std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> callback) { m_on_receive_callback = callback; }
|
||||
void on_receive(std::function<void(ylib::network::tcp::agent*, uint64, int)> callback) { m_on_receive_notdata_callback = callback; }
|
||||
void on_close(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_close_callback = callback; }
|
||||
void on_shutdown(std::function<void(ylib::network::tcp::agent*)> callback) { m_on_shutdown_callback = callback; }
|
||||
void on_prepareconnect(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_prepareconnect_callback = callback; }
|
||||
void on_connect(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_connect_callback = callback; }
|
||||
friend class agent_lst;
|
||||
public:
|
||||
std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> m_on_receive_callback;
|
||||
private:
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> m_on_handshake_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> m_on_send_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64, int)> m_on_receive_notdata_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> m_on_close_callback;
|
||||
std::function<void(ylib::network::tcp::agent*)> m_on_shutdown_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> m_on_prepareconnect_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> 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<typename T>
|
||||
T* extra(uint64 connid)
|
||||
{
|
||||
return (T*)extra_ptr(connid);
|
||||
}
|
||||
ITcpAgent* getHP() {
|
||||
return m_agent;
|
||||
}
|
||||
void on_handshake(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_handshake_callback = callback; }
|
||||
void on_send(std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> callback) { m_on_send_callback = callback; }
|
||||
void on_receive(std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> callback) { m_on_receive_callback = callback; }
|
||||
void on_receive(std::function<void(ylib::network::tcp::agent*, uint64, int)> callback) { m_on_receive_notdata_callback = callback; }
|
||||
void on_close(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_close_callback = callback; }
|
||||
void on_shutdown(std::function<void(ylib::network::tcp::agent*)> callback) { m_on_shutdown_callback = callback; }
|
||||
void on_prepareconnect(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_prepareconnect_callback = callback; }
|
||||
void on_connect(std::function<void(ylib::network::tcp::agent*, uint64)> callback) { m_on_connect_callback = callback; }
|
||||
friend class agent_lst;
|
||||
public:
|
||||
std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> m_on_receive_callback;
|
||||
private:
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> m_on_handshake_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64, const char*, int)> m_on_send_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64, int)> m_on_receive_notdata_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> m_on_close_callback;
|
||||
std::function<void(ylib::network::tcp::agent*)> m_on_shutdown_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> m_on_prepareconnect_callback;
|
||||
std::function<void(ylib::network::tcp::agent*, uint64)> 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;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,24 +3,30 @@
|
||||
#ifdef USE_NET_TCP_AGENT
|
||||
#include <string.h>
|
||||
#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
|
||||
|
||||
@@ -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<void(ylib::network::tcp::client*, const char*, uint32)> callback){m_callback_onrecv = callback;}
|
||||
void on_accept(std::function<void(ylib::network::tcp::client*)> callback){m_callback_onaccept = callback;}
|
||||
void on_close(std::function<void(ylib::network::tcp::client*)> callback){m_callback_onclose = callback;}
|
||||
void on_filter(std::function<void(ylib::network::tcp::client*, uint32)> callback){m_callback_onfilter = callback;}
|
||||
private:
|
||||
std::function<void(ylib::network::tcp::client*)> m_callback_onaccept;
|
||||
std::function<void(ylib::network::tcp::client*)> m_callback_onclose;
|
||||
std::function<void(ylib::network::tcp::client*, const char*, uint32)> m_callback_onrecv;
|
||||
std::function<void(ylib::network::tcp::client*, uint32)> 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<void(ylib::network::tcp::client*, const char*, uint32)> callback) { m_callback_onrecv = callback; }
|
||||
void on_accept(std::function<void(ylib::network::tcp::client*)> callback) { m_callback_onaccept = callback; }
|
||||
void on_close(std::function<void(ylib::network::tcp::client*)> callback) { m_callback_onclose = callback; }
|
||||
void on_filter(std::function<void(ylib::network::tcp::client*, uint32)> callback) { m_callback_onfilter = callback; }
|
||||
private:
|
||||
std::function<void(ylib::network::tcp::client*)> m_callback_onaccept;
|
||||
std::function<void(ylib::network::tcp::client*)> m_callback_onclose;
|
||||
std::function<void(ylib::network::tcp::client*, const char*, uint32)> m_callback_onrecv;
|
||||
std::function<void(ylib::network::tcp::client*, uint32)> 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
|
||||
|
||||
@@ -3,28 +3,34 @@
|
||||
#ifdef USE_NET_TCP_CLIENT
|
||||
#include <string.h>
|
||||
#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
|
||||
|
||||
@@ -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<ylib::buffer>
|
||||
namespace tcp
|
||||
{
|
||||
class forward
|
||||
{
|
||||
public:
|
||||
struct Packages : public std::vector<ylib::buffer>
|
||||
{
|
||||
|
||||
};
|
||||
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<uint64, Packages*> 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<uint64, Packages*> m_wait_send_data;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<typename T>
|
||||
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<void(ylib::network::tcp::server* server, uint64 connid)> callback) { m_callback_accept = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]关闭
|
||||
****************************************************************/
|
||||
void on_close(std::function<void(ylib::network::tcp::server* server, uint64 connid)> callback) { m_callback_close = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]接收
|
||||
****************************************************************/
|
||||
void on_recv(std::function<void(ylib::network::tcp::server* server, uint64 connid, const char* data, uint32 len)> callback) { m_callback_recv = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]握手
|
||||
****************************************************************/
|
||||
void on_handshake(std::function<void(ylib::network::tcp::server* server, uint64 connid)> callback) { m_callback_handshake = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]已发送
|
||||
****************************************************************/
|
||||
void on_send(std::function<void(ylib::network::tcp::server* server, uint64 connid, const char* data, uint32 len)> callback) { m_callback_send = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]过滤接收
|
||||
****************************************************************/
|
||||
void on_recv(std::function<void(ylib::network::tcp::server* server, uint64 connid,uint32 len)> callback) { m_callback_filter = callback; }
|
||||
friend class server_lst;
|
||||
public:
|
||||
std::function<void(ylib::network::tcp::server*, uint64, const char*, uint32)> m_callback_recv;
|
||||
private:
|
||||
std::function<void(ylib::network::tcp::server*, uint64)> m_callback_accept;
|
||||
std::function<void(ylib::network::tcp::server*, uint64)> m_callback_close;
|
||||
std::function<void(ylib::network::tcp::server*, uint64, const char*, uint32)> m_callback_send;
|
||||
std::function<void(ylib::network::tcp::server*, uint64)> m_callback_handshake;
|
||||
std::function<void(ylib::network::tcp::server*, uint64, uint32)> 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<typename T>
|
||||
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<void(ylib::network::tcp::server* server, uint64 connid)> callback) { m_callback_accept = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]关闭
|
||||
****************************************************************/
|
||||
void on_close(std::function<void(ylib::network::tcp::server* server, uint64 connid)> callback) { m_callback_close = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]接收
|
||||
****************************************************************/
|
||||
void on_recv(std::function<void(ylib::network::tcp::server* server, uint64 connid, const char* data, uint32 len)> callback) { m_callback_recv = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]握手
|
||||
****************************************************************/
|
||||
void on_handshake(std::function<void(ylib::network::tcp::server* server, uint64 connid)> callback) { m_callback_handshake = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]已发送
|
||||
****************************************************************/
|
||||
void on_send(std::function<void(ylib::network::tcp::server* server, uint64 connid, const char* data, uint32 len)> callback) { m_callback_send = callback; }
|
||||
/****************************************************************
|
||||
* Fun:[事件]过滤接收
|
||||
****************************************************************/
|
||||
void on_recv(std::function<void(ylib::network::tcp::server* server, uint64 connid, uint32 len)> callback) { m_callback_filter = callback; }
|
||||
friend class server_lst;
|
||||
public:
|
||||
std::function<void(ylib::network::tcp::server*, uint64, const char*, uint32)> m_callback_recv;
|
||||
private:
|
||||
std::function<void(ylib::network::tcp::server*, uint64)> m_callback_accept;
|
||||
std::function<void(ylib::network::tcp::server*, uint64)> m_callback_close;
|
||||
std::function<void(ylib::network::tcp::server*, uint64, const char*, uint32)> m_callback_send;
|
||||
std::function<void(ylib::network::tcp::server*, uint64)> m_callback_handshake;
|
||||
std::function<void(ylib::network::tcp::server*, uint64, uint32)> 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
|
||||
@@ -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
|
||||
|
||||
@@ -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<ylib::buffer>
|
||||
class ttu_client
|
||||
{
|
||||
public:
|
||||
struct Packages : public std::vector<ylib::buffer>
|
||||
{
|
||||
|
||||
};
|
||||
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<uint64, Packages*> 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<uint64, Packages*> m_wait_send_data;
|
||||
// 最大连接数
|
||||
int m_max_connection_count = -1;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -4,26 +4,32 @@
|
||||
#include <functional>
|
||||
#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<void(ylib::network::udp::node* node,const ylib::AddressPort& remote_ap, const char* data, uint32 len)> callback) { m_callback_recv = callback; }
|
||||
const ylib::AddressPort& local() { return m_local_ap; }
|
||||
friend class node_lst;
|
||||
private:
|
||||
std::function<void(ylib::network::udp::node*, const ylib::AddressPort&, const char*, uint32)> 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<void(ylib::network::udp::node* node, const ylib::AddressPort& remote_ap, const char* data, uint32 len)> callback) { m_callback_recv = callback; }
|
||||
const ylib::AddressPort& local() { return m_local_ap; }
|
||||
friend class node_lst;
|
||||
private:
|
||||
std::function<void(ylib::network::udp::node*, const ylib::AddressPort&, const char*, uint32)> m_callback_recv;
|
||||
ylib::AddressPort m_local_ap;
|
||||
IUdpNode* m_node = nullptr;
|
||||
ylib::network::udp::node_lst* m_node_listener = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -3,22 +3,28 @@
|
||||
#if USE_NET_UDP_NODE
|
||||
#include "HPSocket/HPSocket.h"
|
||||
#include <string>
|
||||
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
|
||||
@@ -3,53 +3,56 @@
|
||||
#if USE_NET_HTTP_UTIL
|
||||
#include <string>
|
||||
#include <list>
|
||||
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
|
||||
|
||||
@@ -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<ylib::buffer> {};
|
||||
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<uint64/*TcpAgentConnID*/, Packages*> m_wait_send_data;
|
||||
};
|
||||
class utt_server
|
||||
{
|
||||
public:
|
||||
struct Packages : public std::vector<ylib::buffer> {};
|
||||
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<uint64/*TcpAgentConnID*/, Packages*> m_wait_send_data;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,44 +2,48 @@
|
||||
|
||||
#include <string>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#if 0
|
||||
#include <coroutine>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
@@ -77,3 +78,4 @@ namespace ylib::co
|
||||
co_thread_pool* m_pool = nullptr;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -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<std::string,bool[文件夹]>
|
||||
* @return
|
||||
*/
|
||||
bool list(const std::string& rootPath, std::map<std::string, bool>& list);
|
||||
/**
|
||||
* @brief 列出目录文件
|
||||
* @param rootPath
|
||||
* @param list<std::string,bool[文件夹]>
|
||||
* @return
|
||||
*/
|
||||
bool list(const std::string& rootPath, std::map<std::string, bool>& 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<std::string, ylib::FileType> 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<std::string, ylib::FileType> 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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,14 @@
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,36 +4,40 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#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<std::string>& args = {}, bool wait_close = false, bool show_window = true,size_t* pid = nullptr);
|
||||
// 销毁进程
|
||||
bool destory(uint32 process_id);
|
||||
// 系统进程列表
|
||||
std::list<ylib::process::proc_info> 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<std::string>& args = {}, bool wait_close = false, bool show_window = true, size_t* pid = nullptr);
|
||||
// 销毁进程
|
||||
bool destory(uint32 process_id);
|
||||
// 系统进程列表
|
||||
std::list<ylib::process::proc_info> list();
|
||||
// 是否存在
|
||||
bool exist(const std::string& filepath);
|
||||
// 检测多开
|
||||
bool already_running(const std::string& name);
|
||||
// 设置为启动
|
||||
bool running(const std::string& name);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,40 +1,44 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#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<std::string> 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<std::string> mac();
|
||||
#ifdef _WIN32
|
||||
//打开浏览器
|
||||
void open_browser(const std::string& url);
|
||||
//磁盘列表
|
||||
std::vector<char> 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<char> disk_list();
|
||||
//取磁盘信息
|
||||
ylib::DiskCapacity disk_capacity(char disk_name);
|
||||
//取容量最大磁盘号
|
||||
char disk_max_capacity();
|
||||
//系统版本
|
||||
std::string version();
|
||||
//取系统最后错误描述
|
||||
std::string last_error();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,94 +3,98 @@
|
||||
|
||||
#include <map>
|
||||
#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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<size_t> 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<size_t> 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<size_t>& list, uint32 type, bool ps = true);
|
||||
bool enable(size_t win_handle, bool enable);
|
||||
void rect_sort(std::vector<size_t>& list, uint32 type, bool ps = true);
|
||||
bool enable(size_t win_handle, bool enable);
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user