NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
ftp_protocol.hpp
1#ifndef NEFORCE_NETWORK_FTP_FTP_PROTOCOL_HPP__
2#define NEFORCE_NETWORK_FTP_FTP_PROTOCOL_HPP__
5NEFORCE_BEGIN_NAMESPACE__
6
11struct ftp_exception final : network_exception {
12 explicit ftp_exception(const char* info = "FTP Operation Failed.") noexcept :
13 network_exception(info) {}
14
15 explicit ftp_exception(const exception& e) :
16 network_exception(e) {}
17
18 ~ftp_exception() override = default;
19
20 NEFORCE_NODISCARD const char* type() const noexcept override { return "ftp_exception"; }
21};
22
23class NEFORCE_API ftp_protocol : public ip_socket {
24public:
25 enum class transfer_mode {
26 ascii,
27 binary
28 };
29
30 enum class passive_mode {
31 active,
32 passive
33 };
34
35 enum class tls_mode {
36 none,
37 implicit_,
38 explicit_
39 };
40
41 struct response {
42 int code;
43 string message;
44
45 NEFORCE_NODISCARD bool is_success() const noexcept { return code >= 200 && code < 400; }
46 NEFORCE_NODISCARD bool is_positive_preliminary() const noexcept { return code >= 100 && code < 200; }
47 };
48
49 struct tls_info {
50 bool active = false;
51 string cipher_name;
52 string tls_version;
53 bool peer_verified = false;
54 bool data_channel = false;
55 };
56
57 static constexpr size_t kBufferSize = 8192;
58
59protected:
60 bool tls_active_ = false;
61 ssl_stream ctrl_ssl_;
62 ssl_context* ssl_ctx_ = nullptr;
63 bool data_tls_ = false;
64
65 char buffer_[kBufferSize]{};
66 size_t buffer_pos_ = 0;
67 size_t buffer_size_ = 0;
68
69 ftp_protocol() = default;
70
71 ftp_protocol(ftp_protocol&& other) noexcept;
72 ftp_protocol& operator=(ftp_protocol&& other) noexcept;
73
74 explicit ftp_protocol(native_handle_type fd) :
75 ip_socket(fd) {}
76
77public:
78 ~ftp_protocol() override = default;
79
80protected:
81 ssize_t ctrl_send(const char* data, size_t len);
82 ssize_t ctrl_recv(char* buf, size_t len);
83 bool ctrl_read_line(string& out);
84
85 response read_response();
86 void send_response(int code, const string& msg);
87 void send_response(const response& resp);
88
89 void clear_buffer() {
90 buffer_pos_ = 0;
91 buffer_size_ = 0;
92 }
93};
94
95NEFORCE_END_NAMESPACE__
96#endif // NEFORCE_NETWORK_FTP_FTP_PROTOCOL_HPP__
constexpr none_t none
默认空表示
int64_t ssize_t
有符号大小类型
IP协议族Socket基类
SSL/TLS流封装
const char * type() const noexcept override
获取异常类型