-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaw_order.c
150 lines (124 loc) · 3.48 KB
/
aw_order.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Description:
* History: yang@haipo.me, 2017/04/27, create
*/
# include "aw_config.h"
# include "aw_order.h"
# include "aw_server.h"
static dict_t *dict_sub;
struct sub_unit {
void *ses;
char market[MARKET_NAME_MAX_LEN];
};
static uint32_t dict_sub_hash_func(const void *key)
{
return (uintptr_t)key;
}
static int dict_sub_key_compare(const void *key1, const void *key2)
{
return (uintptr_t)key1 == (uintptr_t)key2 ? 0 : 1;
}
static void dict_sub_val_free(void *val)
{
list_release(val);
}
static int list_node_compare(const void *value1, const void *value2)
{
return memcmp(value1, value2, sizeof(struct sub_unit));
}
static void *list_node_dup(void *value)
{
struct sub_unit *obj = malloc(sizeof(struct sub_unit));
memcpy(obj, value, sizeof(struct sub_unit));
return obj;
}
static void list_node_free(void *value)
{
free(value);
}
int init_order(void)
{
dict_types dt;
memset(&dt, 0, sizeof(dt));
dt.hash_function = dict_sub_hash_func;
dt.key_compare = dict_sub_key_compare;
dt.val_destructor = dict_sub_val_free;
dict_sub = dict_create(&dt, 1024);
if (dict_sub == NULL)
return -__LINE__;
return 0;
}
int order_subscribe(uint32_t user_id, nw_ses *ses, const char *market)
{
void *key = (void *)(uintptr_t)user_id;
dict_entry *entry = dict_find(dict_sub, key);
if (entry == NULL) {
list_type lt;
memset(<, 0, sizeof(lt));
lt.dup = list_node_dup;
lt.free = list_node_free;
lt.compare = list_node_compare;
list_t *list = list_create(<);
if (list == NULL)
return -__LINE__;
entry = dict_add(dict_sub, key, list);
if (entry == NULL)
return -__LINE__;
}
list_t *list = entry->val;
struct sub_unit unit;
memset(&unit, 0, sizeof(unit));
unit.ses = ses;
strncpy(unit.market, market, MARKET_NAME_MAX_LEN - 1);
if (list_find(list, &unit) != NULL)
return 0;
if (list_add_node_tail(list, &unit) == NULL)
return -__LINE__;
return 0;
}
int order_unsubscribe(uint32_t user_id, nw_ses *ses)
{
void *key = (void *)(uintptr_t)user_id;
dict_entry *entry = dict_find(dict_sub, key);
if (entry == NULL)
return 0;
list_t *list = entry->val;
list_iter *iter = list_get_iterator(list, LIST_START_HEAD);
list_node *node;
while ((node = list_next(iter)) != NULL) {
struct sub_unit *unit = node->value;
if (unit->ses == ses) {
list_del(list, node);
}
}
list_release_iterator(iter);
if (list->len == 0) {
dict_delete(dict_sub, key);
}
return 0;
}
int order_on_update(uint32_t user_id, int event, json_t *order)
{
const char *market = json_string_value(json_object_get(order, "market"));
if (market == NULL)
return -__LINE__;
void *key = (void *)(uintptr_t)user_id;
dict_entry *entry = dict_find(dict_sub, key);
if (entry == NULL)
return 0;
json_t *params = json_array();
json_array_append_new(params, json_integer(event));
json_array_append(params, order);
list_t *list = entry->val;
list_iter *iter = list_get_iterator(list, LIST_START_HEAD);
list_node *node;
while ((node = list_next(iter)) != NULL) {
struct sub_unit *unit = node->value;
if (strcmp(unit->market, market) == 0) {
send_notify(unit->ses, "order.update", params);
}
}
list_release_iterator(iter);
json_decref(params);
return 0;
}