-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessor.cpp
More file actions
181 lines (152 loc) · 2.97 KB
/
Copy pathProcessor.cpp
File metadata and controls
181 lines (152 loc) · 2.97 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Processor.h"
#include <stdio.h>
#include <string.h>
namespace gdb {
//////////////////////////////////////////////////////
Handler::Handler()
{
}
Handler::~Handler()
{
}
//////////////////////////////////////////////////////
Processor::Response::Response()
{
handler = NULL;
}
Processor::Response::~Response()
{
}
//////////////////////////////////////////////////////
Processor::Processor()
{
}
Processor::~Processor()
{
}
void
Processor::defineResponseV(const string& cmd, const string& message, va_list args)
{
char* buf = NULL;
vasprintf(&buf, message.c_str(), args);
if(buf) {
responseMap[cmd].message = buf;
responseMap[cmd].handler = NULL;
}
}
void
Processor::defineResponse(const string& cmd, const string& message, ...)
{
va_list args;
va_start(args, message);
defineResponseV(cmd, message, args);
va_end(args);
}
void
Processor::defineResponse(const string& cmd, Handler* handler)
{
responseMap[cmd].handler = handler;
responseMap[cmd].message = "";
}
bool
Processor::getNextToken(const char* buf, const char* &sep, string& cmd, string& param) const
{
static const char delimiters[] = ":;,";
if(sep == NULL) {
return false;
}
if(sep <= buf) {
sep = NULL;
return false;
}
int digits = 0;
for(--sep; sep >= buf; sep--) {
if(strchr(delimiters, *sep) != NULL) {
cmd.assign(buf, sep - buf);
param.assign(sep + 1);
return true;
}
if(*sep == '-' || (*sep >= '0' && *sep <= '9')) {
digits++;
continue;
}
if(digits > 0) {
sep++;
cmd.assign(buf, sep - buf);
param.assign(sep);
return true;
}
}
cmd.assign(buf);
param = "";
return true;
}
void
Processor::serve(const string& name, const string& params)
{
Port* port = NULL;
Socket* s = NULL;
RSP* rsp = NULL;
if((port = Port::createInstance(name, params)) == NULL) {
goto leave;
}
if((s = port->accept()) == NULL) {
goto leave;
}
rsp = new RSP(s);
if(rsp == NULL) {
goto leave;
}
nextPacket:
{
char buf[RSP_DEFAULT_BUFFER_SIZE];
int n = rsp->receivePacket(buf, sizeof(buf));
const char* sep = buf + n;
string cmd = "";
string param = "";
if(n < 0) {
if(n == RSP::INTERRUPTED) {
goto nextPacket;
}
goto leave;
}
if(n == 0) {
//empty command
goto nextPacket;
}
while(sep) {
if(!getNextToken(buf, sep, cmd, param)) {
cmd.assign(buf, 1);
param.assign(buf + 1);
}
ResponseMap::const_iterator response = responseMap.find(cmd);
if(response != responseMap.end()) {
if(response->second.handler == NULL) {
if(rsp->sendPacket(response->second.message.c_str(), response->second.message.length()) < 0) {
goto leave;
}
goto nextPacket;
}
if(response->second.handler->onHandle(rsp, cmd, param)) {
goto nextPacket;
}
}
}
//reply with empty packet
if(rsp->sendPacket("") < 0) {
goto leave;
}
goto nextPacket;
}
leave:
if(rsp) {
delete rsp;
}
if(s) {
delete s;
}
if(port) {
delete port;
}
}
}; //namespace gdb