[zeromq-dev] Compile libdnzmq with /clr support (no old syntax)
Barak Amar
barak.amar at gmail.com
Thu Feb 12 17:04:42 CET 2009
Saw the .NET extension while playing around with 0MQ. In case you are not
planning to support Mono this code should work without the need to compile
with old syntax of clr support.
-- Barak
Index: dnzmq.cpp
===================================================================
--- dnzmq.cpp (revision 1017)
+++ dnzmq.cpp (working copy)
@@ -18,7 +18,9 @@
*/
#include <zmq.hpp>
+#include <msclr/marshal.h>
+using namespace msclr::interop;
using namespace System;
using namespace System::Runtime::InteropServices;
@@ -32,20 +34,18 @@
zmq::api_thread_t *api_thread;
};
-public __gc class Dnzmq
+public ref class Dnzmq
{
public:
- Dnzmq (String *host_);
+ Dnzmq (String^ host_);
~Dnzmq ();
- int create_exchange (String *exchange_, int scope_, String *nic_);
- int create_queue (String *queue_, int scope_, String *nic_, int64_t
hwm_,
- int64_t lwm_, int64_t swap_size_);
- void bind (String *exchange_, String *queue_,
- String *exchange_arguments_, String *queue_arguments_);
- void send (int eid_, Byte data_ __gc []);
- Byte receive () __gc [];
+ int create_exchange (String^ exchange_, int scope_, String^ nic_);
+ int create_queue (String^ queue_, int scope_, String^ nic_, int64_t
hwm_, int64_t lwm_, int64_t swap_size_);
+ void bind (String^ exchange_, String^ queue_, String^
exchange_arguments_, String^ queue_arguments_);
+ void send (int eid_, array<Byte>^ data_);
+ array<Byte>^ receive ();
static const int SCOPE_LOCAL = 0;
static const int SCOPE_GLOBAL = 1;
@@ -55,10 +55,11 @@
Dnzmq_context_t *context;
};
-Dnzmq::Dnzmq(String *host_)
+Dnzmq::Dnzmq(String^ host_)
{
- // Convert input arguments to char*.
- char* host = (char*)(void*) Marshal::StringToHGlobalAnsi(host_);
+ // Convert input arguments to const char*
+ marshal_context^ marsh = gcnew marshal_context ();
+ const char* host = marsh->marshal_as<const char*> (host_);
// Call WSAStartUp.
WORD version_requested = MAKEWORD (2, 2);
@@ -77,11 +78,9 @@
assert (context->dispatcher);
context->io_thread = zmq::select_thread_t::create
(context->dispatcher);
assert (context->io_thread);
- context->api_thread = zmq::api_thread_t::create (context->dispatcher,
- context->locator);
+ context->api_thread = zmq::api_thread_t::create (context->dispatcher,
context->locator);
assert (context->api_thread);
-
- Marshal::FreeHGlobal(host);
+ delete context;
}
Dnzmq::~Dnzmq ()
@@ -92,11 +91,12 @@
delete context;
}
-int Dnzmq::create_exchange (String *exchange_, int scope_, String *nic_)
+int Dnzmq::create_exchange (String^ exchange_, int scope_, String^ nic_)
{
- // Convert input parameters to char*.
- char* exchange = (char*)(void*) Marshal::StringToHGlobalAnsi
(exchange_);
- char* nic = (char*)(void*) Marshal::StringToHGlobalAnsi (nic_);
+ // Convert input parameters to const char*.
+ marshal_context^ marsh = gcnew marshal_context ();
+ const char* exchange = marsh->marshal_as<const char*> (exchange_);
+ const char* nic = marsh->marshal_as<const char*> (nic_);
// Get the scope.
zmq::scope_t scope = zmq::scope_local;
@@ -107,19 +107,19 @@
int eid = context->api_thread->create_exchange (exchange,
scope, nic, context->io_thread, 1, &context->io_thread);
- // Deallocate the parameters.
- Marshal::FreeHGlobal(exchange);
- Marshal::FreeHGlobal(nic);
+ // Deallocate the context
+ delete context;
return eid;
}
-int Dnzmq::create_queue (String *queue_, int scope_, String *nic_,
+int Dnzmq::create_queue (String^ queue_, int scope_, String^ nic_,
int64_t hwm_, int64_t lwm_, int64_t swap_size_)
{
- // Convert input parameters to char*.
- char* queue = (char*)(void*) Marshal::StringToHGlobalAnsi (queue_);
- char* nic = (char*)(void*) Marshal::StringToHGlobalAnsi (nic_);
+ // Convert input parameters to const char*.
+ marshal_context^ marsh = gcnew marshal_context ();
+ const char* queue = marsh->marshal_as<const char*> (queue_);
+ const char* nic = marsh->marshal_as<const char*> (nic_);
// Get the scope.
zmq::scope_t scope = zmq::scope_local;
@@ -130,56 +130,56 @@
int qid = context->api_thread->create_queue (queue,
scope, nic, context->io_thread, 1, &context->io_thread, hwm_, lwm_,
swap_size_);
- Marshal::FreeHGlobal(queue);
- Marshal::FreeHGlobal(nic);
+ delete marsh;
return qid;
}
-void Dnzmq::bind (String *exchange_, String *queue_,
- String *exchange_arguments_, String *queue_arguments_)
+void Dnzmq::bind (String^ exchange_, String^ queue_,
+ String^ exchange_arguments_, String^ queue_arguments_)
{
- // Convert input parameters to char*.
- char* exchange = (char*)(void*) Marshal::StringToHGlobalAnsi
(exchange_);
- char* queue = (char*)(void*) Marshal::StringToHGlobalAnsi (queue_);
- char* exchange_arguments = (char*)(void*) Marshal::StringToHGlobalAnsi
(exchange_arguments_);
- char* queue_arguments = (char*)(void*) Marshal::StringToHGlobalAnsi
(queue_arguments_);
-
- // Forward the call to native 0MQ library.
- context->api_thread->bind (exchange, queue, context->io_thread,
- context->io_thread, exchange_arguments, queue_arguments);
-
- Marshal::FreeHGlobal(exchange);
- Marshal::FreeHGlobal(queue);
- Marshal::FreeHGlobal(exchange_arguments);
- Marshal::FreeHGlobal(queue_arguments);
+ // Convert input parameters to const char*.
+ marshal_context^ marsh = gcnew marshal_context ();
+ const char* exchange = marsh->marshal_as<const char*> (exchange_);
+ const char* queue = marsh->marshal_as<const char*> (queue_);
+ const char* exchange_arguments = marsh->marshal_as<const char*>
(exchange_arguments_);
+ const char* queue_arguments = marsh->marshal_as<const char*>
(queue_arguments_);
+
+ // Forward the call to native 0MQ library.
+ context->api_thread->bind (exchange, queue, context->io_thread,
+ context->io_thread, exchange_arguments, queue_arguments);
+
+ delete context;
}
-void Dnzmq::send (int eid_, Byte data_ __gc [])
+void Dnzmq::send (int eid_, array<Byte>^ data_)
{
- // Pin the data to the fixed memory location so that it doesn't
- // get moved during the call to memcpy.
- byte __pin *tmp_data = &data_ [0];
-
// Copy data to the unmanaged buffer.
void *data = malloc (data_->Length);
assert (data);
- memcpy (data, (byte*) tmp_data, data_->Length);
+
+ // Pin the data to the fixed memory location so that it doesn't
+ // get moved during the call to memcpy.
+ // pinning just for the copy operation
+ {
+ pin_ptr<byte> tmp_data = &data_ [0];
+ memcpy (data, (byte*) tmp_data, data_->Length);
+ }
// Forward the call to native 0MQ library.
zmq::message_t msg (data, data_->Length, free);
context->api_thread->send (eid_, msg);
}
-Byte Dnzmq::receive () __gc []
+array<Byte>^ Dnzmq::receive ()
{
// Forward the call to native 0MQ library.
zmq::message_t msg ;
context->api_thread->receive (&msg);
// Allocate a managed array and pin it down to the memory.
- Byte data __gc [] = new Byte __gc [msg.size ()];
- byte __pin *tmp_data = &data [0];
+ array<Byte>^ data = gcnew array<Byte> (msg.size ());
+ pin_ptr<byte> tmp_data = &data [0];
// Copy the message to the managed buffer.
memcpy ((byte*) tmp_data, msg.data (), msg.size ());
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20090212/aea310a5/attachment.htm>
More information about the zeromq-dev
mailing list