Browse Source

Use a consistent method for sending/receiving messages across unix sockets

master
Con Kolivas 11 years ago
parent
commit
eb041c331f
  1. 1
      src/bitcoin.c
  2. 66
      src/libckpool.c
  3. 2
      src/libckpool.h

1
src/bitcoin.c

@ -274,6 +274,7 @@ void clear_gbtbase(gbtbase_t *gbt)
dealloc(gbt->txn_data);
json_decref(gbt->json);
gbt->json = NULL;
memset(gbt, 0, sizeof(gbtbase_t));
}
static const char *blockcount_req = "{\"method\": \"getblockcount\"}\n";

66
src/libckpool.c

@ -629,6 +629,72 @@ out:
return sockd;
}
/* Use a standard message across the unix sockets:
* 4 byte length of message as little endian encoded uint32_t followed by the
* string.*/
char *recv_unix_msg(int sockd)
{
char *buf = NULL;
uint32_t msglen;
int ret, ofs;
/* Get message length */
ret = read(sockd, &msglen, 4);
if (ret < 4) {
LOGERR("Failed to read 4 byte length in recv_unix_msg");
goto out;
}
msglen = le32toh(msglen);
if (unlikely(msglen < 1)) {
LOGWARNING("Invalid message length zero sent to recv_unix_msg");
goto out;
}
buf = ckalloc(msglen);
ofs = 0;
while (msglen) {
ret = read(sockd, buf + ofs, msglen);
if (unlikely(ret < 0)) {
LOGERR("Failed to read %d bytes in recv_unix_msg", msglen);
ret = 1;
goto out;
}
ofs += ret;
msglen -= ret;
}
out:
return buf;
}
bool send_unix_msg(int sockd, const char *buf)
{
uint32_t msglen, len;
int ret, ofs;
len = strlen(buf);
if (unlikely(!len)) {
LOGWARNING("Zero length message sent to send_unix_msg");
return false;
}
msglen = htole32(len);
ret = write(sockd, &msglen, 4);
if (unlikely(ret < 4)) {
LOGERR("Failed to write 4 byte length in send_unix_msg");
return false;
}
ofs = 0;
while (len) {
ret = write(sockd, buf + ofs, len);
if (unlikely(ret < 0)) {
LOGERR("Failed to write %d bytes in send_unix_msg", len);
return false;
}
ofs += ret;
len -= ret;
}
return true;
}
json_t *json_rpc_call(connsock_t *cs, const char *rpc_req)
{
char http_req[PAGESIZE];

2
src/libckpool.h

@ -217,6 +217,8 @@ void empty_socket(int fd);
void close_unix_socket(const int sockd, const char *server_path);
int open_unix_server(const char *server_path);
int open_unix_client(const char *server_path);
char *recv_unix_msg(int sockd);
bool send_unix_msg(int sockd, const char *buf);
json_t *json_rpc_call(connsock_t *cs, const char *rpc_req);

Loading…
Cancel
Save