From 3dd8d27ab301442cdacc7e130de85c4899ca16fa Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 1 Mar 2008 17:20:08 -0500 Subject: Use erlang term encoding for send/recv between c and erlang for maximum flexibility and code length. --- src/mysqlerl.c | 20 ++++++++++++++------ src/mysqlerl_connection.erl | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/mysqlerl.c b/src/mysqlerl.c index b5ad31c..0ce2703 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -159,11 +159,9 @@ write_cmd(const char *cmd, msglen_t len) { msglen_t nlen; - nlen = htonl(len + 3); + nlen = htonl(len); if (restartable_write((unsigned char *)&nlen, sizeof(nlen)) == -1) return -1; - if (restartable_write((unsigned char *)" - ", 3) == -1) - return -1; if (restartable_write((unsigned char *)cmd, len) == -1) return -1; @@ -177,15 +175,25 @@ dispatch_db_cmd(MYSQL *dbh, msg_t *msg) tag = erl_element(1, msg->cmd); if (strncmp((char *)ERL_ATOM_PTR(tag), QUERY_MSG, sizeof(QUERY_MSG)) == 0) { - ETERM *query; - char *q; + ETERM *query, *resp; + char *q, *buf; + int buflen; + query = erl_element(2, msg->cmd); q = erl_iolist_to_string(query); erl_free_term(query); logmsg("DEBUG: got query msg: %s.", q); - write_cmd(q, strlen(q)); + resp = erl_format("{ok, ~s}", q); erl_free(q); + + buflen = erl_term_len(resp); + buf = (char *)malloc(buflen); + erl_encode(resp, (unsigned char *)buf); + erl_free_term(resp); + + write_cmd(buf, buflen); + free(buf); } else { logmsg("WARNING: message type %s unknown.", (char *)ERL_ATOM_PTR(tag)); erl_free_term(tag); diff --git a/src/mysqlerl_connection.erl b/src/mysqlerl_connection.erl index fb0479c..53ac85f 100644 --- a/src/mysqlerl_connection.erl +++ b/src/mysqlerl_connection.erl @@ -76,7 +76,7 @@ handle_query(Ref, Query) -> make_request(Ref, Req) -> port_command(Ref, term_to_binary(Req)), receive - {Ref, {data, Res}} -> {ok, Res}; + {Ref, {data, Res}} -> binary_to_term(Res); Other -> error_logger:warning_msg("Got unknown query response: ~p~n", [Other]), -- cgit v1.2.3