IPC-NamedPipe

本文介绍了Windows命名管道IPC的关键概念,包括管道的唯一性、客户端与服务器的读写兼容性、类型模式(写模式)、读模式、等待模式以及重叠I/O操作。详细讨论了不同类型管道的读写行为,并解释了如何设置和更改管道的模式。同时,提到了常用函数的作用以及如何避免在单一管道实例上的同步读写操作。
摘要由CSDN通过智能技术生成

keys of windows namedpipe

pipeline只能有一个,但是有多个instance,

client 与 server 读写属性要兼容

左边server,右边是client

Access modeEquivalent generic access right
PIPE_ACCESS_INBOUNDGENERIC_READ
PIPE_ACCESS_OUTBOUNDGENERIC_WRITE
PIPE_ACCESS_DUPLEXGENERIC_READ | GENERIC_WRITE

The CreateFile function allows the pipe client to set overlapped mode (FILE_FLAG_OVERLAPPED) for its pipe handles using the dwFlagsAndAttributes parameter.

Type Mode (write mode)

  • message-type pipe 只能使用write-throgh模式
  • byte-type pipe 可以自定义,(模式模式会累积一定数量的buffer或者超时才会发送)

Write-Through不使用缓冲,每次write都是真正的发送。here type is the same to write

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全部读完或者读完指定数量的字节
消息模式:全部读完或者报错后续再读

  1. 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.
  2. 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.
  3. 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 the GetLastError function returns ERROR_PIPE_LISTENING.

Named Pipe Operations

  • server 可以重复调用CreateNamedPipe 来创建instance
    The server can call CreateNamedPipe 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 the CreateFile or CallNamedPipe 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 the WaitNamedPipe 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 the ConnectNamedPipe 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 the CloseHandle 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.
  • 常用函数
  1. 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.
  2. 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.
  3. 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的区别
defaultovelap
user用户线程阻塞等待用户线程不阻塞等待
systemIO串行执行IO并行执行
  1. 如果overlap操作在api返回之前完成
    • Overlap.hevent不会被触发
    • GetOverlappedResult不会有数据,它只会返回pending result
  1. event maual-set指的是waitobject不会重置event的状态
  • overlap的四个步骤
  1. write/read/connect 传给系统overlap对象
  2. waitobject 等待系统通知
  3. getoverlap 获取结果

如果提前完成就没有2,3步骤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值