Skip to content

Commit 16028af

Browse files
author
杨超
committed
1.调整结构,为跨平台做准备;2修改cmake模式
1 parent 5fafe3e commit 16028af

9 files changed

+228
-71
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
build/
2-
build_tool_for_c/
32
.*
3+
*.cmake
4+
CMakeFiles
5+
MakeFile

CMakeLists.txt

+3-13
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,15 @@ else()
4343
message("can not find boost, please install boost first")
4444
EXIT()
4545
endif()
46+
4647
# add include and lib other project
4748
include_directories(${PROJECT_SOURCE_DIR}/include)
4849
link_directories(${PROJECT_SOURCE_DIR}/lib)
4950

5051
# add my project .h and .cpp
5152
include_directories(${PROJECT_SOURCE_DIR})
5253

53-
aux_source_directory(${PROJECT_SOURCE_DIR}/enjoycnet SRC_LIST)
54-
aux_source_directory(${PROJECT_SOURCE_DIR}/enjoycnet/core SRC_LIST)
55-
56-
# add link libs
57-
#add_executable(${projectName} ${SRC_LIST})
58-
#set(LINK_ARGS ${Boost_LIBRARIES} enjoycco ev glog)
59-
#target_link_libraries(${projectName} ${LINK_ARGS})
60-
61-
set(TARGET ${projectName})
62-
add_library(${TARGET} ${SRC_LIST} )
63-
install(TARGETS ${TARGET} LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib")
64-
install(DIRECTORY ${PROJECT_SOURCE_DIR}/enjoycnet DESTINATION "include" FILES_MATCHING PATTERN "*.h")
65-
54+
add_subdirectory(enjoycnet)
55+
add_subdirectory(test)
6656

6757

enjoycnet/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
project(enjoycnet)
2+
3+
4+
macro(add_src_dir name)
5+
file(GLOB SRC_LIST ${name}/*.h ${name}/*.cpp)
6+
if(WIN32)
7+
list(FILTER SRC_LIST EXCLUDE REGEX _linux.cpp)
8+
else()
9+
list(FILTER SRC_LIST EXCLUDE REGEX _win.cpp)
10+
endif()
11+
12+
endmacro()
13+
add_src_dir(core)
14+
15+
set(TARGET ${projectName})
16+
add_library(${TARGET} ${SRC_LIST} )
17+
install(TARGETS ${TARGET} LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib")
18+
install(DIRECTORY ${PROJECT_SOURCE_DIR} DESTINATION "include" FILES_MATCHING PATTERN "*.h")
19+
20+
21+

enjoycnet/Makefile

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# CMAKE generated file: DO NOT EDIT!
2+
# Generated by "Unix Makefiles" Generator, CMake Version 3.10
3+
4+
# Default target executed when no arguments are given to make.
5+
default_target: all
6+
7+
.PHONY : default_target
8+
9+
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
10+
.NOTPARALLEL:
11+
12+
13+
#=============================================================================
14+
# Special targets provided by cmake.
15+
16+
# Disable implicit rules so canonical targets will work.
17+
.SUFFIXES:
18+
19+
20+
# Remove some rules from gmake that .SUFFIXES does not remove.
21+
SUFFIXES =
22+
23+
.SUFFIXES: .hpux_make_needs_suffix_list
24+
25+
26+
# Suppress display of executed commands.
27+
$(VERBOSE).SILENT:
28+
29+
30+
# A target that is always out of date.
31+
cmake_force:
32+
33+
.PHONY : cmake_force
34+
35+
#=============================================================================
36+
# Set environment variables for the build.
37+
38+
# The shell in which to execute make rules.
39+
SHELL = /bin/sh
40+
41+
# The CMake executable.
42+
CMAKE_COMMAND = /usr/bin/cmake
43+
44+
# The command to remove a file.
45+
RM = /usr/bin/cmake -E remove -f
46+
47+
# Escaping for special characters.
48+
EQUALS = =
49+
50+
# The top-level source directory on which CMake was run.
51+
CMAKE_SOURCE_DIR = /home/ycn2308/work_proj/git_proj/enjoycnet
52+
53+
# The top-level build directory on which CMake was run.
54+
CMAKE_BINARY_DIR = /home/ycn2308/work_proj/git_proj/enjoycnet
55+
56+
#=============================================================================
57+
# Targets provided globally by CMake.
58+
59+
# Special rule for the target rebuild_cache
60+
rebuild_cache:
61+
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
62+
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
63+
.PHONY : rebuild_cache
64+
65+
# Special rule for the target rebuild_cache
66+
rebuild_cache/fast: rebuild_cache
67+
68+
.PHONY : rebuild_cache/fast
69+
70+
# Special rule for the target edit_cache
71+
edit_cache:
72+
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..."
73+
/usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available.
74+
.PHONY : edit_cache
75+
76+
# Special rule for the target edit_cache
77+
edit_cache/fast: edit_cache
78+
79+
.PHONY : edit_cache/fast
80+
81+
# The main all target
82+
all: cmake_check_build_system
83+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(CMAKE_COMMAND) -E cmake_progress_start /home/ycn2308/work_proj/git_proj/enjoycnet/CMakeFiles /home/ycn2308/work_proj/git_proj/enjoycnet/enjoycnet/CMakeFiles/progress.marks
84+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(MAKE) -f CMakeFiles/Makefile2 enjoycnet/all
85+
$(CMAKE_COMMAND) -E cmake_progress_start /home/ycn2308/work_proj/git_proj/enjoycnet/CMakeFiles 0
86+
.PHONY : all
87+
88+
# The main clean target
89+
clean:
90+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(MAKE) -f CMakeFiles/Makefile2 enjoycnet/clean
91+
.PHONY : clean
92+
93+
# The main clean target
94+
clean/fast: clean
95+
96+
.PHONY : clean/fast
97+
98+
# Prepare targets for installation.
99+
preinstall: all
100+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(MAKE) -f CMakeFiles/Makefile2 enjoycnet/preinstall
101+
.PHONY : preinstall
102+
103+
# Prepare targets for installation.
104+
preinstall/fast:
105+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(MAKE) -f CMakeFiles/Makefile2 enjoycnet/preinstall
106+
.PHONY : preinstall/fast
107+
108+
# clear depends
109+
depend:
110+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
111+
.PHONY : depend
112+
113+
# Help Target
114+
help:
115+
@echo "The following are some of the valid targets for this Makefile:"
116+
@echo "... all (the default if no target is provided)"
117+
@echo "... clean"
118+
@echo "... depend"
119+
@echo "... rebuild_cache"
120+
@echo "... edit_cache"
121+
.PHONY : help
122+
123+
124+
125+
#=============================================================================
126+
# Special targets to cleanup operation of make.
127+
128+
# Special rule to run CMake to check the build system integrity.
129+
# No rule that depends on this can have commands that come from listfiles
130+
# because they might be regenerated.
131+
cmake_check_build_system:
132+
cd /home/ycn2308/work_proj/git_proj/enjoycnet && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
133+
.PHONY : cmake_check_build_system
134+

enjoycnet/core/hook_syscall.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <arpa/inet.h>
43

4+
#include "syscall.h"
55
#include "io_context.h"
66
#include <glog/logging.h>
77

enjoycnet/core/proto_tcp.h

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#pragma once
22

3-
#include <unistd.h>
4-
#include <fcntl.h>
5-
#include <arpa/inet.h>
63
#include "endpoint.h"
74
#include "hook_syscall.h"
85

9-
106
namespace enjoyc
117
{
128
namespace net
@@ -121,24 +117,21 @@ namespace enjoyc
121117
private:
122118
int create()
123119
{
124-
int fd = ::socket(AF_INET, SOCK_STREAM, 0);
120+
int fd = get_socket(AF_INET, SOCK_STREAM, 0);
125121

126122
if(fd <= 0)
127123
return -1;
128-
124+
129125
// non-blocking
130-
if(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0)
126+
if(make_socket_nonblocking(fd) < 0)
131127
{
132-
::close(fd);
128+
close_socket(fd);
133129
return -1;
134130
}
135131

136-
//TODO reuse_addr reuse port
137-
int option = 1;
138-
if(::setsockopt(fd, SOL_SOCKET, (SO_REUSEADDR | SO_REUSEPORT),
139-
(char*)&option, sizeof(option)) < 0)
132+
if(make_socket_resue(fd) < 0)
140133
{
141-
::close(fd);
134+
close_socket(fd);
142135
return -1;
143136
}
144137

enjoycnet/core/syscall.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#ifdef __linux__
3+
#include <sys/socket.h>
4+
#elif _WIN32
5+
#include <ws2tcpip.h>
6+
#endif
7+
8+
9+
namespace enjoyc
10+
{
11+
namespace net
12+
{
13+
int get_socket(int family, int type, int protocol);
14+
int close_socket(int fd);
15+
int make_socket_nonblocking(int fd);
16+
int make_socket_resue(int fd);
17+
}//net
18+
}//enjoyc
19+
20+
21+

enjoycnet/core/syscall_linux.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "syscall.h"
2+
3+
#include <unistd.h>
4+
#include <fcntl.h>
5+
#include <arpa/inet.h>
6+
7+
namespace enjoyc
8+
{
9+
namespace net
10+
{
11+
int get_socket(int family, int type, int protocol)
12+
{
13+
return ::socket(family, type, protocol);
14+
}
15+
16+
int close_socket(int fd)
17+
{
18+
return ::close(fd);
19+
}
20+
21+
int make_socket_nonblocking(int fd)
22+
{
23+
return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
24+
}
25+
26+
int make_socket_resue(int fd)
27+
{
28+
int option = 1;
29+
return ::setsockopt(fd, SOL_SOCKET, (SO_REUSEADDR | SO_REUSEPORT),
30+
(char*)&option, sizeof(option));
31+
}
32+
33+
}//net
34+
}//enjoyc
35+
36+
37+
38+

test/CMakeLists.txt

+1-43
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,8 @@
1-
cmake_minimum_required(VERSION 3.2)
2-
set(CMAKE_VERBOSE_MAKEFILE ON)
3-
4-
if (CMAKE_BUILD_TYPE)
5-
else()
6-
set(CMAKE_BUILD_TYPE RelWithDebInfo)
7-
#set(CMAKE_BUILD_TYPE DEBUG)
8-
endif()
9-
10-
11-
if (UNIX)
12-
set(CMAKE_CXX_FLAGS "-std=c++17 -fPIC -Wall -m64 ${CMAKE_CXX_FLAGS}")
13-
elseif (WIN32)
14-
# windows platform
15-
#add_definitions(-D_CRT_SECURE_NO_WARNINGS)
16-
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
17-
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
18-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd /EHsc")
19-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT /EHsc")
20-
endif()
21-
22-
message("------------ Options -------------")
23-
message(" CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
24-
message(" CMAKE_COMMAND: ${CMAKE_COMMAND}")
25-
message(" CMAME_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
26-
message("-------------- Env ---------------")
27-
message(" CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
28-
message(" CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
29-
message(" CMAKE_PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
30-
message("----------------------------------")
31-
32-
# find boost
33-
set(Boost_USE_STATIC_LIBS ON)
34-
find_package(Boost COMPONENTS system context)
35-
if(Boost_FOUND)
36-
else()
37-
message("can not find boost, please install boost first")
38-
EXIT()
39-
endif()
40-
# add include and lib other project
41-
include_directories(${PROJECT_SOURCE_DIR}/../include)
42-
link_directories(${PROJECT_SOURCE_DIR}/../lib)
1+
project(test)
432

443
aux_source_directory(${PROJECT_SOURCE_DIR} SRC_LIST)
454

465
set(LINK_ARGS enjoycnet enjoycco ev glog ${Boost_LIBRARIES})
47-
message(" Link args is ${LINK_ARGS} ")
486
foreach(var ${SRC_LIST})
497
string(REGEX REPLACE ".*/" "" var ${var})
508
string(REGEX REPLACE ".cpp" "" tgt ${var})

0 commit comments

Comments
 (0)