Skip to content

Commit

Permalink
modify CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
IronsDu committed Dec 15, 2017
1 parent bc08556 commit c349659
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 241 deletions.
79 changes: 18 additions & 61 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ elseif(UNIX)
endif()
endif()

set(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib")

set(net_src_list
${PROJECT_SOURCE_DIR}/src/brynet/net/EventLoop.cpp
${PROJECT_SOURCE_DIR}/src/brynet/net/CurrentThread.cpp
${PROJECT_SOURCE_DIR}/src/brynet/net/DataSocket.cpp
${PROJECT_SOURCE_DIR}/src/brynet/net/SocketLibFunction.c
${PROJECT_SOURCE_DIR}/src/brynet/net/SocketLibFunction.cpp
${PROJECT_SOURCE_DIR}/src/brynet/net/TCPService.cpp
${PROJECT_SOURCE_DIR}/src/brynet/net/WrapTCPService.cpp
${PROJECT_SOURCE_DIR}/src/brynet/net/Connector.cpp
Expand All @@ -28,11 +30,6 @@ set(timer_src_list
${PROJECT_SOURCE_DIR}/src/brynet/timer/Timer.cpp
)

set(base_src_list
${PROJECT_SOURCE_DIR}/src/brynet/utils/array.c
${PROJECT_SOURCE_DIR}/src/brynet/utils/buffer.c
)

set(http_src_list
${PROJECT_SOURCE_DIR}/src/brynet/net/http/http_parser.c
${PROJECT_SOURCE_DIR}/src/brynet/net/http/HttpParser.cpp
Expand All @@ -43,70 +40,30 @@ set(utils_src_list
${PROJECT_SOURCE_DIR}/src/brynet/utils/base64.cpp
${PROJECT_SOURCE_DIR}/src/brynet/utils/md5calc.cpp
${PROJECT_SOURCE_DIR}/src/brynet/utils/SHA1.cpp
${PROJECT_SOURCE_DIR}/src/brynet/utils/array.c
${PROJECT_SOURCE_DIR}/src/brynet/utils/buffer.c
)

include_directories("${PROJECT_SOURCE_DIR}/src/")

add_executable(pingpongserver ${net_src_list} ${timer_src_list} ${base_src_list} ${PROJECT_SOURCE_DIR}/examples/PingPongServer.cpp)
if(WIN32)
target_link_libraries(pingpongserver ws2_32)
add_library(brynet ${net_src_list} ${timer_src_list} ${http_src_list} ${utils_src_list})
elseif(UNIX)
add_library(brynet SHARED ${net_src_list} ${timer_src_list} ${http_src_list} ${utils_src_list})
find_package(Threads REQUIRED)
target_link_libraries(pingpongserver pthread)
target_link_libraries(brynet pthread)
endif()
install(TARGETS brynet DESTINATION lib)

add_executable(pingpongclient ${net_src_list} ${timer_src_list} ${base_src_list} ${PROJECT_SOURCE_DIR}/examples/PingPongClient.cpp)
if(WIN32)
target_link_libraries(pingpongclient ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(pingpongclient pthread)
endif()
file(GLOB BRYNET_NET_PUBLIC_HEADERS ${PROJECT_SOURCE_DIR}/src/brynet/net/*.h)
install (FILES ${BRYNET_NET_PUBLIC_HEADERS} DESTINATION "include/brynet/net")

add_executable(broadcastserver ${net_src_list} ${timer_src_list} ${base_src_list} ${PROJECT_SOURCE_DIR}/examples/BroadCastServer.cpp)
if(WIN32)
target_link_libraries(broadcastserver ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(broadcastserver pthread)
endif()
file(GLOB BRYNET_TIMER_PUBLIC_HEADERS ${PROJECT_SOURCE_DIR}/src/brynet/timer/*.h)
install (FILES ${BRYNET_TIMER_PUBLIC_HEADERS} DESTINATION "include/brynet/timer")

add_executable(broadcastclient ${net_src_list} ${timer_src_list} ${base_src_list} ${PROJECT_SOURCE_DIR}/examples/BroadCastClient.cpp)
if(WIN32)
target_link_libraries(broadcastclient ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(broadcastclient pthread)
endif()

add_executable(httpserver ${net_src_list} ${timer_src_list} ${base_src_list} ${utils_src_list} ${http_src_list} ${PROJECT_SOURCE_DIR}/examples/TestHttp.cpp)
if(WIN32)
target_link_libraries(httpserver ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(httpserver pthread)
endif()

add_executable(benchwebsocket ${net_src_list} ${timer_src_list} ${base_src_list} ${utils_src_list} ${http_src_list} ${PROJECT_SOURCE_DIR}/examples/BenchWebsocket.cpp)
if(WIN32)
target_link_libraries(benchwebsocket ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(benchwebsocket pthread)
endif()
file(GLOB BRYNET_UTILS_PUBLIC_HEADERS ${PROJECT_SOURCE_DIR}/src/brynet/utils/*.h)
install (FILES ${BRYNET_UTILS_PUBLIC_HEADERS} DESTINATION "include/brynet/utils")

add_executable(promisereceive ${net_src_list} ${timer_src_list} ${base_src_list} ${utils_src_list} ${http_src_list} ${PROJECT_SOURCE_DIR}/examples/TestPromiseReceive.cpp)
if(WIN32)
target_link_libraries(promisereceive ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(promisereceive pthread)
endif()
file(GLOB BRYNET_HTTP_PUBLIC_HEADERS ${PROJECT_SOURCE_DIR}/src/brynet/net/http/*.h)
install (FILES ${BRYNET_HTTP_PUBLIC_HEADERS} DESTINATION "include/brynet/net/http")

add_executable(web_binary_proxy ${net_src_list} ${timer_src_list} ${base_src_list} ${utils_src_list} ${http_src_list} ${PROJECT_SOURCE_DIR}/examples/WebBinaryProxy.cpp)
if(WIN32)
target_link_libraries(web_binary_proxy ws2_32)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(web_binary_proxy pthread)
endif()
add_subdirectory(examples)
4 changes: 2 additions & 2 deletions examples/BenchWebsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ int main(int argc, char **argv)

for (int i = 0; i < connections; i++)
{
sock fd = ox_socket_connect(false, host, port);
ox_socket_nodelay(fd);
sock fd = brynet::net::base::Connect(false, host, port);
brynet::net::base::SocketNodelay(fd);
service->addSession(fd, [host](const TCPSession::PTR& session) {
HttpService::setup(session, [host](const HttpSession::PTR& httpSession) {
HttpRequest request;
Expand Down
10 changes: 5 additions & 5 deletions examples/BroadCastClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ int main(int argc, char** argv)
int clietNum = atoi(argv[3]);
int packetLen = atoi(argv[4]);

ox_socket_init();
brynet::net::base::InitSocket();

auto clientEventLoop = std::make_shared<EventLoop>();

for (int i = 0; i < clietNum; i++)
{
auto fd = ox_socket_connect(false, ip.c_str(), port);
ox_socket_setsdsize(fd, 32 * 1024);
ox_socket_setrdsize(fd, 32 * 1024);
ox_socket_nodelay(fd);
auto fd = brynet::net::base::Connect(false, ip.c_str(), port);
brynet::net::base::SocketSetSendSize(fd, 32 * 1024);
brynet::net::base::SocketSetRecvSize(fd, 32 * 1024);
brynet::net::base::SocketNodelay(fd);

DataSocket::PTR datasSocket = new DataSocket(fd, 1024 * 1024);
datasSocket->setEnterCallback([packetLen](DataSocket::PTR datasSocket) {
Expand Down
8 changes: 4 additions & 4 deletions examples/BroadCastServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ int main(int argc, char** argv)
}

int port = atoi(argv[1]);
ox_socket_init();
brynet::net::base::InitSocket();

service = std::make_shared<WrapTcpService>();
auto mainLoop = std::make_shared<EventLoop>();
auto listenThrean = ListenThread::Create();

listenThrean->startListen(false, "0.0.0.0", port, [mainLoop, listenThrean](sock fd) {
ox_socket_nodelay(fd);
ox_socket_setsdsize(fd, 32 * 1024);
ox_socket_setrdsize(fd, 32 * 1024);
brynet::net::base::SocketNodelay(fd);
brynet::net::base::SocketSetSendSize(fd, 32 * 1024);
brynet::net::base::SocketSetRecvSize(fd, 32 * 1024);
service->addSession(fd, [mainLoop](const TCPSession::PTR& session) {
mainLoop->pushAsyncProc([session]() {
addClientID(session);
Expand Down
63 changes: 63 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
add_executable(pingpongserver PingPongServer.cpp)
if(WIN32)
target_link_libraries(pingpongserver ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(pingpongserver pthread brynet)
endif()

add_executable(pingpongclient PingPongClient.cpp)
if(WIN32)
target_link_libraries(pingpongclient ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(pingpongclient pthread brynet)
endif()

add_executable(broadcastserver BroadCastServer.cpp)
if(WIN32)
target_link_libraries(broadcastserver ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(broadcastserver pthread brynet)
endif()

add_executable(broadcastclient BroadCastClient.cpp)
if(WIN32)
target_link_libraries(broadcastclient ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(broadcastclient pthread brynet)
endif()

add_executable(httpserver TestHttp.cpp)
if(WIN32)
target_link_libraries(httpserver ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(httpserver pthread brynet)
endif()

add_executable(benchwebsocket BenchWebsocket.cpp)
if(WIN32)
target_link_libraries(benchwebsocket ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(benchwebsocket pthread brynet)
endif()

add_executable(promisereceive TestPromiseReceive.cpp)
if(WIN32)
target_link_libraries(promisereceive ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(promisereceive pthread brynet)
endif()

add_executable(web_binary_proxy WebBinaryProxy.cpp)
if(WIN32)
target_link_libraries(web_binary_proxy ws2_32 brynet)
elseif(UNIX)
find_package(Threads REQUIRED)
target_link_libraries(web_binary_proxy pthread brynet)
endif()
2 changes: 1 addition & 1 deletion examples/PingPongClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char **argv)
{
connector->asyncConnect(argv[1], atoi(argv[2]), std::chrono::seconds(10), [server, tmp](sock fd) {
std::cout << "connect success" << std::endl;
ox_socket_nodelay(fd);
brynet::net::base::SocketNodelay(fd);
server->addSession(fd, [tmp](const TCPSession::PTR& session) {
session->setDataCallback([](const TCPSession::PTR& session, const char* buffer, size_t len) {
session->send(buffer, len);
Expand Down
2 changes: 1 addition & 1 deletion examples/PingPongServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int main(int argc, char **argv)
auto listenThread = ListenThread::Create();

listenThread->startListen(false, "0.0.0.0", atoi(argv[1]), [=](sock fd){
ox_socket_nodelay(fd);
brynet::net::base::SocketNodelay(fd);
server->addSession(fd, [](const TCPSession::PTR& session){
total_client_num++;

Expand Down
2 changes: 1 addition & 1 deletion examples/TestHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int argc, char **argv)

std::cin.get();

sock fd = ox_socket_connect(false, "192.168.12.128", 8080);
sock fd = brynet::net::base::Connect(false, "192.168.12.128", 8080);
if (fd != SOCKET_ERROR)
{
service->addSession(fd, [](const TCPSession::PTR& session) {
Expand Down
2 changes: 1 addition & 1 deletion examples/TestPromiseReceive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(int argc, char **argv)
auto listenThread = ListenThread::Create();

listenThread->startListen(false, "0.0.0.0", atoi(argv[1]), [=](sock fd){
ox_socket_nodelay(fd);
brynet::net::base::SocketNodelay(fd);
server->addSession(fd, [](const TCPSession::PTR& session){

auto promiseReceive = setupPromiseReceive(session);
Expand Down
21 changes: 8 additions & 13 deletions src/brynet/net/Connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ namespace brynet
class AsyncConnectAddr
{
public:
AsyncConnectAddr() BRYNET_NOEXCEPT : mTimeout(std::chrono::nanoseconds::zero())
{
mPort = 0;
}

AsyncConnectAddr(const std::string& ip,
int port,
std::chrono::nanoseconds timeout,
Expand Down Expand Up @@ -184,7 +179,7 @@ void ConnectorWorkInfo::checkConnectStatus(int millsecond)
}
else
{
ox_socket_close(fd);
brynet::net::base::SocketClose(fd);
if (it->second.failedCB != nullptr)
{
it->second.failedCB();
Expand Down Expand Up @@ -214,7 +209,7 @@ void ConnectorWorkInfo::checkTimeout()
mConnectingFds.erase(fd);
mConnectingInfos.erase(it++);

ox_socket_close(fd);
brynet::net::base::SocketClose(fd);
if (cb != nullptr)
{
cb();
Expand All @@ -234,7 +229,7 @@ void ConnectorWorkInfo::causeAllFailed()
mConnectingFds.erase(fd);
mConnectingInfos.erase(it++);

ox_socket_close(fd);
brynet::net::base::SocketClose(fd);
if (cb != nullptr)
{
cb();
Expand All @@ -255,17 +250,17 @@ void ConnectorWorkInfo::processConnect(const AsyncConnectAddr& addr)
#endif
int n = 0;

ox_socket_init();
brynet::net::base::InitSocket();

clientfd = ox_socket_create(AF_INET, SOCK_STREAM, 0);
clientfd = brynet::net::base::SocketCreate(AF_INET, SOCK_STREAM, 0);
if (clientfd == SOCKET_ERROR)
{
goto FAILED;
}

ox_socket_nonblock(clientfd);
brynet::net::base::SocketNonblock(clientfd);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(addr.getIP().c_str());
inet_pton(AF_INET, addr.getIP().c_str(), &server_addr.sin_addr.s_addr);
server_addr.sin_port = htons(addr.getPort());

n = connect(clientfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
Expand All @@ -276,7 +271,7 @@ void ConnectorWorkInfo::processConnect(const AsyncConnectAddr& addr)

if (check_error != sErrno)
{
ox_socket_close(clientfd);
brynet::net::base::SocketClose(clientfd);
clientfd = SOCKET_ERROR;
goto FAILED;
}
Expand Down
35 changes: 19 additions & 16 deletions src/brynet/net/CurrentThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@
#include <linux/unistd.h>
#endif

using namespace brynet::net::CurrentThread;

namespace brynet
{
namespace net
{
namespace CurrentThread
{
#ifdef PLATFORM_WINDOWS
__declspec(thread) THREAD_ID_TYPE cachedTid = 0;
#else
__thread THREAD_ID_TYPE cachedTid = 0;
#endif

void cacheTid()
{
#ifdef PLATFORM_WINDOWS
cachedTid = GetCurrentThreadId();
#else
cachedTid = static_cast<pid_t>(::syscall(SYS_gettid));
#endif
}
#ifdef PLATFORM_WINDOWS
__declspec(thread) THREAD_ID_TYPE cachedTid = 0;
#else
__thread THREAD_ID_TYPE cachedTid = 0;
#endif
}
}
}

brynet::net::CurrentThread::THREAD_ID_TYPE& brynet::net::CurrentThread::tid()
{
if (cachedTid == 0)
{
#ifdef PLATFORM_WINDOWS
cachedTid = GetCurrentThreadId();
#else
cachedTid = static_cast<pid_t>(::syscall(SYS_gettid));
#endif
}

return cachedTid;
}
Loading

0 comments on commit c349659

Please sign in to comment.