forked from yunnian/php-nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.c
70 lines (61 loc) · 2.4 KB
/
command.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
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Zhenyu Wu <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <unistd.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/bufferevent_struct.h>
const static char *NEW_LINE = "\n";
const static int MAX_BUF_SIZE = 128;
void nsq_subscribe(struct bufferevent *bev, const char *topic, const char *channel) {
char b[MAX_BUF_SIZE];
size_t n;
n = sprintf(b, "SUB %s %s%s", topic, channel, NEW_LINE);
bufferevent_write(bev, b, n);
}
void nsq_ready(struct bufferevent *bev, int count) {
char b[MAX_BUF_SIZE];
size_t n;
n = sprintf(b, "RDY %d%s", count, NEW_LINE);
bufferevent_write(bev, b, n);
}
void nsq_finish(struct bufferevent *bev, const char *id) {
char b[MAX_BUF_SIZE];
size_t n;
n = sprintf(b, "FIN %s%s", id, NEW_LINE);
bufferevent_write(bev, b, n);
}
void nsq_touch(struct bufferevent *bev, const char *id) {
char b[MAX_BUF_SIZE];
size_t n;
n = sprintf(b, "TOUCH %s%s", id, NEW_LINE);
//bufferevent_write(bev, b, n);
evutil_socket_t fd = bufferevent_getfd(bev);
//int res = buffer_write(bev->output, fd);
int res = write(fd, b, n);
}
void nsq_nop(struct bufferevent *bev) {
char b[MAX_BUF_SIZE];
size_t n;
n = sprintf(b, "NOP%s", NEW_LINE);
bufferevent_write(bev, b, n);
}
void nsq_requeue(struct bufferevent *bev, const char *id, int timeout_ms) {
char b[MAX_BUF_SIZE];
size_t n;
n = sprintf(b, "REQ %s %d%s", id, timeout_ms, NEW_LINE);
bufferevent_write(bev, b, n);
}