Repository navigation
Expand file tree
/
Copy pathdebugger_thrift_buffer.cpp
More file actions
136 lines (120 loc) · 4.62 KB
/
Copy pathdebugger_thrift_buffer.cpp
File metadata and controls
136 lines (120 loc) · 4.62 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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/debugger/debugger_thrift_buffer.h"
#include "hphp/runtime/base/variable-serializer.h"
#include "hphp/runtime/base/variable-unserializer.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debugger);
String DebuggerThriftBuffer::readImpl() {
TRACE(7, "DebuggerThriftBuffer::readImpl\n");
assert(m_size <= BUFFER_SIZE);
int nread = m_socket->readImpl(m_buffer, m_size);
m_buffer[nread] = '\0';
return String(m_buffer, nread, CopyString);
}
void DebuggerThriftBuffer::flushImpl(const String& data) {
TRACE(7, "DebuggerThriftBuffer::flushImpl\n");
m_socket->write(data);
}
void DebuggerThriftBuffer::throwError(const char *msg, int code) {
TRACE(2, "DebuggerThriftBuffer::throwError\n");
throw Exception("Protocol Error (%d): %s", code, msg);
}
///////////////////////////////////////////////////////////////////////////////
const StaticString
s_hit_limit(LITSTR_INIT("Hit serialization limit")),
s_unknown_exp(LITSTR_INIT("Hit unknown exception")),
s_type_mismatch(LITSTR_INIT("Type mismatch"));
template<typename T>
static inline int serializeImpl(T data, String& sdata) {
TRACE(7, "DebuggerWireHelpers::serializeImpl\n");
VariableSerializer vs(VariableSerializer::Type::DebuggerSerialize);
try {
sdata = vs.serialize(data, true);
} catch (StringBufferLimitException &e) {
sdata = s_hit_limit;
return DebuggerWireHelpers::HitLimit;
} catch (...) {
sdata = s_unknown_exp;
return DebuggerWireHelpers::UnknownError;
}
return DebuggerWireHelpers::NoError;
}
static inline int unserializeImpl(const String& sdata, Variant& data) {
TRACE(7, "DebuggerWireHelpers::unserializeImpl(CStrRef sdata,\n");
if (sdata.same(s_hit_limit)) {
return DebuggerWireHelpers::HitLimit;
}
if (sdata.same(s_unknown_exp)) {
return DebuggerWireHelpers::UnknownError;
}
VariableUnserializer vu(sdata.data(), sdata.size(),
VariableUnserializer::Type::Serialize, true);
try {
data = vu.unserialize();
} catch (Exception &e) {
data = null_variant;
return DebuggerWireHelpers::UnknownError;
}
return DebuggerWireHelpers::NoError;
}
int DebuggerWireHelpers::WireSerialize(CArrRef data, String& sdata) {
TRACE(7, "DebuggerWireHelpers::WireSerialize(CArrRef data,\n");
return serializeImpl(data, sdata);
}
int DebuggerWireHelpers::WireSerialize(CObjRef data, String& sdata) {
TRACE(7, "DebuggerWireHelpers::WireSerialize(CObjRef data,\n");
return serializeImpl(data, sdata);
}
int DebuggerWireHelpers::WireSerialize(CVarRef data, String& sdata) {
TRACE(7, "DebuggerWireHelpers::WireSerialize(CVarRef data,\n");
return serializeImpl(data, sdata);
}
int DebuggerWireHelpers::WireUnserialize(String& sdata, Array& data) {
TRACE(7, "DebuggerWireHelpers::WireUnserialize, Array& data)\n");
Variant v;
int ret = unserializeImpl(sdata, v);
if (ret != NoError) {
return ret;
}
if (!v.isArray() && !v.isNull()) {
sdata = s_type_mismatch;
return TypeMismatch;
}
data = v;
return NoError;
}
int DebuggerWireHelpers::WireUnserialize(String& sdata, Object& data) {
TRACE(7, "DebuggerWireHelpers::WireUnserialize, Object& data\n");
Variant v;
int ret = unserializeImpl(sdata, v);
if (ret != NoError) {
return ret;
}
if (!v.isObject() && !v.isNull()) {
sdata = s_type_mismatch;
return TypeMismatch;
}
data = v.toObject();
return NoError;
}
int DebuggerWireHelpers::WireUnserialize(String& sdata, Variant& data) {
TRACE(7, "DebuggerWireHelpers::WireUnserialize\n");
return unserializeImpl(sdata, data);
}
///////////////////////////////////////////////////////////////////////////////
}