Windows called channels in practice

In Windows named pipe, how to use the CreateNamedPipe , ConnectNamedPipe , DisconnectNamedPipe and CloseHandle calls CreateNamedPipe ?

I am making a server application that connects to a client application that connects and disconnects to a channel several times through a session.

When my records fail because the client is disconnected, I should call DisconnectNamedPipe , CloseHandle or nothing on my descriptor.

Then, to accept the new connection, do I have to call CreateNamedPipe and then ConnectNamedPipe or just ConnectNamedPipe ?

I would very much like to explain the different states that my pipe may be as a result of these calls, because I did not find this elsewhere.

Additional Information:

Language: Python using the win32pipe , win32file and win32api .

Pipe settings: WAIT, without overlap, downstream.

+4
source share
2 answers

I managed to achieve what I wanted. I call CreateNamedPipe and CloseHandle exactly once per session, and I call DisconnectNamedPipe when my record fails, and then another ConnectNamedPipe .

The trick is only to call DisconnectNamedPipe when the pipe is really connected. I called it every time I tried to connect β€œjust to be sure,” and it gave me weird errors.

See also djgandy's answer for more information about pipes.

0
source

It is good practice to call DisconnectNamedPipe , then CloseHandle , although CloseHandle should clear everything.

The MSDN documentation is a bit vague, and their server example is pretty simple. Regarding the reuse of pipe handles, it seems like it's your own choice. The documentation for DisconnectNamedPipe seems to indicate that you can reuse the channel descriptor for the new client by calling ConnectNamedPipe again on that descriptor after the disconnector. The role of ConnectNamedPipe is to assign a handle to the connecting client.

Make sure you clean the pipes, although the MSDN states the following

Each time a named pipe is created, the system creates inbound and / or outbound buffers using a nonpaged pool, which is the physical memory used by the kernel. The number of pipe instances (as well as objects such as threads and processes) that you can create is limited by the available nonpaged pool. Each read or write request requires a buffer space for reading or writing data, as well as additional space for internal data structures.

I also talked about this above if you create / destroy many pipes. I suppose it would be better to manage the pool of pipe handles if there are many clients and they have a growth / compression mechanism for the pool.

+3
source

Source: https://habr.com/ru/post/1495501/


All Articles