Skip to content

Commit

Permalink
refactor: introduce luv_pipe_optflags to handle flags
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozg committed Jul 4, 2023
1 parent c69741b commit d15a473
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
23 changes: 19 additions & 4 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1943,24 +1943,32 @@ read_pipe:read_start(function(err, chunk)
print(chunk)
end)
```

### `uv.pipe_bind2(pipe, name, [flags])`

> method form `pipe:bind2(name, [flags])`
**Parameters:**

- `pipe`: `uv_pipe_t userdata`
- `name`: `string`
- `flags`: `integer`(default: 0)
- `flags`: `integer` or `table` or `nil`(default: 0)

Bind the pipe to a file path (Unix) or a name (Windows).

`Flags` must be zero or `uv.constants.PIPE_NO_TRUNCATE`. Returns `EINVAL` for unsupported flags without performing the bind operation.
`Flags`:

- If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`.
- If `type(flags)` is `table`, it must be `{}` or `{ no_trunate = true|false }`.
- If `type(flags)` is `nil`, it use default value `0`.
- Returns `EINVAL` for unsupported flags without performing the bind operation.

Supports Linux abstract namespace sockets. namelen must include the leading '\0' byte but not the trailing nul byte.

**Returns:** `0` or `fail`

**Note**:

1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes,
typically between 92 and 108 bytes.
2. New in version 1.46.0.
Expand All @@ -1970,21 +1978,28 @@ typically between 92 and 108 bytes.
> method form `pipe:connect2(name, [flags], [callback])`
**Parameters:**

- `pipe`: `uv_pipe_t userdata`
- `name`: `string`
- `flags`: `integer`(default: 0)
- `flags`: `integer` or `table` or `nil`(default: 0)
- `callback`: `callable` or `nil`
- `err`: `nil` or `string`

Connect to the Unix domain socket or the named pipe.

`Flags` must be zero or `uv.constants.PIPE_NO_TRUNCATE`. Returns `EINVAL` for unsupported flags without performing the bind operation.
`Flags`:

- If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`.
- If `type(flags)` is `table`, it must be `{}` or `{ no_trunate = true|false }`.
- If `type(flags)` is `nil`, it use default value `0`.
- Returns `EINVAL` for unsupported flags without performing the bind operation.

Supports Linux abstract namespace sockets. namelen must include the leading nul byte but not the trailing nul byte.

**Returns:** `uv_connect_t userdata` or `fail`

**Note**:

1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes,
typically between 92 and 108 bytes.
2. New in version 1.46.0.
Expand Down
20 changes: 18 additions & 2 deletions src/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,27 @@ static int luv_pipe_connect(lua_State* L) {


#if LUV_UV_VERSION_GEQ(1,46,0)
static int luv_pipe_optflags(lua_State *L, int i, unsigned int flags) {
// flags param can be nil, an integer, or a table
if (lua_type(L, i) == LUA_TNUMBER || lua_isnoneornil(L, i)) {
flags = (unsigned int)luaL_optinteger(L, i, flags);
}
else if (lua_type(L, i) == LUA_TTABLE) {
lua_getfield(L, i, "no_truncate");
if (lua_toboolean(L, -1)) flags |= UV_PIPE_NO_TRUNCATE;
lua_pop(L, 1);
}
else {
return luaL_argerror(L, i, "expected nil, integer, or table");
}
return flags;
}

static int luv_pipe_bind2(lua_State* L) {
size_t namelen;
uv_pipe_t* handle = luv_check_pipe(L, 1);
const char* name = luaL_checklstring(L, 2, &namelen);
unsigned int flags = luaL_optinteger(L, 3, 0);
unsigned int flags = luv_pipe_optflags(L, 3, 0);
int ret = uv_pipe_bind2(handle, name, namelen, flags);
return luv_result(L, ret);
}
Expand All @@ -78,7 +94,7 @@ static int luv_pipe_connect2(lua_State* L) {
luv_ctx_t* ctx = luv_context(L);
uv_pipe_t* handle = luv_check_pipe(L, 1);
const char* name = luaL_checklstring(L, 2, &namelen);
unsigned int flags = luaL_optinteger(L, 3, 0);
unsigned int flags = luv_pipe_optflags(L, 3, 0);
int ref = luv_check_continuation(L, 4);
uv_connect_t* req = (uv_connect_t*)lua_newuserdata(L, uv_req_size(UV_CONNECT));
req->data = luv_setup_req(L, ctx, ref);
Expand Down

0 comments on commit d15a473

Please sign in to comment.