Skip to content

Commit 61a426a

Browse files
committed
feat: add local test directory support, enhance error logging with fd info, update SSL context to accept mode parameter, and implement thread-safe SSL socket operations
1 parent 2a0e289 commit 61a426a

File tree

8 files changed

+64
-12
lines changed

8 files changed

+64
-12
lines changed

demo/web-server/build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ mv ./build/webserver ../bin
1414

1515
cp ../bin/webserver ../pack/web-server
1616
cp ./config.json ../pack/web-server
17+
18+
# this is for author self local test dir
19+
SOURCE_FILE=../bin/webserver
20+
TARGET_DIR=~/file/data/web/chenxuanweb
21+
if [ -d "$TARGET_DIR" ]; then
22+
cp "$SOURCE_FILE" "$TARGET_DIR/"
23+
echo "copy $SOURCE_FILE to $TARGET_DIR success"
24+
fi

src/cppnet/http/server/http_server.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ void HttpServer::HandleRead(TcpServer &server, Socket &event_soc) {
331331
if (soc->err_no() == EAGAIN || soc->err_no() == EWOULDBLOCK) {
332332
logger_->Error("[soc.ReadUntil]: read timeout");
333333
} else {
334-
logger_->Error("[soc.ReadUntil]:" + soc->err_msg());
334+
logger_->Error("[soc.ReadUntil]:" + soc->err_msg() +
335+
" soc:" + std::to_string(event_soc.fd()));
335336
}
336337
server.RemoveSoc(event_soc);
337338
soc->Close();

src/cppnet/server/io_multiplexing/epoll.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace cppnet {
99

10-
Epoll::TriggerType Epoll::trigger_type_ = Epoll::TriggerType::kLevelTrigger;
11-
1210
int Epoll::Init() {
1311
epoll_fd_ = CreateEpoll();
1412
if (epoll_fd_.status() != Socket::kInit) {

src/cppnet/server/io_multiplexing/epoll.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class Epoll : public IOMultiplexingBase {
1919

2020
private:
2121
Socket epoll_fd_;
22-
23-
private:
24-
static TriggerType trigger_type_;
2522
};
2623

2724
} // namespace cppnet

src/cppnet/ssl/ssl_context.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ std::shared_ptr<SSLSocket> SSLContext::CreateSSLSocket() {
180180
return CreateSSLSocket(soc);
181181
}
182182

183-
std::shared_ptr<SSLSocket> SSLContext::AcceptSSL(const Socket &soc) {
183+
std::shared_ptr<SSLSocket> SSLContext::AcceptSSL(const Socket &soc,
184+
SSLSocket::Mode soc_mode) {
184185
auto new_ssl = SSL_new(ssl_ctx_);
185186
if (new_ssl == nullptr) {
186187
err_msg_ = "[syserr]:create ssl failed";
@@ -190,12 +191,12 @@ std::shared_ptr<SSLSocket> SSLContext::AcceptSSL(const Socket &soc) {
190191
auto rc = SSL_accept(new_ssl);
191192
if (rc < 0) {
192193
err_msg_ = std::string("[syserr]:ssl accept failed ") +
193-
ERR_error_string(ERR_get_error(), nullptr);
194+
ERR_error_string(ERR_get_error(), nullptr) + std::to_string(rc);
194195
SSL_free(new_ssl);
195196
ERR_clear_error();
196197
return nullptr;
197198
}
198-
return std::make_shared<SSLSocket>(new_ssl, soc);
199+
return std::make_shared<SSLSocket>(new_ssl, soc, soc_mode);
199200
}
200201

201202
int SSLContext::Close() {

src/cppnet/ssl/ssl_context.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class SSLContext {
4848
/**
4949
* @brief: Accept socket ssl.
5050
*/
51-
std::shared_ptr<SSLSocket> AcceptSSL(const Socket &soc);
51+
std::shared_ptr<SSLSocket>
52+
AcceptSSL(const Socket &soc,
53+
SSLSocket::Mode soc_mode = SSLSocket::Mode::kQuickly);
5254
/**
5355
* @brief: close ssl context
5456
*/

src/cppnet/ssl/ssl_socket.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <memory>
2+
#include <mutex>
13
#ifdef CPPNET_OPENSSL
24
#include "openssl/err.h"
35
#include "openssl/ssl.h"
@@ -7,7 +9,7 @@
79

810
namespace cppnet {
911

10-
SSLSocket::SSLSocket(SSL *ssl, const Socket &soc) {
12+
SSLSocket::SSLSocket(SSL *ssl, const Socket &soc, Mode mode) {
1113
if (ssl == nullptr) {
1214
err_msg_ = "[logicerr]:ssl is nullptr";
1315
return;
@@ -20,6 +22,11 @@ SSLSocket::SSLSocket(SSL *ssl, const Socket &soc) {
2022
ssl_ = ssl;
2123
SSL_set_fd(ssl_, fd_);
2224
status_ = kInit;
25+
26+
if (mode_ == Mode::kSafely) {
27+
pmutex_ = std::make_unique<std::mutex>();
28+
}
29+
mode_ = mode;
2330
}
2431

2532
int SSLSocket::Close() {
@@ -33,6 +40,11 @@ int SSLSocket::Close() {
3340
}
3441

3542
int SSLSocket::CloseSSL() {
43+
std::shared_ptr<std::lock_guard<std::mutex>> plock_guard = nullptr;
44+
if (mode_ == Mode::kSafely && pmutex_ != nullptr) {
45+
plock_guard = std::make_shared<std::lock_guard<std::mutex>>(*pmutex_);
46+
}
47+
3648
if (ssl_ != nullptr) {
3749
SSL_shutdown(ssl_);
3850
SSL_free(ssl_);
@@ -61,6 +73,15 @@ int SSLSocket::Connect(Address &addr) {
6173
}
6274

6375
int SSLSocket::IORead(void *buf, size_t len, int flag) {
76+
std::shared_ptr<std::lock_guard<std::mutex>> plock_guard = nullptr;
77+
if (mode_ == Mode::kSafely && pmutex_ != nullptr) {
78+
plock_guard = std::make_shared<std::lock_guard<std::mutex>>(*pmutex_);
79+
if (ssl_ == nullptr) {
80+
err_msg_ = "[syserr]:ssl is nullptr";
81+
return kLogicErr;
82+
}
83+
}
84+
6485
if (flag == MSG_WAITALL) {
6586
auto rc = 0;
6687
auto total_len = 0;
@@ -80,6 +101,15 @@ int SSLSocket::IORead(void *buf, size_t len, int flag) {
80101
}
81102

82103
int SSLSocket::IOWrite(const void *buf, size_t len, int flag) {
104+
std::shared_ptr<std::lock_guard<std::mutex>> plock_guard = nullptr;
105+
if (mode_ == Mode::kSafely && pmutex_ != nullptr) {
106+
plock_guard = std::make_shared<std::lock_guard<std::mutex>>(*pmutex_);
107+
if (ssl_ == nullptr) {
108+
err_msg_ = "[syserr]:ssl is nullptr";
109+
return kLogicErr;
110+
}
111+
}
112+
83113
return SSL_write(ssl_, buf, len);
84114
}
85115

src/cppnet/ssl/ssl_socket.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
#pragma once
22

3+
#include <memory>
34
#ifdef CPPNET_OPENSSL
45

56
#include "../socket/socket.hpp"
7+
#include <mutex>
68
#include <openssl/ssl.h>
79
#include <string>
810
namespace cppnet {
911

1012
class SSLSocket : public Socket {
1113
public:
12-
SSLSocket(SSL *ssl, const Socket &soc);
14+
/*
15+
* @brief: ssl socket mode
16+
* @note:
17+
* kQuickly: quick connect(not supported as a socket simultaneous scene)
18+
* kSafely: safely connect for thread(use mutex for simultaneous scene)
19+
*/
20+
enum class Mode { kQuickly = 0, kSafely = 1 };
21+
22+
public:
23+
SSLSocket(SSL *ssl, const Socket &soc, Mode mode = Mode::kQuickly);
1324
~SSLSocket() = default;
25+
// forbiden copy
1426
SSLSocket(const SSLSocket &) = delete;
1527
SSLSocket &operator=(const SSLSocket &) = delete;
1628
/**
@@ -29,6 +41,7 @@ class SSLSocket : public Socket {
2941

3042
public:
3143
inline std::string err_msg() const { return err_msg_; }
44+
inline void set_mode(Mode mode) { mode_ = mode; }
3245

3346
protected:
3447
int IORead(void *buf, size_t len, int flag = 0) override;
@@ -37,6 +50,8 @@ class SSLSocket : public Socket {
3750
private:
3851
SSL *ssl_ = nullptr;
3952
std::string err_msg_;
53+
Mode mode_ = Mode::kQuickly;
54+
std::shared_ptr<std::mutex> pmutex_ = nullptr;
4055
};
4156

4257
} // namespace cppnet

0 commit comments

Comments
 (0)