Commit 243aba26 authored by Pieter Hintjens's avatar Pieter Hintjens

Merge pull request #765 from pijyoi/master

make win32 signaler support ephemeral ports
parents a9eb6f79 bd41087a
...@@ -306,16 +306,21 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) ...@@ -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.
# if !defined _WIN32_WCE HANDLE sync = NULL;
HANDLE sync = CreateEvent (&sa, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
# else // Create critical section only if using fixed signaler port
HANDLE sync = CreateEvent (NULL, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync")); if (signaler_port != 0) {
# endif # if !defined _WIN32_WCE
if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED) sync = CreateEvent (&sa, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
sync = OpenEvent (SYNCHRONIZE | EVENT_MODIFY_STATE, # else
FALSE, TEXT ("Global\\zmq-signaler-port-sync")); sync = CreateEvent (NULL, FALSE, TRUE, TEXT ("Global\\zmq-signaler-port-sync"));
# endif
win_assert (sync != NULL); if (sync == NULL && GetLastError () == ERROR_ACCESS_DENIED)
sync = OpenEvent (SYNCHRONIZE | EVENT_MODIFY_STATE,
FALSE, TEXT ("Global\\zmq-signaler-port-sync"));
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_) ...@@ -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);
// Enter the critical section. if (sync != NULL) {
DWORD dwrc = WaitForSingleObject (sync, INFINITE); // Enter the critical section.
zmq_assert (dwrc == WAIT_OBJECT_0); DWORD dwrc = WaitForSingleObject (sync, INFINITE);
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,18 +393,20 @@ int zmq::signaler_t::make_fdpair (fd_t *r_, fd_t *w_) ...@@ -380,18 +393,20 @@ 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);
// Exit the critical section. if (sync != NULL) {
BOOL brc = SetEvent (sync); // Exit the critical section.
win_assert (brc != 0); BOOL brc = SetEvent (sync);
win_assert (brc != 0);
// 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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment