forked from ideawu/icomet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.cpp
More file actions
156 lines (134 loc) · 4.92 KB
/
subscriber.cpp
File metadata and controls
156 lines (134 loc) · 4.92 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
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
150
151
152
153
154
155
#include "subscriber.h"
#include "channel.h"
#include "server.h"
#include "util/log.h"
#include "server_config.h"
static std::string iframe_header = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'><meta http-equiv='Cache-Control' content='no-store'><meta http-equiv='Cache-Control' content='no-cache'><meta http-equiv='Pragma' content='no-cache'><meta http-equiv=' Expires' content='Thu, 1 Jan 1970 00:00:00 GMT'><script type='text/javascript'>window.onError = null;try{ document.domain = (window.location.hostname.match(/^(d{1,3}.){3}d{1,3}$/)) ? window.location.hostname : window.location.hostname.split('.').slice(-2).join('.');}catch(e){};</script></head><body>";
static std::string iframe_chunk_prefix = "<script>parent.icomet_cb(";
static std::string iframe_chunk_suffix = ");</script>";
static void on_sub_disconnect(struct evhttp_connection *evcon, void *arg){
log_debug("subscriber disconnected");
Subscriber *sub = (Subscriber *)arg;
Server *serv = sub->serv;
serv->sub_end(sub);
}
void Subscriber::start(){
evhttp_add_header(req->output_headers, "Connection", "keep-alive");
//evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");
//evhttp_add_header(req->output_headers, "Expires", "0");
evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=utf-8");
evhttp_send_reply_start(req, HTTP_OK, "OK");
evhttp_connection_set_closecb(req->evcon, on_sub_disconnect, this);
if(this->type == POLL){
//
}else if(this->type == IFRAME){
struct evbuffer *buf = evbuffer_new();
evbuffer_add_printf(buf, "%s\n", iframe_header.c_str());
evhttp_send_reply_chunk(this->req, buf);
evbuffer_free(buf);
}
// send buffered messages
if(!channel->msg_list.empty() && channel->seq_next != this->seq_next){
this->send_old_msgs();
}
}
void Subscriber::send_old_msgs(){
std::vector<std::string>::iterator it = channel->msg_list.end();
int msg_seq_min = channel->seq_next - channel->msg_list.size();
if(Channel::SEQ_GT(this->seq_next, channel->seq_next) || Channel::SEQ_LT(this->seq_next, msg_seq_min)){
this->seq_next = msg_seq_min;
}
log_debug("send old msg: [%d, %d]", this->seq_next, channel->seq_next - 1);
it -= (channel->seq_next - this->seq_next);
struct evbuffer *buf = evbuffer_new();
if(this->type == POLL){
if(!this->callback.empty()){
evbuffer_add_printf(buf, "%s(", this->callback.c_str());
}
evbuffer_add_printf(buf, "[");
for(/**/; it != channel->msg_list.end(); it++, this->seq_next++){
std::string &msg = *it;
evbuffer_add_printf(buf,
"{\"type\":\"data\",\"cname\":\"%s\",\"seq\":%d,\"content\":\"%s\"}",
this->channel->name.c_str(),
this->seq_next,
msg.c_str());
if(this->seq_next != channel->seq_next - 1){
evbuffer_add(buf, ",", 1);
}
}
evbuffer_add_printf(buf, "]");
if(!this->callback.empty()){
evbuffer_add_printf(buf, ");");
}
evbuffer_add_printf(buf, "\n");
evhttp_send_reply_chunk(this->req, buf);
this->close();
}else if(this->type == IFRAME || this->type == STREAM){
for(/**/; it != channel->msg_list.end(); it++, this->seq_next++){
std::string &msg = *it;
this->send_chunk(this->seq_next, "data", msg.c_str());
}
}
evbuffer_free(buf);
}
void Subscriber::close(){
evhttp_send_reply_end(this->req);
evhttp_connection_set_closecb(this->req->evcon, NULL, NULL);
this->serv->sub_end(this);
}
void Subscriber::noop(){
this->send_chunk(this->seq_noop, "noop", "");
}
void Subscriber::send_chunk(int seq, const char *type, const char *content){
struct evbuffer *buf = evbuffer_new();
if(this->type == POLL){
if(!this->callback.empty()){
evbuffer_add_printf(buf, "%s(", this->callback.c_str());
}
}else if(this->type == IFRAME){
evbuffer_add_printf(buf, "%s", iframe_chunk_prefix.c_str());
}
evbuffer_add_printf(buf,
"{\"type\":\"%s\",\"cname\":\"%s\",\"seq\":%d,\"content\":\"%s\"}",
type,
this->channel->name.c_str(),
seq,
content);
if(this->type == POLL){
if(!this->callback.empty()){
evbuffer_add_printf(buf, ");");
}
}else if(this->type == IFRAME){
evbuffer_add_printf(buf, "%s", iframe_chunk_suffix.c_str());
}
evbuffer_add_printf(buf, "\n");
evhttp_send_reply_chunk(this->req, buf);
evbuffer_free(buf);
this->idle = 0;
if(this->type == POLL){
this->close();
}
}
void Subscriber::send_error_reply(int sub_type, struct evhttp_request *req, const char *cb, const char *type, const char *content){
struct evbuffer *buf = evbuffer_new();
if(sub_type == POLL){
evbuffer_add_printf(buf, "%s(", cb);
}else if(sub_type == IFRAME){
evbuffer_add_printf(buf, "%s", iframe_chunk_prefix.c_str());
}
evbuffer_add_printf(buf,
"{\"type\":\"%s\",\"cname\":\"%s\",\"seq\":%d,\"content\":\"%s\"}",
type,
"",
0,
content);
if(sub_type == POLL){
evbuffer_add_printf(buf, ");");
}else if(sub_type == IFRAME){
evbuffer_add_printf(buf, "%s", iframe_chunk_suffix.c_str());
}
evbuffer_add_printf(buf, "\n");
evhttp_send_reply(req, HTTP_OK, "OK", buf);
evbuffer_free(buf);
}