Skip to content

Commit

Permalink
增加文件时间检测
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfireitdiy committed Sep 21, 2019
1 parent f4a186f commit b59c7b7
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 210 deletions.
96 changes: 50 additions & 46 deletions network/sf_http_base_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
#pragma once
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#include <tools/sf_cache.h>
#include <functional>
#include <mutex>
#include <utility>
#include <vector>
#include "core/sf_eventloop.h"
#include "core/sf_timer.h"
#include "sf_http_request.h"
Expand All @@ -27,33 +22,37 @@
#include "sf_tcp_server.h"
#include "sf_websocket_utils.h"
#include "tools/sf_json.h"
#include <functional>
#include <mutex>
#include <tools/sf_cache.h>
#include <utility>
#include <vector>

namespace skyfire {

/**
* @brief HTTP服务器基础框架
*/
class sf_http_base_server {
private:
private:
const sf_http_server_config config__;

std::shared_ptr<sf_tcp_server> server__ =
std::make_shared<sf_tcp_server>(true);
std::shared_ptr<sf_tcp_server> server__ = std::make_shared<sf_tcp_server>(true);

sf_eventloop event_loop__;

std::function<void(const sf_http_request &, sf_http_response &)>
std::function<void(const sf_http_request&, sf_http_response&)>
request_callback__;

std::function<void(const sf_http_request &, sf_http_response &)>
std::function<void(const sf_http_request&, sf_http_response&)>
websocket_request_callback__;
std::function<void(SOCKET, const std::string &url, const byte_array &data)>
std::function<void(SOCKET, const std::string& url, const byte_array& data)>
websocket_binary_data_callback__;
std::function<void(SOCKET, const std::string &url, const std::string &data)>
std::function<void(SOCKET, const std::string& url, const std::string& data)>
websocket_text_data_callback__;
std::function<void(SOCKET, const std::string &url)>
std::function<void(SOCKET, const std::string& url)>
websocket_open_callback__;
std::function<void(SOCKET, const std::string &url)>
std::function<void(SOCKET, const std::string& url)>
websocket_close_callback__;

std::unordered_map<SOCKET, sf_request_context_t> request_context__;
Expand All @@ -71,66 +70,71 @@ class sf_http_base_server {
sf_json data;
};

struct cahce_data_t {
byte_array data;
std::chrono::system_clock::time_point modify_time;
};

mutable std::recursive_mutex mu_session__;
std::unordered_map<std::string, std::shared_ptr<session_data_t>>
session_data__;
sf_timer session_timer__;

void flush_session__();

void raw_data_coming__(SOCKET sock, const byte_array &data);
void raw_data_coming__(SOCKET sock, const byte_array& data);

template <typename T>
bool analysis_websocket_pkg__(SOCKET sock, const T *header,
int &resolve_pos, unsigned long long &len,
byte_array &body, bool &fin, int &op_code);
bool analysis_websocket_pkg__(SOCKET sock, const T* header,
int& resolve_pos, unsigned long long& len,
byte_array& body, bool& fin, int& op_code);

void websocket_data_coming__(SOCKET sock, const byte_array &data);
void websocket_data_coming__(SOCKET sock, const byte_array& data);

void build_new_request__(SOCKET sock);

void on_socket_closed__(SOCKET sock);

byte_array append_multipart_data__(
sf_multipart_data_context_t &multipart_data,
const byte_array &data) const;
sf_multipart_data_context_t& multipart_data,
const byte_array& data) const;

void file_response__(SOCKET sock, sf_http_response &res) const;
void file_response__(SOCKET sock, sf_http_response& res) const;

void normal_response__(SOCKET sock, sf_http_response &res) const;
void normal_response__(SOCKET sock, sf_http_response& res) const;

void multipart_response__(SOCKET sock, sf_http_response &res);
void multipart_response__(SOCKET sock, sf_http_response& res);

static bool check_analysis_multipart_file__(
std::vector<sf_http_response::multipart_info_t> &multipart_data);
std::vector<sf_http_response::multipart_info_t>& multipart_data);

void close_request__(SOCKET sock);

void http_handler__(SOCKET sock, const sf_http_request &http_request);
void http_handler__(SOCKET sock, const sf_http_request& http_request);

void build_boundary_context_data(SOCKET sock,
const sf_http_request &request);
const sf_http_request& request);

void build_websocket_context_data__(SOCKET sock,
const sf_http_request &request);
const sf_http_request& request);

