diff options
author | Robert Engels <rengels@google.com> | 2022-07-20 11:24:02 -0500 |
---|---|---|
committer | Robert Engels <rengels@google.com> | 2022-07-22 19:31:14 -0500 |
commit | bcfccb74158b7f968fb6db7c2eb79db8a96f6ae9 (patch) | |
tree | fe6102db2218879dcf4e0f3162867c5ba85d64ea /emulator/Conn | |
parent | 026dc66eef4238d52ef2055922e78958f81dfc8b (diff) | |
download | car-bcfccb74158b7f968fb6db7c2eb79db8a96f6ae9.tar.gz |
protect emulator communication from concurrent access
Bug: 239879472
Test: tested using emulator and 3000 vendor vhal properties. socket
communications is untested.
Change-Id: I923145f55d84ed1c074124d7ef35441ca390b0dc
Diffstat (limited to 'emulator/Conn')
-rw-r--r-- | emulator/Conn/CommConn/CommConn.cpp | 2 | ||||
-rw-r--r-- | emulator/Conn/CommConn/include/CommConn.h | 33 | ||||
-rw-r--r-- | emulator/Conn/PipeComm/PipeComm.cpp | 2 | ||||
-rw-r--r-- | emulator/Conn/PipeComm/include/PipeComm.h | 9 | ||||
-rw-r--r-- | emulator/Conn/SocketComm/include/SocketComm.h | 27 |
5 files changed, 29 insertions, 44 deletions
diff --git a/emulator/Conn/CommConn/CommConn.cpp b/emulator/Conn/CommConn/CommConn.cpp index c800edc..241c6a9 100644 --- a/emulator/Conn/CommConn/CommConn.cpp +++ b/emulator/Conn/CommConn/CommConn.cpp @@ -48,6 +48,8 @@ void CommConn::sendMessage(vhal_proto::EmulatorMessage const& msg) { return; } + std::lock_guard<std::mutex> lock(mSendMessageLock); + write(buffer); } diff --git a/emulator/Conn/CommConn/include/CommConn.h b/emulator/Conn/CommConn/include/CommConn.h index ba5fd9f..ae6b9fb 100644 --- a/emulator/Conn/CommConn/include/CommConn.h +++ b/emulator/Conn/CommConn/include/CommConn.h @@ -97,6 +97,24 @@ class CommConn : public MessageSender { virtual bool isOpen() = 0; /** + * Serialized and send the given message to the other side. + */ + void sendMessage(const vhal_proto::EmulatorMessage& msg) final; + + protected: + MessageProcessor* mMessageProcessor; + + private: + std::unique_ptr<std::thread> mReadThread; + std::mutex mSendMessageLock; + + /** + * A thread that reads messages in a loop, and responds. You can stop this thread by calling + * stop(). + */ + void readThread(); + + /** * Blocking call to read data from the connection. * * @return std::vector<uint8_t> Serialized protobuf data received from emulator. This will be @@ -112,21 +130,6 @@ class CommConn : public MessageSender { * @return int Number of bytes transmitted, or -1 if failed. */ virtual int write(const std::vector<uint8_t>& data) = 0; - - /** - * Serialized and send the given message to the other side. - */ - void sendMessage(const vhal_proto::EmulatorMessage& msg); - - protected: - std::unique_ptr<std::thread> mReadThread; - MessageProcessor* mMessageProcessor; - - /** - * A thread that reads messages in a loop, and responds. You can stop this thread by calling - * stop(). - */ - void readThread(); }; } // namespace impl diff --git a/emulator/Conn/PipeComm/PipeComm.cpp b/emulator/Conn/PipeComm/PipeComm.cpp index 491f062..789cffb 100644 --- a/emulator/Conn/PipeComm/PipeComm.cpp +++ b/emulator/Conn/PipeComm/PipeComm.cpp @@ -80,8 +80,6 @@ std::vector<uint8_t> PipeComm::read() { int PipeComm::write(const std::vector<uint8_t>& data) { int retVal = 0; - std::lock_guard<std::mutex> lock(mPipeWriteLock); - if (mPipeFd != -1) { retVal = qemud_channel_send(mPipeFd, data.data(), data.size()); } diff --git a/emulator/Conn/PipeComm/include/PipeComm.h b/emulator/Conn/PipeComm/include/PipeComm.h index 6c24037..2043ac4 100644 --- a/emulator/Conn/PipeComm/include/PipeComm.h +++ b/emulator/Conn/PipeComm/include/PipeComm.h @@ -44,17 +44,16 @@ class PipeComm : public CommConn { void start() override; void stop() override; - std::vector<uint8_t> read() override; - int write(const std::vector<uint8_t>& data) override; - inline bool isOpen() override { return mPipeFd > 0; } private: int mPipeFd; - std::mutex mPipeWriteLock; + + std::vector<uint8_t> read() override; + int write(const std::vector<uint8_t>& data) override; }; -} // impl +} // namespace impl } // namespace V2_0 } // namespace vehicle diff --git a/emulator/Conn/SocketComm/include/SocketComm.h b/emulator/Conn/SocketComm/include/SocketComm.h index 4e385c3..7da546b 100644 --- a/emulator/Conn/SocketComm/include/SocketComm.h +++ b/emulator/Conn/SocketComm/include/SocketComm.h @@ -81,36 +81,19 @@ class SocketComm : public MessageSender { * SocketConn represents a single connection to a client. */ class SocketConn : public CommConn { - public: + public: SocketConn(MessageProcessor* messageProcessor, int sfd); virtual ~SocketConn() = default; - /** - * Blocking call to read data from the connection. - * - * @return std::vector<uint8_t> Serialized protobuf data received from emulator. This will be - * an empty vector if the connection was closed or some other error occurred. - */ - std::vector<uint8_t> read() override; - - /** - * Closes a connection if it is open. - */ void stop() override; - /** - * Transmits a string of data to the emulator. - * - * @param data Serialized protobuf data to transmit. - * - * @return int Number of bytes transmitted, or -1 if failed. - */ - int write(const std::vector<uint8_t>& data) override; - inline bool isOpen() override { return mSockFd > 0; } - private: + private: int mSockFd; + + std::vector<uint8_t> read() override; + int write(const std::vector<uint8_t>& data) override; }; } // impl |