From a3de167bc7aafee6a721e43b8c1609d3eda034b8 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 20 Apr 2020 08:42:53 +0800 Subject: [PATCH] For zerocopy research, support delay in server --- trunk/research/msg_zerocopy/Makefile | 8 ++++++++ trunk/research/msg_zerocopy/client.cpp | 4 ++-- trunk/research/msg_zerocopy/server.cpp | 19 +++++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/trunk/research/msg_zerocopy/Makefile b/trunk/research/msg_zerocopy/Makefile index 628939550..cd67c1885 100644 --- a/trunk/research/msg_zerocopy/Makefile +++ b/trunk/research/msg_zerocopy/Makefile @@ -1,4 +1,6 @@ +.PHONY: default clean + default: server client server: server.cpp ../../objs/st/libst.a @@ -6,3 +8,9 @@ server: server.cpp ../../objs/st/libst.a client: client.cpp ../../objs/st/libst.a gcc -g -O0 -I../../objs/st/ $^ -o $@ + +../../objs/st/libst.a: ../../Makefile + (cd ../../ && $(MAKE) st) + +clean: + rm -f server client ../../objs/st/libst.a diff --git a/trunk/research/msg_zerocopy/client.cpp b/trunk/research/msg_zerocopy/client.cpp index a4126145d..acae19459 100644 --- a/trunk/research/msg_zerocopy/client.cpp +++ b/trunk/research/msg_zerocopy/client.cpp @@ -16,9 +16,9 @@ int main(int argc, char** argv) exit(-1); } - int port = atoi(argv[2]); char* host = argv[1]; - bool pong = !strcmp(argv[3], "pong"); + int port = atoi(argv[2]); + bool pong = !strcmp(argv[3], "true"); printf("Server listen %s:%d, pong %d\n", host, port, pong); assert(!st_set_eventsys(ST_EVENTSYS_ALT)); diff --git a/trunk/research/msg_zerocopy/server.cpp b/trunk/research/msg_zerocopy/server.cpp index 57978c4be..417e8c529 100644 --- a/trunk/research/msg_zerocopy/server.cpp +++ b/trunk/research/msg_zerocopy/server.cpp @@ -9,17 +9,20 @@ int main(int argc, char** argv) { - if (argc < 4) { - printf("Usage: %s \n", argv[0]); + if (argc < 5) { + printf("Usage: %s \n", argv[0]); + printf(" pong Whether response pong, true|false\n"); + printf(" delay The delay in ms to response pong.\n"); printf("For example:\n"); - printf(" %s 0.0.0.0 8000 true\n", argv[0]); + printf(" %s 0.0.0.0 8000 true 100\n", argv[0]); exit(-1); } - int port = atoi(argv[2]); char* host = argv[1]; - bool pong = !strcmp(argv[3], "pong"); - printf("Server listen %s:%d, pong %d\n", host, port, pong); + int port = atoi(argv[2]); + bool pong = !strcmp(argv[3], "true"); + int delay = ::atoi(argv[4]); + printf("Server listen %s:%d, pong %d, delay: %dms\n", host, port, pong, delay); assert(!st_set_eventsys(ST_EVENTSYS_ALT)); assert(!st_init()); @@ -74,6 +77,10 @@ int main(int argc, char** argv) msg.msg_iov->iov_len = 5; if (pong) { + if (delay > 0) { + st_usleep(delay * 1000); + } + r0 = st_sendmsg(stfd, &msg, 0, ST_UTIME_NO_TIMEOUT); assert(r0 > 0); printf("Pong %s:%d %d bytes, flags %#x, %s\n", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port), r0,