Skip to content

Commit b856888

Browse files
committed
reorg code
1 parent cec0bf8 commit b856888

1 file changed

Lines changed: 42 additions & 55 deletions

File tree

src/brpc/policy/baidu_rpc_protocol.cpp

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ ParseResult ParseRpcMessage(butil::IOBuf* source, Socket* socket,
109109

110110
char header_buf[12];
111111
size_t n = 0;
112+
uint32_t body_size;
113+
uint32_t meta_size;
114+
ParseError pe = PARSE_OK;
115+
112116
#if BRPC_WITH_GDR
113117
void* prefetch_d2h_data = NULL;
114-
115118
uint64_t data_meta = source->get_first_data_meta();
116119
bool is_gpu_memory = (data_meta > 0 && data_meta <= UINT_MAX);
117120
butil::gdr::BlockPoolAllocator* host_allocator = butil::gdr::BlockPoolAllocators::singleton()->get_cpu_allocator();
@@ -129,74 +132,58 @@ ParseResult ParseRpcMessage(butil::IOBuf* source, Socket* socket,
129132
#else
130133
n = source->copy_to(header_buf, sizeof(header_buf));
131134
#endif // BRPC_WITH_GDR
132-
if (n >= 4) {
133-
void* dummy = header_buf;
134-
if (*(const uint32_t*)dummy != *(const uint32_t*)"PRPC") {
135-
#if BRPC_WITH_GDR
136-
if (is_gpu_memory) {
137-
host_allocator->DeallocateRaw(prefetch_d2h_data);
135+
136+
do {
137+
if (n >= 4) {
138+
void* dummy = header_buf;
139+
if (*(const uint32_t*)dummy != *(const uint32_t*)"PRPC") {
140+
pe = PARSE_ERROR_TRY_OTHERS;
141+
break;
138142
}
139-
#endif // BRPC_WITH_GDR
140-
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
141-
}
142-
} else {
143-
if (memcmp(header_buf, "PRPC", n) != 0) {
144-
#if BRPC_WITH_GDR
145-
if (is_gpu_memory) {
146-
host_allocator->DeallocateRaw(prefetch_d2h_data);
143+
} else {
144+
if (memcmp(header_buf, "PRPC", n) != 0) {
145+
pe = PARSE_ERROR_TRY_OTHERS;
146+
break;
147147
}
148-
#endif // BRPC_WITH_GDR
149-
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
150148
}
151-
}
152-
if (n < sizeof(header_buf)) {
153-
#if BRPC_WITH_GDR
154-
if (is_gpu_memory) {
155-
host_allocator->DeallocateRaw(prefetch_d2h_data);
149+
if (n < sizeof(header_buf)) {
150+
pe = PARSE_ERROR_NOT_ENOUGH_DATA;
151+
break;
156152
}
157-
#endif // BRPC_WITH_GDR
158-
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
159-
}
160-
uint32_t body_size;
161-
uint32_t meta_size;
162-
butil::RawUnpacker(header_buf + 4).unpack32(body_size).unpack32(meta_size);
163-
if (body_size > 128 * 1024 * 1024) {
164-
LOG(ERROR) << "body_size=" << body_size << " from "
165-
<< socket->remote_side() << " is too large";
166-
}
167-
if (body_size > FLAGS_max_body_size) {
168-
// We need this log to report the body_size to give users some clues
169-
// which is not printed in InputMessenger.
170-
LOG(ERROR) << "body_size=" << body_size << " from "
171-
<< socket->remote_side() << " is too large";
172-
#if BRPC_WITH_GDR
173-
if (is_gpu_memory) {
174-
host_allocator->DeallocateRaw(prefetch_d2h_data);
153+
butil::RawUnpacker(header_buf + 4).unpack32(body_size).unpack32(meta_size);
154+
if (body_size > FLAGS_max_body_size) {
155+
// We need this log to report the body_size to give users some clues
156+
// which is not printed in InputMessenger.
157+
LOG(ERROR) << "body_size=" << body_size << " from "
158+
<< socket->remote_side() << " is too large";
159+
pe = PARSE_ERROR_TOO_BIG_DATA;
160+
break;
161+
} else if (source->length() < sizeof(header_buf) + body_size) {
162+
pe = PARSE_ERROR_NOT_ENOUGH_DATA;
163+
break;
175164
}
176-
#endif // BRPC_WITH_GDR
177-
return MakeParseError(PARSE_ERROR_TOO_BIG_DATA);
178-
} else if (source->length() < sizeof(header_buf) + body_size) {
179-
#if BRPC_WITH_GDR
180-
if (is_gpu_memory) {
181-
host_allocator->DeallocateRaw(prefetch_d2h_data);
165+
if (meta_size > body_size) {
166+
LOG(ERROR) << "meta_size=" << meta_size << " is bigger than body_size="
167+
<< body_size;
168+
// Pop the message
169+
source->pop_front(sizeof(header_buf) + body_size);
170+
pe = PARSE_ERROR_TRY_OTHERS;
171+
break;
182172
}
183-
#endif // BRPC_WITH_GDR
184-
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
185-
}
186-
if (meta_size > body_size) {
187-
LOG(ERROR) << "meta_size=" << meta_size << " is bigger than body_size="
188-
<< body_size;
189-
// Pop the message
190-
source->pop_front(sizeof(header_buf) + body_size);
173+
} while (0);
174+
175+
if (pe != PARSE_OK) {
191176
#if BRPC_WITH_GDR
192177
if (is_gpu_memory) {
193178
host_allocator->DeallocateRaw(prefetch_d2h_data);
194179
}
195180
#endif // BRPC_WITH_GDR
196-
return MakeParseError(PARSE_ERROR_TRY_OTHERS);
181+
return MakeParseError(pe);
197182
}
183+
198184
source->pop_front(sizeof(header_buf));
199185
MostCommonMessage* msg = MostCommonMessage::Get();
186+
200187
#if BRPC_WITH_GDR
201188
if (is_gpu_memory) {
202189
if (header_size + meta_size <= n) {

0 commit comments

Comments
 (0)