AnDiXL commented on code in PR #3559:
URL: https://github.com/apache/brpc/pull/3559#discussion_r4470939744


##########
src/brpc/policy/flatbuffers_protocol.cpp:
##########
@@ -0,0 +1,475 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/policy/flatbuffers_protocol.h"
+
+#if BRPC_WITH_FLATBUFFERS
+#include <algorithm>
+#include <cstring>
+#include <limits>
+#include <memory>
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "brpc/controller.h"
+#include "brpc/server.h"
+#include "brpc/socket.h"
+#include "brpc/flatbuffers/message.h"
+#include "brpc/flatbuffers/service.h"
+#include "brpc/policy/most_common_message.h"
+#include "brpc/details/controller_private_accessor.h"
+#include "brpc/details/server_private_accessor.h"
+#include "brpc/details/usercode_backup_pool.h"
+
+extern "C" void bthread_assign_data(void* data);
+
+namespace brpc {
+namespace policy {
+namespace {
+
+const size_t kHeaderSize = 12;
+const size_t kRequestMetaSize = 24;
+const size_t kResponseMetaSize = 20;
+
+// Every integer in the header and in the fixed metadata prefix is in network
+// byte order (big-endian). meta_size permits appended fields. memcpy handles
+// fragmented or unaligned input without packed structs or aliasing issues.
+uint32_t Load32(const unsigned char* p) {
+    uint32_t value;
+    memcpy(&value, p, sizeof(value));
+    return butil::NetToHost32(value);
+}
+
+uint64_t Load64(const unsigned char* p) {
+    uint64_t value;
+    memcpy(&value, p, sizeof(value));
+    return butil::NetToHost64(value);
+}
+
+void Store32(unsigned char* p, uint32_t value) {
+    const uint32_t net = butil::HostToNet32(value);
+    memcpy(p, &net, sizeof(net));
+}
+
+void Store64(unsigned char* p, uint64_t value) {
+    const uint64_t net = butil::HostToNet64(value);
+    memcpy(p, &net, sizeof(net));
+}

Review Comment:
   Done. The local `Load32/64` and `Store32/64` helpers are gone; the header 
and fixed metadata are packed with `butil::RawPacker` and read with 
`butil::RawUnpacker`. The local buffers are `alignas(4)` and every field sits 
at a multiple of 4, so the 32-bit accesses are aligned. The wire bytes are 
unchanged and `RequestFrameBytesAreBigEndian` still checks them byte by byte.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to