aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-03 20:40:50 +0100
committerMikkel Oscar Lyderik <mikkeloscar@gmail.com>2016-01-03 20:40:50 +0100
commit7298a9c67aed2d7a5c5c1df55bfa6052cac04d51 (patch)
treeaf92196751fdc7cc326b8e6a32c1c62983fd30bb /common
parent19833fbc8b607ce881fe9a7f65116240c7ecc87c (diff)
Add type to returned response.
Makes `ipc_recv_response` return a struct with size, type and payload rather than just the payload string. This is useful if the type has to be checked on the client.
Diffstat (limited to 'common')
-rw-r--r--common/ipc-client.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c
index e92a09fe..81348913 100644
--- a/common/ipc-client.c
+++ b/common/ipc-client.c
@@ -10,6 +10,7 @@
#include "stringop.h"
#include "ipc.h"
#include "readline.h"
+#include "ipc-client.h"
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
@@ -39,7 +40,7 @@ int ipc_open_socket(const char *socket_path) {
return socketfd;
}
-char *ipc_recv_response(int socketfd, uint32_t *len) {
+struct ipc_response *ipc_recv_response(int socketfd) {
char data[ipc_header_size];
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
@@ -52,21 +53,29 @@ char *ipc_recv_response(int socketfd, uint32_t *len) {
total += received;
}
+ struct ipc_response *response = malloc(sizeof(struct ipc_response));
total = 0;
- *len = data32[0];
- char *response = malloc(*len + 1);
- while (total < *len) {
- ssize_t received = recv(socketfd, response + total, *len - total, 0);
+ response->size = data32[0];
+ response->type = data32[1];
+ char *payload = malloc(response->size + 1);
+ while (total < response->size) {
+ ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
if (received < 0) {
sway_abort("Unable to receive IPC response");
}
total += received;
}
- response[*len] = '\0';
+ payload[response->size] = '\0';
+ response->payload = payload;
return response;
}
+void free_ipc_response(struct ipc_response *response) {
+ free(response->payload);
+ free(response);
+}
+
char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
char data[ipc_header_size];
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
@@ -82,5 +91,10 @@ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint3
sway_abort("Unable to send IPC payload");
}
- return ipc_recv_response(socketfd, len);
+ struct ipc_response *resp = ipc_recv_response(socketfd);
+ char *response = resp->payload;
+ *len = resp->size;
+ free(resp);
+
+ return response;
}