| 148 | |
| 149 | template <class M> |
| 150 | Status SubprocessProtocol::SendMessage(const M& message) { |
| 151 | VLOG(1) << "Sending message: " << pb_util::SecureDebugString(message); |
| 152 | |
| 153 | faststring buf; |
| 154 | switch (serialization_mode_) { |
| 155 | case SerializationMode::JSON: |
| 156 | { |
| 157 | string serialized; |
| 158 | const auto& google_status = |
| 159 | google::protobuf::util::MessageToJsonString(message, &serialized); |
| 160 | if (!google_status.ok()) { |
| 161 | return Status::InvalidArgument(Substitute( |
| 162 | "unable to serialize JSON: $0", pb_util::SecureDebugString(message)), |
| 163 | google_status.message().ToString()); |
| 164 | } |
| 165 | |
| 166 | buf.append(serialized); |
| 167 | buf.append("\n"); |
| 168 | break; |
| 169 | } |
| 170 | case SerializationMode::PB: |
| 171 | { |
| 172 | size_t msg_size = message.ByteSizeLong(); |
| 173 | buf.resize(sizeof(uint32_t) + msg_size); |
| 174 | NetworkByteOrder::Store32(buf.data(), msg_size); |
| 175 | if (!message.SerializeWithCachedSizesToArray(buf.data() + sizeof(uint32_t))) { |
| 176 | return Status::Corruption("failed to serialize PB to array"); |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | default: |
| 181 | break; |
| 182 | } |
| 183 | RETURN_NOT_OK_PREPEND(DoWrite(buf), "unable to send message"); |
| 184 | return Status::OK(); |
| 185 | } |
| 186 | |
| 187 | Status SubprocessProtocol::DoRead(faststring* buf) const { |
| 188 | DCHECK_LE(buf->length(), std::numeric_limits<ssize_t>::max()); |