forked from leverich/mutilate
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Protocol.h
71 lines (56 loc) · 1.76 KB
/
Protocol.h
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
// -*- c++-mode -*-
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <event2/bufferevent.h>
#include "ConnectionOptions.h"
using namespace std;
class Connection;
class Protocol {
public:
Protocol(options_t _opts, Connection* _conn, bufferevent* _bev):
opts(_opts), conn(_conn), bev(_bev) {};
~Protocol() {};
virtual bool setup_connection_w() = 0;
virtual bool setup_connection_r(evbuffer* input) = 0;
virtual int get_request(const char* key) = 0;
virtual int set_request(const char* key, const char* value, int len) = 0;
virtual bool handle_response(evbuffer* input, bool &done) = 0;
protected:
options_t opts;
Connection* conn;
bufferevent* bev;
};
class ProtocolAscii : public Protocol {
public:
ProtocolAscii(options_t opts, Connection* conn, bufferevent* bev):
Protocol(opts, conn, bev) {
read_state = IDLE;
};
~ProtocolAscii() {};
virtual bool setup_connection_w() { return true; }
virtual bool setup_connection_r(evbuffer* input) { return true; }
virtual int get_request(const char* key);
virtual int set_request(const char* key, const char* value, int len);
virtual bool handle_response(evbuffer* input, bool &done);
private:
enum read_fsm {
IDLE,
WAITING_FOR_GET,
WAITING_FOR_GET_DATA,
WAITING_FOR_END,
};
read_fsm read_state;
int data_length;
};
class ProtocolBinary : public Protocol {
public:
ProtocolBinary(options_t opts, Connection* conn, bufferevent* bev):
Protocol(opts, conn, bev) {};
~ProtocolBinary() {};
virtual bool setup_connection_w();
virtual bool setup_connection_r(evbuffer* input);
virtual int get_request(const char* key);
virtual int set_request(const char* key, const char* value, int len);
virtual bool handle_response(evbuffer* input, bool &done);
};
#endif