forked from ideawu/icomet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.cpp
More file actions
76 lines (67 loc) · 1.55 KB
/
channel.cpp
File metadata and controls
76 lines (67 loc) · 1.55 KB
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
#include "../build.h"
#include "channel.h"
#include <event2/buffer.h>
#include <event2/keyvalq_struct.h>
#include "util/log.h"
#include "util/list.h"
#include "server.h"
#include "subscriber.h"
#include "server_config.h"
Channel::Channel(){
reset();
}
Channel::~Channel(){
}
void Channel::reset(){
seq_next = 0;
idle = -1;
token.clear();
msg_list.clear();
}
void Channel::add_subscriber(Subscriber *sub){
sub->channel = this;
subs.push_back(sub);
}
void Channel::del_subscriber(Subscriber *sub){
sub->channel = NULL;
subs.remove(sub);
}
void Channel::create_token(){
// TODO: rand() is not safe?
struct timeval tv;
gettimeofday(&tv, NULL);
token.resize(33);
char *buf = (char *)token.data();
int offset = 0;
while(1){
int r = rand() + tv.tv_usec;
int len = snprintf(buf + offset, token.size() - offset, "%08x", r);
if(len == -1){
break;
}
offset += len;
if(offset >= token.size() - 1){
break;
}
}
token.resize(32);
}
void Channel::clear(){
msg_list.clear();
}
void Channel::send(const char *type, const char *content){
LinkedList<Subscriber *>::Iterator it = subs.iterator();
while(Subscriber *sub = it.next()){
sub->send_chunk(this->seq_next, type, content);
}
if(strcmp(type, "data") == 0){
msg_list.push_back(content);
seq_next ++;
if(msg_list.size() >= ServerConfig::channel_buffer_size * 1.5){
std::vector<std::string>::iterator it;
it = msg_list.end() - ServerConfig::channel_buffer_size;
msg_list.assign(it, msg_list.end());
log_trace("resize msg_list to %d, seq_next: %d", msg_list.size(), seq_next);
}
}
}