void send_response_file_part__(
SOCKET sock, const sf_http_response::response_file_info_t &file,
std::ifstream &fi) const;
SOCKET sock, const sf_http_response::response_file_info_t& file,
std::ifstream& fi) const;

public:
public:
/**
* 获取session
* @param session_key session key
* @return session的json对象(注意是浅拷贝)
*/
sf_json session(const std::string &session_key);
sf_json session(const std::string& session_key);
/**
* @brief 保持session活动(赋予session生命值)
*
* @param session_key seesion key
*/
void keep_session_alive(const std::string &session_key);
void keep_session_alive(const std::string& session_key);

/**
* 设置session
Expand All @@ -140,22 +144,22 @@ class sf_http_base_server {
* @param value 值
*/
template <typename T>
void set_session(const std::string &session_key, const std::string &key,
const T &value);
void set_session(const std::string& session_key, const std::string& key,
const T& value);

/**
* 构造函数
* @param config http配置
*/
explicit sf_http_base_server(const sf_http_server_config &config);
explicit sf_http_base_server(const sf_http_server_config& config);

/**
* 设置请求回调函数
* @param request_callback 请求回调函数,函数接收参数为const
* sf_http_request&类型参数req和sf_http_response&参数res,处理者根据req来设置res的状态,框架会解析res的状态来返回至客户端
*/
void set_request_callback(
std::function<void(const sf_http_request &, sf_http_response &)>
std::function<void(const sf_http_request&, sf_http_response&)>
request_callback);

/**
Expand All @@ -164,7 +168,7 @@ class sf_http_base_server {
* sf_http_request&类型参数req和sf_http_response&参数res,处理者根据req来设置res的状态,框架会解析res的状态来返回至客户端(websocket握手在此函数中实现)
*/
void set_websocket_request_callback(
std::function<void(const sf_http_request &, sf_http_response &)>
std::function<void(const sf_http_request&, sf_http_response&)>
websocket_request_callback);

/**
Expand All @@ -173,15 +177,15 @@ class sf_http_base_server {
* websocket二进制数据回调函数,函数接收参数为SOCKET(websocket连接,可以在send_websocket_data中使用此标识发送数据)、url(WebSocket地址)、byte_array(接收到的二进制数据)
*/
void set_websocket_binary_data_callback(
std::function<void(SOCKET, const std::string &url, const byte_array &)>
std::function<void(SOCKET, const std::string& url, const byte_array&)>
websocket_binary_data_callback);
/**
* 设置websocket文本数据的响应
* @param websocket_text_data_callback
* websocket文本数据回调函数,函数接收参数为SOCKET(websocket连接,可以在send_websocket_data中使用此标识发送数据)、url(WebSocket地址)、string(接收到的文本数据)
*/
void set_websocket_text_data_callback(
std::function<void(SOCKET, const std::string &url, const std::string &)>
std::function<void(SOCKET, const std::string& url, const std::string&)>
websocket_text_data_callback);

/**
Expand All @@ -190,7 +194,7 @@ class sf_http_base_server {
* websocket打开回调函数,函数接收参数为SOCKET(websocket连接,可以在send_websocket_data中使用此标识发送数据)和url(WebSocket地址)
*/
void set_websocket_open_callback(
std::function<void(SOCKET, const std::string &url)>
std::function<void(SOCKET, const std::string& url)>
websocket_open_callback);

/**
Expand All @@ -199,7 +203,7 @@ class sf_http_base_server {
* websocket打开回调函数,函数接收参数为SOCKET(websocket连接)和url(WebSocket地址)
*/
void set_websocket_close_callback(
std::function<void(SOCKET, const std::string &url)>
std::function<void(SOCKET, const std::string& url)>
websocket_close_callback);

/**
Expand All @@ -222,7 +226,7 @@ class sf_http_base_server {
* @return 是否发送成功
*/
template <typename T>
bool send_websocket_data(SOCKET sock, const T &data);
bool send_websocket_data(SOCKET sock, const T& data);

/**
* 给所有的websocket发送消息(广播),注意:此函数没有返回值,无法判断是否发送成功
Expand All @@ -231,13 +235,13 @@ class sf_http_base_server {
* @param data 数据(二进制或者文本数据)
*/
template <typename T>
void send_websocket_data(const T &data);
void send_websocket_data(const T& data);

/**
* 关闭特定的websocket
* @param sock websocket标识
*/
void close_websocket(SOCKET sock);
};
} // namespace skyfire
#pragma clang diagnostic pop
} // namespace skyfire
#pragma clang diagnostic pop
Loading

0 comments on commit b59c7b7

Please sign in to comment.