forked from xiaojiaqi/C1000kPracticeGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is a very old code, we need check it. Signed-off-by: Max <[email protected]>
- Loading branch information
Showing
18 changed files
with
798 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
################################################################################ | ||
# Automatically-generated file. Do not edit! | ||
################################################################################ | ||
|
||
-include ../makefile.init | ||
|
||
RM := rm -rf | ||
|
||
# All of the sources participating in the build are defined here | ||
-include sources.mk | ||
-include src/subdir.mk | ||
-include subdir.mk | ||
-include objects.mk | ||
|
||
ifneq ($(MAKECMDGOALS),clean) | ||
ifneq ($(strip $(C++_DEPS)),) | ||
-include $(C++_DEPS) | ||
endif | ||
ifneq ($(strip $(C_DEPS)),) | ||
-include $(C_DEPS) | ||
endif | ||
ifneq ($(strip $(CC_DEPS)),) | ||
-include $(CC_DEPS) | ||
endif | ||
ifneq ($(strip $(CPP_DEPS)),) | ||
-include $(CPP_DEPS) | ||
endif | ||
ifneq ($(strip $(CXX_DEPS)),) | ||
-include $(CXX_DEPS) | ||
endif | ||
ifneq ($(strip $(C_UPPER_DEPS)),) | ||
-include $(C_UPPER_DEPS) | ||
endif | ||
endif | ||
|
||
-include ../makefile.defs | ||
|
||
# Add inputs and outputs from these tool invocations to the build variables | ||
|
||
# All Target | ||
all: client_spider | ||
|
||
# Tool invocations | ||
client_spider: $(OBJS) $(USER_OBJS) | ||
@echo 'Building target: $@' | ||
@echo 'Invoking: GCC C++ Linker' | ||
g++ -o "client_spider" $(OBJS) $(USER_OBJS) $(LIBS) | ||
@echo 'Finished building target: $@' | ||
@echo ' ' | ||
|
||
# Other Targets | ||
clean: | ||
-$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) client_spider | ||
-@echo ' ' | ||
|
||
.PHONY: all clean dependents | ||
.SECONDARY: | ||
|
||
-include ../makefile.targets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
################################################################################ | ||
# Automatically-generated file. Do not edit! | ||
################################################################################ | ||
|
||
USER_OBJS := | ||
|
||
LIBS := -lpthread | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
################################################################################ | ||
# Automatically-generated file. Do not edit! | ||
################################################################################ | ||
|
||
O_SRCS := | ||
CPP_SRCS := | ||
C_UPPER_SRCS := | ||
C_SRCS := | ||
S_UPPER_SRCS := | ||
OBJ_SRCS := | ||
ASM_SRCS := | ||
CXX_SRCS := | ||
C++_SRCS := | ||
CC_SRCS := | ||
OBJS := | ||
C++_DEPS := | ||
C_DEPS := | ||
CC_DEPS := | ||
CPP_DEPS := | ||
EXECUTABLES := | ||
CXX_DEPS := | ||
C_UPPER_DEPS := | ||
|
||
# Every subdirectory with source files must be described here | ||
SUBDIRS := \ | ||
src \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
################################################################################ | ||
# Automatically-generated file. Do not edit! | ||
################################################################################ | ||
|
||
# Add inputs and outputs from these tool invocations to the build variables | ||
CPP_SRCS += \ | ||
../src/client.cpp \ | ||
../src/network.cpp \ | ||
../src/worker.cpp | ||
|
||
CC_SRCS += \ | ||
../src/http_client.cc \ | ||
../src/listenfd.cc \ | ||
../src/main.cc \ | ||
../src/main_event.cc \ | ||
../src/socket_util.cc \ | ||
../src/timers.cc | ||
|
||
OBJS += \ | ||
./src/client.o \ | ||
./src/http_client.o \ | ||
./src/listenfd.o \ | ||
./src/main.o \ | ||
./src/main_event.o \ | ||
./src/network.o \ | ||
./src/socket_util.o \ | ||
./src/timers.o \ | ||
./src/worker.o | ||
|
||
CC_DEPS += \ | ||
./src/http_client.d \ | ||
./src/listenfd.d \ | ||
./src/main.d \ | ||
./src/main_event.d \ | ||
./src/socket_util.d \ | ||
./src/timers.d | ||
|
||
CPP_DEPS += \ | ||
./src/client.d \ | ||
./src/network.d \ | ||
./src/worker.d | ||
|
||
|
||
# Each subdirectory must supply rules for building sources it contributes | ||
src/%.o: ../src/%.cpp | ||
@echo 'Building file: $<' | ||
@echo 'Invoking: GCC C++ Compiler' | ||
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" | ||
@echo 'Finished building: $<' | ||
@echo ' ' | ||
|
||
src/%.o: ../src/%.cc | ||
@echo 'Building file: $<' | ||
@echo 'Invoking: GCC C++ Compiler' | ||
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" | ||
@echo 'Finished building: $<' | ||
@echo ' ' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "network.h" | ||
|
||
extern PFdProcess gFdProcess[MAX_FD]; | ||
|
||
uint64_t sumread = 0; | ||
|
||
int client_readfun(int epoll, int fd, timer_link* timer) { | ||
char buff[1024]; | ||
// Log << "read on call" << std::endl; | ||
while (true) { | ||
|
||
int len = read(fd, buff, sizeof(buff) - 1); | ||
if (len == -1) { | ||
buff[0] = 0; | ||
break; | ||
|
||
} else if (len == 0) { | ||
buff[len] = 0; | ||
gFdProcess[fd]->m_activeclose = true; | ||
return -1; | ||
} | ||
|
||
if (len > 0) { | ||
// Log << "."; | ||
sumread += len; | ||
// Log << buff << std::endl; | ||
} | ||
} | ||
return 1; | ||
} | ||
|
||
int client_writefun(int epoll, int fd, timer_link* timer) { return 1; } | ||
|
||
int client_closefun(int epoll, int fd, timer_link* timer) { | ||
gFdProcess[fd]->init(); | ||
::close(fd); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "network.h" | ||
|
||
extern PFdProcess gFdProcess[MAX_FD]; | ||
|
||
extern volatile int jobs; | ||
|
||
int Simconnect(std::string host, int port) { | ||
struct sockaddr_in addr; | ||
int clientfd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (clientfd <= 0) { | ||
return -1; | ||
} | ||
printf(" socket new fd %d \n", clientfd); | ||
if (clientfd >= MAX_FD) { // 太多链接 系统无法接受 | ||
close(clientfd); | ||
return -2; | ||
} | ||
|
||
memset(&addr, 0, sizeof(struct sockaddr_in)); | ||
addr.sin_family = AF_INET; | ||
addr.sin_addr.s_addr = inet_addr(host.c_str()); | ||
addr.sin_port = htons(port); | ||
|
||
set_noblock(clientfd); | ||
|
||
int i = connect(clientfd, (struct sockaddr *)&addr, sizeof(addr)); | ||
if (i < 0 && errno != EINPROGRESS) { | ||
Log << "errno " << errno << " " << strerror(errno) << std::endl; | ||
close(clientfd); | ||
return -1; | ||
} else { | ||
|
||
return clientfd; | ||
} | ||
} | ||
|
||
int add_epoll(int epollfd, int fd, bool r, bool w, bool add_or_mod) { | ||
|
||
struct epoll_event ev = {0}; | ||
ev.events |= EPOLLRDHUP; | ||
if (r) ev.events |= EPOLLIN | EPOLLET; | ||
if (w) ev.events |= EPOLLOUT | EPOLLET; | ||
|
||
ev.data.fd = fd; | ||
if (add_or_mod) { | ||
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) { | ||
perror("epoll_ctl: fd "); | ||
exit(-1); | ||
} | ||
} else { | ||
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev) == -1) { | ||
perror("epoll_ctl: fd "); | ||
exit(-1); | ||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef HTTP_CLIENT_H | ||
#define HTTP_CLIENT_H | ||
|
||
#include <string> | ||
|
||
int Simconnect(std::string host, int port); | ||
|
||
int add_epoll(int epollfd, int fd, bool r = true, bool w = true, | ||
bool add_or_mod = true); | ||
|
||
#endif |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "network.h" | ||
#include "socket_util.h" | ||
|
||
#include "timers.h" | ||
|
||
int listenport = 8888; | ||
|
||
pthread_t t_all[MAX_CPU] = {0}; | ||
cpu_set_t mask[MAX_CPU]; | ||
|
||
extern PFdProcess gFdProcess[MAX_FD]; | ||
extern uint64_t sumread; | ||
// 命令管道 | ||
// int sv[MAX_CPU][2] = { 0}; | ||
bool close_fun(int fd) { return true; } | ||
|
||
volatile int jobs = 0; | ||
|
||
struct epoll_event *m_events; | ||
|
||
int epollfd; | ||
|
||
int main(int argc, char *argv[]) { | ||
|
||
// pipe | ||
int num = 50000; | ||
if (argc >= 2) { | ||
num = atol(argv[1]); | ||
} | ||
if (argc >= 3) { | ||
listenport = atol(argv[2]); | ||
} | ||
signal(SIGPIPE, SIG_IGN); // sigpipe 信号屏蔽 | ||
|
||
// bind | ||
m_events = (struct epoll_event *)malloc(MAXEPOLLEVENT * | ||
sizeof(struct epoll_event)); | ||
epollfd = epoll_create(MAXEPOLLEVENT); | ||
|
||
for (int i = 0; i < MAX_FD; ++i) { | ||
PFdProcess p = new FdProcess(); | ||
assert(p); | ||
gFdProcess[i] = p; | ||
} | ||
for (int i = 0; i < num; ++i) { | ||
int fd = Simconnect("172.16.31.208", listenport); | ||
|
||
if (fd < 0) { | ||
// exit(-3); | ||
continue; | ||
} | ||
|
||
add_epoll(epollfd, fd); | ||
|
||
gFdProcess[fd]->m_readfun = client_readfun; | ||
gFdProcess[fd]->m_writefun = client_writefun; | ||
gFdProcess[fd]->m_closefun = client_closefun; | ||
} | ||
|
||
main_thread(NULL); | ||
/* | ||
while (1) { | ||
::sleep(1000); | ||
} | ||
*/ | ||
return 0; | ||
} | ||
|
||
void *main_thread(void *arg) { | ||
|
||
timer_link global_timer; | ||
int outime = 1000; | ||
while (true) { | ||
process_event(epollfd, m_events, outime, &global_timer); | ||
outime = global_timer.get_mintimer(); | ||
if (0 == outime) outime = 1000; | ||
|
||
if (jobs < 0) { | ||
free(m_events); | ||
return 0; | ||
} | ||
} | ||
} |
Oops, something went wrong.