int err = 0;
double t;
- D("... stat %s\n", file);
+ D("... stat %s", file);
ret = stat(file, &st);
if (ret < 0) return NULL;
- D("... load header\n");
+ D("... load header");
t = get_time();
ie = evas_cache_image_request(cache, file, key, load_opts, &err);
t = get_time() - t;
- D("... header done\n");
+ D("... header done");
if (!ie) return NULL;
- D("... ie->cache = %p\n", ie->cache);
+ D("... ie->cache = %p", ie->cache);
img = (Img *)ie;
img->stats.load1 = t;
img->key = eina_stringshare_add(bufkey);
static void
cache_clean(void)
{
- D("... cache clean!!! do\n");
+ D("... cache clean!!! do");
LKL(cache_lock);
while ((cache_usage > ((cache_max_usage + cache_max_adjust) * 1024)) &&
(cache_images))
{
Img *img;
Eina_List *l;
-
- D("... clean loop %i > %i\n", cache_usage, (cache_max_usage + cache_max_adjust) * 1024);
+
+ D("... clean loop %i > %i", cache_usage, (cache_max_usage + cache_max_adjust) * 1024);
l = eina_list_last(cache_images); // THREAD: called from thread. happens to be safe as it uses no unlocked shared resources
if (!l) break;
img = l->data;
if (!img) break;
LKL(img->lock);
- D("... REMOVE %p '%s'\n", img, img->file.file);
+ D("... REMOVE %p '%s'", img, img->file.file);
#ifdef BUILD_PTHREAD
img->killme = 1;
img->useless = 1;
cache_images = eina_list_remove_list(cache_images, l); // FIXME: called from thread
img->incache--;
cache_usage -= img->usage;
- D("... IMG FREE %p\n", img);
+ D("... IMG FREE %p", img);
img_free(img);
#endif
}
if (cache_max_adjust < -cache_max_usage)
cache_max_adjust = -cache_max_usage;
- D("... cache_max_adjust = %i\n", cache_max_adjust);
+ D("... cache_max_adjust = %i", cache_max_adjust);
if (pval != cache_max_adjust)
{
- D("... cache clean\n");
+ D("... cache clean");
// FIXME lock problem
cache_clean();
}
eina_hash_del(active_images, img->key, img);
if (img->dead)
{
- D("... img %p '%s' dead\n", img , img->file.file);
+ D("... img %p '%s' dead", img , img->file.file);
img_free(img);
return;
}
if ((cache_usage + img->usage) > ((cache_max_usage + cache_max_adjust) * 1024))
{
- D("... img %p '%s' too big for cache\n", img , img->file.file);
+ D("... img %p '%s' too big for cache", img , img->file.file);
img_free(img);
return;
}
- D("... img %p '%s' cached += %i\n", img , img->file.file, img->usage);
+ D("... img %p '%s' cached += %i", img , img->file.file, img->usage);
if (img->incache > 0)
{
printf("EEEEEEEEEEEEEEEEK!\n");
Eina_List *l, *l_next;
if (!file) return NULL;
- D("... img_load '%s'\n", file);
- if (key) D("... ... key '%s'\n", key);
+ D("... img_load '%s'", file);
+ if (key) D("... ... key '%s'", key);
if (key)
snprintf(buf, sizeof(buf), "%s///::/%s/\001/%i/%1.8f/%ix%i",
file, key,
load_opts->scale_down_by,
load_opts->dpi,
load_opts->w, load_opts->h);
- D("... find '%s'\n", buf);
+ D("... find '%s'", buf);
img = eina_hash_find(active_images, buf);
if ((img) && (img_ok(img)))
{
- D("... found!\n");
+ D("... found!");
img->stats.load1saved++;
img->ref++;
- D("... sats update\n");
+ D("... sats update");
stats_update();
- D("... return %p\n", img);
+ D("... return %p", img);
return img;
}
LKL(img->lock);
if (img_ok(img))
{
- D("... found cached\n");
+ D("... found cached");
cache_images = eina_list_remove_list(cache_images, l);
img->incache--;
cache_usage -= img->usage;
img->stats.load1saved++;
img->ref++;
eina_hash_direct_add(active_images, img->key, img);
- D("... sats update\n");
+ D("... sats update");
stats_update();
- D("... return %p\n", img);
+ D("... return %p", img);
LKU(img->lock);
LKU(cache_lock);
return img;
}
}
LKU(cache_lock);
- D("... ned new img\n");
+ D("... ned new img");
return img_new(file, key, load_opts, buf);
}
return;
}
img->ref--;
- D("... img ref-- = %i\n", img->ref);
+ D("... img ref-- = %i", img->ref);
if (img->ref == 0)
{
- D("... img cache %p '%s'\n", img, img->file.file);
+ D("... img cache %p '%s'", img, img->file.file);
img_cache(img);
}
}
static void
img_unloaddata(Img *img)
{
- D("img_unloaddata() %p '%s'\n", img, img->file.file);
+ D("img_unloaddata() %p '%s'", img, img->file.file);
if ((img->dref <= 0) && (img->useless) && (img->mem))
{
Image_Entry *ie = (Image_Entry *)img;
- D("... really do forced unload\n");
+ D("... really do forced unload");
if (!img->active) cache_usage -= img->usage;
img->usage -=
(4096 * (((img->image.w * img->image.h * sizeof(DATA32)) + 4095) / 4096)) +
img->mem = NULL;
img->image.data = NULL;
img->dref = 0;
- D("... done\n");
+ D("... done");
ie->flags.loaded = 0;
ie->allocated.w = 0;
static void
img_useless(Img *img)
{
- D("img_useless() %p\n", img);
+ D("img_useless() %p", img);
img->useless = 1;
if (img->dref <= 0) img_unloaddata(img);
}
static void
img_forcedunload(Img *img)
{
- D("img_forcedunload() %p\n", img);
+ D("img_forcedunload() %p", img);
img->dead = 1;
img_unload(img);
}
static void
img_preload(Img *img)
{
- D("img_preload() %p\n", img);
- printf("preload '%s'\n", img->file.file);
+ D("img_preload() %p", img);
}
static void
Img *img;
images = data;
- D("... CLIENT DEL %i\n", c->pid);
+ D("... CLIENT DEL %i", c->pid);
EINA_LIST_FREE(images, img)
{
- D("... unloaddata img %p\n", img);
+ D("... unloaddata img %p", img);
img_unloaddata(img);
- D("... unload img %p\n", img);
+ D("... unload img %p", img);
img_unload(img);
}
}
msg.mem.id = img->mem->id;
msg.mem.offset = img->mem->offset;
msg.mem.size = img->mem->size;
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg));
LKU(c->lock);
return NULL;
else
msg.mem.id = msg.mem.offset = msg.mem.size = 0;
LKU(img->lock);
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg));
LKU(c->lock);
return NULL;
message(void *fdata, Server *s, Client *c, int opcode, int size, unsigned char *data)
{
t_now = time(NULL);
- D("message @ %i...\n", (int)t_now);
+ D("message @ %i...", (int)t_now);
switch (opcode)
{
case OP_INIT:
memset(&msg, 0, sizeof(msg));
msg.pid = getpid();
msg.server_id = server_id;
+ msg.handle = c;
rep = (Op_Init *)data;
c->pid = rep->pid;
+ if (rep->server_id == 1) // 2nd channel conn
+ {
+ c->client_main = rep->handle;
+ }
c->func = client_del;
c->data = NULL;
- D("OP_INIT %i\n", c->pid);
- D("... reply\n");
+ D("OP_INIT %i", c->pid);
+ D("... reply");
evas_cserve_client_send(c, OP_INIT, sizeof(msg), (unsigned char *)(&msg));
}
break;
RGBA_Image_Loadopts lopt = {0, 0.0, 0, 0};
char *file = NULL, *key = NULL;
- D("OP_LOAD %i\n", c->pid);
+ D("OP_LOAD %i", c->pid);
rep = (Op_Load *)data;
file = data + sizeof(Op_Load);
key = file + strlen(file) + 1;
lopt.dpi = rep->lopt.dpi;
lopt.w = rep->lopt.w;
lopt.h = rep->lopt.h;
- D("... img_load '%s'\n", file);
- if (key) D("'%s'\n", key);
- else D(" '%s'\n", NULL);
- D(" lopt { %i %1.1f %i %i}\n",
+ D("... img_load '%s'", file);
+ if (key) D("'%s'", key);
+ else D(" '%s'", NULL);
+ D(" lopt { %i %1.1f %i %i}",
lopt.scale_down_by, lopt.dpi, lopt.w, lopt.h);
img = img_load(file, key, &lopt);
- D("... img_load = %p\n", img);
+ D("... img_load = %p", img);
if (img)
{
- D("... add image to client list\n");
- c->data = eina_list_append(c->data, img);
+ D("... add image to client list");
+ if (c->client_main)
+ c->client_main->data = eina_list_append(c->client_main->data, img);
+ else
+ c->data = eina_list_append(c->data, img);
}
memset(&msg, 0, sizeof(msg));
msg.handle = img;
msg.image.h = img->image.h;
msg.image.alpha = img->image.alpha;
}
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_LOAD, sizeof(msg), (unsigned char *)(&msg));
}
break;
Op_Unload *rep;
Img *img;
- D("OP_UNLOAD %i\n", c->pid);
+ D("OP_UNLOAD %i", c->pid);
rep = (Op_Unload *)data;
img = rep->handle;
if ((img) && (rep->server_id == server_id))
{
- D("... remove %p from list\n", img);
- c->data = eina_list_remove(c->data, img);
- D("... unload %p\n", img);
+ Eina_Bool doflush = 0;
+
+ D("... remove %p from list", img);
+ if (c->client_main)
+ c->client_main->data = eina_list_remove(c->client_main->data, img);
+ else
+ c->data = eina_list_remove(c->data, img);
+ D("... unload %p", img);
LKL(img->lock);
img->ref++;
img_unload(img);
img->ref--;
+ if (img->ref == 0) doflush = 1;
LKU(img->lock);
+ if (doflush)
+ img_cache(img);
cache_clean();
}
}
Op_Loaddata_Reply msg;
Img *img;
- D("OP_LOADDATA %i\n", c->pid);
+ D("OP_LOADDATA %i", c->pid);
rep = (Op_Loaddata *)data;
img = rep->handle;
if ((img) && (rep->server_id == server_id))
{
if (img->mem)
{
- D("... load saved - cached %p\n", img);
+ D("... load saved - cached %p", img);
img->stats.load2saved++;
stats_update();
memset(&msg, 0, sizeof(msg));
}
else
msg.mem.id = msg.mem.offset = msg.mem.size = 0;
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg));
}
else
pthread_attr_t attr;
Load_Inf *li;
- D("... load data %p\n", img);
+ D("... load data %p", img);
pthread_attr_init(&attr);
li = calloc(1, sizeof(Load_Inf));
if (li)
}
else
msg.mem.id = msg.mem.offset = msg.mem.size = 0;
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg));
#endif
}
Op_Unloaddata *rep;
Img *img;
- D("OP_UNLOADDATA %i\n", c->pid);
+ D("OP_UNLOADDATA %i", c->pid);
rep = (Op_Unloaddata *)data;
img = rep->handle;
if ((img) && (rep->server_id == server_id))
{
- D("... dref--\n");
+ D("... dref--");
LKL(img->lock);
img->dref--;
if (img->dref < 0) img->dref = 0;
- D("... unload data %p '%s'\n", img, img->file.file);
+ D("... unload data %p '%s'", img, img->file.file);
img_unloaddata(img);
LKU(img->lock);
}
Op_Unloaddata *rep;
Img *img;
- D("OP_USELESSDATA %i\n", c->pid);
+ D("OP_USELESSDATA %i", c->pid);
rep = (Op_Unloaddata *)data;
img = rep->handle;
if ((img) && (rep->server_id == server_id))
{
- D("... dref--\n");
+ D("... dref--");
LKL(img->lock);
img->dref--;
if (img->dref < 0) img->dref = 0;
- D("... useless %p\n", img);
+ D("... useless %p", img);
img_useless(img);
LKU(img->lock);
}
Op_Preload *rep;
Img *img;
- D("OP_PRELOAD %i\n", c->pid);
+ D("OP_PRELOAD %i", c->pid);
rep = (Op_Preload *)data;
img = rep->handle;
if ((img) && (rep->server_id == server_id))
{
LKL(img->lock);
- c->data = eina_list_remove(c->data, img);
+ if (c->client_main)
+ c->client_main->data = eina_list_remove(c->client_main->data, img);
+ else
+ c->data = eina_list_remove(c->data, img);
// FIXME: preload doesnt work async
img_preload(img);
LKU(img->lock);
Op_Forcedunload *rep;
Img *img;
- D("OP_FORCEDUNLOAD %i\n", c->pid);
+ D("OP_FORCEDUNLOAD %i", c->pid);
rep = (Op_Forcedunload *)data;
img = rep->handle;
if ((img) && (rep->server_id == server_id))
{
+ Eina_Bool doflush = 0;
+
LKL(img->lock);
- D("remove %p from list\n", img);
- c->data = eina_list_remove(c->data, img);
- D("... forced unload now\n");
+ D("remove %p from list", img);
+ if (c->client_main)
+ c->client_main->data = eina_list_remove(c->client_main->data, img);
+ else
+ c->data = eina_list_remove(c->data, img);
+ D("... forced unload now");
img->ref++;
img_forcedunload(img);
img->ref--;
+ if (img->ref == 0) doflush = 1;
LKU(img->lock);
+ if (doflush)
+ img_cache(img);
cache_clean();
}
}
{
Op_Getconfig_Reply msg;
- D("OP_GETCONFIG %i\n", c->pid);
+ D("OP_GETCONFIG %i", c->pid);
msg.cache_max_usage = cache_max_usage;
msg.cache_item_timeout = cache_item_timeout;
msg.cache_item_timeout_check = cache_item_timeout_check;
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_GETCONFIG, sizeof(msg), (unsigned char *)(&msg));
}
break;
{
Op_Setconfig *rep;
- D("OP_SETCONFIG %i\n", c->pid);
+ D("OP_SETCONFIG %i", c->pid);
rep = (Op_Setconfig *)data;
cache_max_usage = rep->cache_max_usage;
cache_item_timeout = rep->cache_item_timeout;
cache_item_timeout_check = rep->cache_item_timeout_check;
- D("... cache timeout\n");
+ D("... cache timeout");
cache_timeout(t_now);
- D("... cache clean\n");
+ D("... cache clean");
cache_clean();
}
break;
{
Op_Getstats_Reply msg;
- D("OP_GETSTATS %i\n", c->pid);
+ D("OP_GETSTATS %i", c->pid);
stats_calc();
msg.saved_memory = saved_memory;
msg.wasted_memory = (real_memory - alloced_memory);
msg.wasted_memory_peak = (real_memory_peak - alloced_memory_peak);
msg.saved_time_image_header_load = saved_load_lifetime + saved_load_time;
msg.saved_time_image_data_load = saved_loaddata_lifetime + saved_loaddata_time;
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_GETSTATS, sizeof(msg), (unsigned char *)(&msg));
}
break;
Eina_List *imgs = NULL, *l;
Img *img;
- D("OP_GETINFO %i\n", c->pid);
+ D("OP_GETINFO %i", c->pid);
len = sizeof(Op_Getinfo_Reply);
- D("... foreach\n");
+ D("... foreach");
if (active_images)
eina_hash_foreach(active_images, getinfo_hash_image_cb, &imgs);
- D("... walk foreach list output\n");
+ D("... walk foreach list output");
LKL(cache_lock);
EINA_LIST_FOREACH(cache_images, l, img)
{
imgs = eina_list_append(imgs, img);
}
LKU(cache_lock);
- D("... walk image cache\n");
+ D("... walk image cache");
EINA_LIST_FOREACH(imgs, l, img)
{
len += sizeof(Op_Getinfo_Item);
if (img->file.key) len += strlen(img->file.key);
len++;
}
- D("... malloc msg\n");
+ D("... malloc msg");
msg = malloc(len);
if (msg)
{
unsigned char *p;
- D("... init msg\n");
+ D("... init msg");
memset(msg, 0, len);
p = (unsigned char *)msg;
msg->active.mem_total = 0;
msg->cached.mem_total = 0;
msg->cached.count = 0;
p += sizeof(Op_Getinfo_Reply);
- D("... walk all imgs\n");
+ D("... walk all imgs");
EINA_LIST_FOREACH(imgs, l, img)
{
Op_Getinfo_Item *itt, it;
LKL(img->lock);
- D("... img %p\n", img);
+ D("... img %p", img);
memset(&it, 0, sizeof(Op_Getinfo_Item));
itt = (Op_Getinfo_Item *)p;
it.file_key_size = 0;
}
it.dead = img->dead;
it.useless = img->useless;
- D("... memcpy %p %p %i \n",
+ D("... memcpy %p %p %i ",
itt, &it, sizeof(Op_Getinfo_Item));
memcpy(itt, &it, sizeof(Op_Getinfo_Item));
- D("... memcpy done n\n", img);
+ D("... memcpy done n", img);
p += sizeof(Op_Getinfo_Item) + it.file_key_size;
LKU(img->lock);
}
- D("... walk all imgs done\n");
+ D("... walk all imgs done");
msg->active.mem_total =
(msg->active.mem_total + 1023) / 1024;
msg->cached.mem_total =
(msg->cached.mem_total + 1023) / 1024;
- D("... reply\n");
+ D("... reply");
evas_cserve_client_send(c, OP_GETINFO, len, msg);
free(msg);
}
else
{
- D("... reply empty\n");
+ D("... reply empty");
evas_cserve_client_send(c, OP_GETINFO, 0, NULL);
}
- D("... free imgs list\n");
+ D("... free imgs list");
if (imgs) eina_list_free(imgs);
}
break;
default:
- D("OP_... UNKNOWN??? %i opcode: %i\n", c->pid, opcode);
+ D("OP_... UNKNOWN??? %i opcode: %i", c->pid, opcode);
break;
}
return 0;
unsetenv("EVAS_CSERVE");
- D("eina init...\n");
+ D("eina init...");
eina_init();
- D("evas init...\n");
+ D("evas init...");
evas_init();
D("img init...");
img_init();
- D("signal init...\n");
+ D("signal init...");
signal_init();
- D("cserve add...\n");
+ D("cserve add...");
s = evas_cserve_server_add();
if (!s)
{
- printf("ERROR: server socket init fail. abort.\n");
+ printf("ERROR: server socket init fail. abort.");
goto error;
}
- D("mem open (status)...\n");
+ D("mem open (status)...");
stat_mem = evas_cserve_mem_open(0, 0, "status", sizeof(int), 0);
if (stat_mem)
{
- printf("WARNING: previous evas_cserve left garbage. cleaning up.\n");
+ printf("WARNING: previous evas_cserve left garbage. cleaning up.");
stat_clean(stat_mem);
evas_cserve_mem_close(stat_mem);
stat_mem = NULL;
}
- D("mem new (status)...\n");
+ D("mem new (status)...");
stat_mem = evas_cserve_mem_new(sizeof(int), "status");
if (!stat_mem)
{
- printf("ERROR: cannot create status shmseg. abort.\n");
+ printf("ERROR: cannot create status shmseg. abort.");
goto error;
}
- D("init status...\n");
+ D("init status...");
if (!stat_init(stat_mem))
{
- printf("ERROR: cannot init status shmseg. abort.\n");
+ printf("ERROR: cannot init status shmseg. abort.");
evas_cserve_mem_free(stat_mem);
stat_mem = NULL;
goto error;
}
- D("cset server message handler...\n");
+ D("cset server message handler...");
evas_cserve_server_message_handler_set(s, message, NULL);
last_check = time(NULL);
t_next = 0;
if (cache_item_timeout_check > 0) t_next = cache_item_timeout_check;
- D("LOOP!!! ...\n");
+ D("LOOP!!! ...");
for (;;)
{
/* fixme: timeout 0 only her - future use timeouts for timed
* housekeping */
if (exit_flag) break;
- D("wait for messages...\n");
+ D("wait for messages...");
evas_cserve_server_wait(s, t_next * 1000000);
if (exit_flag) break;
t = time(NULL);
if ((cache_item_timeout_check > 0) &&
((t_next) >= cache_item_timeout_check))
{
- D("check timeout of items...\n");
+ D("check timeout of items...");
t_next = cache_item_timeout_check;
last_check = t;
- D("cache timeout...\n");
+ D("cache timeout...");
cache_timeout(t);
- D("meminfo check...\n");
+ D("meminfo check...");
meminfo_check();
- D("mem cache adjust...\n");
+ D("mem cache adjust...");
mem_cache_adjust();
}
if ((t_next <= 0) && (cache_item_timeout_check > 0))
t_next = 1;
- D("sleep for %isec...\n", t_next);
+ D("sleep for %isec...", t_next);
LKL(strshr_freeme_lock);
if (strshr_freeme_count > 0)
Eina_List *l;
Img *img;
Eina_List *kills = NULL;
-
+
EINA_LIST_FOREACH(cache_images, l, img)
{
LKL(img->lock);
cache_images = eina_list_remove(cache_images, img);
img->incache--;
cache_usage -= img->usage;
- D("... IMG FREE %p\n", img);
+ D("... IMG FREE %p", img);
img_free(img);
}
cache_cleanme = 0;
}
LKU(cache_lock);
}
- D("end loop...\n");
+ D("end loop...");
error:
- D("cleanup...\n");
+ D("cleanup...");
if (stat_mem)
{
- D("clean mem stat...\n");
+ D("clean mem stat...");
stat_clean(stat_mem);
}
- D("signal shutdown...\n");
+ D("signal shutdown...");
signal_shutdown();
- D("img shutdown...\n");
+ D("img shutdown...");
img_shutdown();
if (stat_mem)
{
- D("free stat mem...\n");
+ D("free stat mem...");
evas_cserve_mem_free(stat_mem);
stat_mem = NULL;
}
if (s)
{
- D("del server...\n");
+ D("del server...");
evas_cserve_server_del(s);
}
- D("evas shutdown...\n");
+ D("evas shutdown...");
evas_shutdown();
- D("eina shutdown...\n");
+ D("eina shutdown...");
eina_shutdown();
- D("exit..\n");
+ D("exit..");
return 0;
}
s = calloc(1, sizeof(Server));
if (!s) return NULL;
- s->fd = -1;
+ s->ch[0].fd = -1;
+ s->ch[1].fd = -1;
snprintf(buf, sizeof(buf), "/tmp/.evas-cserve-%x", getuid());
s->socket_path = strdup(buf);
if (!s->socket_path)
free(s);
return NULL;
}
- s->fd = socket(AF_UNIX, SOCK_STREAM, 0);
- if (s->fd < 0) goto error;
- if (fcntl(s->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
- if (setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
+ s->ch[0].fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s->ch[0].fd < 0) goto error;
+ if (fcntl(s->ch[0].fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
+ if (setsockopt(s->ch[0].fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
goto error;
socket_unix.sun_family = AF_UNIX;
strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
- if (connect(s->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) goto error;
+ if (connect(s->ch[0].fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) goto error;
+
+ s->ch[1].fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (s->ch[1].fd < 0) goto error;
+ if (fcntl(s->ch[1].fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
+ if (setsockopt(s->ch[1].fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
+ goto error;
+ socket_unix.sun_family = AF_UNIX;
+ strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
+ socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
+ if (connect(s->ch[1].fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) goto error;
+
return s;
error:
- if (s->fd >= 0) close(s->fd);
+ if (s->ch[0].fd >= 0) close(s->ch[0].fd);
+ if (s->ch[1].fd >= 0) close(s->ch[1].fd);
free(s->socket_path);
free(s);
return NULL;
static void
server_disconnect(Server *s)
{
- close(s->fd);
+ close(s->ch[0].fd);
+ close(s->ch[1].fd);
free(s->socket_path);
free(s);
}
static int
-server_send(Server *s, int opcode, int size, unsigned char *data)
+server_send(Server *s, int channel, int opcode, int size, unsigned char *data)
{
int ints[3];
int num;
pipe_handle(1);
ints[0] = size;
ints[1] = opcode;
- s->req_to++;
- ints[2] = s->req_to;
- num = write(s->fd, ints, (sizeof(int) * 3));
+ s->ch[channel].req_to++;
+ ints[2] = s->ch[channel].req_to;
+ num = write(s->ch[channel].fd, ints, (sizeof(int) * 3));
if (num < 0)
{
pipe_handle(0);
cserve = NULL;
return 0;
}
- num = write(s->fd, data, size);
+ num = write(s->ch[channel].fd, data, size);
if (num < 0)
{
pipe_handle(0);
}
static unsigned char *
-server_read(Server *s, int *opcode, int *size)
+server_read(Server *s, int channel, int *opcode, int *size)
{
int ints[3], num, left;
unsigned char *data;
- num = read(s->fd, ints, sizeof(int) * 3);
+ num = read(s->ch[channel].fd, ints, sizeof(int) * 3);
if (num != (sizeof(int) * 3))
{
if (cserve) server_disconnect(cserve);
*size = ints[0];
*opcode = ints[1];
if ((*size < 0) || (*size > (1024 * 1024))) return NULL;
- if (ints[2] != (s->req_from + 1))
+ if (ints[2] != (s->ch[channel].req_from + 1))
{
printf("EEK! sequence number mismatch from serer with pid: %i\n"
"---- num %i is not 1 more than %i\n"
,
- s->pid, ints[2], s->req_from);
+ s->pid, ints[2], s->ch[channel].req_from);
return NULL;
}
- s->req_from++;
+ s->ch[channel].req_from++;
data = malloc(*size);
if (!data) return NULL;
- num = read(s->fd, data, *size);
+ num = read(s->ch[channel].fd, data, *size);
if (num < 0)
{
free(data);
left = *size - num;
while (left > 0)
{
- num = read(s->fd, data + (*size - left), left);
+ num = read(s->ch[channel].fd, data + (*size - left), left);
if (num < 0)
{
free(data);
msg.pid = getpid();
msg.server_id = 0;
- if (!server_send(s, OP_INIT, sizeof(msg), (unsigned char *)(&msg)))
+ msg.handle = NULL;
+ if (!server_send(s, 0, OP_INIT, sizeof(msg), (unsigned char *)(&msg)))
return 0;
- rep = (Op_Init *)server_read(s, &opcode, &size);
+ rep = (Op_Init *)server_read(s, 0, &opcode, &size);
if ((rep) && (opcode == OP_INIT) && (size == sizeof(Op_Init)))
{
s->pid = rep->pid;
s->server_id = rep->server_id;
- free(rep);
+ s->main_handle = rep->handle;
connect_num++;
- return 1;
+ msg.pid = getpid();
+ msg.server_id = 1;
+ msg.handle = rep->handle;
+ free(rep);
+ if (!server_send(s, 1, OP_INIT, sizeof(msg), (unsigned char *)(&msg)))
+ return 0;
+ rep = (Op_Init *)server_read(s, 1, &opcode, &size);
+ if ((rep) && (opcode == OP_INIT) && (size == sizeof(Op_Init)))
+ {
+ free(rep);
+ return 1;
+ }
+ if (rep) free(rep);
+ return 0;
}
if (rep) free(rep);
return 0;
strcpy(buf + sizeof(msg), file);
strcpy(buf + sizeof(msg) + flen, key);
if (!buf) return 0;
- if (!server_send(cserve, OP_LOAD,
+ if (!server_send(cserve, ie->channel, OP_LOAD,
sizeof(msg) + flen + klen,
buf))
{
}
free(buf);
if (!cserve) return 0;
- rep = (Op_Load_Reply *)server_read(cserve, &opcode, &size);
+ rep = (Op_Load_Reply *)server_read(cserve, ie->channel, &opcode, &size);
if ((rep) && (opcode == OP_LOAD) && (size == sizeof(Op_Load_Reply)))
{
ie->w = rep->image.w;
memset(&msg, 0, sizeof(msg));
msg.handle = ie->data1;
msg.server_id = cserve->server_id;
- if (!server_send(cserve, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg)))
+ if (!server_send(cserve, ie->channel, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg)))
return 0;
if (!cserve) return 0;
- rep = (Op_Loaddata_Reply *)server_read(cserve, &opcode, &size);
+ rep = (Op_Loaddata_Reply *)server_read(cserve, ie->channel, &opcode, &size);
if ((rep) && (opcode == OP_LOADDATA) && (size == sizeof(Op_Loaddata_Reply)))
{
if (rep->mem.size <= 0)
if (ie->connect_num == connect_num)
{
if (ie->server_id == cserve->server_id)
- server_send(cserve, OP_UNLOAD, sizeof(msg), (unsigned char *)(&msg));
+ server_send(cserve, ie->channel, OP_UNLOAD, sizeof(msg), (unsigned char *)(&msg));
}
}
ie->data1 = NULL;
if (ie->connect_num == connect_num)
{
if (ie->server_id == cserve->server_id)
- server_send(cserve, OP_UNLOADDATA, sizeof(msg), (unsigned char *)(&msg));
+ server_send(cserve, ie->channel, OP_UNLOADDATA, sizeof(msg), (unsigned char *)(&msg));
}
}
if (ie->connect_num == connect_num)
{
if (ie->server_id == cserve->server_id)
- server_send(cserve, OP_USELESSDATA, sizeof(msg), (unsigned char *)(&msg));
+ server_send(cserve, ie->channel, OP_USELESSDATA, sizeof(msg), (unsigned char *)(&msg));
}
}
if (csrve_init > 0) server_reinit();
else return 0;
if (!cserve) return 0;
- if (!server_send(cserve, OP_GETCONFIG, 0, NULL)) return 0;
- rep = (Op_Getconfig_Reply *)server_read(cserve, &opcode, &size);
+ if (!server_send(cserve, 0, OP_GETCONFIG, 0, NULL)) return 0;
+ rep = (Op_Getconfig_Reply *)server_read(cserve, 0, &opcode, &size);
if ((rep) && (opcode == OP_GETCONFIG) && (size == sizeof(Op_Getconfig_Reply)))
{
memcpy(config, rep, sizeof(Op_Getconfig_Reply));
if (csrve_init > 0) server_reinit();
else return 0;
if (!cserve) return 0;
- if (!server_send(cserve, OP_SETCONFIG, sizeof(Op_Setconfig), (unsigned char *)config)) return 0;
+ if (!server_send(cserve, 0, OP_SETCONFIG, sizeof(Op_Setconfig), (unsigned char *)config)) return 0;
return 1;
}
if (csrve_init > 0) server_reinit();
else return 0;
if (!cserve) return 0;
- if (!server_send(cserve, OP_GETSTATS, 0, NULL)) return 0;
- rep = (Op_Getstats_Reply *)server_read(cserve, &opcode, &size);
+ if (!server_send(cserve, 0, OP_GETSTATS, 0, NULL)) return 0;
+ rep = (Op_Getstats_Reply *)server_read(cserve, 0, &opcode, &size);
if ((rep) && (opcode == OP_GETSTATS) && (size == sizeof(Op_Getstats_Reply)))
{
memcpy(stats, rep, sizeof(Op_Getstats_Reply));
if (csrve_init > 0) server_reinit();
else return NULL;
if (!cserve) return NULL;
- if (!server_send(cserve, OP_GETINFO, 0, NULL)) return NULL;
- rep = (Op_Getinfo_Reply *)server_read(cserve, &opcode, &size);
+ if (!server_send(cserve, 0, OP_GETINFO, 0, NULL)) return NULL;
+ rep = (Op_Getinfo_Reply *)server_read(cserve, 0, &opcode, &size);
if ((rep) && (opcode == OP_GETINFO) && (size >= sizeof(Op_Getinfo_Reply)))
{
return rep;