8 #include <sys/socket.h>
12 #include "evas_cserve2.h"
14 static const char *SOCK_PATH = "/tmp/cserve2.socket";
15 static unsigned int _rid_count = 0;
17 static struct sockaddr_un socket_local;
19 #define UNIX_PATH_MAX sizeof(socket_local.sun_path)
23 debug_msg(const void *buf, int size)
25 const char *str = buf;
29 for (i = 0; i < size; i++)
30 printf("%x ", str[i]);
36 _read_line(char *buf, int size)
41 fgets(buf, size, stdin);
47 c = strchr(buf, '\n');
57 parse_input_open(int *size)
62 int path_len, key_len;
67 _read_line(line, sizeof(line));
68 path_len = _read_line(path, sizeof(path));
69 key_len = _read_line(key, sizeof(key));
71 sscanf(line, "%d", &file_id);
73 buf = malloc(sizeof(msg) + path_len + key_len);
75 msg.base.rid = _rid_count++;
76 msg.base.type = CSERVE2_OPEN;
77 msg.file_id = file_id;
79 msg.key_offset = path_len;
81 memcpy(buf, &msg, sizeof(msg));
82 memcpy(buf + sizeof(msg), path, path_len);
83 memcpy(buf + sizeof(msg) + path_len, key, key_len);
85 *size = sizeof(msg) + path_len + key_len;
91 parse_input_setopts(int *size)
95 int file_id, image_id;
102 // reading file_id, image_id
103 _read_line(line, sizeof(line));
104 sscanf(line, "%d %d", &file_id, &image_id);
107 _read_line(line, sizeof(line));
111 _read_line(line, sizeof(line));
112 sscanf(line, "%d %d", &w, &h);
114 // reading load scale down
115 _read_line(line, sizeof(line));
116 sscanf(line, "%d", &scale);
118 // reading load region
119 _read_line(line, sizeof(line));
120 sscanf(line, "%d %d %d %d", &rx, &ry, &rw, &rh);
122 // reading orientation
123 _read_line(line, sizeof(line));
124 sscanf(line, "%d", &orientation);
127 msg = calloc(1, sizeof(*msg));
129 msg->base.rid = _rid_count++;
130 msg->base.type = CSERVE2_SETOPTS;
131 msg->file_id = file_id;
132 msg->image_id = image_id;
136 msg->opts.scale_down = scale;
141 msg->opts.orientation = !!orientation;
143 *size = sizeof(*msg);
149 parse_input_load(int *size)
156 _read_line(line, sizeof(line));
157 sscanf(line, "%d", &image_id);
159 msg = calloc(1, sizeof(*msg));
161 msg->base.rid = _rid_count++;
162 msg->base.type = CSERVE2_LOAD;
163 msg->image_id = image_id;
165 *size = sizeof(*msg);
171 parse_input_preload(int *size)
178 _read_line(line, sizeof(line));
179 sscanf(line, "%d", &image_id);
181 msg = calloc(1, sizeof(*msg));
183 msg->base.rid = _rid_count++;
184 msg->base.type = CSERVE2_PRELOAD;
185 msg->image_id = image_id;
187 *size = sizeof(*msg);
193 parse_input_unload(int *size)
200 _read_line(line, sizeof(line));
201 sscanf(line, "%d", &image_id);
203 msg = calloc(1, sizeof(*msg));
205 msg->base.rid = _rid_count++;
206 msg->base.type = CSERVE2_UNLOAD;
207 msg->image_id = image_id;
209 *size = sizeof(*msg);
215 parse_input_close(int *size)
222 _read_line(line, sizeof(line));
223 sscanf(line, "%d", &file_id);
225 msg = calloc(1, sizeof(*msg));
227 msg->base.rid = _rid_count++;
228 msg->base.type = CSERVE2_CLOSE;
229 msg->file_id = file_id;
231 *size = sizeof(*msg);
237 parse_answer_opened(const void *buf)
239 const Msg_Opened *msg = buf;
240 printf("OPENED rid = %d\n", msg->base.rid);
241 printf("size: %dx%d, alpha: %d\n\n",
242 msg->image.w, msg->image.h, msg->image.alpha);
246 parse_answer_setoptsed(const void *buf)
248 const Msg_Setoptsed *msg = buf;
249 printf("SETOPTSED rid = %d\n", msg->base.rid);
253 parse_answer_loaded(const void *buf)
255 const Msg_Loaded *msg = buf;
258 path = ((const char *)msg) + sizeof(*msg);
260 printf("LOADED rid = %d\n", msg->base.rid);
261 printf("shm mmap_offset = 0x%x, use_offset = 0x%x, mmap size = %d bytes\n",
262 msg->shm.mmap_offset, msg->shm.use_offset, msg->shm.mmap_size);
263 printf("shm path: \"%s\"\n\n", path);
267 parse_answer_preloaded(const void *buf)
269 const Msg_Preloaded *msg = buf;
271 printf("PRELOADED rid = %d\n", msg->base.rid);
275 parse_answer_error(const void *buf)
277 const Msg_Error *msg = buf;
279 printf("ERROR rid = %d, error = %d\n", msg->base.rid, msg->error);
283 parse_answer(const void *buf)
285 const Msg_Base *msg = buf;
290 parse_answer_opened(buf);
292 case CSERVE2_SETOPTSED:
293 parse_answer_setoptsed(buf);
296 parse_answer_loaded(buf);
298 case CSERVE2_PRELOADED:
299 parse_answer_preloaded(buf);
302 parse_answer_error(buf);
305 printf("unhandled answer: %d\n", msg->type);
312 void *(*parse_func)(int *size);
314 { "OPEN", CSERVE2_OPEN, parse_input_open },
315 { "OPENED", CSERVE2_OPENED, NULL },
316 { "SETOPTS", CSERVE2_SETOPTS, parse_input_setopts },
317 { "SETOPTSED", CSERVE2_SETOPTSED, NULL },
318 { "LOAD", CSERVE2_LOAD, parse_input_load },
319 { "LOADED", CSERVE2_LOADED, NULL },
320 { "PRELOAD", CSERVE2_PRELOAD, parse_input_preload },
321 { "PRELOADED", CSERVE2_PRELOADED, NULL },
322 { "UNLOAD", CSERVE2_UNLOAD, parse_input_unload },
323 { "CLOSE", CSERVE2_CLOSE, parse_input_close },
329 int s, t, len, skip_cmd = 0;
330 struct sockaddr_un remote;
331 char msgbuf[4096], buf[UNIX_PATH_MAX], *env;
333 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
339 printf("Trying to connect...\n");
341 remote.sun_family = AF_UNIX;
342 env = getenv("EVAS_CSERVE2_SOCKET");
345 snprintf(buf, sizeof(buf), "/tmp/.evas-cserve2-%x.socket",
349 strncpy(remote.sun_path, env, UNIX_PATH_MAX - 1);
350 len = strlen(remote.sun_path) + sizeof(remote.sun_family);
351 if (connect(s, (struct sockaddr *)&remote, len) == -1)
357 printf("Connected.\n");
370 fgets(cmd, sizeof(cmd), stdin);
371 len = strlen(cmd) - 1;
380 for (i = 0; _msg_types[i].name; i++)
382 if (!strcmp(cmd, _msg_types[i].name))
386 // discards the end of the message if we can't parse it
387 if (!_msg_types[i].name)
389 printf("Invalid command.\n");
393 if (!_msg_types[i].parse_func)
395 printf("Command %s still unhandled.\n", _msg_types[i].name);
399 msg = _msg_types[i].parse_func(&size);
401 if (send(s, &size, sizeof(size), MSG_NOSIGNAL) == -1)
406 if (send(s, msg, size, MSG_NOSIGNAL) == -1)
416 if ((t=recv(s, &size, sizeof(size), MSG_DONTWAIT)) > 0)
418 len = recv(s, msgbuf, size, 0);
419 printf("size of received message: %d\n", len);
422 printf("couldn't read entire message.\n");
425 debug_msg(&size, sizeof(size));
426 debug_msg(msgbuf, size);
427 parse_answer(msgbuf);
431 if (t < 0 && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))
433 else fprintf(stderr, "Server closed connection\n");