mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-29 00:32:34 +08:00
Merge pull request #237 from skaller/master
Fix comments to conform to style guide.
This commit is contained in:
commit
21571cf085
29
src/zmq.cpp
29
src/zmq.cpp
@ -78,6 +78,8 @@
|
||||
typedef char check_msg_t_size
|
||||
[sizeof (zmq::msg_t) == sizeof (zmq_msg_t) ? 1 : -1];
|
||||
|
||||
// Version.
|
||||
|
||||
void zmq_version (int *major_, int *minor_, int *patch_)
|
||||
{
|
||||
*major_ = ZMQ_VERSION_MAJOR;
|
||||
@ -85,11 +87,20 @@ void zmq_version (int *major_, int *minor_, int *patch_)
|
||||
*patch_ = ZMQ_VERSION_PATCH;
|
||||
}
|
||||
|
||||
// Errors.
|
||||
|
||||
const char *zmq_strerror (int errnum_)
|
||||
{
|
||||
return zmq::errno_to_string (errnum_);
|
||||
}
|
||||
|
||||
int zmq_errno ()
|
||||
{
|
||||
return errno;
|
||||
}
|
||||
|
||||
// Contexts.
|
||||
|
||||
static zmq::ctx_t *inner_init (int io_threads_)
|
||||
{
|
||||
if (io_threads_ < 0) {
|
||||
@ -180,6 +191,8 @@ int zmq_term (void *ctx_)
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Sockets.
|
||||
|
||||
void *zmq_socket (void *ctx_, int type_)
|
||||
{
|
||||
if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
|
||||
@ -255,7 +268,8 @@ int zmq_connect (void *s_, const char *addr_)
|
||||
return result;
|
||||
}
|
||||
|
||||
// sending functions
|
||||
// Sending functions.
|
||||
|
||||
static int inner_sendmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_)
|
||||
{
|
||||
int sz = (int) zmq_msg_size (msg_);
|
||||
@ -303,7 +317,8 @@ int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
|
||||
return rc;
|
||||
}
|
||||
|
||||
// receiving functions
|
||||
// Receiving functions.
|
||||
|
||||
static int inner_recvmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_)
|
||||
{
|
||||
int rc = s_->recv ((zmq::msg_t*) msg_, flags_);
|
||||
@ -359,7 +374,8 @@ int zmq_recv (void *s_, void *buf_, size_t len_, int flags_)
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
// message manipulators
|
||||
// Message manipulators.
|
||||
|
||||
int zmq_msg_init (zmq_msg_t *msg_)
|
||||
{
|
||||
return ((zmq::msg_t*) msg_)->init ();
|
||||
@ -420,6 +436,8 @@ int zmq_getmsgopt (zmq_msg_t *msg_, int option_, void *optval_,
|
||||
}
|
||||
}
|
||||
|
||||
// Polling.
|
||||
|
||||
int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
||||
{
|
||||
#if defined ZMQ_POLL_BASED_ON_POLL
|
||||
@ -772,11 +790,6 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
||||
#endif
|
||||
}
|
||||
|
||||
int zmq_errno ()
|
||||
{
|
||||
return errno;
|
||||
}
|
||||
|
||||
#if defined ZMQ_POLL_BASED_ON_SELECT
|
||||
#undef ZMQ_POLL_BASED_ON_SELECT
|
||||
#endif
|
||||
|
@ -17,6 +17,7 @@ if !ON_MINGW
|
||||
noinst_PROGRAMS += test_shutdown_stress \
|
||||
test_pair_ipc \
|
||||
test_reqrep_ipc \
|
||||
test_ts_context \
|
||||
test_timeo
|
||||
endif
|
||||
|
||||
@ -35,6 +36,7 @@ test_shutdown_stress_SOURCES = test_shutdown_stress.cpp
|
||||
test_pair_ipc_SOURCES = test_pair_ipc.cpp testutil.hpp
|
||||
test_reqrep_ipc_SOURCES = test_reqrep_ipc.cpp testutil.hpp
|
||||
test_timeo_SOURCES = test_timeo.cpp
|
||||
test_ts_context_SOURCES = test_ts_context.cpp
|
||||
endif
|
||||
|
||||
TESTS = $(noinst_PROGRAMS)
|
||||
|
129
tests/test_ts_context.cpp
Normal file
129
tests/test_ts_context.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
/*
|
||||
Copyright (c) 2010-2011 250bpm s.r.o.
|
||||
Copyright (c) 2011 iMatix Corporation
|
||||
Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
|
||||
|
||||
This file is part of 0MQ.
|
||||
|
||||
0MQ is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
0MQ is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../include/zmq.h"
|
||||
#include "../include/zmq_utils.h"
|
||||
|
||||
#define THREAD_COUNT 30
|
||||
#define NMESSAGES 20
|
||||
|
||||
struct thread_data_t
|
||||
{
|
||||
int thread_index;
|
||||
void *socket;
|
||||
pthread_t pthr;
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static void *source(void *client_data)
|
||||
{
|
||||
// Wait a bit util all threads created and subscriber ready.
|
||||
zmq_sleep(2); // ms
|
||||
|
||||
// Our thread number and socket.
|
||||
thread_data_t *td = (thread_data_t *) client_data;
|
||||
|
||||
// Buffer for messages.
|
||||
char buffer[20];
|
||||
memset (buffer, 0, 20);
|
||||
|
||||
// Send messages.
|
||||
for (int i = 0; i < NMESSAGES; ++i)
|
||||
{
|
||||
sprintf(buffer,"Th %02d count %02d", td->thread_index, i);
|
||||
int rc = zmq_send(td->socket,buffer,20,0);
|
||||
assert (rc == 20);
|
||||
zmq_sleep(1); // Don't overload the socket.
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
fprintf (stderr, "test_ts_context running...\n");
|
||||
|
||||
// Make a thread safe context.
|
||||
void *ctx = zmq_init_thread_safe (1);
|
||||
assert (ctx);
|
||||
|
||||
// Create a publisher.
|
||||
void *pub = zmq_socket (ctx, ZMQ_PUB);
|
||||
assert (pub);
|
||||
int rc = zmq_bind (pub, "tcp://127.0.0.1:5560");
|
||||
assert (rc == 0);
|
||||
|
||||
// Create a subscriber.
|
||||
void *sub = zmq_socket (ctx, ZMQ_SUB);
|
||||
assert (sub);
|
||||
rc = zmq_connect (sub, "tcp://127.0.0.1:5560");
|
||||
assert (rc == 0);
|
||||
|
||||
// Subscribe for all messages.
|
||||
rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
|
||||
assert (rc == 0);
|
||||
|
||||
thread_data_t threads [THREAD_COUNT];
|
||||
|
||||
// Create workers.
|
||||
for(int i = 0; i < THREAD_COUNT; ++i)
|
||||
{
|
||||
threads [i].thread_index = i;
|
||||
threads [i].socket = pub;
|
||||
rc = pthread_create(&threads[i].pthr, NULL, source, threads+i);
|
||||
assert (rc == 0);
|
||||
}
|
||||
|
||||
// Gather all the Messages.
|
||||
char buff [20];
|
||||
for(int i= 1; i<=THREAD_COUNT * NMESSAGES; ++i)
|
||||
{
|
||||
rc = zmq_recv (sub, buff, 20, 0);
|
||||
//fprintf (stderr, "%d/%d: %s\n",i,THREAD_COUNT * NMESSAGES, buff); // debug it
|
||||
assert (rc >= 0);
|
||||
}
|
||||
|
||||
// Wait for worker death.
|
||||
for(int i = 0; i < THREAD_COUNT; ++i)
|
||||
{
|
||||
rc = pthread_join(threads[i].pthr, NULL);
|
||||
assert (rc == 0);
|
||||
}
|
||||
|
||||
|
||||
// Clean up.
|
||||
rc = zmq_close (pub);
|
||||
assert (rc == 0);
|
||||
rc = zmq_close (sub);
|
||||
assert (rc == 0);
|
||||
rc = zmq_term (ctx);
|
||||
assert (rc == 0);
|
||||
|
||||
return 0 ;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user