This commit is contained in:
manojampalam 2016-03-03 23:04:49 -08:00
parent ab8d9f0eda
commit d721cce789
4 changed files with 1224 additions and 1187 deletions

View File

@ -14,9 +14,10 @@
#define errno_from_Win32LastError() errno_from_Win32Error(GetLastError())
static int errno_from_Win32Error(int win32_error)
static int
errno_from_Win32Error(int win32_error)
{
switch (win32_error){
switch (win32_error) {
case ERROR_ACCESS_DENIED:
return EACCES;
case ERROR_OUTOFMEMORY:
@ -30,9 +31,10 @@ static int errno_from_Win32Error(int win32_error)
}
}
int pipe_number = 0;
static int pipe_number = 0;
int fileio_pipe(struct w32_io* pio[2]) {
int
fileio_pipe(struct w32_io* pio[2]) {
HANDLE read_handle = INVALID_HANDLE_VALUE, write_handle = INVALID_HANDLE_VALUE;
struct w32_io *pio_read = NULL, *pio_write = NULL;
char pipe_name[MAX_PATH];
@ -123,7 +125,8 @@ struct createFile_flags {
DWORD dwFlagsAndAttributes;
};
static int createFile_flags_setup(int flags, int mode, struct createFile_flags* cf_flags){
static int
createFile_flags_setup(int flags, int mode, struct createFile_flags* cf_flags) {
//check flags
int rwflags = flags & 0xf;
@ -188,7 +191,8 @@ static int createFile_flags_setup(int flags, int mode, struct createFile_flags*
return 0;
}
struct w32_io* fileio_open(const char *pathname, int flags, int mode) {
struct w32_io*
fileio_open(const char *pathname, int flags, int mode) {
struct w32_io* pio = NULL;
struct createFile_flags cf_flags;
HANDLE handle;
@ -244,7 +248,8 @@ VOID CALLBACK ReadCompletionRoutine(
#define READ_BUFFER_SIZE 100*1024
int fileio_ReadFileEx(struct w32_io* pio) {
int
fileio_ReadFileEx(struct w32_io* pio) {
if (pio->read_details.buf == NULL)
{
@ -272,7 +277,8 @@ int fileio_ReadFileEx(struct w32_io* pio) {
return 0;
}
int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
int
fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
int bytes_copied;
if ((pio->type == PIPE_FD) && (pio->internal.state == PIPE_WRITE_END)) {
@ -351,7 +357,8 @@ VOID CALLBACK WriteCompletionRoutine(
}
#define WRITE_BUFFER_SIZE 100*1024
int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
int
fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
int bytes_copied;
if ((pio->type == PIPE_FD) && (pio->internal.state == PIPE_READ_END)) {
@ -409,7 +416,7 @@ int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
return -1;
}
}
if (!pio->write_details.pending && pio->write_details.error){
if (!pio->write_details.pending && pio->write_details.error) {
errno = errno_from_Win32Error(pio->write_details.error);
pio->write_details.error = 0;
debug("ERROR:%d", errno);
@ -430,7 +437,8 @@ int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
}
int fileio_fstat(struct w32_io* pio, struct stat *buf) {
int
fileio_fstat(struct w32_io* pio, struct stat *buf) {
errno = ENOTSUP;
return -1;
@ -446,12 +454,14 @@ int fileio_fstat(struct w32_io* pio, struct stat *buf) {
}
int fileio_isatty(struct w32_io* pio) {
int
fileio_isatty(struct w32_io* pio) {
return (GetFileType(pio->handle) == FILE_TYPE_CHAR) ? TRUE : FALSE;
}
FILE* fileio_fdopen(struct w32_io* pio, const char *mode) {
FILE*
fileio_fdopen(struct w32_io* pio, const char *mode) {
int fd_flags = 0;
debug2("pio:%p", pio);
@ -489,7 +499,8 @@ FILE* fileio_fdopen(struct w32_io* pio, const char *mode) {
return _fdopen(fd, mode);
}
int fileio_on_select(struct w32_io* pio, BOOL rd) {
int
fileio_on_select(struct w32_io* pio, BOOL rd) {
if (!rd)
return 0;
@ -501,7 +512,8 @@ int fileio_on_select(struct w32_io* pio, BOOL rd) {
}
int fileio_close(struct w32_io* pio) {
int
fileio_close(struct w32_io* pio) {
debug2("pio:%p", pio);
CancelIo(pio->handle);
//let queued APCs (if any) drain
@ -519,8 +531,9 @@ int fileio_close(struct w32_io* pio) {
return 0;
}
BOOL fileio_is_io_available(struct w32_io* pio, BOOL rd) {
if (rd){
BOOL
fileio_is_io_available(struct w32_io* pio, BOOL rd) {
if (rd) {
if (pio->read_details.remaining || pio->read_details.error)
return TRUE;
else

View File

@ -16,7 +16,8 @@
// - time out (errno = ETIMEOUT)
// Returns 0 on IO completion and -1 on rest
// if milli_seconds is 0, this function returns 0, its called with 0 to execute any scheduled APCs
int wait_for_any_event(HANDLE* events, int num_events, DWORD milli_seconds)
int
wait_for_any_event(HANDLE* events, int num_events, DWORD milli_seconds)
{
//todo - implement signal catching and handling
if (num_events)

View File

@ -15,7 +15,8 @@
#define errno_from_WSALastError() errno_from_WSAError(WSAGetLastError())
static int errno_from_WSAError(int wsaerrno)
static
int errno_from_WSAError(int wsaerrno)
{
switch (wsaerrno) {
case WSAEWOULDBLOCK:
@ -31,12 +32,14 @@ static int errno_from_WSAError(int wsaerrno)
}
}
int socketio_initialize() {
int
socketio_initialize() {
WSADATA wsaData = { 0 };
return WSAStartup(MAKEWORD(2, 2), &wsaData);
}
int socketio_done() {
int
socketio_done() {
WSACleanup();
return 0;
}
@ -49,7 +52,8 @@ struct acceptEx_context {
};
int socketio_acceptEx(struct w32_io* pio) {
int
socketio_acceptEx(struct w32_io* pio) {
struct acceptEx_context *context;
debug2("io:%p", pio);
@ -124,7 +128,8 @@ int socketio_acceptEx(struct w32_io* pio) {
return 0;
}
void CALLBACK WSARecvCompletionRoutine(
void
CALLBACK WSARecvCompletionRoutine(
IN DWORD dwError,
IN DWORD cbTransferred,
IN LPWSAOVERLAPPED lpOverlapped,
@ -142,7 +147,8 @@ void CALLBACK WSARecvCompletionRoutine(
pio->read_details.pending = FALSE;
}
int socketio_WSARecv(struct w32_io* pio, BOOL* completed) {
int
socketio_WSARecv(struct w32_io* pio, BOOL* completed) {
int ret = 0;
WSABUF wsabuf;
DWORD recv_flags = 0;
@ -197,7 +203,8 @@ int socketio_WSARecv(struct w32_io* pio, BOOL* completed) {
return 0;
}
struct w32_io* socketio_socket(int domain, int type, int protocol) {
struct w32_io*
socketio_socket(int domain, int type, int protocol) {
struct w32_io *pio = (struct w32_io*)malloc(sizeof(struct w32_io));
if (!pio) {
errno = ENOMEM;
@ -227,8 +234,9 @@ struct w32_io* socketio_socket(int domain, int type, int protocol) {
return ret; \
} while (0)
int socketio_setsockopt(struct w32_io* pio, int level, int optname, const char* optval, int optlen) {
if ( (optname == SO_KEEPALIVE) || (optname == SO_REUSEADDR) || (optname == TCP_NODELAY) || (optname == IPV6_V6ONLY) )
int
socketio_setsockopt(struct w32_io* pio, int level, int optname, const char* optval, int optlen) {
if ((optname == SO_KEEPALIVE) || (optname == SO_REUSEADDR) || (optname == TCP_NODELAY) || (optname == IPV6_V6ONLY))
SET_ERRNO_ON_ERROR(setsockopt(pio->sock, level, optname, optval, optlen));
else {
debug("ERROR: unsupported optname:%d io:%p", optname, pio);
@ -237,19 +245,23 @@ int socketio_setsockopt(struct w32_io* pio, int level, int optname, const char*
}
}
int socketio_getsockopt(struct w32_io* pio, int level, int optname, char* optval, int* optlen) {
int
socketio_getsockopt(struct w32_io* pio, int level, int optname, char* optval, int* optlen) {
SET_ERRNO_ON_ERROR(getsockopt(pio->sock, level, optname, optval, optlen));
}
int socketio_getsockname(struct w32_io* pio, struct sockaddr* name, int* namelen) {
int
socketio_getsockname(struct w32_io* pio, struct sockaddr* name, int* namelen) {
SET_ERRNO_ON_ERROR(getsockname(pio->sock, name, namelen));
}
int socketio_getpeername(struct w32_io* pio, struct sockaddr* name, int* namelen) {
int
socketio_getpeername(struct w32_io* pio, struct sockaddr* name, int* namelen) {
SET_ERRNO_ON_ERROR(getpeername(pio->sock, name, namelen));
}
int socketio_listen(struct w32_io* pio, int backlog) {
int
socketio_listen(struct w32_io* pio, int backlog) {
if (SOCKET_ERROR == listen(pio->sock, backlog)) {
errno = errno_from_WSALastError();
debug("ERROR:%d io:%p", errno, pio);
@ -259,11 +271,13 @@ int socketio_listen(struct w32_io* pio, int backlog) {
return 0;
}
int socketio_bind(struct w32_io* pio, const struct sockaddr *name, int namelen) {
int
socketio_bind(struct w32_io* pio, const struct sockaddr *name, int namelen) {
SET_ERRNO_ON_ERROR(bind(pio->sock, name, namelen));
}
int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
int
socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
BOOL completed = FALSE;
debug2("io:%p", pio);
@ -389,7 +403,8 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
}
void CALLBACK WSASendCompletionRoutine(
void
CALLBACK WSASendCompletionRoutine(
IN DWORD dwError,
IN DWORD cbTransferred,
IN LPWSAOVERLAPPED lpOverlapped,
@ -404,7 +419,8 @@ void CALLBACK WSASendCompletionRoutine(
pio->write_details.pending = FALSE;
}
int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
int
socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
int ret = 0;
WSABUF wsabuf;
@ -518,11 +534,13 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
}
int socketio_shutdown(struct w32_io* pio, int how) {
int
socketio_shutdown(struct w32_io* pio, int how) {
SET_ERRNO_ON_ERROR(shutdown(pio->sock, how));
}
int socketio_close(struct w32_io* pio) {
int
socketio_close(struct w32_io* pio) {
debug2("io:%p", pio);
closesocket(pio->sock);
//wait for pending io to abort
@ -560,7 +578,8 @@ int socketio_close(struct w32_io* pio) {
return 0;
}
struct w32_io* socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* addrlen) {
struct w32_io*
socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* addrlen) {
struct w32_io *accept_io = NULL;
int iResult = 0;
struct acceptEx_context* context;
@ -637,7 +656,8 @@ on_error:
return NULL;
}
int socketio_connectex(struct w32_io* pio, const struct sockaddr* name, int namelen) {
int
socketio_connectex(struct w32_io* pio, const struct sockaddr* name, int namelen) {
struct sockaddr_in tmp_addr4;
struct sockaddr_in6 tmp_addr6;
@ -713,7 +733,8 @@ int socketio_connectex(struct w32_io* pio, const struct sockaddr* name, int name
return 0;
}
int socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namelen) {
int
socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namelen) {
if (pio->write_details.pending == FALSE)
{
@ -762,7 +783,8 @@ int socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namele
return 0;
}
BOOL socketio_is_io_available(struct w32_io* pio, BOOL rd) {
BOOL
socketio_is_io_available(struct w32_io* pio, BOOL rd) {
if ((pio->internal.state == SOCK_LISTENING) || (pio->internal.state == SOCK_CONNECTING)) {
DWORD numBytes = 0;
@ -797,7 +819,8 @@ BOOL socketio_is_io_available(struct w32_io* pio, BOOL rd) {
}
/*start async io (if needed) for accept and recv*/
int socketio_on_select(struct w32_io* pio, BOOL rd) {
int
socketio_on_select(struct w32_io* pio, BOOL rd) {
enum w32_io_sock_state sock_state = pio->internal.state;

View File

@ -267,7 +267,7 @@ w32_shutdown(int fd, int how) {
int
w32_pipe(int *pfds){
w32_pipe(int *pfds) {
int read_index, write_index;
struct w32_io* pio[2];
@ -377,7 +377,7 @@ w32_fcntl(int fd, int cmd, ... /* arg */) {
CHECK_FD(fd);
switch (cmd){
switch (cmd) {
case F_GETFL:
return fd_table.w32_ios[fd]->fd_status_flags;
case F_SETFL:
@ -409,7 +409,7 @@ w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const
memset(&read_ready_fds, 0, sizeof(fd_set));
memset(&write_ready_fds, 0, sizeof(fd_set));
if (fds > MAX_FDS ) {
if (fds > MAX_FDS) {
errno = EINVAL;
debug("ERROR: fds: %d", fds);
return -1;