From da1172ca782618101a0034937602057cae1849e7 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 30 十二月 2020 16:51:33 +0800
Subject: [PATCH] update
---
test_net_socket/CMakeLists.txt | 9 ++
/dev/null | 114 --------------------------------------
src/CMakeLists.txt | 12 ---
CMakeLists.txt | 7 ++
4 files changed, 16 insertions(+), 126 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 15ec6a0..7c64d13 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,6 +7,13 @@
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
+# control where the static and shared libraries are built so that on windows
+# we don't need to tinker with the path to run the executable
+set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
+# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
+
+option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/include/usgcommon")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0e9667a..2a320d4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -29,25 +29,17 @@
)
target_include_directories(shm_queue PUBLIC
- "${PROJECT_BINARY_DIR}"
${EXTRA_INCLUDES}
)
target_include_directories(shm_queue PUBLIC
+ ${PROJECT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/shm
${CMAKE_CURRENT_SOURCE_DIR}/queue
${CMAKE_CURRENT_SOURCE_DIR}/socket
)
-
-# state that anybody linking to us needs to include the current source dir
-# to find MathFunctions.h, while we don't.
-target_include_directories(shm_queue INTERFACE
- ${CMAKE_CURRENT_SOURCE_DIR}
- ${CMAKE_CURRENT_SOURCE_DIR}/shm
- ${CMAKE_CURRENT_SOURCE_DIR}/queue
- ${CMAKE_CURRENT_SOURCE_DIR}/socket
- )
+
# install rules
install(TARGETS shm_queue DESTINATION lib)
diff --git a/test_net_socket/CMakeLists.txt b/test_net_socket/CMakeLists.txt
index d9e0d62..08132f7 100644
--- a/test_net_socket/CMakeLists.txt
+++ b/test_net_socket/CMakeLists.txt
@@ -1,7 +1,12 @@
-
+# add command
+add_custom_command(
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/net_mod_socket.sh
+ COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/net_mod_socket.sh ${CMAKE_CURRENT_BINARY_DIR}/net_mod_socket.sh
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/net_mod_socket.sh
+ )
# add the executable
-add_executable(test_net_mod_socket test_net_mod_socket.cpp)
+add_executable(test_net_mod_socket test_net_mod_socket.cpp ${CMAKE_CURRENT_BINARY_DIR}/net_mod_socket.sh)
target_link_libraries(test_net_mod_socket PUBLIC shm_queue ${EXTRA_LIBS} )
add_executable(heart_beat heart_beat.cpp)
diff --git a/test_net_socket/is_seqnum.h b/test_net_socket/is_seqnum.h
deleted file mode 100755
index 5d55d91..0000000
--- a/test_net_socket/is_seqnum.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/*************************************************************************\
-* Copyright (C) Michael Kerrisk, 2019. *
-* *
-* This program is free software. You may use, modify, and redistribute it *
-* under the terms of the GNU General Public License as published by the *
-* Free Software Foundation, either version 3 or (at your option) any *
-* later version. This program is distributed without any warranty. See *
-* the file COPYING.gpl-v3 for details. *
-\*************************************************************************/
-
-/* Listing 59-5 */
-
-/* is_seqnum.h
-
- Header file for is_seqnum_sv.c and is_seqnum_cl.c.
-*/
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <signal.h>
-#include "usg_common.h"
-
-#define PORT_NUM "50000" /* Port number for server */
-
-#define INT_LEN 30 /* Size of string able to hold largest
- integer (including terminating '\n') */
-
-ssize_t
-readLine(int fd, void *buffer, size_t n)
-{
- ssize_t numRead; /* # of bytes fetched by last read() */
- size_t totRead; /* Total bytes read so far */
- char *buf;
- char ch;
-
- if (n <= 0 || buffer == NULL) {
- errno = EINVAL;
- return -1;
- }
-
- buf = (char *)buffer; /* No pointer arithmetic on "void *" */
-
- totRead = 0;
- for (;;) {
- numRead = read(fd, &ch, 1);
-
- if (numRead == -1) {
- if (errno == EINTR) /* Interrupted --> restart read() */
- continue;
- else
- return -1; /* Some other error */
-
- } else if (numRead == 0) { /* EOF */
- if (totRead == 0) /* No bytes read; return 0 */
- return 0;
- else /* Some bytes read; add '\0' */
- break;
-
- } else { /* 'numRead' must be 1 if we get here */
- if (totRead < n - 1) { /* Discard > (n - 1) bytes */
- totRead++;
- *buf++ = ch;
- }
-
- if (ch == '\n')
- break;
- }
- }
-
- *buf = '\0';
- return totRead;
-}
diff --git a/test_net_socket/is_seqnum_cl.cpp b/test_net_socket/is_seqnum_cl.cpp
deleted file mode 100755
index 911c282..0000000
--- a/test_net_socket/is_seqnum_cl.cpp
+++ /dev/null
@@ -1,78 +0,0 @@
-
-
-/* is_seqnum_cl.c
-
- A simple Internet stream socket client. This client requests a sequence
- number from the server.
-
- See also is_seqnum_sv.c.
-*/
-#include <netdb.h>
-#include "is_seqnum.h"
-
-int
-main(int argc, char *argv[])
-{
- char request[1024]; /* Requested length of sequence */
- char response[1024];
- int cfd;
- ssize_t numRead;
- struct addrinfo hints;
- struct addrinfo *result, *rp;
-
- if (argc < 2 || strcmp(argv[1], "--help") == 0)
- printf("%s server-host\n", argv[0]);
-
- /* Call getaddrinfo() to obtain a list of addresses that
- we can try connecting to */
-
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
- hints.ai_family = AF_UNSPEC; /* Allows IPv4 or IPv6 */
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_NUMERICSERV;
-
- if (getaddrinfo(argv[1], PORT_NUM, &hints, &result) != 0)
- err_exit(errno, "getaddrinfo");
-
- /* Walk through returned list until we find an address structure
- that can be used to successfully connect a socket */
-
- for (rp = result; rp != NULL; rp = rp->ai_next) {
- cfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
- if (cfd == -1)
- continue; /* On error, try next address */
-
- if (connect(cfd, rp->ai_addr, rp->ai_addrlen) != -1)
- break; /* Success */
-
- /* Connect failed: close this socket and try next address */
-
- close(cfd);
- }
-
- if (rp == NULL)
- err_exit(errno, "Could not connect socket to any address");
-
- freeaddrinfo(result);
-
- /* Send requested sequence length, with terminating newline */
-
- fputs("request:", stdout);
- if(fgets(request, 1024, stdin) != NULL) {
- if (write(cfd, request, strlen(request)) != strlen(request))
- err_exit(errno, "Partial/failed write (reqLenStr)");
-
- numRead = readLine(cfd, response, 1024);
- if (numRead == -1)
- err_exit(errno, "readLine");
- if (numRead == 0)
- err_exit(errno, "Unexpected EOF from server");
-
- printf("response: %s", response); /* Includes '\n' */
- puts("request:");
- }
- exit(EXIT_SUCCESS);
-}
diff --git a/test_net_socket/is_seqnum_sv.cpp b/test_net_socket/is_seqnum_sv.cpp
deleted file mode 100755
index 75ebb91..0000000
--- a/test_net_socket/is_seqnum_sv.cpp
+++ /dev/null
@@ -1,114 +0,0 @@
-/* is_seqnum_sv.c
-
- A simple Internet stream socket server. Our service is to provide
- unique sequence numbers to clients.
-
- Usage: is_seqnum_sv [init-seq-num]
- (default = 0)
-
- See also is_seqnum_cl.c.
-*/
-#define _BSD_SOURCE /* To get definitions of NI_MAXHOST and
- NI_MAXSERV from <netdb.h> */
-#include <netdb.h>
-#include "is_seqnum.h"
-
-#define BACKLOG 50
-
-int
-main(int argc, char *argv[])
-{
- char line[1024]; /* Length of requested sequence */
- struct sockaddr_storage claddr;
- int lfd, cfd, optval;
- socklen_t addrlen;
- struct addrinfo hints;
- struct addrinfo *result, *rp;
-#define ADDRSTRLEN (NI_MAXHOST + NI_MAXSERV + 10)
- char addrStr[ADDRSTRLEN];
- char host[NI_MAXHOST];
- char service[NI_MAXSERV];
-
- if (argc > 1 && strcmp(argv[1], "--help") == 0)
- printf("%s [init-seq-num]\n", argv[0]);
-
-
- /* Ignore the SIGPIPE signal, so that we find out about broken connection
- errors via a failure from write(). */
-
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) err_exit(errno, "signal");
-
- /* Call getaddrinfo() to obtain a list of addresses that
- we can try binding to */
-
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_family = AF_UNSPEC; /* Allows IPv4 or IPv6 */
- hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
- /* Wildcard IP address; service name is numeric */
-
- if (getaddrinfo(NULL, PORT_NUM, &hints, &result) != 0)
- err_exit(errno, "getaddrinfo");
-
- /* Walk through returned list until we find an address structure
- that can be used to successfully create and bind a socket */
-
- optval = 1;
- for (rp = result; rp != NULL; rp = rp->ai_next) {
- lfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
- if (lfd == -1)
- continue; /* On error, try next address */
-
- if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1)
- err_exit(errno, "setsockopt");
-
- if (bind(lfd, rp->ai_addr, rp->ai_addrlen) == 0)
- break; /* Success */
-
- /* bind() failed: close this socket and try next address */
- close(lfd);
- }
-
- if (rp == NULL)
- err_exit(errno, "Could not bind socket to any address");
-
- if (listen(lfd, BACKLOG) == -1)
- err_exit(errno, "listen");
-
- freeaddrinfo(result);
-
- for (;;) { /* Handle clients iteratively */
-
- /* Accept a client connection, obtaining client's address */
-
- addrlen = sizeof(struct sockaddr_storage);
- cfd = accept(lfd, (struct sockaddr *) &claddr, &addrlen);
- if (cfd == -1) {
- err_msg(errno, "accept");
- continue;
- }
-
- if (getnameinfo((struct sockaddr *) &claddr, addrlen,
- host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
- snprintf(addrStr, ADDRSTRLEN, "(%s, %s)", host, service);
- else
- snprintf(addrStr, ADDRSTRLEN, "(?UNKNOWN?)");
- printf("Connection from %s\n", addrStr);
-
- /* Read client request, send sequence number back */
-
- if (readLine(cfd, line, 1024) <= 0) {
- close(cfd);
- continue; /* Failed read; skip request */
- }
-
- if (write(cfd, line, strlen(line)) != strlen(line))
- fprintf(stderr, "Error on write");
-
- if (close(cfd) == -1)
- err_msg(errno, "close");
- }
-}
--
Gitblit v1.8.0