Skip to content

Commit

Permalink
debug and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
microcai committed Jan 5, 2025
1 parent 8f0809d commit 964052e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
12 changes: 4 additions & 8 deletions example/unbufcpy/unbufcp1/unbufcp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ DWORD GetTickCount()
// File handles for the copy operation. All read operations are
// from SourceFile. All write operations are to DestFile.
//
HANDLE SourceFile;
HANDLE DestFile;
HANDLE SourceFile = INVALID_HANDLE_VALUE;
HANDLE DestFile = INVALID_HANDLE_VALUE;

//
// I/O completion port. All read and writes to the files complete
// to this port.
//
HANDLE IoPort;
HANDLE IoPort = INVALID_HANDLE_VALUE;

//
// Key values used to determine whether a read or a write
Expand Down Expand Up @@ -154,7 +154,7 @@ int main(
//

SourceFile = CreateFile(argv[1],
GENERIC_READ | GENERIC_WRITE,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
Expand Down Expand Up @@ -420,10 +420,6 @@ void CopyLoop(ULARGE_INTEGER FileSize)
exit(1);
}

//
// decrement pending I/O count
//
--PendingIO;

} else if (Key == WriteKey) {

Expand Down
2 changes: 1 addition & 1 deletion iocp4linux/src/internal_iocp_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct SOCKET_emu_class final : public base_handle
{
if (_iocp)
{
_iocp->submit_io([this](io_uring_sqe* sqe)
_iocp->submit_io_immediatly([this](io_uring_sqe* sqe)
{
io_uring_prep_close(sqe, _socket_fd);
io_uring_sqe_set_data(sqe, nullptr);
Expand Down
21 changes: 16 additions & 5 deletions iocp4linux/src/iocp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,17 +1003,26 @@ IOCP_DECL HANDLE CreateFileA(
_In_opt_ HANDLE hTemplateFile
)
{
assert(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED);
// assert(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED);

int oflag = 0;
int mode = 0644;
if (dwDesiredAccess & GENERIC_READ)
{
oflag |= O_RDONLY;
oflag = O_RDONLY;
}
if (dwDesiredAccess & GENERIC_WRITE)
{
oflag |= O_WRONLY;
oflag = O_WRONLY;
}
if ((dwDesiredAccess & (GENERIC_WRITE|GENERIC_READ)) == (GENERIC_WRITE|GENERIC_READ))
{
oflag = O_RDWR;
}

if (dwFlagsAndAttributes & FILE_FLAG_NO_BUFFERING)
{
oflag |= __O_DIRECT;
}

if (dwDesiredAccess & GENERIC_EXECUTE)
Expand Down Expand Up @@ -1101,7 +1110,8 @@ IOCP_DECL BOOL SetEndOfFile(_In_ HANDLE hFile)
{
int fd = hFile->native_handle();
off_t cur_pos = lseek(fd, 0, SEEK_CUR);
return ftruncate(fd, cur_pos) == 0;
auto result = ftruncate(fd, cur_pos);
return result == 0;
}

IOCP_DECL BOOL ReadFile(
Expand Down Expand Up @@ -1140,9 +1150,10 @@ IOCP_DECL BOOL ReadFile(
op->overlapped_ptr = lpOverlapped;
lpOverlapped->Internal = reinterpret_cast<ULONG_PTR>(op);
op->CompletionKey = s->_completion_key;
auto fd = s->native_handle();
iocp->submit_io([&](struct io_uring_sqe* sqe)
{
io_uring_prep_read(sqe, s->native_handle(), lpBuffer, nNumberOfBytesToRead, lpOverlapped->offset_64);
io_uring_prep_read(sqe, fd, lpBuffer, nNumberOfBytesToRead, lpOverlapped->offset_64);
io_uring_sqe_set_data(sqe, op);
});

Expand Down
15 changes: 9 additions & 6 deletions iocp_asio/src/iocp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,6 @@ int setsockopt(SOCKET __fd, int __level, int __optname, const void *__optval, so
/***********************************************************************************
* Overlapped File IO
************************************************************************************/

IOCP_DECL HANDLE CreateFileA(
_In_ LPCSTR lpFileName,
_In_ DWORD dwDesiredAccess,
Expand All @@ -760,17 +759,21 @@ IOCP_DECL HANDLE CreateFileA(
_In_opt_ HANDLE hTemplateFile
)
{
assert(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED);
// assert(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED);

int oflag = 0;
int mode = 0644;
if (dwDesiredAccess & GENERIC_READ)
{
oflag |= O_RDONLY;
oflag = O_RDONLY;
}
if (dwDesiredAccess & GENERIC_WRITE)
{
oflag |= O_WRONLY;
oflag = O_WRONLY;
}
if ((dwDesiredAccess & (GENERIC_WRITE|GENERIC_READ)) == (GENERIC_WRITE|GENERIC_READ))
{
oflag = O_RDWR;
}

if (dwDesiredAccess & GENERIC_EXECUTE)
Expand Down Expand Up @@ -821,7 +824,6 @@ IOCP_DECL HANDLE CreateFileW(
return CreateFileA(utf8_str.c_str(), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}


IOCP_DECL DWORD GetFileSize(
_In_ HANDLE hFile,
_Out_opt_ LPDWORD lpFileSizeHigh)
Expand Down Expand Up @@ -859,7 +861,8 @@ IOCP_DECL BOOL SetEndOfFile(_In_ HANDLE hFile)
{
int fd = hFile->native_handle();
off_t cur_pos = lseek(fd, 0, SEEK_CUR);
return ftruncate(fd, cur_pos) == 0;
auto result = ftruncate(fd, cur_pos);
return result == 0;
}

IOCP_DECL BOOL ReadFile(
Expand Down

0 comments on commit 964052e

Please sign in to comment.