joyent / libuv

Go to
https://github.com/libuv/libuv
3.27k stars 653 forks source link

Add uv_device_t as stream on windows and linux, handle device I/O asyncly. #1580

Closed zhaozg closed 9 years ago

zhaozg commented 10 years ago

I'm a multi-language programmer, but not family with english, so maybe trouble with options. libuv is a good i/o framework, and easy to extent, I hope persist on it to do my work.

When I want to do r/w device on windows, I try filesystem api, it works for device handle, but not fit my needs. I need a real stream, I try wrap handle as pipe, and fail to work. When I study TTY and TCP, I realize that it not hard to implication a device type stream, because just file operation with overlapped, even simpler than other stream types. When I finish my job on windows, i realize libUV is born for linux, simple to add a device type, so i did it.

There are only three type related api.

UV_EXTERN int uv_device_init(uv_loop_t* handle, uv_device_t* device,  const char* path, int flags);
UV_EXTERN int uv_device_ioctl(uv_device_t* device, int cmd, uv_ioargs_t* args);

uv_device_init will init and create a uv_deive_t, path should be '/dev/??' on linux or ".\Global\???" on windows. flags should be O_RDONLY,O_WRONLY or O_RDWR.

uv_device_ioctl will set or get device paramater, arg is relative with OS platform, return >=0 for success, detail please see header file.

because not test widely, so maybe have bugs, please attention.

Happy for do something for libUV.

cjdelisle commented 10 years ago

In general it looks like it will work for my use case, I'm still (of course) favorable to my own patch, in this case because it's fewer lines of code, but if this is code which I do not maintain, I'm happy with that so +1 in theory. If you feel like patching https://github.com/cjdelisle/cjdns/blob/master/interface/tuntap/windows/TAPInterface.c to work with your version then I'll really love you :)

saghul commented 10 years ago

Before getting into reviewing any code, I'd like to hear some general thoughts from @indutny, @bnoordhuis and @piscisaureus.

The approach taken here looks clean and nice to me, if that's enough for people wanting to use serial devices (for example) not to (ab)use uv_fs_open, I'm all in :-)

saghul commented 10 years ago

Thanks @zhaozg! I did an initial review of the code (I didn't cover it all yet though).

zhaozg commented 10 years ago

OK, now I update finished according to libvu style, and thanks @saghul very much.

sequoiar commented 10 years ago

@zhaozg amazing. And, are you going integrate device stream to node.js ?

zhaozg commented 10 years ago

not node.js, but luvit https://github.com/luvit/luvit

bnoordhuis commented 10 years ago

Sorry, have to cut the review short. At a quick glance, I notice there are a bunch of style errors in the test but nothing worse.

zhaozg commented 10 years ago

thanks @bnoordhuis, code updated.

cjdelisle commented 10 years ago

Without having really followed this conversation except to periodically sweep the mess of mail out of my inbox, I thought perhaps you would enjoy reading parkinson's law :)

saghul commented 10 years ago

@cjdelisle :-)

zhaozg commented 9 years ago

@saghul I should make a new RP to libuv/libuv?

saghul commented 9 years ago

If you don't mind, that would be great! On Nov 29, 2014 5:21 AM, "George Zhao" notifications@github.com wrote:

@saghul https://github.com/saghul I should make a new RP to libuv/libuv?

— Reply to this email directly or view it on GitHub https://github.com/joyent/libuv/pull/1580#issuecomment-64941097.

zhaozg commented 9 years ago

Close this RP, because replaced with https://github.com/libuv/libuv/pull/19.