This commit is contained in:
manojampalam 2016-02-27 14:53:57 -08:00
parent 9fe3ef60e6
commit d42ef60f9c
8 changed files with 36 additions and 47 deletions

View File

@ -147,9 +147,6 @@
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="stdafx.h" /> <ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" /> <ClInclude Include="targetver.h" />

View File

@ -14,9 +14,6 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="stdafx.h"> <ClInclude Include="stdafx.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>

View File

@ -1,6 +1,7 @@
#define debug(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__) #define debug(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__)
#define debug2(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__)
void debug_initialize(); void debug_initialize();
void debug_done(); void debug_done();

View File

@ -233,7 +233,7 @@ VOID CALLBACK ReadCompletionRoutine(
_Inout_ LPOVERLAPPED lpOverlapped _Inout_ LPOVERLAPPED lpOverlapped
) { ) {
struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, read_overlapped)); struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, read_overlapped));
debug("pio:%p, pending_state:%d, error:%d, transferred:%d", pio, pio->read_details.pending, dwErrorCode, dwNumberOfBytesTransfered); debug2("pio:%p, pending_state:%d, error:%d, transferred:%d", pio, pio->read_details.pending, dwErrorCode, dwNumberOfBytesTransfered);
pio->read_details.error = dwErrorCode; pio->read_details.error = dwErrorCode;
pio->read_details.remaining = dwNumberOfBytesTransfered; pio->read_details.remaining = dwNumberOfBytesTransfered;
pio->read_details.completed = 0; pio->read_details.completed = 0;
@ -276,7 +276,7 @@ int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
//if read is pending //if read is pending
if (pio->read_details.pending) { if (pio->read_details.pending) {
errno = EAGAIN; errno = EAGAIN;
debug("IO is already pending"); debug2("IO is already pending");
return -1; return -1;
} }
@ -285,7 +285,7 @@ int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
if (-1 == fileio_ReadFileEx(pio)) { if (-1 == fileio_ReadFileEx(pio)) {
if ((pio->type == PIPE_FD) && (errno == ERROR_NEGATIVE_SEEK)) {//write end of the pipe closed if ((pio->type == PIPE_FD) && (errno == ERROR_NEGATIVE_SEEK)) {//write end of the pipe closed
debug("no more data"); debug2("no more data");
errno = 0; errno = 0;
return 0; return 0;
} }
@ -303,7 +303,7 @@ int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
} }
else if (pio->read_details.pending) { else if (pio->read_details.pending) {
errno = EAGAIN; errno = EAGAIN;
debug("IO is pending"); debug2("IO is pending");
return -1; return -1;
} }
} }
@ -312,7 +312,7 @@ int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
errno = errno_from_Win32Error(pio->read_details.error); errno = errno_from_Win32Error(pio->read_details.error);
if ((errno == ERROR_BROKEN_PIPE) || //write end of the pipe is closed or pipe broken if ((errno == ERROR_BROKEN_PIPE) || //write end of the pipe is closed or pipe broken
(errno == ERROR_HANDLE_EOF)) { //end of file reached (errno == ERROR_HANDLE_EOF)) { //end of file reached
debug("no more data"); debug2("no more data");
errno = 0; errno = 0;
pio->read_details.error = 0; pio->read_details.error = 0;
return 0; return 0;
@ -326,7 +326,7 @@ int fileio_read(struct w32_io* pio, void *dst, unsigned int max) {
memcpy(dst, pio->read_details.buf + pio->read_details.completed, bytes_copied); memcpy(dst, pio->read_details.buf + pio->read_details.completed, bytes_copied);
pio->read_details.remaining -= bytes_copied; pio->read_details.remaining -= bytes_copied;
pio->read_details.completed += bytes_copied; pio->read_details.completed += bytes_copied;
debug("read %d bytes", bytes_copied); debug2("read %d bytes", bytes_copied);
return bytes_copied; return bytes_copied;
} }
@ -336,7 +336,7 @@ VOID CALLBACK WriteCompletionRoutine(
_Inout_ LPOVERLAPPED lpOverlapped _Inout_ LPOVERLAPPED lpOverlapped
) { ) {
struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, write_overlapped)); struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, write_overlapped));
debug("pio:%p, pending_state:%d, remaining:%d, error:%d, transferred:%d", pio, pio->write_details.pending, pio->write_details.remaining, dwErrorCode, dwNumberOfBytesTransfered); debug2("pio:%p, pending_state:%d, remaining:%d, error:%d, transferred:%d", pio, pio->write_details.pending, pio->write_details.remaining, dwErrorCode, dwNumberOfBytesTransfered);
pio->write_details.error = dwErrorCode; pio->write_details.error = dwErrorCode;
//assert that remaining == dwNumberOfBytesTransfered //assert that remaining == dwNumberOfBytesTransfered
pio->write_details.remaining -= dwNumberOfBytesTransfered; pio->write_details.remaining -= dwNumberOfBytesTransfered;
@ -352,7 +352,7 @@ int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
{ {
//this covers the scenario when the fd was previously non blocking (and hence io is still pending) //this covers the scenario when the fd was previously non blocking (and hence io is still pending)
//wait for previous io to complete //wait for previous io to complete
debug("waiting for prior unblocking write to complete before proceeding with this blocking write"); debug2("waiting for prior unblocking write to complete before proceeding with this blocking write");
while (pio->write_details.pending) { while (pio->write_details.pending) {
if (wait_for_any_event(NULL, 0, INFINITE) == -1) if (wait_for_any_event(NULL, 0, INFINITE) == -1)
return -1; return -1;
@ -360,7 +360,7 @@ int fileio_write(struct w32_io* pio, const void *buf, unsigned int max) {
} }
else { else {
errno = EAGAIN; errno = EAGAIN;
debug("IO is already pending"); debug2("IO is already pending");
return -1; return -1;
} }
} }
@ -420,7 +420,7 @@ 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) {
int fd = _open_osfhandle((intptr_t)pio->handle, 0); int fd = _open_osfhandle((intptr_t)pio->handle, 0);
debug("pio:%p", pio); debug2("pio:%p", pio);
if (fd == -1) { if (fd == -1) {
errno = EOTHER; errno = EOTHER;
return -1; return -1;
@ -438,7 +438,7 @@ int fileio_isatty(struct w32_io* pio) {
FILE* fileio_fdopen(struct w32_io* pio, const char *mode) { FILE* fileio_fdopen(struct w32_io* pio, const char *mode) {
int fd_flags = 0; int fd_flags = 0;
debug("pio:%p", pio); debug2("pio:%p", pio);
if (mode[1] == '\0') { if (mode[1] == '\0') {
switch (*mode) { switch (*mode) {
@ -489,7 +489,7 @@ int fileio_on_select(struct w32_io* pio, BOOL rd) {
int fileio_close(struct w32_io* pio) { int fileio_close(struct w32_io* pio) {
debug("pio:%p", pio); debug2("pio:%p", pio);
CancelIo(pio->handle); CancelIo(pio->handle);
//let queued APCs (if any) drain //let queued APCs (if any) drain
SleepEx(0, TRUE); SleepEx(0, TRUE);

View File

@ -133,7 +133,7 @@ void CALLBACK WSARecvCompletionRoutine(
) )
{ {
struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, read_overlapped)); struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, read_overlapped));
debug("pio:%p, pending_state:%d, remaining:%d, completed:%d, error:%d, transferred: %d", debug2("pio:%p, pending_state:%d, remaining:%d, completed:%d, error:%d, transferred: %d",
pio, pio->read_details.pending, pio->read_details.remaining, pio->read_details.pending, dwError, cbTransferred); pio, pio->read_details.pending, pio->read_details.remaining, pio->read_details.pending, dwError, cbTransferred);
if (!dwError && !cbTransferred) if (!dwError && !cbTransferred)
dwError = ERROR_GRACEFUL_DISCONNECT; dwError = ERROR_GRACEFUL_DISCONNECT;
@ -176,7 +176,7 @@ int socketio_WSARecv(struct w32_io* pio, BOOL* completed) {
{ {
pio->read_details.pending = TRUE; pio->read_details.pending = TRUE;
//receive has completed but APC is pending to be scheduled //receive has completed but APC is pending to be scheduled
debug("WSARecv immediate completion"); debug2("WSARecv immediate completion");
if (completed) if (completed)
*completed = TRUE; *completed = TRUE;
} }
@ -258,7 +258,7 @@ int socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namele
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; BOOL completed = FALSE;
debug("pio: %p", pio); debug2("pio: %p", pio);
if ((buf == NULL) || (len == 0)){ if ((buf == NULL) || (len == 0)){
errno = EINVAL; errno = EINVAL;
@ -275,7 +275,7 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
//if io is already pending //if io is already pending
if (pio->read_details.pending) { if (pio->read_details.pending) {
errno = EAGAIN; errno = EAGAIN;
debug("Read is already pending"); debug2("Read is already pending");
return -1; return -1;
} }
@ -286,14 +286,14 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
memcpy(buf, pio->read_details.buf + pio->read_details.completed, num_bytes_copied); memcpy(buf, pio->read_details.buf + pio->read_details.completed, num_bytes_copied);
pio->read_details.remaining -= num_bytes_copied; pio->read_details.remaining -= num_bytes_copied;
pio->read_details.completed += num_bytes_copied; pio->read_details.completed += num_bytes_copied;
debug("returning %d bytes from prior completed IO, remaining: %d", num_bytes_copied, pio->read_details.remaining); debug2("returning %d bytes from prior completed IO, remaining: %d", num_bytes_copied, pio->read_details.remaining);
return num_bytes_copied; return num_bytes_copied;
} }
//if there was an error on async call, return //if there was an error on async call, return
if (pio->read_details.error) { if (pio->read_details.error) {
if (pio->read_details.error == ERROR_GRACEFUL_DISCONNECT) { if (pio->read_details.error == ERROR_GRACEFUL_DISCONNECT) {
debug("connection closed"); debug2("connection closed");
//connection is closed //connection is closed
return 0; return 0;
} }
@ -330,7 +330,7 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
else { else {
if (socketio_is_io_available(pio, TRUE) == FALSE) { if (socketio_is_io_available(pio, TRUE) == FALSE) {
errno = EAGAIN; errno = EAGAIN;
debug("IO is pending"); debug2("IO is pending");
return -1; return -1;
} }
} }
@ -340,7 +340,7 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
{ {
if (pio->read_details.error == ERROR_GRACEFUL_DISCONNECT) { if (pio->read_details.error == ERROR_GRACEFUL_DISCONNECT) {
//connection is closed //connection is closed
debug("connection closed"); debug2("connection closed");
return 0; return 0;
} }
else { else {
@ -356,7 +356,7 @@ int socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags) {
memcpy(buf, pio->read_details.buf, num_bytes_copied); memcpy(buf, pio->read_details.buf, num_bytes_copied);
pio->read_details.remaining -= num_bytes_copied; pio->read_details.remaining -= num_bytes_copied;
pio->read_details.completed = num_bytes_copied; pio->read_details.completed = num_bytes_copied;
debug("returning %d bytes from completed IO, remaining: %d", num_bytes_copied, pio->read_details.remaining); debug2("returning %d bytes from completed IO, remaining: %d", num_bytes_copied, pio->read_details.remaining);
return num_bytes_copied; return num_bytes_copied;
} }
else { else {
@ -376,7 +376,7 @@ void CALLBACK WSASendCompletionRoutine(
) )
{ {
struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, write_overlapped)); struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, write_overlapped));
debug("pio: %p, pending_state:%d, error:%d, transferred:%d, remaining: %d", pio, pio->write_details.pending, dwError, cbTransferred, pio->write_details.remaining); debug2("pio: %p, pending_state:%d, error:%d, transferred:%d, remaining: %d", pio, pio->write_details.pending, dwError, cbTransferred, pio->write_details.remaining);
pio->write_details.error = dwError; pio->write_details.error = dwError;
//assert that remaining == cbTransferred //assert that remaining == cbTransferred
pio->write_details.remaining -= cbTransferred; pio->write_details.remaining -= cbTransferred;
@ -387,7 +387,7 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
int ret = 0; int ret = 0;
WSABUF wsabuf; WSABUF wsabuf;
debug("pio: %p", pio); debug2("pio: %p", pio);
if ((buf == NULL) || (len == 0)){ if ((buf == NULL) || (len == 0)){
errno = EINVAL; errno = EINVAL;
@ -408,7 +408,7 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
{ {
//this covers the scenario when the fd was previously non blocking (and hence io is still pending) //this covers the scenario when the fd was previously non blocking (and hence io is still pending)
//wait for previous io to complete //wait for previous io to complete
debug("waiting for IO on a previous nonblocking send to complete"); debug2("waiting for IO on a previous nonblocking send to complete");
while (pio->write_details.pending) { while (pio->write_details.pending) {
if (wait_for_any_event(NULL, 0, INFINITE) == -1) if (wait_for_any_event(NULL, 0, INFINITE) == -1)
return -1; return -1;
@ -416,7 +416,7 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
} }
else { else {
errno = EAGAIN; errno = EAGAIN;
debug("IO pending"); debug2("IO pending");
return -1; return -1;
} }
} }
@ -456,7 +456,7 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
if (ret == 0) if (ret == 0)
{ {
//send has completed and APC is scheduled, let it run //send has completed and APC is scheduled, let it run
debug("WSASend immediate completion"); debug2("WSASend immediate completion");
pio->write_details.pending = TRUE; pio->write_details.pending = TRUE;
pio->write_details.remaining = wsabuf.len; pio->write_details.remaining = wsabuf.len;
SleepEx(1, TRUE); SleepEx(1, TRUE);
@ -473,7 +473,7 @@ int socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags) {
if (WSAGetLastError() == WSA_IO_PENDING) if (WSAGetLastError() == WSA_IO_PENDING)
{ {
//io is initiated and pending //io is initiated and pending
debug("IO pending"); debug2("IO pending");
pio->write_details.pending = TRUE; pio->write_details.pending = TRUE;
pio->write_details.remaining = wsabuf.len; pio->write_details.remaining = wsabuf.len;
if (w32_io_is_blocking(pio)) if (w32_io_is_blocking(pio))
@ -500,12 +500,12 @@ int socketio_shutdown(struct w32_io* pio, int how) {
} }
int socketio_close(struct w32_io* pio) { int socketio_close(struct w32_io* pio) {
debug("pio: %p", pio); debug2("pio: %p", pio);
closesocket(pio->sock); closesocket(pio->sock);
//wait for pending io to abort //wait for pending io to abort
SleepEx(0, TRUE); SleepEx(0, TRUE);
if (pio->read_details.pending || pio->write_details.pending) if (pio->read_details.pending || pio->write_details.pending)
debug("IO is still pending on closed socket. read:%d, write:%d", pio->read_details.pending, pio->write_details.pending); debug2("IO is still pending on closed socket. read:%d, write:%d", pio->read_details.pending, pio->write_details.pending);
if (pio->type == LISTEN_FD) { if (pio->type == LISTEN_FD) {
if (pio->read_overlapped.hEvent) if (pio->read_overlapped.hEvent)
CloseHandle(pio->read_overlapped.hEvent); CloseHandle(pio->read_overlapped.hEvent);
@ -529,7 +529,7 @@ struct w32_io* socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* a
int iResult = 0; int iResult = 0;
struct acceptEx_context* context = (struct acceptEx_context*)pio->context; struct acceptEx_context* context = (struct acceptEx_context*)pio->context;
debug("pio:%p", pio); debug2("pio:%p", pio);
//start io if not already started //start io if not already started
if (pio->read_details.pending == FALSE) { if (pio->read_details.pending == FALSE) {
if (socketio_acceptEx(pio) != 0) { if (socketio_acceptEx(pio) != 0) {
@ -551,7 +551,7 @@ struct w32_io* socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* a
//if i/o is not ready //if i/o is not ready
if (FALSE == socketio_is_io_available(pio, TRUE)) { if (FALSE == socketio_is_io_available(pio, TRUE)) {
errno = EAGAIN; errno = EAGAIN;
debug("accept is pending"); debug2("accept is pending");
return NULL; return NULL;
} }
@ -578,7 +578,7 @@ struct w32_io* socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* a
context->accept_socket = INVALID_SOCKET; context->accept_socket = INVALID_SOCKET;
pio->read_details.pending = FALSE; pio->read_details.pending = FALSE;
ResetEvent(pio->read_overlapped.hEvent); ResetEvent(pio->read_overlapped.hEvent);
debug("accept io:%p", accept_io); debug2("accept io:%p", accept_io);
return accept_io; return accept_io;
} }
@ -613,7 +613,7 @@ BOOL socketio_is_io_available(struct w32_io* pio, BOOL rd) {
int socketio_on_select(struct w32_io* pio, BOOL rd) { int socketio_on_select(struct w32_io* pio, BOOL rd) {
debug("pio:%p", pio); debug2("pio:%p", pio);
if (rd && pio->read_details.pending) if (rd && pio->read_details.pending)
return 0; return 0;

View File

@ -392,7 +392,7 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
*readfds = read_ready_fds; *readfds = read_ready_fds;
if (writefds) if (writefds)
*writefds = write_ready_fds; *writefds = write_ready_fds;
debug("IO ready:%d, no wait", out_ready_fds); debug2("IO ready:%d, no wait", out_ready_fds);
return out_ready_fds; return out_ready_fds;
} }
@ -450,7 +450,7 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
if (out_ready_fds) if (out_ready_fds)
break; break;
debug("wait ended without any IO completion, looping again"); debug2("wait ended without any IO completion, looping again");
} while (1); } while (1);

View File

@ -135,9 +135,6 @@
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="debug.c" /> <ClCompile Include="debug.c" />
<ClCompile Include="fileio.c" /> <ClCompile Include="fileio.c" />

View File

@ -14,9 +14,6 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="socketio.c"> <ClCompile Include="socketio.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>