学习搜狗的workflow项目的改写window下的异步文件io问题记录

class __WFFilepreadTask : public WFFilepreadTask
{
public:
__WFFilepreadTask(const std::string& path, void *buf, size_t count,
off_t offset, IOService *service, fio_callback_t&& cb):
WFFilepreadTask(-1, buf, count, offset, service, std::move(cb)),
pathname(path)
{
}

protected:
virtual int prepare()
{

// if (fd != -1) {

// intptr_t osfhandle = _get_osfhandle(fd);
// if (osfhandle != -1) {
// HANDLE handle = (HANDLE)osfhandle;
// // 现在你可以使用Windows句柄(handle)了
// }
HANDLE handle = CreateFile(
this->pathname.c_str(),// 文件路径
GENERIC_READ,// 打开文件以进行读取
FILE_SHARE_READ,// 共享模式
NULL,// 安全属性(可以为NULL)
OPEN_EXISTING,// 打开现有文件
FILE_ATTRIBUTE_NORMAL,// 文件属性
NULL// 模板文件句柄(可以为NULL)
);
///在Windows上使用POSIX风格的I/O,并且想要将HANDLE转换为fd,你可以使用_open_osfhandle函数,这个函数是Microsoft C运行时库提供的,用于将一个Windows文件句柄转换为一个POSIX文件描述符。
int fd = _open_osfhandle((intptr_t)handle, 0);
this->args.fd =fd;

    if (this->args.fd < 0)
        return -1;

    return WFFilepreadTask::prepare();
}

virtual SubTask *done()
{
    if (this->args.fd >= 0)
    {
          close(this->args.fd);
        this->args.fd = -1;
    }

    return WFFilepreadTask::done();
}

protected:
std::string pathname;
};

class __WFFilepwriteTask : public WFFilepwriteTask
{
public:
__WFFilepwriteTask(const std::string& path, const void *buf, size_t count,
off_t offset, IOService *service, fio_callback_t&& cb):
WFFilepwriteTask(-1, buf, count, offset, service, std::move(cb)),
pathname(path)
{
}

protected:
virtual int prepare()
{
HANDLE handle = CreateFile(
this->pathname.c_str(),// 文件路径
GENERIC_READ,// 打开文件以进行读取
FILE_SHARE_READ,// 共享模式
NULL,// 安全属性(可以为NULL)
OPEN_EXISTING,// 打开现有文件
FILE_ATTRIBUTE_NORMAL,// 文件属性
NULL// 模板文件句柄(可以为NULL)
);
int fd = _open_osfhandle((intptr_t)handle, 0);
this->args.fd =fd;
if (this->args.fd < 0)
return -1;

    return WFFilepwriteTask::prepare();
}

virtual SubTask *done()
{
    if (this->args.fd >= 0)
    {
         close(this->args.fd);
        this->args.fd = -1;
    }

    return WFFilepwriteTask::done();
}

protected:
std::string pathname;
};

class __WFFilepreadvTask : public WFFilepreadvTask
{
public:
__WFFilepreadvTask(const std::string& path, const struct iovec *iov,
int iovcnt, off_t offset, IOService *service,
fvio_callback_t&& cb) :
WFFilepreadvTask(-1, iov, iovcnt, offset, service, std::move(cb)),
pathname(path)
{
}

protected:
virtual int prepare()
{
HANDLE handle = CreateFile(
this->pathname.c_str(),// 文件路径
GENERIC_READ,// 打开文件以进行读取
FILE_SHARE_READ,// 共享模式
NULL,// 安全属性(可以为NULL)
OPEN_EXISTING,// 打开现有文件
FILE_ATTRIBUTE_NORMAL,// 文件属性
NULL// 模板文件句柄(可以为NULL)
);
int fd = _open_osfhandle((intptr_t)handle, 0);
this->args.fd =fd;
if (this->args.fd < 0)
return -1;

    return WFFilepreadvTask::prepare();
}

virtual SubTask *done()
{
    if (this->args.fd >= 0)
    {
       close(this->args.fd);
        this->args.fd = -1;
    }

    return WFFilepreadvTask::done();
}

protected:
std::string pathname;
};

class __WFFilepwritevTask : public WFFilepwritevTask
{
public:
__WFFilepwritevTask(const std::string& path, const struct iovec *iov,
int iovcnt, off_t offset, IOService *service,
fvio_callback_t&& cb) :
WFFilepwritevTask(-1, iov, iovcnt, offset, service, std::move(cb)),
pathname(path)
{
}

protected:
virtual int prepare()
{
HANDLE handle = CreateFile(
this->pathname.c_str(),// 文件路径
GENERIC_READ,// 打开文件以进行读取
FILE_SHARE_READ,// 共享模式
NULL,// 安全属性(可以为NULL)
OPEN_EXISTING,// 打开现有文件
FILE_ATTRIBUTE_NORMAL,// 文件属性
NULL// 模板文件句柄(可以为NULL)
);
int fd = _open_osfhandle((intptr_t)handle, 0);
this->args.fd =fd;
if (this->args.fd < 0)
return -1;

    return WFFilepwritevTask::prepare();
}

protected:
virtual SubTask *done()
{
if (this->args.fd >= 0)
{
close(this->args.fd);
this->args.fd = -1;
}

    return WFFilepwritevTask::done();
}

protected:
std::string pathname;
};

static int __writefile_io(IOCPData *iocp_data, int timeout)
{

WriteContext *ctx = (WriteContext *)iocp_data->data.context;

int ret=WriteFile(iocp_data->data.handle, ctx->entry, ctx->count,NULL,
                    &iocp_data->overlap);
if (ret == 0 || WSAGetLastError() == WSA_IO_PENDING)
{
    if (ret != 0 && timeout == 0)
        CancelIoEx(iocp_data->data.handle, &iocp_data->overlap);

    return  -1;
}
errno = WSAGetLastError();
return 0; // 成功启动异步操作

}

static int __readfile_io(IOCPData *iocp_data, int timeout)
{

ReadContext *ctx = (ReadContext *)iocp_data->data.context;
int ret=ReadFile(iocp_data->data.handle,  ctx->entry, ctx->msgsize,NULL,
                   &iocp_data->overlap);

if (ret == 0 || WSAGetLastError() == WSA_IO_PENDING)
{
    if (ret != 0 && timeout == 0)
        CancelIoEx(iocp_data->data.handle, &iocp_data->overlap);

    return  -1;
}
errno = WSAGetLastError();
return 0; // 成功启动异步操作

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值