2009-07-29 12:07:54 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2009 FastMQ Inc.
|
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the Lesser GNU 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
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#include "../include/zmq.h"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include "i_api.hpp"
|
|
|
|
#include "err.hpp"
|
2009-08-08 16:01:58 +02:00
|
|
|
#include "dispatcher.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "msg.hpp"
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_init (zmq_msg *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
msg_->content = (zmq_msg_content*) ZMQ_VSM;
|
2009-07-29 12:07:54 +02:00
|
|
|
msg_->vsm_size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_init_size (zmq_msg *msg_, size_t size_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
if (size_ <= ZMQ_MAX_VSM_SIZE) {
|
|
|
|
msg_->content = (zmq_msg_content*) ZMQ_VSM;
|
2009-07-29 12:07:54 +02:00
|
|
|
msg_->vsm_size = (uint16_t) size_;
|
|
|
|
}
|
|
|
|
else {
|
2009-08-03 11:30:13 +02:00
|
|
|
msg_->content = (zmq_msg_content*) malloc (sizeof (zmq_msg_content) +
|
2009-07-29 12:07:54 +02:00
|
|
|
size_);
|
|
|
|
if (!msg_->content) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
msg_->shared = 0;
|
|
|
|
|
|
|
|
msg_->content->data = (void*) (msg_->content + 1);
|
|
|
|
msg_->content->size = size_;
|
|
|
|
msg_->content->ffn = NULL;
|
2009-08-03 11:30:13 +02:00
|
|
|
new (&msg_->content->refcnt) zmq::atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_init_data (zmq_msg *msg_, void *data_, size_t size_,
|
|
|
|
zmq_free_fn *ffn_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
msg_->shared = 0;
|
2009-08-03 11:30:13 +02:00
|
|
|
msg_->content = (zmq_msg_content*) malloc (sizeof (zmq_msg_content));
|
|
|
|
zmq_assert (msg_->content);
|
2009-07-29 12:07:54 +02:00
|
|
|
msg_->content->data = data_;
|
|
|
|
msg_->content->size = size_;
|
|
|
|
msg_->content->ffn = ffn_;
|
2009-08-03 11:30:13 +02:00
|
|
|
new (&msg_->content->refcnt) zmq::atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_close (zmq_msg *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// For VSMs and delimiters there are no resources to free
|
2009-08-03 11:30:13 +02:00
|
|
|
if (msg_->content == (zmq_msg_content*) ZMQ_DELIMITER ||
|
|
|
|
msg_->content == (zmq_msg_content*) ZMQ_VSM ||
|
|
|
|
msg_->content == (zmq_msg_content*) ZMQ_GAP)
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If the content is not shared, or if it is shared and the reference
|
|
|
|
// count has dropped to zero, deallocate it.
|
|
|
|
if (!msg_->shared || !msg_->content->refcnt.sub (1)) {
|
|
|
|
|
|
|
|
// We used "placement new" operator to initialize the reference
|
|
|
|
// counter so we call its destructor now.
|
|
|
|
msg_->content->refcnt.~atomic_counter_t ();
|
|
|
|
|
|
|
|
if (msg_->content->ffn)
|
|
|
|
msg_->content->ffn (msg_->content->data);
|
|
|
|
free (msg_->content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_move (zmq_msg *dest_, zmq_msg *src_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_close (dest_);
|
2009-07-29 12:07:54 +02:00
|
|
|
*dest_ = *src_;
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_init (src_);
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_copy (zmq_msg *dest_, zmq_msg *src_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_close (dest_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// VSMs and delimiters require no special handling.
|
|
|
|
if (src_->content !=
|
2009-08-03 11:30:13 +02:00
|
|
|
(zmq_msg_content*) ZMQ_DELIMITER &&
|
|
|
|
src_->content != (zmq_msg_content*) ZMQ_VSM &&
|
|
|
|
src_->content != (zmq_msg_content*) ZMQ_GAP) {
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// One reference is added to shared messages. Non-shared messages
|
|
|
|
// are turned into shared messages and reference count is set to 2.
|
|
|
|
if (src_->shared)
|
|
|
|
src_->content->refcnt.add (1);
|
|
|
|
else {
|
|
|
|
src_->shared = true;
|
|
|
|
src_->content->refcnt.set (2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest_ = *src_;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void *zmq_msg_data (zmq_msg *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
if (msg_->content == (zmq_msg_content*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return msg_->vsm_data;
|
|
|
|
if (msg_->content ==
|
2009-08-03 11:30:13 +02:00
|
|
|
(zmq_msg_content*) ZMQ_DELIMITER ||
|
|
|
|
msg_->content == (zmq_msg_content*) ZMQ_GAP)
|
2009-07-29 12:07:54 +02:00
|
|
|
return NULL;
|
|
|
|
return msg_->content->data;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
size_t zmq_msg_size (zmq_msg *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
if (msg_->content == (zmq_msg_content*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return msg_->vsm_size;
|
|
|
|
if (msg_->content ==
|
2009-08-03 11:30:13 +02:00
|
|
|
(zmq_msg_content*) ZMQ_DELIMITER ||
|
|
|
|
msg_->content == (zmq_msg_content*) ZMQ_GAP)
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
return msg_->content->size;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_msg_type (zmq_msg *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// If it's a genuine message, return 0.
|
2009-08-03 11:30:13 +02:00
|
|
|
if (msg_->content >= (zmq_msg_content*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Trick the compiler to believe that content is an integer.
|
|
|
|
unsigned char *offset = 0;
|
|
|
|
return (((const unsigned char*) msg_->content) - offset);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void *zmq_init (int app_threads_, int io_threads_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-08 16:01:58 +02:00
|
|
|
// There should be at least a single thread managed by the dispatcher.
|
2009-07-29 12:07:54 +02:00
|
|
|
if (app_threads_ < 0 || io_threads_ < 0 ||
|
|
|
|
app_threads_ + io_threads_ == 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
zmq::dispatcher_t *dispatcher = new zmq::dispatcher_t (app_threads_,
|
|
|
|
io_threads_);
|
|
|
|
zmq_assert (dispatcher);
|
|
|
|
return (void*) dispatcher;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
int zmq_term (void *dispatcher_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-08 16:01:58 +02:00
|
|
|
delete (zmq::dispatcher_t*) dispatcher_;
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
void *zmq_socket (void *dispatcher_, int type_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-08 16:01:58 +02:00
|
|
|
return (void*) (((zmq::dispatcher_t*) dispatcher_)->create_socket (type_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_close (void *s_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
((zmq::i_api*) s_)->close ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
int zmq_setsockopt (void *s_, int option_, void *optval_, size_t optvallen_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 09:24:48 +02:00
|
|
|
return (((zmq::i_api*) s_)->setsockopt (option_, optval_, optvallen_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
int zmq_bind (void *s_, const char *addr_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 09:24:48 +02:00
|
|
|
return (((zmq::i_api*) s_)->bind (addr_));
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_connect (void *s_, const char *addr_)
|
|
|
|
{
|
|
|
|
return (((zmq::i_api*) s_)->connect (addr_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_subscribe (void *s_, const char *criteria_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
return (((zmq::i_api*) s_)->subscribe (criteria_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_send (void *s_, zmq_msg *msg_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
return (((zmq::i_api*) s_)->send (msg_, flags_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_flush (void *s_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
return (((zmq::i_api*) s_)->flush ());
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_recv (void *s_, zmq_msg *msg_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
return (((zmq::i_api*) s_)->recv (msg_, flags_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|