Hi,<br><br>I'm having a problem wrapping the Rabbit MQ C client<br>library (<a href="https://github.com/alanxz/rabbitmq-c">https://github.com/alanxz/rabbitmq-c</a>) using the ffi. My<br>problem is with the amqp_queue_declare function. The C<br>
declaration is:<br><br>amqp_queue_declare_ok_t *<br>amqp_queue_declare(amqp_connection_state_t state,<br> amqp_channel_t channel,<br> amqp_bytes_t queue,<br> amqp_boolean_t passive,<br>
amqp_boolean_t durable,<br> amqp_boolean_t exclusive,<br> amqp_boolean_t auto_delete,<br> amqp_table_t arguments);<br><br>It seems to run successfully when wrapped to return an opaque<br>
pointer:<br><br>(define _amqp-queue-declare-ok-pointer (_cpointer 'QUEUE-DECLARE-OK))<br><br>(define-amqp amqp_queue_declare<br> (_fun _amqp-connection-state-pointer<br> _amqp_channel_t _pointer _int _int _int _int _pointer<br>
-> _amqp-queue-declare-ok-pointer))<br><br>> #<cpointer:QUEUE-DECLARE-OK><br><br>Wrapping it to return a cstruct pointer fails with a SEGV.<br><br>> SIGSEGV MAPERR si_code 1 fault on addr (nil)<br><br>
The wrapping code for the c structs is as follows:<br><br>(define _size_t _uint32)<br>(define _amqp_channel_t _uint16)<br><br>(define-cstruct _amqp_bytes_t<br> ([len _size_t]<br> [bytes _pointer]))<br><br>;; c definition of amqp_bytes_t is:<br>
;; typedef struct amqp_bytes_t_ {<br>;; size_t len;<br>;; void *bytes;<br>;; } amqp_bytes_t;<br><br>(define-cstruct _amqp_queue_declare_ok_t<br> ([queue _amqp_bytes_t]<br> [message_count _uint32]<br> [consumer_count _uint32]))<br>
<br>;; typedef struct amqp_queue_declare_ok_t_ {<br>;; amqp_bytes_t queue;<br>;; uint32_t message_count;<br>;; uint32_t consumer_count;<br>;; } amqp_queue_declare_ok_t;<br><br>(define-amqp amqp_queue_declare<br> (_fun _amqp-connection-state-pointer<br>
_amqp_channel_t _pointer _int _int _int _int _pointer<br> -> _amqp_queue_declare_ok_t))<br><br>Any ideas what I'm doing wrong here?<br><br>The full code so far is below:<br><br>#lang racket/base<br><br>
(require ffi/unsafe<br> ffi/unsafe/define)<br><br>(define lib-rabbit-mq (ffi-lib "librabbitmq"))<br>(define-ffi-definer define-amqp lib-rabbit-mq)<br><br>;; C types<br><br>(define _size_t _uint32)<br>(define _amqp_channel_t _uint16)<br>
<br>(define-cstruct _amqp_bytes_t<br> ([len _size_t]<br> [bytes _pointer]))<br><br>(define-cstruct _amqp_queue_declare_ok_t<br> ([queue _amqp_bytes_t]<br> [message_count _uint32]<br> [consumer_count _uint32]))<br>
<br>(define-cstruct _amqp_method_t<br> ([id _uint32]<br> [decoded _pointer]))<br><br>(define-cstruct _amqp_rpc_reply_t<br> ([reply_type _int]<br> [reply _amqp_method_t]<br> [library_error _int]))<br><br>(define _amqp-connection-state-pointer (_cpointer 'CONNECTION-STATE))<br>
(define _amqp-channel-open_ok-pointer (_cpointer 'CHANNEL-OPEN-OK))<br><br>;; C functions<br><br>(define-amqp amqp_error_string (_fun _int -> _string))<br>(define-amqp amqp_get_rpc_reply (_fun _amqp-connection-state-pointer -><br>
_amqp_rpc_reply_t))<br><br>(define-amqp amqp_new_connection (_fun -> _amqp-connection-state-pointer))<br>(define-amqp amqp_open_socket (_fun _string _int -> _int))<br>(define-amqp amqp_set_sockfd (_fun _amqp-connection-state-pointer<br>
_int -> _int))<br>(define-amqp amqp_login (_fun _amqp-connection-state-pointer<br> _string _int _int _int _int _string _string -><br> _amqp_rpc_reply_t))<br>
(define-amqp amqp_channel_open (_fun _amqp-connection-state-pointer<br> _amqp_channel_t -><br> _amqp-channel-open_ok-pointer))<br><br>;; *** THIS DOES NOT WORK<br>
<br>(define-amqp amqp_queue_declare<br> (_fun _amqp-connection-state-pointer<br> _amqp_channel_t _pointer _int _int _int _int _pointer<br> -> _amqp_queue_declare_ok_t))<br><br>;; *** THIS SEEMS TO WORK<br>
<br>(define _amqp-queue-declare-ok-pointer (_cpointer 'QUEUE-DECLARE-OK))<br><br>(define-amqp amqp_queue_declare<br> (_fun _amqp-connection-state-pointer<br> _amqp_channel_t _pointer _int _int _int _int _pointer<br>
-> _amqp-queue-declare-ok-pointer))<br><br>;; main<br><br>(define conn (amqp_new_connection))<br>(define sockfd (amqp_open_socket "localhost" 5672))<br>(when (< sockfd 0) (amqp_error_string (* -1 sockfd)))<br>
(amqp_set_sockfd conn sockfd)<br>(define *AMQP_SASL_METHOD_PLAIN* 0)<br>(define resp (amqp_login conn "/" 0 131072 0<br> *AMQP_SASL_METHOD_PLAIN* "user" "pwd"))<br><br>
(amqp_channel_open conn 1)<br>(amqp_queue_declare conn 1 #f 0 0 0 1 #f)<br>