From 1153d9c99528062368d2645043069dd0f0603aec Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sat, 18 Apr 2009 22:16:00 +0200 Subject: [PATCH] Use src/lib/w32/w32io.c for client Replaces w32_writev_socket() and w32_readv_fd(). Split w32types.h off w32misc.h, to avoid putting irrelevant stuff into client tarball. --- Make.mk | 3 ++ src/client/Makefile.in | 11 +++++- src/client/configure.ac | 4 ++ src/client/expect.c | 4 +- src/client/play.c | 1 + src/client/ringbuf.c | 9 ----- src/client/sysdep_w32.c | 81 ----------------------------------------- src/client/sysdep_w32.h | 13 ------- src/lib/w32/sys/uio.h | 2 +- src/lib/w32/unistd.h | 3 +- src/lib/w32/w32misc.h | 8 ---- src/lib/w32/w32types.h | 46 +++++++++++++++++++++++ 12 files changed, 68 insertions(+), 117 deletions(-) create mode 100644 src/lib/w32/w32types.h diff --git a/Make.mk b/Make.mk index c385a6240..afa731c18 100644 --- a/Make.mk +++ b/Make.mk @@ -74,6 +74,7 @@ gamedir := $(localstatedir)/empire builtindir := $(datadir)/empire/builtin einfodir := $(datadir)/empire/info.nr ehtmldir := $(datadir)/empire/info.html +client/w32 := sys/uio.h w32io.c w32types.h # How to substitute Autoconf output variables # Recursively expanded so that $@ and $< work. @@ -133,6 +134,7 @@ endif ifeq ($(empthread),Windows) # really: W32, regardless of thread package libs += lib/libw32.a +$(client): lib/libw32.a endif # Cleanliness @@ -333,6 +335,7 @@ dist-client: $(cli_distgen) $(notdir $(filter src/client/%, $(src)) $(cli_distgen)) \ -C $(srcdir)/include proto.h version.h \ -C $(srcdir)/src/lib/global version.c \ + -C $(srcdir)/src/lib $(addprefix w32/, $(client/w32)) \ -C $(srcdir)/man empire.6 \ -C $(srcdir) COPYING INSTALL install-sh diff --git a/src/client/Makefile.in b/src/client/Makefile.in index 9b5e5a568..004913511 100644 --- a/src/client/Makefile.in +++ b/src/client/Makefile.in @@ -35,6 +35,7 @@ CC = @CC@ CFLAGS = @CFLAGS@ @CPPFLAGS@ @DEFS@ -I. LDFLAGS = @LDFLAGS@ +LIBOBJS := @LIBOBJS@ LIBS = @LIBS@ E = @EXEEXT@ O = @OBJEXT@ @@ -54,7 +55,8 @@ VPATH = @srcdir@ prog = empire$E obj = expect.$O host.$O ipglob.$O linebuf.$O login.$O main.$O play.$O \ -ringbuf.$O secure.$O servcmd.$O sysdep_w32.$O termlib.$O version.$O +ringbuf.$O secure.$O servcmd.$O sysdep_w32.$O termlib.$O version.$O \ +$(LIBOBJS) all: $(prog) @@ -85,10 +87,15 @@ linebuf.$O: linebuf.h login.$O: misc.h sysdep_w32.h proto.h main.$O: misc.h sysdep_w32.h version.h play.$O: linebuf.h misc.h sysdep_w32.h proto.h ringbuf.h secure.h -ringbuf.$O: misc.h sysdep_w32.h ringbuf.h +ringbuf.$O: ringbuf.h secure.$O: ringbuf.h secure.h servcmd.$O: misc.h sysdep_w32.h proto.h secure.h sysdep_w32.$O: misc.h sysdep_w32.h termlib.$O: misc.h version.$O: version.h $(obj): config.h + +expect.$O: w32/w32types.h +play.$O: w32/w32types.h +ringbuf.$O: w32/sys/uio.h w32/w32types.h +w32/w32io.$O: misc.h w32/sys/uio.h w32/w32types.h diff --git a/src/client/configure.ac b/src/client/configure.ac index 7a38f4393..9625e435f 100644 --- a/src/client/configure.ac +++ b/src/client/configure.ac @@ -54,6 +54,9 @@ MY_WINDOWS_API ### Checks for libraries. LIBS="$LIBS_SOCKETS $LIBS" LIB_SOCKET_NSL +if test "$Windows_API" = yes; then + AC_LIBOBJ([w32/w32io]) +fi ### Checks for header files. @@ -62,6 +65,7 @@ LIB_SOCKET_NSL ### Checks for typedefs, structures, and compiler characteristics. if test "$Windows_API" = yes; then + CPPFLAGS="$CPPFLAGS -Iw32" CFLAGS="$CFLAGS -mthreads" fi diff --git a/src/client/expect.c b/src/client/expect.c index 2660b97d9..63735ffa0 100644 --- a/src/client/expect.c +++ b/src/client/expect.c @@ -39,7 +39,9 @@ #include #include #include -#ifndef _WIN32 +#ifdef _WIN32 +#include "w32types.h" +#else #include #include #include diff --git a/src/client/play.c b/src/client/play.c index 7f5e8f45a..313dbeb52 100644 --- a/src/client/play.c +++ b/src/client/play.c @@ -45,6 +45,7 @@ #else #include #include +#include "w32types.h" #endif #include "linebuf.h" #include "misc.h" diff --git a/src/client/ringbuf.c b/src/client/ringbuf.c index 72ee7ba0b..bae84fffc 100644 --- a/src/client/ringbuf.c +++ b/src/client/ringbuf.c @@ -36,16 +36,7 @@ #include #include #include -#ifndef _WIN32 #include -#include -#else -#define readv(fd, iov, iovcnt) \ - w32_readv_fd((fd), (iov), (iovcnt)) -#define writev(fd, iov, iovcnt) \ - w32_writev_socket((fd), (iov), (iovcnt)) -#endif -#include "misc.h" #include "ringbuf.h" /* diff --git a/src/client/sysdep_w32.c b/src/client/sysdep_w32.c index 61c129ecf..9fc08f2a6 100644 --- a/src/client/sysdep_w32.c +++ b/src/client/sysdep_w32.c @@ -185,43 +185,6 @@ w32_recv(int sockfd, void *buffer, size_t buf_size, int flags) return result; } -/* - * POSIX compatible writev() replacement specialized to sockets - * Modelled after the GNU's libc/sysdeps/posix/writev.c - */ -ssize_t -w32_writev_socket(int sockfd, const struct iovec *iov, int iovcnt) -{ - SOCKET sock = W32_FD_TO_SOCKET(sockfd); - int i; - char *buffer, *buffer_location; - size_t total_bytes = 0; - int bytes_written; - - for (i = 0; i < iovcnt; i++) - total_bytes += iov[i].iov_len; - - buffer = malloc(total_bytes); - if (buffer == NULL && total_bytes != 0) { - errno = ENOMEM; - return -1; - } - - buffer_location = buffer; - for (i = 0; i < iovcnt; i++) { - memcpy(buffer_location, iov[i].iov_base, iov[i].iov_len); - buffer_location += iov[i].iov_len; - } - - bytes_written = send(sock, buffer, total_bytes, 0); - free(buffer); - if (bytes_written == SOCKET_ERROR) { - w32_set_winsock_errno(); - return -1; - } - return bytes_written; -} - /* * POSIX compatible send() replacement */ @@ -260,48 +223,4 @@ w32_close(int fd) return _close(fd); } -/* - * POSIX compatible readv() replacement specialized to files. - * Modelled after the GNU's libc/sysdeps/posix/readv.c - */ -ssize_t -w32_readv_fd(int fd, const struct iovec *iov, int iovcnt) -{ - int i; - char *buffer, *buffer_location; - size_t total_bytes = 0; - int bytes_read; - size_t bytes_left; - - for (i = 0; i < iovcnt; i++) { - total_bytes += iov[i].iov_len; - } - - buffer = malloc(total_bytes); - if (buffer == NULL && total_bytes != 0) { - errno = ENOMEM; - return -1; - } - - bytes_read = read(fd, buffer, total_bytes); - if (bytes_read < 0) - return -1; - - bytes_left = bytes_read; - buffer_location = buffer; - for (i = 0; i < iovcnt; i++) { - size_t copy = MIN(iov[i].iov_len, bytes_left); - - memcpy(iov[i].iov_base, buffer_location, copy); - - buffer_location += copy; - bytes_left -= copy; - if (bytes_left == 0) - break; - } - - free(buffer); - return bytes_read; -} - #endif /* _WIN32 */ diff --git a/src/client/sysdep_w32.h b/src/client/sysdep_w32.h index 15b3afe1d..0ab4c303d 100644 --- a/src/client/sysdep_w32.h +++ b/src/client/sysdep_w32.h @@ -39,19 +39,10 @@ #include #include -#ifdef _MSC_VER -typedef int __w64 ssize_t; -#endif - struct passwd { char *pw_name; }; -struct iovec { - void *iov_base; - size_t iov_len; -}; - #define W32_FD_TO_SOCKET(fd) ((SOCKET)_get_osfhandle((fd))) #define W32_SOCKET_TO_FD(fh) (_open_osfhandle((long)(fh), O_RDWR | O_BINARY)) @@ -62,10 +53,6 @@ extern int w32_send(int sockfd, const void *, size_t, int flags); extern int w32_close(int fd); extern int w32_socket(int domain, int type, int protocol); extern int w32_connect(int sockfd, const struct sockaddr *, int addrlen); -extern ssize_t w32_readv_fd(int fd, const struct iovec *iov, - int iovcnt); -extern ssize_t w32_writev_socket(int sockfd, const struct iovec *iov, - int iovcnt); extern struct passwd *w32_getpw(void); extern void w32_sysdep_init(void); diff --git a/src/lib/w32/sys/uio.h b/src/lib/w32/sys/uio.h index 07542b118..5a388a36c 100644 --- a/src/lib/w32/sys/uio.h +++ b/src/lib/w32/sys/uio.h @@ -35,7 +35,7 @@ #define SYS_UIO_H #include -#include "w32misc.h" +#include "w32types.h" struct iovec { /* Base address of a memory region for input or output. */ diff --git a/src/lib/w32/unistd.h b/src/lib/w32/unistd.h index e7ff6a95b..eeb1c6e1b 100644 --- a/src/lib/w32/unistd.h +++ b/src/lib/w32/unistd.h @@ -50,8 +50,7 @@ #include #include #include - -#include "w32misc.h" +#include "w32types.h" /* * w32file.c diff --git a/src/lib/w32/w32misc.h b/src/lib/w32/w32misc.h index ffeff1ed8..f89408323 100644 --- a/src/lib/w32/w32misc.h +++ b/src/lib/w32/w32misc.h @@ -75,14 +75,6 @@ extern long __random(void); extern char *__setstate(char *state); extern void __srandom(unsigned seed); -/* sys/types.h */ -#ifdef _MSC_VER -typedef unsigned short mode_t; -typedef long off_t; -typedef int pid_t; -typedef int __w64 ssize_t; -#endif - /* time.h */ struct tm; extern char *strptime(const char *buf, const char *fmt, struct tm *tm); diff --git a/src/lib/w32/w32types.h b/src/lib/w32/w32types.h new file mode 100644 index 000000000..7479015c9 --- /dev/null +++ b/src/lib/w32/w32types.h @@ -0,0 +1,46 @@ +/* + * Empire - A multi-player, client/server Internet based war game. + * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak, + * Ken Stevens, Steve McClure + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * --- + * + * See files README, COPYING and CREDITS in the root of the source + * tree for related information and legal notices. It is expected + * that future projects/authors will amend these files as needed. + * + * --- + * + * w32types.h: POSIX emulation for WIN32, missing types + * + * Known contributors to this file: + * Ron Koenderink, 2007 + * Markus Armbruster, 2009 + */ + +#ifndef W32TYPES_H +#define W32TYPES_H + +/* sys/types.h */ +#ifdef _MSC_VER +typedef unsigned short mode_t; +typedef long off_t; +typedef int pid_t; +typedef int __w64 ssize_t; +#endif + +#endif -- 2.43.0