summaryrefslogtreecommitdiffstats
path: root/src/client.c
blob: 578d51c32539cabf536beff44f593760a9885c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "client.h"

struct client *client_new(struct core *core, const char *protocol_name, char *name) {
    struct client *c;
    int r;
    assert(core);

    c = malloc(sizeof(struct client));
    assert(c);
    c->protocol_name = protocol_name;
    c->name = name ? strdup(name) : NULL;
    c->kill = NULL;
    c->kill_userdata = NULL;
    c->core = core;

    r = idxset_put(core->clients, c, &c->index);
    assert(c->index != IDXSET_INVALID && r >= 0);

    fprintf(stderr, "client: created %u \"%s\"\n", c->index, c->name);
    
    return c;
}

void client_free(struct client *c) {
    assert(c && c->core);

    idxset_remove_by_data(c->core->clients, c, NULL);
    fprintf(stderr, "client: freed %u \"%s\"\n", c->index, c->name);
    free(c->name);
    free(c);
}

void client_set_kill_callback(struct client *c, void (*kill)(struct client *c, void *userdata), void *userdata) {
    assert(c && kill);
    c->kill = kill;
    c->kill_userdata = userdata;
}

void client_kill(struct client *c) {
    assert(c);
    c->kill(c, c->kill_userdata);
}