Fix Write() method of Port 54/265654/1
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 26 Oct 2021 09:00:41 +0000 (18:00 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 26 Oct 2021 09:00:41 +0000 (18:00 +0900)
If the data is larger than the maximum socket buffer size, the port divides
the data by the maximum socket buffer size and transmits it.
And, the queue size of delayed messages is changed to '1024 * 1024 * 10'.

Change-Id: I357efb0c55af9bf61abb06dcc49e63f031ba1091
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/port-internal.cc
src/port-internal.hh

index aaccee8..7dd3ce6 100644 (file)
@@ -35,7 +35,7 @@ namespace rpc_port {
 namespace internal {
 namespace {
 
-constexpr const int QUEUE_SIZE_MAX = 1024 * 1024;
+constexpr const int QUEUE_SIZE_MAX = 1024 * 1024 * 10;
 constexpr const int MAX_RETRY_CNT = 10;
 constexpr const int MAX_TIMEOUT = 1000;
 constexpr const int MIN_TIMEOUT = 50;
@@ -71,10 +71,13 @@ Port::Port(int fd, std::string id)
   uuid_generate(u);
   uuid_unparse(u, uuid);
   instance_ = std::string(uuid) + ":" + id_;
+  send_buffer_size_ = GetSendBufferSize();
 }
 
 Port::Port(int fd, std::string id, std::string instance)
-    : fd_(fd), id_(std::move(id)), instance_(std::move(instance)), seq_(0) {}
+    : fd_(fd), id_(std::move(id)), instance_(std::move(instance)), seq_(0) {
+  send_buffer_size_ = GetSendBufferSize();
+}
 
 Port::~Port() {
   ClearQueue();
@@ -179,6 +182,19 @@ int Port::Read(void* buf, unsigned int size) {
   return RPC_PORT_ERROR_NONE;
 }
 
+int Port::GetSendBufferSize() {
+  int value;
+  socklen_t len = sizeof(int);
+  int ret = getsockopt(fd_, SOL_SOCKET, SO_SNDBUF,
+      reinterpret_cast<void*>(&value), &len);
+  if (ret < 0) {
+    _E("getsockopt() is failed. errno(%d)", errno);
+    return 163840;
+  }
+
+  return value;
+}
+
 bool Port::CanRead(int timeout) {
   struct pollfd fds[1];
   fds[0].fd = fd_;
@@ -259,7 +275,8 @@ int Port::Write(const void* buf, unsigned int size, int* sent_bytes) {
   }
 
   while (left && (retry_cnt < MAX_RETRY_CNT)) {
-    nb = send(fd_, buffer, left, MSG_NOSIGNAL);
+    size_t len = left > send_buffer_size_ ? send_buffer_size_ : left;
+    nb = send(fd_, buffer, len, MSG_NOSIGNAL);
     if (nb == -1) {
       if (errno == EINTR) {
         LOGI("write_socket: EINTR continue ...");
index 853b27f..028e786 100644 (file)
@@ -67,6 +67,7 @@ class Port : public std::enable_shared_from_this<Port> {
   }
 
  private:
+  int GetSendBufferSize();
   bool CanRead(int timeout);
   bool CanWrite();
   void IgnoreIOEvent();
@@ -102,6 +103,7 @@ class Port : public std::enable_shared_from_this<Port> {
   int fd_;
   std::string id_;
   std::string instance_;
+  size_t send_buffer_size_;
   std::atomic<uint32_t> seq_;
   mutable std::recursive_mutex mutex_;
   std::queue<std::shared_ptr<DelayMessage>> queue_;