diff options
author | Brian Cully <bjc@kublai.com> | 2008-03-01 17:20:08 -0500 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2008-03-01 17:20:08 -0500 |
commit | 3dd8d27ab301442cdacc7e130de85c4899ca16fa (patch) | |
tree | d358ace4b0a77f491d530050c4424ec7c9ac5283 /src | |
parent | 876136763a49adc76eb4e9fe4a7a9dc78fb9a171 (diff) | |
download | mysqlerl-3dd8d27ab301442cdacc7e130de85c4899ca16fa.tar.gz mysqlerl-3dd8d27ab301442cdacc7e130de85c4899ca16fa.zip |
Use erlang term encoding for send/recv between c and erlang for maximum flexibility and code length.
Diffstat (limited to 'src')
-rw-r--r-- | src/mysqlerl.c | 20 | ||||
-rw-r--r-- | 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]), |