OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/security_key/remote_security_key_message_writer.h" |
| 6 |
| 7 #include <cstdint> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "remoting/host/security_key/security_key_message.h" |
| 12 |
| 13 namespace remoting { |
| 14 |
| 15 RemoteSecurityKeyMessageWriter::RemoteSecurityKeyMessageWriter( |
| 16 base::File output_file) |
| 17 : output_stream_(std::move(output_file)) {} |
| 18 |
| 19 RemoteSecurityKeyMessageWriter::~RemoteSecurityKeyMessageWriter() {} |
| 20 |
| 21 bool RemoteSecurityKeyMessageWriter::WriteMessage( |
| 22 RemoteSecurityKeyMessageType message_type) { |
| 23 return WriteMessageWithPayload(message_type, std::string()); |
| 24 } |
| 25 |
| 26 bool RemoteSecurityKeyMessageWriter::WriteMessageWithPayload( |
| 27 RemoteSecurityKeyMessageType message_type, |
| 28 const std::string& message_payload) { |
| 29 if (write_failed_ || !output_stream_.IsValid()) { |
| 30 return false; |
| 31 } |
| 32 |
| 33 int message_payload_size_bytes = message_payload.size(); |
| 34 uint32_t total_message_size_bytes = |
| 35 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload_size_bytes; |
| 36 CHECK(SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)); |
| 37 |
| 38 // First we send the message header which is the length of the message_type |
| 39 // and message_payload in bytes. |
| 40 if (!WriteBytesToOutput(reinterpret_cast<char*>(&total_message_size_bytes), |
| 41 SecurityKeyMessage::kHeaderSizeBytes)) { |
| 42 LOG(ERROR) << "Failed to send message header."; |
| 43 return false; |
| 44 } |
| 45 |
| 46 // Next we send the message_type. |
| 47 if (!WriteBytesToOutput(reinterpret_cast<char*>(&message_type), |
| 48 SecurityKeyMessage::kMessageTypeSizeBytes)) { |
| 49 LOG(ERROR) << "Failed to send message type."; |
| 50 return false; |
| 51 } |
| 52 |
| 53 // Lastly, send the message data if appropriate. |
| 54 if (!message_payload.empty()) { |
| 55 if (!WriteBytesToOutput(message_payload.data(), |
| 56 message_payload_size_bytes)) { |
| 57 LOG(ERROR) << "Failed to send message payload."; |
| 58 return false; |
| 59 } |
| 60 } |
| 61 |
| 62 return true; |
| 63 } |
| 64 |
| 65 bool RemoteSecurityKeyMessageWriter::WriteBytesToOutput(const char* message, |
| 66 int bytes_to_write) { |
| 67 DCHECK(message); |
| 68 DCHECK_GT(bytes_to_write, 0); |
| 69 |
| 70 int result = output_stream_.WriteAtCurrentPos(message, bytes_to_write); |
| 71 if (result != bytes_to_write) { |
| 72 LOG(ERROR) << "Failed to write all bytes to output stream. bytes written: " |
| 73 << result << ", file error: " |
| 74 << base::File::ErrorToString(output_stream_.error_details()); |
| 75 write_failed_ = true; |
| 76 return false; |
| 77 } |
| 78 |
| 79 return true; |
| 80 } |
| 81 |
| 82 } // namespace remoting |
OLD | NEW |