【发布时间】:2017-02-02 14:27:58
【问题描述】:
与匿名管道建立连接之后的步骤需要服务器调用DisposeLocalCopyOfClientHandle。 MSDN 解释:
DisposeLocalCopyOfClientHandle 方法应该在 客户端句柄已传递给客户端。如果这个方法不 调用,AnonymousPipeServerStream 对象将不会收到通知 当客户端释放其 PipeStream 对象时。
试图理解为什么当客户端关闭时服务器不会被注意到,我继续查看参考源上的DisposeLocalCopyOfClientHandle:
// This method is an annoying one but it has to exist at least until we make passing handles between
// processes first class. We need this because once the child handle is inherited, the OS considers
// the parent and child's handles to be different. Therefore, if a child closes its handle, our
// Read/Write methods won't throw because the OS will think that there is still a child handle around
// that can still Write/Read to/from the other end of the pipe.
//
// Ideally, we would want the Process class to close this handle after it has been inherited. See
// the pipe spec future features section for more information.
//
// Right now, this is the best signal to set the anonymous pipe as connected; if this is called, we
// know the client has been passed the handle and so the connection is live.
[System.Security.SecurityCritical]
public void DisposeLocalCopyOfClientHandle() {
if (m_clientHandle != null && !m_clientHandle.IsClosed) {
m_clientHandle.Dispose();
}
}
这句话让我很困惑:
一旦子句柄被继承,操作系统就会认为父句柄和子句柄不同。
父母的句柄和孩子的句柄(即,服务器的m_handle 和服务器的 m_clientHandle,传递给孩子)一开始就不是不同的吗?这里的“不同”是指“引用不同的对象”(我是这样理解的),还是有其他含义?
【问题讨论】: