keys of windows namedpipe
pipeline只能有一个,但是有多个instance,
client 与 server 读写属性要兼容
左边server,右边是client
Access mode | Equivalent generic access right |
---|---|
PIPE_ACCESS_INBOUND | GENERIC_READ |
PIPE_ACCESS_OUTBOUND | GENERIC_WRITE |
PIPE_ACCESS_DUPLEX | GENERIC_READ | GENERIC_WRITE |
The
CreateFile
function allows the pipe client to set overlapped mode (FILE_FLAG_OVERLAPPED) for its pipe handles using thedwFlagsAndAttributes
parameter.
Type Mode (write mode)
- message-type pipe 只能使用write-throgh模式
- byte-type pipe 可以自定义,(模式模式会累积一定数量的buffer或者超时才会发送)
Write-Through不使用缓冲,每次write都是真正的发送。here
type
is the same towrite
Read Mode
- A handle to a byte-type pipe can be in byte-read mode only.
- A handle to a message-type pipe can be in either byte-read or message-read mode.
字节流模式:当pipe全部读完或者读完指定数量的字节
消息模式:全部读完或者报错后续再读
- To create the pipe handle in byte-read mode, specify PIPE_READMODE_BYTE or use the default value. Data is read from the pipe as a stream of bytes. A read operation is completed successfully when all available bytes in the pipe are read or when the specified number of bytes is read.
- To create the pipe handle in message-read mode, specify PIPE_READMODE_MESSAGE. Data is read from the pipe as a stream of messages. A read operation is completed successfully only when the entire message is read. If the specified number of bytes to read is less than the size of the next message, the function reads as much of the message as possible before returning zero (the GetLastError function returns ERROR_MORE_DATA). The remainder of the message can be read using another read operation.
- For a pipe client, a pipe handle returned by CreateFile is always in byte-read mode initially. Both pipe clients and pipe servers can use the SetNamedPipeHandleState function to change the read mode of a pipe handle. The pipe handle must have the FILE_WRITE_ATTRIBUTES access right.
Wait Mode
read
write
ConnectNamedPipe
是否立即返回
write
in non-wait mode, the write operation returns a nonzero value immediately, without writing any bytes (for a message-type pipe) or after writing as many bytes as the buffer holds (for a byte-type pipe).ConnectNamedPipe
With a nonblocking-wait handle, the connect operation returns zero immediately, and theGetLastError
function returnsERROR_PIPE_LISTENING
.
Named Pipe Operations
- server 可以重复调用
CreateNamedPipe
来创建instance
The server can callCreateNamedPipe
repeatedly to create additional instances of the pipe, as long as it does not exceed the maximum number of instances. If the function succeeds, each call returns a handle to the server end of a named pipe instance. - client 使用
CreateFile
来连接pipe, 或者使用WaitNamedPipe
等待连接
As soon as the pipe server creates a pipe instance, a pipe client can connect to it by calling theCreateFile
orCallNamedPipe
function. If a pipe instance is available, CreateFile returns a handle to the client end of the pipe instance. If no instances of the pipe are available, a pipe client can use theWaitNamedPipe
function to wait until a pipe becomes available. - server 使用
ConnectNamedPipe
来确定是否有client连接成功
A pipe server can determine when a pipe client is connected to a pipe instance by calling theConnectNamedPipe
function. If the pipe handle is in blocking-wait mode,ConnectNamedPipe
does not return until a client is connected. - 连接断开以后,server使用
CloseHandle
销毁,也可以ConnectNamedPipe
再次使用
After the client is disconnected, the server calls theCloseHandle
function to close its handle to the pipe instance. Alternatively, the server can use ConnectNamedPipe to enable a new client to connect to this instance of the pipe. - 常用函数
GetNamedPipeInfo
function, which returns the type of the pipe, the size of the input and output buffers, and the maximum number of pipe instances that can be created.GetNamedPipeHandleState
function reports on the read and wait modes of a pipe handle, the current number of pipe instances, and additional information for pipes that communicate over a network.SetNamedPipeHandleState
function sets the read mode and wait modes of a pipe handle. For pipe clients communicating with a remote server, the function also controls the maximum number of bytes to collect or the maximum time to wait before transmitting a message (assuming the client’s handle was not opened with write-through mode enabled).
overlap
- 避免在同一个pipeline上执行同步读写操作
avoids simultaneous operations on a single pipe instance by using the event object in the OVERLAPPED structure. Because the same event object is used for read, write, and connect operations for each instance, there is no way to know which operation’s completion caused the event to be set to the signaled state for simultaneous operations using the same pipe instance.
- write read 函数改变overlap object 状态
read write 异步操作时, 置为non-signal 以及 在IO完成之后置 sigaled 状态
- overlap与default的区别
default | ovelap | |
---|---|---|
user | 用户线程阻塞等待 | 用户线程不阻塞等待 |
system | IO串行执行 | IO并行执行 |
- 如果overlap操作在api返回之前完成
- Overlap.hevent不会被触发
GetOverlappedResult
不会有数据,它只会返回pending result
- event maual-set指的是waitobject不会重置event的状态
- overlap的四个步骤
- write/read/connect 传给系统overlap对象
- waitobject 等待系统通知
- getoverlap 获取结果
如果提前完成就没有2,3步骤