1

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer! Only blip I had was the no support for little endian, though not hard to make a endian swapping function. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.