Fix Port::Read() method
[platform/core/appfw/rpc-port.git] / src / port-internal.hh
1 /*
2  * Copyright (c) 2017 - 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef PORT_INTERNAL_HH_
18 #define PORT_INTERNAL_HH_
19
20 #include <gio/gio.h>
21 #include <glib.h>
22
23 #include <algorithm>
24 #include <atomic>
25 #include <memory>
26 #include <mutex>
27 #include <queue>
28 #include <string>
29 #include <thread>
30 #include <vector>
31
32 namespace rpc_port {
33 namespace internal {
34
35 class Port : public std::enable_shared_from_this<Port> {
36  public:
37   Port(int fd, std::string id, std::string instance);
38   Port(int fd, std::string id);
39   virtual ~Port();
40
41   void Disconnect();
42   int SetPrivateSharing(const char* paths[], unsigned int size);
43   int SetPrivateSharing(const char* path);
44   int UnsetPrivateSharing();
45
46   int Read(void* buf, unsigned int size);
47   int Write(const void* buf, unsigned int size);
48   int Write(const void* buf, unsigned int size, int* sent_bytes);
49   int GetFd() const {
50     return fd_;
51   }
52
53   const std::string& GetId() const {
54     return id_;
55   }
56
57   std::recursive_mutex& GetMutex() const {
58     return mutex_;
59   }
60
61   const std::string& GetInstance() const {
62     return instance_;
63   }
64
65   uint32_t GetSeq() {
66     return ++seq_;
67   }
68
69  private:
70   // LCOV_EXCL_START
71   int SetReceiveTimeout(int timeout);
72   bool CanWrite();
73   void IgnoreIOEvent();
74   int ListenIOEvent();
75
76   class DelayMessage {
77    public:
78     DelayMessage(const char* msg, int start_index, int size);
79     ~DelayMessage() = default;
80     void SetIndex(int index);
81     int GetSize();
82     int GetOriginalSize();
83     char* GetMessage();
84
85    private:
86     std::vector<unsigned char> message_;
87     int index_;
88     int size_;
89   };
90
91   enum PortStatus {
92     PORT_STATUS_ERROR_NONE,
93     PORT_STATUS_ERROR_IO_ERROR,
94     PORT_STATUS_ERROR_RESOURCE_UNAVAILABLE
95   };
96
97   int PushDelayedMessage(std::shared_ptr<DelayMessage> dm);
98   int PopDelayedMessage();
99   static gboolean OnEventReceived(GIOChannel* io,
100       GIOCondition condition, gpointer data);
101   void ClearQueue();
102   // LCOV_EXCL_STOP
103
104   int fd_;
105   std::string id_;
106   std::string instance_;
107   std::atomic<uint32_t> seq_;
108   mutable std::recursive_mutex mutex_;
109   mutable std::recursive_mutex rw_mutex_;
110   std::queue<std::shared_ptr<DelayMessage>> queue_;
111   int delayed_message_size_ = 0;
112   GIOChannel* channel_ = nullptr;
113   guint source_id_ = 0;
114 };
115
116 }  // namespace internal
117 }  // namespace rpc_port
118
119 #endif  // PORT_INTERNAL_HH_