From 1bf6f295a8bd8b81a10ae12ada2de7efa48ba2a4 Mon Sep 17 00:00:00 2001 From: Alessio Igor Bogani <alessioigorbogani@gmail.com> Date: Fri, 15 Mar 2024 11:00:57 +0100 Subject: [PATCH] Consolidate two read() functins --- src/Socket2.cpp | 73 ++++++++++++++++++++++--------------------------- src/Socket2.h | 3 +- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/Socket2.cpp b/src/Socket2.cpp index 79626de..3595914 100644 --- a/src/Socket2.cpp +++ b/src/Socket2.cpp @@ -573,7 +573,7 @@ Tango::DevVarCharArray *Socket2::read(Tango::DevLong argin) "", "Out of limit", __PRETTY_FUNCTION__); } - read(argin); + _read(argin); argout = new Tango::DevVarCharArray(); vector<unsigned char> transfer(data.begin(), data.begin() + argin); @@ -618,7 +618,7 @@ Tango::DevVarCharArray *Socket2::read_until(const Tango::DevVarCharArray *argin) break; } } - read(dsize + 1); + _read(dsize + 1); } while (true); argout = new Tango::DevVarCharArray(); @@ -753,12 +753,38 @@ ssize_t Socket2::_write(int fd, const void *buf, size_t count) return ret; } -ssize_t Socket2::_read(int fd, void *buf, size_t count) +void Socket2::_read(size_t bytes_to_read) { - int ret = proto == UDP? - ::recvfrom(fd, buf, count, 0, (sockaddr*) &sa, &sa_len): - ::read(fd, buf, count); - return ret; + unsigned char buffer[10000]; + size_t bytes_total = data.size(); + + while (bytes_total < bytes_to_read) { + if (! wait_for(READ)) + goto timeout; + + size_t count = min((size_t)max(input_queue_length(), 0), sizeof(buffer)); + ssize_t bytes_readed = proto == UDP? + ::recvfrom(fd, buffer, count, 0, (sockaddr*) &sa, &sa_len): + ::read(fd, buffer, count); + + if (bytes_readed > 0) { + data.insert(data.end(), &buffer[0], &buffer[bytes_readed]); + bytes_total += bytes_readed; + } else if (bytes_readed == 0) { + if (multiplexing == SELECT) + goto error; + /* Continue if multiplexing == SLEEP */ + } else { /* bytes_readed < 0 */ + check_state(true); + goto error; + } + } + return; +error: + sleep(tout); +timeout: + Tango::Except::throw_exception( + "", "Timeout expired", __PRETTY_FUNCTION__); } void Socket2::check_state(bool forcing) @@ -871,39 +897,6 @@ bool Socket2::wait_for(event_type et) return false; } -void Socket2::read(size_t bytes_to_read) -{ - unsigned char buffer[10000]; - size_t bytes_total = data.size(); - - while (bytes_total < bytes_to_read) { - if (! wait_for(READ)) - goto timeout; - - ssize_t bytes_readed = _read(fd, buffer, - min((size_t)max(input_queue_length(), 0), - sizeof(buffer))); - - if (bytes_readed > 0) { - data.insert(data.end(), &buffer[0], &buffer[bytes_readed]); - bytes_total += bytes_readed; - } else if (bytes_readed == 0) { - if (multiplexing == SELECT) - goto error; - /* Continue if multiplexing == SLEEP */ - } else { /* bytes_readed < 0 */ - check_state(true); - goto error; - } - } - return; -error: - sleep(tout); -timeout: - Tango::Except::throw_exception( - "", "Timeout expired", __PRETTY_FUNCTION__); -} - void Socket2::resolve() { DEBUG_STREAM << "Resolving " << hostname << "... " << endl; diff --git a/src/Socket2.h b/src/Socket2.h index 8054eef..06bf334 100644 --- a/src/Socket2.h +++ b/src/Socket2.h @@ -265,10 +265,9 @@ public: int output_queue_length(); void close(); ssize_t _write(int, const void*, size_t); - ssize_t _read(int, void*, size_t); + void _read(size_t); void check_state(bool); bool wait_for(event_type); - void read(size_t); void resolve(); /*----- PROTECTED REGION END -----*/ // Socket2::Additional Method prototypes }; -- GitLab