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.
This commit is contained in:
Markus Armbruster 2009-04-18 22:16:00 +02:00
parent c8231b120b
commit 1153d9c995
12 changed files with 68 additions and 117 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -39,7 +39,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#ifdef _WIN32
#include "w32types.h"
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

View file

@ -45,6 +45,7 @@
#else
#include <process.h>
#include <io.h>
#include "w32types.h"
#endif
#include "linebuf.h"
#include "misc.h"

View file

@ -36,16 +36,7 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
#include <sys/uio.h>
#include <unistd.h>
#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"
/*

View file

@ -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 */

View file

@ -39,19 +39,10 @@
#include <ws2tcpip.h>
#include <windows.h>
#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);

View file

@ -35,7 +35,7 @@
#define SYS_UIO_H
#include <sys/types.h>
#include "w32misc.h"
#include "w32types.h"
struct iovec {
/* Base address of a memory region for input or output. */

View file

@ -50,8 +50,7 @@
#include <io.h>
#include <direct.h>
#include <sys/stat.h>
#include "w32misc.h"
#include "w32types.h"
/*
* w32file.c

View file

@ -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);

46
src/lib/w32/w32types.h Normal file
View file

@ -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