From 8e96750046a7d03e557cda2f347b16a39759c19b Mon Sep 17 00:00:00 2001 From: peri4 Date: Thu, 6 Feb 2025 11:13:43 +0300 Subject: [PATCH] version 4.7.0 add PIEthernet socket options setReadBufferSize() and setWriteBufferSize() add PIByteArray::dataAs --- CMakeLists.txt | 2 +- libs/main/io_devices/piethernet.cpp | 41 ++++++++++++++++++++++++++++- libs/main/io_devices/piethernet.h | 14 +++++++--- libs/main/types/pibytearray.h | 5 ++++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d178844d..57d258ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(PIP) set(PIP_MAJOR 4) -set(PIP_MINOR 6) +set(PIP_MINOR 7) set(PIP_REVISION 0) set(PIP_SUFFIX ) set(PIP_COMPANY SHS) diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index cd7740a0..69cc2f30 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -163,6 +163,30 @@ PIEthernet::~PIEthernet() { } +void PIEthernet::setReadTimeout(PISystemTime tm) { + setProperty("readTimeout", tm); + applyTimeouts(); +} + + +void PIEthernet::setWriteTimeout(PISystemTime tm) { + setProperty("writeTimeout", tm); + applyTimeouts(); +} + + +void PIEthernet::setReadBufferSize(int bytes) { + rcv_buf = bytes; + applyBuffers(); +} + + +void PIEthernet::setWriteBufferSize(int bytes) { + snd_buf = bytes; + applyBuffers(); +} + + void PIEthernet::construct() { // piCout << " PIEthernet" << uint(this); setOption(BlockingWrite); @@ -211,6 +235,7 @@ void PIEthernet::init() { } applyParameters(); applyTimeouts(); + applyBuffers(); applyOptInt(IPPROTO_IP, IP_TTL, TTL()); // piCoutObj << "inited" << path(); } @@ -296,6 +321,7 @@ bool PIEthernet::openDevice() { while (!mcast_queue.isEmpty()) joinMulticastGroup(mcast_queue.dequeue()); applyTimeouts(); + applyBuffers(); applyOptInt(IPPROTO_IP, IP_TTL, TTL()); addr_lr.clear(); return true; @@ -338,9 +364,22 @@ void PIEthernet::applyTimeouts() { } +void PIEthernet::applyBuffers() { + if (sock < 0) return; + if (rcv_buf > 0) ethSetsockoptInt(sock, SOL_SOCKET, SO_RCVBUF, rcv_buf); + if (snd_buf > 0) { + if (sock_s != sock) { + ethSetsockoptInt(sock_s, SOL_SOCKET, SO_SNDBUF, snd_buf); + } else { + ethSetsockoptInt(sock, SOL_SOCKET, SO_SNDBUF, snd_buf); + } + } +} + + void PIEthernet::applyTimeout(int fd, int opt, PISystemTime tm) { if (fd == 0) return; - // piCoutObj << "setReadIsBlocking" << yes; + // piCoutObj << "setReadIsBlocking" << yes; #ifdef WINDOWS DWORD _tm = tm.toMilliseconds(); #else diff --git a/libs/main/io_devices/piethernet.h b/libs/main/io_devices/piethernet.h index 7b16d0b1..97f8ca3b 100644 --- a/libs/main/io_devices/piethernet.h +++ b/libs/main/io_devices/piethernet.h @@ -180,10 +180,17 @@ public: PISystemTime writeTimeout() const { return property("writeTimeout").toSystemTime(); } //! Set timeout for read - void setReadTimeout(PISystemTime tm) { setProperty("readTimeout", tm); } + void setReadTimeout(PISystemTime tm); //! Set timeout for write - void setWriteTimeout(PISystemTime tm) { setProperty("writeTimeout", tm); } + void setWriteTimeout(PISystemTime tm); + + + //! Set socket receive buffer size + void setReadBufferSize(int bytes); + + //! Set socket send buffer size + void setWriteBufferSize(int bytes); //! Returns TTL (Time To Live) @@ -477,11 +484,12 @@ protected: bool closeDevice() override; void closeSocket(int & sd); void applyTimeouts(); + void applyBuffers(); void applyTimeout(int fd, int opt, PISystemTime tm); void applyOptInt(int level, int opt, int val); PRIVATE_DECLARATION(PIP_EXPORT) - int sock = -1, sock_s = -1; + int sock = -1, sock_s = -1, rcv_buf = 0, snd_buf = 0; std::atomic_bool connected_ = {false}, connecting_ = {false}, listen_threaded = {false}, server_bounded = {false}; bool is_server_client = false; mutable PINetworkAddress addr_r, addr_s, addr_lr; diff --git a/libs/main/types/pibytearray.h b/libs/main/types/pibytearray.h index e415eaf2..e8637e72 100644 --- a/libs/main/types/pibytearray.h +++ b/libs/main/types/pibytearray.h @@ -492,6 +492,11 @@ public: //! По умолчанию указывает на начало массива. inline const uchar * data(size_t index = 0) const { return d.data(index); } + template + inline T dataAs(size_t index = 0) { + return *(T *)d.data(index); + } + //! \~english Clear array, remove all elements. //! \~russian Очищает массив, удаляет все элементы. //! \~\details