1
votes

I am trying to create C-style structs in Clojure, so I can call a poorly documented C++ API from Clojure.

The API is designed to send and receive serialized protobuf messages (the good) preceded by a C Header struct (the bad). The initial handshake is an RPCHandshakeHeader struct and the process is roughly described in the code below:

struct RPCHandshakeHeader {
    char magic[8];
    int version;

    static const char REQUEST_MAGIC[9];
    static const char RESPONSE_MAGIC[9];
};
[...snip...]

const char RPCHandshakeHeader::REQUEST_MAGIC[9] = "FooBar?\n";
[...snip...]

RPCHandshakeHeader header;
memcpy(header.magic, RPCHandshakeHeader::REQUEST_MAGIC, sizeof(header.magic));
header.version = 1;

socket = new CActiveSocket();
socket->Initialize();
socket->Open((const uint8 *)"localhost", 5000);

socket->Send((uint8*)&header, sizeof(header));
[...code to read response...]

How can I do this in clojure? Do I need to use JNA/JNI?

Is there a way to create a C struct, turn it into binary and send it over a socket? (I think this is what I need to do)

1

1 Answers

2
votes

Sounds like a job for gloss! I don't know the details of this part of the API, but you want to look particularly at compile-frame, and repeated for the character strings.