mirror of
https://github.com/zeromq/libzmq.git
synced 2025-03-19 01:43:50 +00:00
Merge pull request #765 from pijyoi/master
make win32 signaler support ephemeral ports
This commit is contained in:
commit
243aba265b
@ -306,16 +306,21 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
|||||||
// Note that if the event object already exists, the CreateEvent requests
|
// Note that if the event object already exists, the CreateEvent requests
|
||||||
// EVENT_ALL_ACCESS access right. If this fails, we try to open
|
// EVENT_ALL_ACCESS access right. If this fails, we try to open
|
||||||
// the event object asking for SYNCHRONIZE access only.
|
// the event object asking for SYNCHRONIZE access only.
|
||||||
|
HANDLE sync = NULL;
|
||||||
|
|
||||||
|
// Create critical section only if using fixed signaler port
|
||||||
|
if (signaler_port != 0) {
|
||||||
# if !defined _WIN32_WCE
|
# if !defined _WIN32_WCE
|
||||||
HANDLE sync = CreateEvent (&sa, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
|
sync = CreateEvent (&sa, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
|
||||||
# else
|
# else
|
||||||
HANDLE sync = CreateEvent (NULL, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
|
sync = CreateEvent (NULL, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
|
||||||
# endif
|
# endif
|
||||||
if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED)
|
if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED)
|
||||||
sync = OpenEvent (SYNCHRONIZE | EVENT_MODIFY_STATE,
|
sync = OpenEvent (SYNCHRONIZE | EVENT_MODIFY_STATE,
|
||||||
FALSE, TEXT ("Global\\zmq-signaler-port-sync"));
|
FALSE, TEXT ("Global\\zmq-signaler-port-sync"));
|
||||||
|
|
||||||
win_assert (sync != NULL);
|
win_assert (sync != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
// Windows has no 'socketpair' function. CreatePipe is no good as pipe
|
// Windows has no 'socketpair' function. CreatePipe is no good as pipe
|
||||||
// handles cannot be polled on. Here we create the socketpair by hand.
|
// handles cannot be polled on. Here we create the socketpair by hand.
|
||||||
@ -353,13 +358,21 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
|||||||
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
|
(char *)&tcp_nodelay, sizeof (tcp_nodelay));
|
||||||
wsa_assert (rc != SOCKET_ERROR);
|
wsa_assert (rc != SOCKET_ERROR);
|
||||||
|
|
||||||
|
if (sync != NULL) {
|
||||||
// Enter the critical section.
|
// Enter the critical section.
|
||||||
DWORD dwrc = WaitForSingleObject (sync, INFINITE);
|
DWORD dwrc = WaitForSingleObject (sync, INFINITE);
|
||||||
zmq_assert (dwrc == WAIT_OBJECT_0);
|
zmq_assert (dwrc == WAIT_OBJECT_0);
|
||||||
|
}
|
||||||
|
|
||||||
// Bind listening socket to signaler port.
|
// Bind listening socket to signaler port.
|
||||||
rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
|
rc = bind (listener, (const struct sockaddr*) &addr, sizeof (addr));
|
||||||
|
|
||||||
|
if (rc != SOCKET_ERROR && signaler_port == 0) {
|
||||||
|
// Retrieve ephemeral port number
|
||||||
|
int addrlen = sizeof (addr);
|
||||||
|
rc = getsockname (listener, (struct sockaddr*) &addr, &addrlen);
|
||||||
|
}
|
||||||
|
|
||||||
// Listen for incoming connections.
|
// Listen for incoming connections.
|
||||||
if (rc != SOCKET_ERROR)
|
if (rc != SOCKET_ERROR)
|
||||||
rc = listen (listener, 1);
|
rc = listen (listener, 1);
|
||||||
@ -380,6 +393,7 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
|||||||
// We don't need the listening socket anymore. Close it.
|
// We don't need the listening socket anymore. Close it.
|
||||||
closesocket (listener);
|
closesocket (listener);
|
||||||
|
|
||||||
|
if (sync != NULL) {
|
||||||
// Exit the critical section.
|
// Exit the critical section.
|
||||||
BOOL brc = SetEvent (sync);
|
BOOL brc = SetEvent (sync);
|
||||||
win_assert (brc != 0);
|
win_assert (brc != 0);
|
||||||
@ -387,11 +401,12 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_)
|
|||||||
// Release the kernel object
|
// Release the kernel object
|
||||||
brc = CloseHandle (sync);
|
brc = CloseHandle (sync);
|
||||||
win_assert (brc != 0);
|
win_assert (brc != 0);
|
||||||
|
}
|
||||||
|
|
||||||
if (*r_ != INVALID_SOCKET) {
|
if (*r_ != INVALID_SOCKET) {
|
||||||
# if !defined _WIN32_WCE
|
# if !defined _WIN32_WCE
|
||||||
// On Windows, preventing sockets to be inherited by child processes.
|
// On Windows, preventing sockets to be inherited by child processes.
|
||||||
brc = SetHandleInformation ((HANDLE) *r_, HANDLE_FLAG_INHERIT, 0);
|
BOOL brc = SetHandleInformation ((HANDLE) *r_, HANDLE_FLAG_INHERIT, 0);
|
||||||
win_assert (brc);
|
win_assert (brc);
|
||||||
# endif
|
# endif
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user