Skip to content

Commit 994db27

Browse files
authored
Remove std::any (#40)
Remove the config.h Remove USE_OPENSSL macro from header files Initialize OpenSSL only once globally Modify methods of the context update CMakeLists.txt Remove some std::dynamic_pointer_cast calls
1 parent ad24f2c commit 994db27

19 files changed

+382
-282
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.idea
22
build
33
cmake-build-debug
4-
trantor/utils/config.h
4+
.vscode

CMakeLists.txt

Lines changed: 73 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
1-
cmake_minimum_required(VERSION 3.2)
2-
project(trantor)
3-
4-
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
5-
#Clang, use c++17
6-
set(DEFS "USE_STD_ANY")
7-
set(USE_STD_ANY 1)
8-
set(CMAKE_CXX_STD_FLAGS c++17)
9-
else()
10-
EXEC_PROGRAM (gcc ARGS "--version | grep '^gcc'|awk '{print $3}' | sed s'/)//g' | sed s'/-.*//g'" OUTPUT_VARIABLE version)
11-
MESSAGE(STATUS "This is gcc version:: " ${version})
12-
if(version LESS 5.4.0)
13-
MESSAGE(STATUS "gcc is too old")
14-
stop()
15-
elseif(version LESS 7.1.0)
16-
set(CMAKE_CXX_STD_FLAGS c++14)
17-
MESSAGE(STATUS "c++14")
18-
else()
19-
set(CMAKE_CXX_STD_FLAGS c++17)
20-
set(DEFS "USE_STD_ANY")
21-
set(USE_STD_ANY 1)
22-
MESSAGE(STATUS "c++17")
23-
endif()
24-
endif()
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
2+
PROJECT(trantor)
253

264
# include directories
275
INCLUDE_DIRECTORIES(
@@ -33,90 +11,98 @@ INCLUDE_DIRECTORIES(
3311
/usr/include
3412
)
3513

36-
if(CMAKE_BUILD_TYPE STREQUAL "")
37-
set(CMAKE_BUILD_TYPE Release)
38-
endif()
39-
40-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -std=${CMAKE_CXX_STD_FLAGS}")
41-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -std=${CMAKE_CXX_STD_FLAGS}")
42-
43-
4414
# lib directories
4515
LINK_DIRECTORIES(
4616
${PROJECT_BINARY_DIR}/
4717
/usr/local/lib
4818
/usr/lib
4919
)
5020

51-
AUX_SOURCE_DIRECTORY(trantor/utils DIR_SRC)
52-
AUX_SOURCE_DIRECTORY(trantor/net DIR_SRC)
53-
AUX_SOURCE_DIRECTORY(trantor/net/inner DIR_SRC)
54-
find_package (OpenSSL)
55-
if(OpenSSL_FOUND)
56-
#add_definitions(-DUSE_OPENSSL)
57-
set(DEFS "USE_OPENSSL;${DEFS}")
21+
SET(public_net_headers
22+
trantor/net/EventLoop.h
23+
trantor/net/EventLoopThread.h
24+
trantor/net/EventLoopThreadPool.h
25+
trantor/net/InetAddress.h
26+
trantor/net/TcpClient.h
27+
trantor/net/TcpConnection.h
28+
trantor/net/TcpServer.h
29+
trantor/net/callbacks.h)
30+
31+
SET(public_utils_headers
32+
trantor/utils/AsyncFileLogger.h
33+
trantor/utils/ConcurrentTaskQueue.h
34+
trantor/utils/Date.h
35+
trantor/utils/Funcs.h
36+
trantor/utils/LockFreeQueue.h
37+
trantor/utils/LogStream.h
38+
trantor/utils/Logger.h
39+
trantor/utils/MsgBuffer.h
40+
trantor/utils/NonCopyable.h
41+
trantor/utils/ObjectPool.h
42+
trantor/utils/SerialTaskQueue.h
43+
trantor/utils/TaskQueue.h
44+
trantor/utils/TimingWheel.h)
45+
46+
SOURCE_GROUP( "Public API" FILES ${public_net_headers} ${public_utils_headers})
47+
48+
SET(trantor_sources
49+
trantor/utils/AsyncFileLogger.cc
50+
trantor/utils/ConcurrentTaskQueue.cc
51+
trantor/utils/Date.cc
52+
trantor/utils/LogStream.cc
53+
trantor/utils/Logger.cc
54+
trantor/utils/MsgBuffer.cc
55+
trantor/utils/SerialTaskQueue.cc
56+
trantor/utils/TimingWheel.cc
57+
trantor/net/EventLoop.cc
58+
trantor/net/EventLoopThread.cc
59+
trantor/net/EventLoopThreadPool.cc
60+
trantor/net/InetAddress.cc
61+
trantor/net/TcpClient.cc
62+
trantor/net/TcpServer.cc
63+
trantor/net/inner/Acceptor.cc
64+
trantor/net/inner/Channel.cc
65+
trantor/net/inner/Connector.cc
66+
trantor/net/inner/Poller.cc
67+
trantor/net/inner/Socket.cc
68+
trantor/net/inner/TcpConnectionImpl.cc
69+
trantor/net/inner/Timer.cc
70+
trantor/net/inner/TimerQueue.cc)
71+
72+
FIND_PACKAGE(OpenSSL)
73+
IF(OpenSSL_FOUND)
5874
include_directories(${OPENSSL_INCLUDE_DIR})
59-
link_libraries(${OPENSSL_LIBRARIES})
60-
AUX_SOURCE_DIRECTORY(trantor/net/ssl DIR_SRC)
61-
endif()
75+
LINK_LIBRARIES(${OPENSSL_LIBRARIES})
76+
SET(trantor_sources ${trantor_sources} trantor/net/ssl/SSLConnection.cc)
77+
ELSE()
78+
SET(trantor_sources ${trantor_sources} trantor/net/ssl/SSLConnectionSkipped.cc)
79+
ENDIF()
6280

6381
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
64-
ADD_LIBRARY(trantor ${DIR_SRC} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/EpollPoller.cc)
82+
SET(trantor_sources ${trantor_sources} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/EpollPoller.cc)
6583
MESSAGE(STATUS "current platform: Linux ")
6684
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
67-
MESSAGE(STATUS "current platform: Windows")
85+
MESSAGE(FATAL_ERROR "Error: Currently does not support Windows")
6886
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
69-
ADD_LIBRARY(trantor ${DIR_SRC} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/KQueue.cc)
87+
SET(trantor_sources ${trantor_sources} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/KQueue.cc)
7088
MESSAGE(STATUS "current platform: FreeBSD")
7189
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
72-
#ADD_LIBRARY(trantor ${DIR_SRC} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/PollPoller.cc)
73-
ADD_LIBRARY(trantor ${DIR_SRC} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/KQueue.cc)
90+
SET(trantor_sources ${trantor_sources} ${PROJECT_SOURCE_DIR}/trantor/net/inner/poller/KQueue.cc)
7491
MESSAGE(STATUS "current platform: MacOS")
7592
ELSE ()
7693
MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
7794
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")
7895

96+
ADD_LIBRARY(trantor ${trantor_sources})
97+
SET_PROPERTY(TARGET trantor PROPERTY CXX_STANDARD 14)
98+
SET_PROPERTY(TARGET trantor PROPERTY CXX_STANDARD_REQUIRED ON)
99+
SET_PROPERTY(TARGET trantor PROPERTY CXX_EXTENSIONS OFF)
79100

80-
SET(CONFIG_HEADER "${PROJECT_SOURCE_DIR}/config.h")
81-
file(WRITE "${CONFIG_HEADER}" "#pragma once\n")
82-
foreach(loop_var ${DEFS})
83-
file(APPEND "${CONFIG_HEADER}" "#define ${loop_var}\n")
84-
endforeach(loop_var)
85-
file(APPEND "${CONFIG_HEADER}" "\n")
86-
87-
if(DEFINED USE_STD_ANY)
88-
89-
file(APPEND "${CONFIG_HEADER}" "#include <any>\n")
90-
file(APPEND "${CONFIG_HEADER}" "using std::any;\n")
91-
file(APPEND "${CONFIG_HEADER}" "using std::any_cast;\n\n")
92-
file(APPEND "${CONFIG_HEADER}" "#include <string_view>\n")
93-
file(APPEND "${CONFIG_HEADER}" "typedef std::string_view string_view;\n\n")
94-
95-
else()
96-
97-
file(APPEND "${CONFIG_HEADER}" "#include <experimental/any>\n")
98-
file(APPEND "${CONFIG_HEADER}" "using std::experimental::any;\n")
99-
file(APPEND "${CONFIG_HEADER}" "using std::experimental::any_cast;\n\n")
100-
file(APPEND "${CONFIG_HEADER}" "#include <experimental/string_view>\n")
101-
file(APPEND "${CONFIG_HEADER}" "typedef std::experimental::basic_string_view<char> string_view;\n\n")
102-
103-
endif()
104-
105-
EXEC_PROGRAM(${PROJECT_SOURCE_DIR}/update_config.sh ARGS "${CONFIG_HEADER} ${PROJECT_SOURCE_DIR}/trantor/utils/config.h")
106-
107-
if (MAKETEST STREQUAL YES)
101+
IF (MAKETEST STREQUAL YES)
108102
ADD_SUBDIRECTORY(trantor/tests)
109-
endif ()
110-
111-
#install
112-
113-
install(TARGETS trantor DESTINATION lib)
114-
115-
install(DIRECTORY trantor/utils/ DESTINATION include/trantor/utils
116-
FILES_MATCHING PATTERN "*.h")
117-
118-
file(GLOB trantor_net_headers "${CMAKE_CURRENT_SOURCE_DIR}/trantor/net/*.h")
119-
install(FILES ${trantor_net_headers} DESTINATION include/trantor/net)
120-
103+
ENDIF ()
121104

105+
INSTALL(TARGETS trantor DESTINATION lib)
122106

107+
INSTALL(FILES ${public_net_headers} DESTINATION include/trantor/net)
108+
INSTALL(FILES ${public_utils_headers} DESTINATION include/trantor/utils)

trantor/net/EventLoop.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void EventLoop::abortNotInLoopThread()
176176
{
177177
LOG_FATAL << "It is forbidden to run loop on threads other than event-loop "
178178
"thread";
179-
exit(-1);
179+
exit(1);
180180
}
181181
void EventLoop::runInLoop(const Func &cb)
182182
{

trantor/net/TcpClient.cc

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
#include <trantor/net/TcpClient.h>
1313

1414
#include <trantor/utils/Logger.h>
15-
#ifdef USE_OPENSSL
1615
#include "ssl/SSLConnection.h"
17-
#endif
1816
#include "Connector.h"
1917
#include "inner/TcpConnectionImpl.h"
2018
#include <trantor/net/EventLoop.h>
@@ -100,7 +98,7 @@ TcpClient::~TcpClient()
10098
_loop->runInLoop([conn, loop]() {
10199
conn->setCloseCallback([loop](const TcpConnectionPtr &connPtr) {
102100
loop->queueInLoop([connPtr]() {
103-
std::dynamic_pointer_cast<TcpConnectionImpl>(connPtr)
101+
static_cast<TcpConnectionImpl *>(connPtr.get())
104102
->connectDestroyed();
105103
});
106104
});
@@ -148,9 +146,8 @@ void TcpClient::newConnection(int sockfd)
148146
_loop->assertInLoopThread();
149147
InetAddress peerAddr(Socket::getPeerAddr(sockfd));
150148
InetAddress localAddr(Socket::getLocalAddr(sockfd));
151-
// TODO poll with zero timeout to double confirm the new connection
152-
// TODO use make_shared if necessary
153-
#ifdef USE_OPENSSL
149+
// TODO poll with zero timeout to double confirm the new connection
150+
// TODO use make_shared if necessary
154151
std::shared_ptr<TcpConnectionImpl> conn;
155152
if (_sslCtxPtr)
156153
{
@@ -164,10 +161,6 @@ void TcpClient::newConnection(int sockfd)
164161
localAddr,
165162
peerAddr);
166163
}
167-
#else
168-
auto conn =
169-
std::make_shared<TcpConnectionImpl>(_loop, sockfd, localAddr, peerAddr);
170-
#endif
171164
conn->setConnectionCallback(_connectionCallback);
172165
conn->setRecvMsgCallback(_messageCallback);
173166
conn->setWriteCompleteCallback(_writeCompleteCallback);
@@ -201,26 +194,10 @@ void TcpClient::removeConnection(const TcpConnectionPtr &conn)
201194
}
202195
}
203196

204-
#ifdef USE_OPENSSL
205197
void TcpClient::enableSSL()
206198
{
207-
// init OpenSSL
208-
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
209-
(defined(LIBRESSL_VERSION_NUMBER) && \
210-
LIBRESSL_VERSION_NUMBER < 0x20700000L)
211-
// Initialize OpenSSL once;
212-
static std::once_flag once;
213-
std::call_once(once, []() {
214-
SSL_library_init();
215-
ERR_load_crypto_strings();
216-
SSL_load_error_strings();
217-
OpenSSL_add_all_algorithms();
218-
});
219-
#endif
199+
// init OpenSSL
200+
initOpenSSL();
220201
/* Create a new OpenSSL context */
221-
_sslCtxPtr =
222-
std::shared_ptr<SSL_CTX>(SSL_CTX_new(SSLv23_method()),
223-
[](SSL_CTX *ctx) { SSL_CTX_free(ctx); });
224-
assert(_sslCtxPtr);
202+
_sslCtxPtr = newSSLContext();
225203
}
226-
#endif

trantor/net/TcpClient.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,18 @@
1717
// Author: Tao An
1818

1919
#pragma once
20-
#include <trantor/utils/config.h>
2120
#include <trantor/net/EventLoop.h>
2221
#include <trantor/net/InetAddress.h>
2322
#include <trantor/net/TcpConnection.h>
2423
#include <functional>
2524
#include <thread>
2625
#include <atomic>
2726
#include <signal.h>
28-
#ifdef USE_OPENSSL
29-
#include <openssl/ssl.h>
30-
#include <openssl/err.h>
31-
#endif
3227
namespace trantor
3328
{
3429
class Connector;
3530
typedef std::shared_ptr<Connector> ConnectorPtr;
36-
31+
class SSLContext;
3732
class TcpClient : NonCopyable
3833
{
3934
public:
@@ -108,9 +103,7 @@ class TcpClient : NonCopyable
108103
{
109104
_writeCompleteCallback = std::move(cb);
110105
}
111-
#ifdef USE_OPENSSL
112106
void enableSSL();
113-
#endif
114107

115108
private:
116109
/// Not thread safe, but in loop
@@ -130,9 +123,7 @@ class TcpClient : NonCopyable
130123
// always in loop thread
131124
mutable std::mutex _mutex;
132125
TcpConnectionPtr _connection; // @GuardedBy _mutex
133-
#ifdef USE_OPENSSL
134-
std::shared_ptr<SSL_CTX> _sslCtxPtr;
135-
#endif
126+
std::shared_ptr<SSLContext> _sslCtxPtr;
136127
class IgnoreSigPipe
137128
{
138129
public:

trantor/net/TcpConnection.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*/
1414

1515
#pragma once
16-
17-
#include <trantor/utils/config.h>
1816
#include <trantor/net/EventLoop.h>
1917
#include <trantor/net/InetAddress.h>
2018
#include <trantor/utils/NonCopyable.h>
@@ -59,19 +57,45 @@ class TcpConnection
5957
virtual void forceClose() = 0;
6058
virtual EventLoop *getLoop() = 0;
6159

62-
virtual void setContext(const any &context) = 0;
63-
virtual void setContext(any &&context) = 0;
64-
virtual const any &getContext() const = 0;
60+
/// Set custom data on the connection
61+
void setContext(const std::shared_ptr<void> &context)
62+
{
63+
_contextPtr = context;
64+
}
65+
void setContext(std::shared_ptr<void> &&context)
66+
{
67+
_contextPtr = std::move(context);
68+
}
6569

66-
virtual any *getMutableContext() = 0;
70+
/// Get custom data from the connection
71+
template <typename T>
72+
std::shared_ptr<T> getContext() const
73+
{
74+
return std::static_pointer_cast<T>(_contextPtr);
75+
}
6776

77+
/// Return true if the context is set by user.
78+
bool hasContext() const
79+
{
80+
return (bool)_contextPtr;
81+
}
82+
83+
/// Clear the context.
84+
void clearContext()
85+
{
86+
_contextPtr.reset();
87+
}
88+
6889
// Call this method to avoid being kicked off by TcpServer, refer to
6990
// the kickoffIdleConnections method in the TcpServer class.
7091
virtual void keepAlive() = 0;
7192
virtual bool isKeepAlive() = 0;
7293

7394
virtual size_t bytesSent() const = 0;
7495
virtual size_t bytesReceived() const = 0;
96+
97+
private:
98+
std::shared_ptr<void> _contextPtr;
7599
};
76100

77101
} // namespace trantor

0 commit comments

Comments
 (0)