-
Notifications
You must be signed in to change notification settings - Fork 48
/
client_session.c
87 lines (73 loc) · 2.16 KB
/
client_session.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "socket_io.h"
#include "endpoint.h"
#include "store.h"
#include "safe_mem.h"
void broadcast_clients(char *except_sessionid, char *message) {
GList *list = get_store_list();
GList *it = NULL;
for (it = list; it; it = it->next) {
char *sessionid = it->data;
if (sessionid == NULL) {
log_warn("the sessioin is NULL ****************");
continue;
}
if (except_sessionid != NULL && strcmp(except_sessionid, sessionid) == 0) {
continue;
}
send_msg(sessionid, message);
}
g_list_free(list);
}
void send_msg(char *sessionid, char *message) {
session_t *session = store_lookup(sessionid);
if (session == NULL) {
log_warn("The sessionid %s has no value !", sessionid);
return;
}
GQueue *queue = session->queue;
if (queue == NULL) {
log_warn("The queue is NULL !");
return;
}
g_queue_push_head(queue, g_strdup(message));
if (session->state != CONNECTED_STATE) {
return;
}
client_t *client = session->client;
if (client) {
transports_fn *trans_fn = get_transport_fn(client);
if (trans_fn) {
trans_fn->output_callback(session);
} else {
log_warn("Got NO transport struct !");
}
}
}
void notice_connect(message_fields *msg_fields, char *sessionid, char *post_msg) {
session_t *session = store_lookup(sessionid);
if (!session) {
log_warn("The sessionid %s has no value !", sessionid);
return;
}
session->state = CONNECTED_STATE;
session->endpoint = g_strdup(msg_fields->endpoint);
send_msg(sessionid, post_msg);
}
void notice_disconnect(message_fields *msg_fields, char *sessionid) {
session_t *session = store_lookup(sessionid);
if (session == NULL) {
log_warn("The sessionid %s has no value !", sessionid);
return;
}
session->state = DISCONNECTED_STATE;
client_t *client = session->client;
if (client) {
on_close(client);
session->client = NULL;
}
store_remove(sessionid);
free(session);
}