Debian Codegraph @

protolib/src/common/protoZMQ.cpp

raw
1 #include "protoZMQ.h"
2 #include "protoDebug.h"
3
4 #ifdef WIN32
5 #include <processthreadsapi.h> // for GetCurrentProcessId()
6 #else
7 #include <unistd.h> // for getpid()
8 #endif
9
10 // Static member initializations
11 ProtoZmq::PollerThread* ProtoZmq::Socket::default_poller_thread = NULL;
12 ProtoMutex ProtoZmq::Socket::default_poller_mutex;
13
14 ProtoZmq::Socket::Socket()
15 : state(CLOSED), zmq_ctx(NULL), zmq_sock(NULL), poll_flags(ZMQ_POLLIN)
16 {
17 }
18
19 ProtoZmq::Socket::~Socket()
20 {
21 Close();
22 }
23
24 bool ProtoZmq::Socket::Open(int socketType, void* zmqSocket, void* zmqContext)
25 {
26 if (IsOpen()) Close();
27 ext_ctx = false;
28 if (NULL == zmqContext)
29 zmqContext = zmq_ctx_new();
30 else
31 ext_ctx = true;
32 if (NULL == zmqContext)
33 {
34 PLOG(PL_ERROR, " ProtoZmq::Socket::Open() zmq_ctx_new() error: %s\n", GetErrorString());
35 return false;
36 }
37 zmq_ctx = zmqContext;
38 ext_sock = false;
39 if (NULL == zmqSocket)
40 zmqSocket = zmq_socket(zmq_ctx, socketType);
41 else
42 ext_sock = true;
43 if (NULL == zmqSocket)
44 {
45 PLOG(PL_ERROR, " ProtoZmq::Socket::Open() zmq_socket() error: %s\n", GetErrorString());
46 Close();
47 return false;
48 }
49 zmq_sock = zmqSocket;
50 if (!ProtoEvent::Open())
51 {
52
53 PLOG(PL_ERROR, " ProtoZmq::Socket::Open() ProtoEvent::Open() error: %s\n", GetErrorString());
54 Close();
55 return false;
56 }
57 state = IDLE;
58 return true;
59 } // emd ProtoZmq::Socket::Open()
60
61 void ProtoZmq::Socket::Close()
62 {
63 state = CLOSED;
64 UpdateNotification();
65 if (ProtoEvent::IsOpen())
66 ProtoEvent::Close();
67 if (NULL != zmq_sock)
68 {
69 if (!ext_sock)
70 zmq_close(zmq_sock);
71 zmq_sock = NULL;
72 ext_sock = false;
73 }
74 if (NULL != zmq_ctx)
75 {
76 if (!ext_ctx)
77 zmq_ctx_term(zmq_ctx);
78 zmq_ctx = NULL;
79 ext_ctx = false;
80 }
81
82 } // end ProtoZmq::Socket::Close()
83
84 bool ProtoZmq::Socket::StartInputNotification()
85 {
86 if (0 == (ZMQ_POLLIN & poll_flags))
87 {
88 poll_flags |= ZMQ_POLLIN;
89 if (!UpdateNotification())
90 {
91 PLOG(PL_ERROR, "ProtoZmq::Socket::StartInputNotification() error: unable to update notifications\n");
92 poll_flags &= ~ZMQ_POLLIN;
93 return false;
94 }
95 }
96 return true;
97 } // end ProtoZmq::Socket::StartInputNotification()
98
99 bool ProtoZmq::Socket::StopInputNotification()
100 {
101 if (0 != (ZMQ_POLLIN & poll_flags))
102 {
103 poll_flags &= ~ZMQ_POLLIN;
104 if (!UpdateNotification())
105 {
106 PLOG(PL_ERROR, "ProtoZmq::Socket::StopInputNotification() error: unable to update notifications\n");
107 poll_flags |= ~ZMQ_POLLIN;
108 return false;
109 }
110 }
111 return true;
112 } // end ProtoZmq::Socket::StartInputNotification()
113
114 bool ProtoZmq::Socket::StartOutputNotification()
115 {
116 if (0 == (ZMQ_POLLOUT & poll_flags))
117 {
118 poll_flags |= ZMQ_POLLOUT;
119 if (!UpdateNotification())
120 {
121 PLOG(PL_ERROR, "ProtoZmq::Socket::StartOutputNotification() error: unable to update notifications\n");
122 poll_flags &= ~ZMQ_POLLOUT;
123 return false;
124 }
125 }
126 return true;
127 } // end ProtoZmq::Socket::StopInputNotification()
128
129 bool ProtoZmq::Socket::StopOutputNotification()
130 {
131 if (0 != (ZMQ_POLLOUT & poll_flags))
132 {
133 poll_flags &= ~ZMQ_POLLOUT;
134 if (!UpdateNotification())
135 {
136 PLOG(PL_ERROR, "ProtoZmq::Socket::StopOutputNotification() error: unable to update notifications\n");
137 poll_flags |= ~ZMQ_POLLOUT;
138 return false;
139 }
140 }
141 return true;
142 } // end ProtoZmq::Socket::StopOutputNotification()
143
144 bool ProtoZmq::Socket::SetNotifier(ProtoEvent::Notifier* theNotifier, PollerThread* pollerThread)
145 {
146 if (NULL != theNotifier)
147 {
148 if (theNotifier == GetNotifier()) return true; // already set
149
150 if (HasNotifier())
151 {
152 if ((NULL != poller_thread) && poller_active)
153 {
154 poller_thread->RemoveSocket(*this);
155 poller_active = false;
156 }
157 ProtoEvent::SetNotifier(NULL);
158 }
159 if (NULL == pollerThread)
160 {
161 // Use default poller thread
162 default_poller_mutex.Lock();
163 if (NULL == default_poller_thread)
164 {
165 if (NULL == (default_poller_thread = new PollerThread()))
166 {
167 PLOG(PL_ERROR, "ProtoZmq::Socket::SetNotifier() new default_poller_thread error: %s\n", GetErrorString());
168 default_poller_mutex.Unlock();
169 return false;
170 }
171 }
172 poller_thread = default_poller_thread;
173 default_poller_mutex.Unlock();
174 }
175 else
176 {
177 poller_thread = pollerThread;
178 }
179 }
180 else if (HasNotifier())
181 {
182 if ((NULL != poller_thread) && poller_active)
183 {
184 poller_thread->RemoveSocket(*this);
185 poller_active = false;
186 }
187 poller_thread = NULL;
188 }
189 ProtoEvent::SetNotifier(theNotifier);
190 return UpdateNotification(); // will install socket to poller_thread as needed
191 } // end ProtoZmq::Socket::SetNotifier()
192
193 bool ProtoZmq::Socket::UpdateNotification()
194 {
195 if (CONNECTED == state)
196 {
197 if (HasNotifier())
198 {
199 ASSERT(NULL != poller_thread);
200 if (poller_active)
201 {
202 if (0 != poll_flags)
203 {
204 if (!poller_thread->ModSocket(*this))
205 {
206 PLOG(PL_ERROR, "ProtoZmq::Socket::UpdateNotification() error: unable to modify socket on poller_thread!\n");
207 return false;
208 }
209 }
210 else
211 {
212 poller_thread->RemoveSocket(*this);
213 poller_active = false;
214 }
215 }
216 else
217 {
218
219 if (!poller_thread->AddSocket(*this))
220 {
221 PLOG(PL_ERROR, "ProtoZmq::Socket::UpdateNotification() error: unable to add socket to poller_thread!\n");
222 return false;
223 }
224 poller_active = true;
225 }
226 }
227 }
228 else if ((NULL != poller_thread) && poller_active)
229 {
230 poller_thread->RemoveSocket(*this);
231 poller_active = false;
232 }
233 return true;
234 } // end ProtoZmq::Socket::UpdateNotification()
235
236 bool ProtoZmq::Socket::Bind(const char* endpoint)
237 {
238 if (!IsOpen())
239 {
240 PLOG(PL_ERROR, "ProtoZmq::Socket::Bind() error: socket not open!\n");
241 return false;
242 }
243 if (0 != zmq_bind(zmq_sock, endpoint))
244 {
245 PLOG(PL_ERROR, "ProtoZmq::Socket::Bind() zmq_bind() error: %s\n", GetErrorString());
246 return false;
247 }
248 state = CONNECTED; // ZMQ sockets are immediately "connected" since ZMQ state routines will be used
249 if (!UpdateNotification())
250 {
251 PLOG(PL_ERROR, "ProtoZmq::Socket::Bind() error: unable to update notification status\n");
252 Close();
253 return false;
254 }
255 return true;
256 } // end ProtoZmq::Socket::Bind()
257
258 bool ProtoZmq::Socket::Connect(const char* endpoint)
259 {
260 if (!IsOpen())
261 {
262 PLOG(PL_ERROR, "ProtoZmq::Socket::Connect() error: socket not open!\n");
263 return false;
264 }
265 if (0 != zmq_connect(zmq_sock, endpoint))
266 {
267 PLOG(PL_ERROR, "ProtoZmq::Socket::Connect() zmq_bind() error: %s\n", GetErrorString());
268 return false;
269 }
270 state = CONNECTED; // ZMQ sockets are immediately "connected" since ZMQ state routines will be used
271 if (!UpdateNotification())
272 {
273 PLOG(PL_ERROR, "ProtoZmq::Socket::Connect() error: unable to update notification status\n");
274 Close();
275 return false;
276 }
277 return true;
278 } // end ProtoZmq::Socket::Connect()
279
280
281 bool ProtoZmq::Socket::Subscribe(const char* prefix, unsigned int length)
282 {
283 if (NULL == zmq_sock)
284 {
285 PLOG(PL_ERROR, "ProtoZmq::Socket::Subscribe() error: socket not open\n");
286 return false;
287 }
288 if (NULL == prefix)
289 length = 0;
290 else if (0 == length)
291 length = (unsigned int)strlen(prefix);
292 if (0 != zmq_setsockopt(zmq_sock, ZMQ_SUBSCRIBE, prefix, length))
293 {
294 PLOG(PL_ERROR, "ProtoZmq::Socket::Subscribe() zmq_setsockopt(ZMQ_SUBSCRIBE) error: %s\n", zmq_strerror(zmq_errno()));
295 return false;
296 }
297 return true;
298 } // end ProtoZmq::Socket::Subscribe()
299
300 // for ZMQ_DISH sockets
301 bool ProtoZmq::Socket::Join(const char* group)
302 {
303 if (NULL == zmq_sock)
304 {
305 PLOG(PL_ERROR, "ProtoZmq::Socket::Join() error: socket not open\n");
306 return false;
307 }
308 if (0 != zmq_join(zmq_sock, group))
309 {
310 PLOG(PL_ERROR, "ProtoZmq::Socket::Join() zmq_join() error: %s\n", zmq_strerror(zmq_errno()));
311 return false;
312 }
313 return true;
314 } // end ProtoZmq::Socket::Join()
315
316 bool ProtoZmq::Socket::Send(char* buffer, unsigned int& numBytes)
317 {
318 // TBD - support both blocking and non-blocking operation as well as multi-part messages
319 int result = zmq_send(zmq_sock, buffer, numBytes, ZMQ_DONTWAIT);
320 if (poller_active && (0 != (ZMQ_POLLOUT & poll_flags)))
321 UpdateNotification(); // resets poll_flags and PollerThread notification
322 if (result < 0)
323 {
324 switch (zmq_errno())
325 {
326 case EAGAIN:
327 case EINTR: // do we need to have a loop here to retry on EINTR?
328 numBytes = 0;
329 break;
330 default:
331 PLOG(PL_ERROR, "ProtoZmq::Socket::Send() zmq_send() error: %s\n", GetErrorString());
332 numBytes = 0;
333 return false;
334 }
335 }
336 else
337 {
338 // TBD - do we need to handle 0 == result differently
339 numBytes = result;
340 }
341 return true;
342 } // end ProtoZmq::Socket::Send()
343
344 bool ProtoZmq::Socket::Recv(char* buffer, unsigned int& numBytes)
345 {
346 int result = zmq_recv(zmq_sock, buffer, numBytes, ZMQ_DONTWAIT);
347 if (poller_active && (0 != (ZMQ_POLLIN & poll_flags)))
348 UpdateNotification(); // resets poll_flags and PollerThread notification
349 if (result < 0)
350 {
351 switch (zmq_errno())
352 {
353 case EAGAIN:
354 case EINTR: // do we need to have a loop here to retry on EINTR?
355 numBytes = 0;
356 break;
357 default:
358 PLOG(PL_ERROR, "ProtoZmq::Socket::Recv() zmq_recv() error: %s\n", GetErrorString());
359 numBytes = 0;
360 return false;
361 }
362 }
363 else
364 {
365 // TBD - do we need to handle 0 == result differently
366 numBytes = result;
367 }
368 return true;
369 } // end ProtoZmq::Socket::Recv()
370
371 bool ProtoZmq::Socket::RecvMsg(zmq_msg_t* zmqMsg)
372 {
373 int result = zmq_msg_recv(zmqMsg, zmq_sock, ZMQ_DONTWAIT);
374 if (poller_active && (0 != (ZMQ_POLLIN & poll_flags)))
375 UpdateNotification(); // resets poll_flags and PollerThread notification
376 if (result < 0)
377 {
378 switch (zmq_errno())
379 {
380 case EAGAIN:
381 case EINTR: // do we need to have a loop here to retry on EINTR?
382 break;
383 default:
384 PLOG(PL_ERROR, "ProtoZmq::Socket::RecvMsg() zmq_recv() error: %s\n", GetErrorString());
385 return false;
386 }
387 }
388 return true;
389 } // end ProtoZmq::Socket::Recv()
390
391 ProtoZmq::PollerThread::PollerThread()
392 : zmq_ctx(NULL), ext_ctx(false), zmq_poller(NULL), event_array(NULL),
393 event_array_length(0), socket_count(0)
394 {
395 }
396
397 ProtoZmq::PollerThread::~PollerThread()
398 {
399 // This code guarantees safe destruction of the default poller thread
400 bool isDefault = (this == ProtoZmq::Socket::default_poller_thread);
401 if (isDefault)
402 {
403 ProtoZmq::Socket::default_poller_mutex.Lock();
404 if (NULL == ProtoZmq::Socket::default_poller_thread)
405 {
406 ProtoZmq::Socket::default_poller_mutex.Unlock();
407 return; // another thread already cleaned it up.
408 }
409 }
410 Close();
411 if (isDefault)
412 {
413 ProtoZmq::Socket::default_poller_thread = NULL;
414 ProtoZmq::Socket::default_poller_mutex.Unlock();
415 }
416 }
417
418 bool ProtoZmq::PollerThread::Open(void* zmqContext, bool retainLock)
419 {
420 suspend_mutex.Lock();
421 if (NULL != zmq_poller)
422 {
423 // This makes sure we don't conflict with another thread who was already opening the thread
424 if (!retainLock) suspend_mutex.Unlock();
425 return true;
426 }
427 if (NULL == (zmq_poller = zmq_poller_new()))
428 {
429 PLOG(PL_ERROR, "ProtoZmq::PollerThread::Open() zmoq_poller_new() error %s\n", zmq_strerror(zmq_errno()));
430 suspend_mutex.Unlock();
431 return false;
432 }
433
434 ext_ctx = false;
435 if (NULL == zmqContext)
436 zmqContext = zmq_ctx_new();
437 else
438 ext_ctx = true;
439 if (NULL == zmqContext)
440 {
441 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() zmq_ctx_new() error: %s\n", zmq_strerror(zmq_errno()));
442 ClosePrivate();
443 suspend_mutex.Unlock();
444 return false;
445 }
446 zmq_ctx = zmqContext;
447
448 // We use ZMQ_SERVER and ZMQ_CLIENT for the break_server/break_client because they are thread safe
449 if (!break_server.Open(ZMQ_SERVER, NULL, zmq_ctx))
450 {
451 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() error: unable to open break_server\n");
452 ClosePrivate();
453 suspend_mutex.Unlock();
454 return false;
455 }
456 // Create a ZMQ "inproc" socket name unique to this process/pollerThread
457 char endpoint[256];
458 endpoint[255] = '\0';
459 #ifdef WIN32
460 snprintf(endpoint, 255, "inproc://ProtoZmq-%d-%p", GetCurrentProcessId(), this);
461 #else
462 snprintf(endpoint, 255, "inproc://ProtoZmq-%d-%p",getpid(), this);
463 #endif // WIN32/UNIX
464 if (!break_server.Bind(endpoint))
465 {
466 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() error: unable to bind break_server\n");
467 ClosePrivate();
468 suspend_mutex.Unlock();
469 return false;
470 }
471 if (!AddSocketPrivate(break_server))
472 {
473 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() error: unable to add break_server socket to poller\n");
474 ClosePrivate();
475 suspend_mutex.Unlock();
476 return false;
477 }
478 if (!break_client.Open(ZMQ_CLIENT, NULL, zmq_ctx))
479 {
480 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() error: unable to open break_client\n");
481 ClosePrivate();
482 suspend_mutex.Unlock();
483 return false;
484 }
485 if (!break_client.Connect(endpoint))
486 {
487 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() error: unable to connect break_client\n");
488 ClosePrivate();
489 suspend_mutex.Unlock();
490 return false;
491 }
492 if (!StartThread())
493 {
494 PLOG(PL_ERROR, " ProtoZmq::PollerThread::Open() error: unable to start poller thread!\n");
495 ClosePrivate();
496 suspend_mutex.Unlock();
497 }
498 while(!poller_running);// TRACE("waiting for thread to start\n"); // makes sure the ProtoThread::RunThread() has been entered
499 if (!retainLock) suspend_mutex.Unlock();
500 return true;
501 } // end ProtoZmq::PollerThread::Open()
502
503 void ProtoZmq::PollerThread::Close()
504 {
505 suspend_mutex.Lock();
506 ClosePrivate();
507 suspend_mutex.Unlock();
508 if (this == ProtoZmq::Socket::default_poller_thread)
509 {
510 delete ProtoZmq::Socket::default_poller_thread; // its destructor NULLs default_poller_thread pointer
511 }
512 } // end ProtoZmq::PollerThread::Close()
513
514
515 void ProtoZmq::PollerThread::ClosePrivate()
516 {
517 if (poller_running)
518 {
519 poller_running = false;
520 if (Signal()) // sends message to break out of zmq_poller->poll()
521 {
522 suspend_mutex.Unlock(); // allows thread to exit
523 StopThread(); // sets exit_code and closes thread operation
524 suspend_mutex.Lock();
525 signal_mutex.Unlock();
526 }
527 // else someone else already stopped it
528 }
529 // TBD - remove all sockets from poller
530
531 if (break_client.IsOpen())
532 {
533 break_client.Close();
534 }
535 if (break_server.IsOpen()) break_server.Close();
536
537 if (NULL != zmq_ctx)
538 {
539 if (!ext_ctx)
540 zmq_ctx_term(zmq_ctx);
541 zmq_ctx = NULL;
542 ext_ctx = false;
543 }
544 if (NULL != zmq_poller)
545 {
546 zmq_poller_destroy(&zmq_poller);
547 zmq_poller = NULL;
548 }
549 } // end ProtoZmq::PollerThread::ClosePrivate()
550
551 int ProtoZmq::PollerThread::RunThread()
552 {
553 poller_running = true;
554 suspend_mutex.Lock();
555 while(poller_running)
556 {
557 signal_mutex.Lock();
558 suspend_mutex.Unlock();
559 int result = zmq_poller_wait_all(zmq_poller, event_array, event_array_length, -1);
560 signal_mutex.Unlock();
561 // <-- this is where a the loop waits after PollerThread::Signal() is called
562 suspend_mutex.Lock();
563 if (!poller_running) break;
564 if (result < 0)
565 {
566 switch (zmq_errno())
567 {
568 case ETERM:
569 PLOG(PL_ERROR, " ProtoZmq::PollerThread::RunThread() zmq_poller_wait_all() error: %s\n", zmq_strerror(ETERM));
570 poller_running = false;
571 break;
572 case EINTR:
573 case EAGAIN: // this one shouldn't happen
574 break;
575 default:
576 PLOG(PL_ERROR, " ProtoZmq::PollerThread::RunThread() zmq_poller_wait_all() error: %s\n", zmq_strerror(zmq_errno()));
577 poller_running = false;
578 break;
579 }
580 continue;
581 }
582 // Dispatch events for signaled sockets
583 for (int i = 0; i < result; i++)
584 {
585 zmq_poller_event_t* item = event_array + i;
586 if (break_server.GetSocket() == item->socket)
587 {
588 // It's our "break" socket, so consume any 'break' messages
589 char dummy[64];
590 unsigned int numBytes;
591 do
592 {
593 numBytes = 64;
594 if (!break_server.Recv(dummy, numBytes))
595 {
596 PLOG(PL_ERROR, "ProtoZmq::PollerThread::RunThread() error: break_server.Recv() failure!\n");
597 break;
598 }
599 } while (0 != numBytes);
600 continue;
601 }
602 Socket* zmqSocket = reinterpret_cast<Socket*>(item->user_data);
603 // Filter events so we don't get redundant polling notifications.
604 // (Will be reset by Socket::Recv() and/or Socket::Send() methods when invoked)
605 zmqSocket->SetPollStatus(item->events);
606 int tempFlags = zmqSocket->GetPollFlags() & ~item->events;
607 zmq_poller_modify(zmq_poller, item->socket, tempFlags | ZMQ_POLLERR);
608 zmqSocket->SetEvent();
609 }
610 }
611 suspend_mutex.Unlock();
612 return ProtoThread::GetExitCode();
613 } // end ProtoZmq::PollerThread::RunThread()
614
615 bool ProtoZmq::PollerThread::AddSocket(Socket& zmqSocket)
616 {
617 void* zmqContext = zmqSocket.UsingExternalContext() ? zmqSocket.GetContext() : NULL;
618 // Make sure poller thread is open and we are in control
619 if (!Open(zmqContext, true))
620 {
621 PLOG(PL_ERROR, "ProtoZmq::PollerThread::AddSocket() error: unable to open/start poller thread!\n");
622 return false;
623 }
624 // At this point, we have an open, running poller thread and we need to signal it and add the socket
625 if (!Signal())
626 {
627 // Another thread shut the poller down? (This should happen after the Open(), though)
628 PLOG(PL_ERROR, "ProtoZmq::PollerThread::AddSocket() error: unable to signal thread!\n");
629 suspend_mutex.Unlock();
630 return false;
631 }
632 if (!AddSocketPrivate(zmqSocket))
633 {
634 PLOG(PL_ERROR, "ProtoZmq::PollerThread::AddSocket() error: unable to add socket to poller!\n");
635 Unsignal();
636 suspend_mutex.Unlock();
637 return false;
638 }
639 Unsignal();
640 suspend_mutex.Unlock();
641 return true;
642 } // end ProtoZmq::PollerThread::AddSocket()
643
644 bool ProtoZmq::PollerThread::ModSocket(Socket& zmqSocket)
645 {
646 suspend_mutex.Lock();
647 ASSERT (NULL != zmq_poller);
648 if (!Signal())
649 {
650 // Another thread shut the poller down?
651 PLOG(PL_ERROR, " ProtoZmq::PollerThread::ModSocket() error: unable to signal poller thread!\n");
652 suspend_mutex.Unlock();
653 return false;
654 }
655 if (-1 == zmq_poller_modify(zmq_poller, zmqSocket.GetSocket(), zmqSocket.GetPollFlags() | ZMQ_POLLERR))
656 {
657 PLOG(PL_ERROR, " ProtoZmq::PollerThread::ModSocket() zmq_poller_modify() error: %s\n", zmq_strerror(zmq_errno()));
658 Unsignal();
659 suspend_mutex.Unlock();
660 return false;
661 }
662 Unsignal();
663 suspend_mutex.Unlock();
664 return true;
665 } // end ProtoZmq::PollerThread::ModSocket()
666
667 bool ProtoZmq::PollerThread::AddSocketPrivate(Socket& zmqSocket)
668 {
669 if (socket_count >= event_array_length)
670 {
671 // Need to increase event_array size to add socket
672 unsigned int length = (0 != event_array_length) ? event_array_length*2 : 2;
673 zmq_poller_event_t* tempArray = new zmq_poller_event_t[length];
674 if (NULL == tempArray)
675 {
676 PLOG(PL_ERROR, " ProtoZmq::PollerThread::AddSocketPrivate() new event_array error: %s\n", GetErrorString());
677 return false;
678 }
679 if (NULL != event_array) delete[] event_array; // delete old array
680 event_array = tempArray; // point to new array
681 event_array_length = length; // save new length
682 }
683 if (-1 == zmq_poller_add(zmq_poller, zmqSocket.GetSocket(), &zmqSocket, zmqSocket.GetPollFlags() | ZMQ_POLLERR))
684 {
685 PLOG(PL_ERROR, " ProtoZmq::PollerThread::AddSocketPrivate() zmq_poller_add() error: %s\n", zmq_strerror(zmq_errno()));
686 return false;
687 }
688 socket_count += 1;
689 return true;
690 } // end ProtoZmq::PollerThread::AddSocketPrivate()
691
692 bool ProtoZmq::PollerThread::RemoveSocket(Socket& zmqSocket)
693 {
694 suspend_mutex.Lock();
695 if (NULL == zmq_poller)
696 {
697 // Already shutdown
698 suspend_mutex.Unlock();
699 return true;
700 }
701 if (!Signal())
702 {
703 // Another thread shut the poller down?
704 PLOG(PL_ERROR, " ProtoZmq::PollerThread::ModSocket() error: unable to signal poller thread!\n");
705 suspend_mutex.Unlock();
706 return false;
707 }
708 if (-1 == zmq_poller_remove(zmq_poller, zmqSocket.GetSocket()))
709 {
710 PLOG(PL_ERROR, " ProtoZmq::PollerThread::RemoveSocket() zmq_poller_remove() error: %s\n", zmq_strerror(zmq_errno()));
711 Unsignal();
712 suspend_mutex.Unlock();
713 return false;
714 }
715 socket_count -= 1;
716 Unsignal();
717 if (socket_count < 2)
718 ClosePrivate(); // terminate poller thread when there are no active sockets
719 suspend_mutex.Unlock();
720 return true;
721 } // end ProtoZmq::PollerThread::RemoveSocket()
722
723 bool ProtoZmq::PollerThread::Signal()
724 {
725 // IMPORTANT - suspend_mutex MUST be locked before callin this!!!
726 // stops thread in known state outside of zmq_poller->poll() call
727 if (IsStarted() && !IsMyself())
728 {
729 char dummy = 0;
730 unsigned int one;
731 // The do loop makes sure that the message is sent regardless of interrupts
732 do
733 {
734 one = 1;
735 if (!break_client.Send(&dummy, one))
736 {
737 PLOG(PL_ERROR, "ProtoDispatcher::Signal() error: SetBreak() failed!\n");
738 return false;
739 }
740 } while (0 == one);
741 signal_mutex.Lock();
742 }
743 if (!poller_running)
744 {
745 // Somebody else closed the poller before we grabbed the signal_mutex
746 signal_mutex.Unlock();
747 return false;
748 }
749 return true;
750 } // end ProtoZmq::PollerThread::Signal()
751
752 void ProtoZmq::PollerThread::Unsignal()
753 {
754 // Resumes signaled thread in known state outside of zmq_poller->poll() call
755 if (IsStarted() && !IsMyself())
756 {
757 signal_mutex.Unlock();
758 }
759 } // end ProtoZmq::PollerThread::Unsignal()
760