You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by rb...@apache.org on 2020/10/06 19:04:44 UTC
[celix] branch
feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
created (now c6c7131)
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a change to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git.
at c6c7131 Add
This branch includes the following new commits:
new 63fe56e Add Message segmentation
new 3eb8877 Update for non blocking
new bec2254 Fix unit test
new 78ed431 Add maxmsg
new 5eac987 Merge master
new 050c396 Add interceptor
new dbf6bdc Update handler
new a35435e Merge branch 'master' into feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
new 7579473 Fix buffers
new 4cfc6ec Fix
new c6c7131 Add
The 11 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[celix] 11/11: Add
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit c6c7131cbf6a4629ff5d8fb86a750a9659de28ed
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Wed Aug 12 21:59:22 2020 +0200
Add
---
CMakeLists.txt | 2 +-
.../publisher/private/src/pubsub_publisher.c | 24 +-
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 469 +++++++++++++++------
.../pubsub_admin_tcp/src/pubsub_tcp_handler.h | 1 +
.../pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c | 1 +
.../pubsub_admin_zmq/src/pubsub_zmq_topic_sender.c | 2 +
.../src/pubsub_wire_protocol_common.c | 6 +-
.../src/pubsub_wire_v2_protocol_impl.c | 6 +-
.../pubsub/pubsub_spi/include/pubsub_protocol.h | 4 +-
9 files changed, 364 insertions(+), 151 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9ede833..788200f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -47,7 +47,7 @@ set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}")
# Set C++ specific flags
set(CMAKE_CXX_FLAGS "-std=c++11 -fno-rtti ${CMAKE_CXX_FLAGS}")
-set(CMAKE_CXX_FLAGS "-Wall -Werror -Wextra -Weffc++ ${CMAKE_CXX_FLAGS}")
+#set(CMAKE_CXX_FLAGS "-Wall -Werror -Wextra -Weffc++ ${CMAKE_CXX_FLAGS}")
if(APPLE)
set(CMAKE_MACOSX_RPATH 1)
diff --git a/bundles/pubsub/examples/pubsub/publisher/private/src/pubsub_publisher.c b/bundles/pubsub/examples/pubsub/publisher/private/src/pubsub_publisher.c
index 03857dd..92e0e65 100644
--- a/bundles/pubsub/examples/pubsub/publisher/private/src/pubsub_publisher.c
+++ b/bundles/pubsub/examples/pubsub/publisher/private/src/pubsub_publisher.c
@@ -54,15 +54,16 @@ static void* send_thread(void* arg) {
//poi_t point = calloc(1,sizeof(*point));
location_t place = calloc(1, sizeof(*place));
- char *desc = calloc(64, sizeof(char));
- snprintf(desc, 64, "fw-%s [TID=%lu]", fwUUID, (unsigned long)pthread_self());
+ char *desc = calloc(1, sizeof(char)); //calloc(64, sizeof(char));
+ //snprintf(desc, 64, "fw-%s [TID=%lu]", fwUUID, (unsigned long)pthread_self());
- char *name = calloc(64, sizeof(char));
- snprintf(name, 64, "Bundle#%ld", publisher->bundleId);
+ char *name = calloc(1, sizeof(char));//calloc(64, sizeof(char));
+ //snprintf(name, 64, "Bundle#%ld", publisher->bundleId);
place->name = name;
place->description = desc;
- place->extra = "extra value";
+ //place->extra = "extra value";
+ place->extra = calloc(1, 1);
printf("TOPIC : %s\n", st_struct->topic);
unsigned int msgId = 0;
@@ -77,18 +78,21 @@ static void* send_thread(void* arg) {
if (msgId > 0) {
place->position.lat = randCoordinate(MIN_LAT, MAX_LAT);
place->position.lon = randCoordinate(MIN_LON, MAX_LON);
- int nr_char = (int) randCoordinate(5, 100000);
+ //int nr_char = (int) randCoordinate(5, 100000);
+ int nr_char = 1;//(int) randCoordinate(5, 20);
place->data = calloc(nr_char, 1);
for (int i = 0; i < (nr_char - 1); i++) {
place->data[i] = i % 10 + '0';
}
place->data[nr_char - 1] = '\0';
if (publish_svc->send) {
- celix_properties_t *metadata = celix_properties_create();
- celix_properties_set(metadata, "Key", "Value");
+ celix_properties_t *metadata = NULL;
+ //celix_properties_t *metadata = celix_properties_create();
+ //celix_properties_set(metadata, "Key", "Value");
+
if (publish_svc->send(publish_svc->handle, msgId, place, metadata) == 0) {
- printf("Sent %s [%f, %f] (%s, %s) data len = %d\n", st_struct->topic,
- place->position.lat, place->position.lon, place->name, place->description, nr_char);
+ // printf("Sent %s [%f, %f] (%s, %s) data len = %d\n", st_struct->topic,
+ // place->position.lat, place->position.lon, place->name, place->description, nr_char);
}
} else {
printf("No send for %s\n", st_struct->topic);
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 411e9d2..2749794 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -48,6 +48,7 @@
#define MAX_EVENTS 64
#define MAX_DEFAULT_BUFFER_SIZE 4u
+#define PADDING_BUFFER_SIZE 4u
#define READ_STATE_INIT 0u
#define READ_STATE_HEADER 1u
@@ -86,17 +87,24 @@ typedef struct psa_tcp_connection_entry {
unsigned int maxMsgSize;
unsigned int syncSize;
unsigned int headerSize;
- unsigned int headerBufferSize; // Size of headerBuffer, size = 0, no headerBuffer -> included in payload
- void *headerBuffer;
- unsigned int footerSize;
- void *footerBuffer;
+ unsigned int readHeaderBufferSize; // Size of headerBuffer, size = 0, no headerBuffer -> included in payload
+ void *readHeaderBuffer;
+ unsigned int writeHeaderBufferSize; // Size of headerBuffer, size = 0, no headerBuffer -> included in payload
+ void *writeHeaderBuffer;
+ unsigned int readFooterSize;
+ void *readFooterBuffer;
+ unsigned int writeFooterSize;
+ void *writeFooterBuffer;
unsigned int bufferSize;
void *buffer;
unsigned int bufferReadReadOffset;
unsigned int expectedBufferReadSize;
unsigned int msgSizeReadSize;
- unsigned int metaBufferSize;
- void *metaBuffer;
+ size_t readMetaBufferSize;
+ void *readMetaBuffer;
+ size_t writeMetaBufferSize;
+ void *writeMetaBuffer;
+ void *writePaddingBuffer;
unsigned int retryCount;
} psa_tcp_connection_entry_t;
@@ -130,6 +138,7 @@ struct pubsub_tcpHandler {
double rcvTimeout;
celix_thread_t thread;
bool running;
+ bool isEndPoint;
};
static inline int pubsub_tcpHandler_closeConnectionEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, bool lock);
@@ -324,19 +333,22 @@ pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, ch
handle->protocol->getHeaderSize(handle->protocol->handle, &size);
entry->headerSize = size;
handle->protocol->getHeaderBufferSize(handle->protocol->handle, &size);
- entry->headerBufferSize = size;
+ entry->readHeaderBufferSize = size;
+ entry->writeHeaderBufferSize = size;
handle->protocol->getSyncHeaderSize(handle->protocol->handle, &size);
entry->syncSize = size;
entry->maxMsgSize = (handle->maxMsgSize) ? handle->maxMsgSize : UINT32_MAX;
handle->protocol->getFooterSize(handle->protocol->handle, &size);
- entry->footerSize = size;
+ entry->readFooterSize = size;
+ entry->writeFooterSize = size;
entry->bufferSize = handle->bufferSize;
entry->connected = false;
- if (entry->headerBufferSize) {
- entry->headerBuffer = calloc(sizeof(char), entry->headerSize);
- }
- if (entry->footerSize) entry->footerBuffer = calloc(sizeof(char), entry->footerSize);
+ if (entry->readHeaderBufferSize) entry->readHeaderBuffer = calloc(sizeof(char), entry->readHeaderBufferSize);
+ if (entry->writeHeaderBufferSize) entry->writeHeaderBuffer = calloc(sizeof(char), entry->writeHeaderBufferSize);
+ if (entry->readFooterSize) entry->readFooterBuffer = calloc(sizeof(char), entry->readFooterSize);
+ if (entry->writeFooterSize) entry->writeFooterBuffer = calloc(sizeof(char), entry->writeFooterSize);
if (entry->bufferSize) entry->buffer = calloc(sizeof(char), entry->bufferSize);
+ entry->writePaddingBuffer = calloc(sizeof(char),PADDING_BUFFER_SIZE);
pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
}
return entry;
@@ -348,40 +360,17 @@ pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, ch
static inline void
pubsub_tcpHandler_freeEntry(psa_tcp_connection_entry_t *entry) {
if (entry) {
- if (entry->url) {
- free(entry->url);
- entry->url = NULL;
- }
- if (entry->interface_url) {
- free(entry->interface_url);
- entry->interface_url = NULL;
- }
- if (entry->fd >= 0) {
- close(entry->fd);
- entry->fd = -1;
- }
- if (entry->buffer) {
- free(entry->buffer);
- entry->buffer = NULL;
- entry->bufferSize = 0;
- }
- if (entry->headerBuffer) {
- free(entry->headerBuffer);
- entry->headerBuffer = NULL;
- entry->headerBufferSize = 0;
- }
-
- if (entry->footerBuffer) {
- free(entry->footerBuffer);
- entry->footerBuffer = NULL;
- }
-
- if (entry->metaBuffer) {
- free(entry->metaBuffer);
- entry->metaBuffer = NULL;
- entry->metaBufferSize = 0;
- }
- entry->connected = false;
+ if (entry->url) free(entry->url);
+ if (entry->interface_url) free(entry->interface_url);
+ if (entry->fd >= 0) close(entry->fd);
+ if (entry->buffer) free(entry->buffer);
+ if (entry->readHeaderBuffer) free(entry->readHeaderBuffer);
+ if (entry->writeHeaderBuffer) free(entry->writeHeaderBuffer);
+ if (entry->readFooterBuffer) free(entry->readFooterBuffer);
+ if (entry->writeFooterBuffer) free(entry->writeFooterBuffer);
+ if (entry->readMetaBuffer) free(entry->readMetaBuffer);
+ if (entry->writeMetaBuffer) free(entry->writeMetaBuffer);
+ if (entry->writePaddingBuffer) free(entry->writePaddingBuffer);
free(entry);
}
}
@@ -403,8 +392,7 @@ pubsub_tcpHandler_releaseEntryBuffer(pubsub_tcpHandler_t *handle, int fd, unsign
//
int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
int rc = 0;
- psa_tcp_connection_entry_t *entry =
- hashMap_get(handle->connection_url_map, (void *) (intptr_t) url);
+ psa_tcp_connection_entry_t *entry = hashMap_get(handle->connection_url_map, (void *) (intptr_t) url);
if (entry == NULL) {
pubsub_utils_url_t *url_info = pubsub_utils_url_parse(url);
int fd = pubsub_tcpHandler_open(handle, url_info->interface_url);
@@ -418,7 +406,7 @@ int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
if ((rc >= 0) && addr) {
rc = connect(fd, (struct sockaddr *) addr, sizeof(struct sockaddr));
if (rc < 0 && errno != EINPROGRESS) {
- L_ERROR("[TCP Socket] Cannot connect to %s:%d: using; %s err: %s\n", url_info->hostname, url_info->port_nr, interface_url,
+ L_ERROR("[TCP Socket] Cannot connect to %s:%d: using; %s err(%d): %s\n", url_info->hostname, url_info->port_nr, interface_url, errno,
strerror(errno));
close(fd);
} else {
@@ -445,7 +433,7 @@ int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
L_ERROR("[TCP Socket] Cannot create poll event %s\n", strerror(errno));
entry = NULL;
}
- rc = pubsub_tcpHandler_makeNonBlocking(handle, entry->fd);
+ //rc = pubsub_tcpHandler_makeNonBlocking(handle, entry->fd);
if (rc < 0) {
pubsub_tcpHandler_freeEntry(entry);
L_ERROR("[TCP Socket] Cannot make not blocking %s\n", strerror(errno));
@@ -652,7 +640,7 @@ int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int si
if (handle != NULL) {
celixThreadRwlock_writeLock(&handle->dbLock);
handle->maxMsgSize = size;
- handle->maxMsgSize = 4;
+ handle->maxMsgSize = 16;
celixThreadRwlock_unlock(&handle->dbLock);
}
return 0;
@@ -767,6 +755,14 @@ void pubsub_tcpHandler_setReceiveTimeOut(pubsub_tcpHandler_t *handle, double tim
}
}
+void pubsub_tcpHandler_setEndPoint(pubsub_tcpHandler_t *handle,bool isEndPoint) {
+ if (handle != NULL) {
+ celixThreadRwlock_writeLock(&handle->dbLock);
+ handle->isEndPoint = isEndPoint;
+ celixThreadRwlock_unlock(&handle->dbLock);
+ }
+}
+
static inline
int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, int flag ) {
int long nbytes = entry->expectedBufferReadSize;
@@ -775,8 +771,10 @@ int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection
// Read the message header
nbytes = recv(fd, &buffer[entry->bufferReadReadOffset], entry->expectedBufferReadSize, flag | MSG_NOSIGNAL);
// Update buffer administration
- entry->bufferReadReadOffset += nbytes;
- entry->expectedBufferReadSize-= nbytes;
+ if (nbytes > 0) {
+ entry->bufferReadReadOffset += nbytes;
+ entry->expectedBufferReadSize -= nbytes;
+ }
}
return nbytes;
}
@@ -789,9 +787,8 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
handle->protocol->decodePayload(handle->protocol->handle, entry->buffer, entry->header.header.payloadSize, &entry->header);
}
if (entry->header.header.metadataSize > 0) {
- handle->protocol->decodeMetadata(handle->protocol->handle, entry->metaBuffer,
- entry->header.header.metadataSize, &entry->header);
- entry->metaBufferSize = entry->header.header.metadataSize;
+ handle->protocol->decodeMetadata(handle->protocol->handle, entry->readMetaBuffer,
+ entry->header.header.metadataSize, &entry->header);
}
if (handle->processMessageCallback && entry->header.payload.payload != NULL && entry->header.payload.length) {
struct timespec receiveTime;
@@ -809,12 +806,12 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
entry->expectedBufferReadSize = entry->headerSize;
entry->state = READ_STATE_HEADER;
} else if (entry->state == READ_STATE_HEADER) {
- if (entry->header.header.payloadSize) {
+ if (entry->header.header.payloadSize && entry->header.header.payloadPartSize) {
entry->state = READ_STATE_PAYLOAD;
entry->bufferReadReadOffset = entry->header.header.payloadOffset;
- entry->expectedBufferReadSize = (entry->header.header.payloadPartSize) ? entry->header.header.payloadPartSize : entry->header.header.payloadSize;
+ entry->expectedBufferReadSize = entry->header.header.payloadPartSize;
// For header less messages adjust offset and msg size;
- if (!entry->headerBufferSize) {
+ if (!entry->readHeaderBufferSize) {
entry->bufferReadReadOffset += entry->headerSize;
entry->expectedBufferReadSize -= entry->headerSize;
}
@@ -822,10 +819,10 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
entry->state = READ_STATE_META;
entry->expectedBufferReadSize = entry->header.header.metadataSize;
} else if (!entry->header.header.payloadSize && !entry->header.header.metadataSize) {
- if (entry->footerSize) {
+ if (entry->readFooterSize) {
entry->state = READ_STATE_FOOTER;
- entry->expectedBufferReadSize = entry->footerSize;
- } else if (entry->header.header.isLastSegment) {
+ entry->expectedBufferReadSize = entry->readFooterSize;
+ } else if (entry->header.header.isLastSegment == 0x01) {
entry->state = READ_STATE_READY;
entry->expectedBufferReadSize = 0;
} else {
@@ -838,10 +835,10 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
entry->state = READ_STATE_META;
entry->expectedBufferReadSize = entry->header.header.metadataSize;
} else {
- if (entry->footerSize) {
+ if (entry->readFooterSize) {
entry->state = READ_STATE_FOOTER;
- entry->expectedBufferReadSize = entry->footerSize;
- } else if (entry->header.header.isLastSegment) {
+ entry->expectedBufferReadSize = entry->readFooterSize;
+ } else if (entry->header.header.isLastSegment == 0x01) {
entry->state = READ_STATE_READY;
entry->expectedBufferReadSize = 0;
} else {
@@ -850,10 +847,10 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
}
}
} else if (entry->state == READ_STATE_META) {
- if (entry->footerSize) {
+ if (entry->readFooterSize) {
entry->state = READ_STATE_FOOTER;
- entry->expectedBufferReadSize = entry->footerSize;
- } else if (entry->header.header.isLastSegment) {
+ entry->expectedBufferReadSize = entry->readFooterSize;
+ } else if (entry->header.header.isLastSegment == 0x01) {
entry->state = READ_STATE_READY;
entry->expectedBufferReadSize = 0;
} else {
@@ -861,7 +858,7 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
entry->expectedBufferReadSize = entry->headerSize;
}
} else if (entry->state == READ_STATE_FOOTER) {
- if (entry->header.header.isLastSegment) {
+ if (entry->header.header.isLastSegment == 0x01) {
entry->state = READ_STATE_READY;
} else {
entry->state = READ_STATE_HEADER;
@@ -890,6 +887,170 @@ static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHand
// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
// If the message is completely reassembled true is returned and the index and size have valid values
//
+int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
+ celixThreadRwlock_writeLock(&handle->dbLock);
+ psa_tcp_connection_entry_t *entry = hashMap_get(handle->interface_fd_map, (void *) (intptr_t) fd);
+ if (entry == NULL)
+ entry = hashMap_get(handle->connection_fd_map, (void *) (intptr_t) fd);
+ // Find FD entry
+ if (entry == NULL) {
+ celixThreadRwlock_unlock(&handle->dbLock);
+ return -1;
+ }
+ // If it's not connected return from function
+ if (!entry->connected) {
+ celixThreadRwlock_unlock(&handle->dbLock);
+ return -1;
+ }
+
+ if (entry->readHeaderBufferSize && entry->readHeaderBuffer) entry->readHeaderBuffer = malloc(entry->readHeaderBufferSize);
+
+ // Message buffer is to small, reallocate to make it bigger
+ if ((!entry->readHeaderBufferSize) && (entry->headerSize > entry->bufferSize)) {
+ handle->bufferSize = MAX(handle->bufferSize, entry->headerSize);
+ char *buffer = realloc(entry->buffer, (size_t) handle->bufferSize);
+ if (buffer) {
+ entry->buffer = buffer;
+ entry->bufferSize = handle->bufferSize;
+ }
+ }
+ // Read the message
+ // Read the message
+ entry->msg.msg_iovlen = 0;
+ entry->msg.msg_iov[entry->msg.msg_iovlen].iov_base = (entry->headerBufferSize) ? entry->headerBuffer
+ : entry->buffer;
+ entry->msg.msg_iov[entry->msg.msg_iovlen].iov_len = entry->headerSize;
+ entry->msg.msg_iovlen++;
+ int nbytes = recvmsg(fd, &entry->msg, MSG_PEEK | MSG_NOSIGNAL);
+ long int nbytes = UINT32_MAX;
+ char *header_buffer = (entry->readHeaderBufferSize) ? entry->readHeaderBuffer : entry->buffer;
+ if (entry->state == READ_STATE_SYNC) {
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
+ nbytes = recv(fd, header_buffer, entry->expectedBufferReadSize, flag | MSG_NOSIGNAL);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
+ if (entry->state == READ_STATE_HEADER) {
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, MSG_PEEK);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) { // Check header message buffer
+ if (handle->protocol->decodeHeader(handle->protocol->handle,
+ header_buffer,
+ entry->headerSize,
+ &entry->header) != CELIX_SUCCESS) {
+ // Did not receive correct header
+ // skip sync word and try to read next header
+ if (!entry->headerError) {
+ L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
+ }
+ entry->headerError = true;
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_SYNC);
+ } else {
+ // Read header message from queue
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
+ if ((nbytes > 0) && (nbytes == entry->headerSize)) {
+ entry->headerError = false;
+ entry->msgSizeReadSize = 0;
+ // For headerless message, add header to bufferReadSize;
+ if (!entry->readHeaderBufferSize) entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
+ }
+ }
+
+ if (entry->state == READ_STATE_PAYLOAD) {
+ // Alloc message buffers
+ if (entry->header.header.payloadSize > entry->bufferSize) {
+ handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize + PADDING_BUFFER_SIZE);
+ if (entry->buffer) {
+ free(entry->buffer);
+ }
+ entry->buffer = malloc((size_t) handle->bufferSize);
+ entry->bufferSize = handle->bufferSize;
+ }
+
+ //if (entry->header.header.isLastSegment) entry->expectedBufferReadSize+=4;
+ // Read payload data from queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, 0);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
+ entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
+
+ if (entry->state == READ_STATE_META) {
+ if (entry->header.header.metadataSize > entry->readMetaBufferSize) {
+ if (entry->readMetaBuffer) {
+ free(entry->readMetaBuffer);
+ L_WARN("[TCP Socket] socket: %d, url: %s, realloc read meta buffer: (%d, %d) \n", entry->fd,
+ entry->url, entry->readMetaBufferSize, entry->header.header.metadataSize);
+ }
+ entry->readMetaBufferSize = entry->header.header.metadataSize + PADDING_BUFFER_SIZE;
+ entry->readMetaBuffer = malloc((size_t) entry->readMetaBufferSize);
+ }
+
+ // Read meta data from (queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->readMetaBuffer,0);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
+ entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
+ if (entry->state == READ_STATE_FOOTER) {
+ // Check for end of message using, footer of message. Because of streaming protocol
+ if (!entry->readFooterBuffer) entry->readFooterBuffer = malloc(entry->readFooterSize);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->readFooterBuffer, 0);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
+ if (handle->protocol->decodeFooter(handle->protocol->handle,
+ entry->readFooterBuffer,
+ entry->readFooterSize,
+ &entry->header) == CELIX_SUCCESS) {
+ // valid footer, this means that the message is valid
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ } else {
+ // Did not receive correct footer
+ L_ERROR(
+ "[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)",
+ entry->header.header.seqNr,
+ entry->fd,
+ entry->url);
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ }
+ }
+ }
+ if (entry->state == READ_STATE_READY) {
+ // Complete message is received
+ pubsub_tcpHandler_decodePayload(handle, entry);
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+
+ if (nbytes > 0) {
+ entry->retryCount = 0;
+ } else if (nbytes < 0) {
+ if ((errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) {
+ // Non blocking interrupt
+ entry->retryCount = 0;
+ } else if (entry->retryCount < handle->maxRcvRetryCount) {
+ entry->retryCount++;
+ L_WARN(
+ "[TCP Socket] Failed to receive message (fd: %d), try again. error(%d): %s, Retry count %u of %u.",
+ entry->fd, errno, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
+ } else {
+ L_ERROR("[TCP Socket] Failed to receive message (fd: %d) after %u retries! Closing connection... Error: %s",
+ entry->fd, handle->maxRcvRetryCount, strerror(errno));
+ nbytes = 0; //Return 0 as indicator to close the connection
+ }
+ }
+ celixThreadRwlock_unlock(&handle->dbLock);
+ return (int)nbytes;
+}
+
+//
+// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
+// If the message is completely reassembled true is returned and the index and size have valid values
+//
int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
celixThreadRwlock_writeLock(&handle->dbLock);
psa_tcp_connection_entry_t *entry = hashMap_get(handle->interface_fd_map, (void *) (intptr_t) fd);
@@ -906,8 +1067,10 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
return -1;
}
+ if (entry->readHeaderBufferSize && entry->readHeaderBuffer) entry->readHeaderBuffer = malloc(entry->readHeaderBufferSize);
+
// Message buffer is to small, reallocate to make it bigger
- if ((!entry->headerBufferSize) && (entry->headerSize > entry->bufferSize)) {
+ if ((!entry->readHeaderBufferSize) && (entry->headerSize > entry->bufferSize)) {
handle->bufferSize = MAX(handle->bufferSize, entry->headerSize);
if (entry->buffer)
free(entry->buffer);
@@ -916,16 +1079,16 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
}
// Read the message
long int nbytes = UINT32_MAX;
- char *header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
+ char *header_buffer = (entry->readHeaderBufferSize) ? entry->readHeaderBuffer : entry->buffer;
if (entry->state == READ_STATE_SYNC) {
nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
- if (nbytes > 0) {
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
if (entry->state == READ_STATE_HEADER) {
nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, MSG_PEEK);
- if (nbytes >= entry->headerSize) { // Check header message buffer
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) { // Check header message buffer
if (handle->protocol->decodeHeader(handle->protocol->handle,
header_buffer,
entry->headerSize,
@@ -945,8 +1108,7 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
entry->headerError = false;
entry->msgSizeReadSize = 0;
// For headerless message, add header to bufferReadSize;
- if (!entry->headerBufferSize)
- entry->msgSizeReadSize += nbytes;
+ if (!entry->readHeaderBufferSize) entry->msgSizeReadSize += nbytes;
pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
@@ -956,51 +1118,61 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
if (entry->state == READ_STATE_PAYLOAD) {
// Alloc message buffers
if (entry->header.header.payloadSize > entry->bufferSize) {
- handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize);
- if (entry->buffer)
+ handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize + PADDING_BUFFER_SIZE);
+ if (entry->buffer) {
free(entry->buffer);
+ }
entry->buffer = malloc((size_t) handle->bufferSize);
entry->bufferSize = handle->bufferSize;
}
+ //if (entry->header.header.isLastSegment) entry->expectedBufferReadSize+=4;
// Read payload data from queue
nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, 0);
- if (nbytes > 0) {
- if (nbytes >= entry->header.header.payloadPartSize) {
- entry->msgSizeReadSize += nbytes;
- pubsub_tcpHandler_setReadStateMachine(handle, entry);
- }
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
+ entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
if (entry->state == READ_STATE_META) {
- if (entry->header.header.metadataSize > entry->metaBufferSize) {
- if (entry->metaBuffer) {
- free(entry->metaBuffer);
- entry->metaBuffer = malloc((size_t) entry->header.header.metadataSize);
- entry->metaBufferSize = entry->header.header.metadataSize;
+ if (entry->header.header.metadataSize > entry->readMetaBufferSize) {
+ if (entry->readMetaBuffer) {
+ free(entry->readMetaBuffer);
L_WARN("[TCP Socket] socket: %d, url: %s, realloc read meta buffer: (%d, %d) \n", entry->fd,
- entry->url, entry->metaBufferSize, entry->header.header.metadataSize);
+ entry->url, entry->readMetaBufferSize, entry->header.header.metadataSize);
}
+ entry->readMetaBufferSize = entry->header.header.metadataSize + PADDING_BUFFER_SIZE;
+ entry->readMetaBuffer = malloc((size_t) entry->readMetaBufferSize);
}
// Read meta data from (queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0);
- if ((nbytes > 0) && (nbytes >= entry->header.header.metadataSize)) {
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->readMetaBuffer,0);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
entry->msgSizeReadSize += nbytes;
pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
if (entry->state == READ_STATE_FOOTER) {
// Check for end of message using, footer of message. Because of streaming protocol
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer, 0);
- if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
- // valid footer, this means that the message is valid
- pubsub_tcpHandler_setReadStateMachine(handle, entry);
- } else {
- // Did not receive correct header
- L_ERROR("[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)", entry->header.header.seqNr, entry->fd, entry->url);
- pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ if (!entry->readFooterBuffer) entry->readFooterBuffer = malloc(entry->readFooterSize);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->readFooterBuffer, 0);
+ if (nbytes && (entry->expectedBufferReadSize <= 0)) {
+ if (handle->protocol->decodeFooter(handle->protocol->handle,
+ entry->readFooterBuffer,
+ entry->readFooterSize,
+ &entry->header) == CELIX_SUCCESS) {
+ // valid footer, this means that the message is valid
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ } else {
+ // Did not receive correct footer
+ L_ERROR(
+ "[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)",
+ entry->header.header.seqNr,
+ entry->fd,
+ entry->url);
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ }
}
}
if (entry->state == READ_STATE_READY) {
@@ -1114,7 +1286,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
int nofConnToClose = 0;
if (handle) {
hash_map_iterator_t iter = hashMapIterator_construct(handle->connection_fd_map);
- size_t max_msg_iov_len = IOV_MAX - 2;
+ size_t max_msg_iov_len = IOV_MAX - 3; // header , footer, padding
while (hashMapIterator_hasNext(&iter)) {
psa_tcp_connection_entry_t *entry = hashMapIterator_nextValue(&iter);
if (!entry->connected) continue;
@@ -1136,11 +1308,13 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
void *metadataData = NULL;
size_t metadataSize = 0;
if (message->metadata.metadata) {
- metadataData = entry->metaBuffer;
+ metadataSize = entry->writeMetaBufferSize;
+ metadataData = entry->writeMetaBuffer;
handle->protocol->encodeMetadata(handle->protocol->handle, message,
&metadataData,
&metadataSize);
- entry->metaBufferSize = metadataSize;
+ entry->writeMetaBufferSize = MAX(metadataSize, entry->writeMetaBufferSize);
+ if (metadataData && entry->writeMetaBuffer != metadataData) entry->writeMetaBuffer = metadataData;
}
message->header.metadataSize = metadataSize;
size_t totalMessageSize = payloadSize + metadataSize;
@@ -1156,15 +1330,19 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
void *footerData = NULL;
size_t footerDataSize = 0;
- if (entry->footerSize) {
- footerData = entry->footerBuffer;
+ if (entry->writeFooterSize) {
+ footerDataSize = entry->writeFooterSize;
+ footerData = entry->writeFooterBuffer;
handle->protocol->encodeFooter(handle->protocol->handle, message,
&footerData,
&footerDataSize);
- entry->footerSize = footerDataSize;
+ entry->writeFooterSize = MAX(footerDataSize, entry->writeFooterSize);
+ if (footerData && entry->writeFooterBuffer != footerData) entry->writeFooterBuffer = footerData;
}
size_t msgSize = 0;
+ size_t msgPayloadSize = 0;
+ size_t msgMetaDataSize = 0;
size_t msgIovLen = 0;
long int nbytes = UINT32_MAX;
while (msgSize < totalMessageSize && nbytes > 0) {
@@ -1176,22 +1354,26 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
msg.msg_flags = flags;
msg.msg_iov = msg_iov;
size_t msgPartSize = 0;
+ size_t msgMetaDataPartSize = 0;
message->header.payloadPartSize = 0;
message->header.payloadOffset = 0;
message->header.metadataSize = 0;
+ message->header.metadataPartSize = 0;
+ message->header.metadataOffset = 0;
message->header.isLastSegment = 0;
// Write generic seralized payload in vector buffer
- if (msgSize < payloadSize) {
+ if (msgPayloadSize < payloadSize) {
if (payloadSize && payloadData) {
- char *payloadDataBuffer = payloadData;
+ char *buffer = payloadData;
msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = &payloadDataBuffer[msgSize];
- msg.msg_iov[msg.msg_iovlen].iov_len = MIN((payloadSize - msgSize), entry->maxMsgSize);
+ msg.msg_iov[msg.msg_iovlen].iov_base = &buffer[msgPayloadSize];
+ msg.msg_iov[msg.msg_iovlen].iov_len = MIN((payloadSize - msgPayloadSize), entry->maxMsgSize);
msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
message->header.payloadPartSize = msgPartSize;
- message->header.payloadOffset = msgSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ message->header.payloadOffset = msgPayloadSize;
+ msgPayloadSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ msgSize = msgPayloadSize;
} else {
// copy serialized vector into vector buffer
for (size_t i = 0; i < MIN(msg_iov_len, max_msg_iov_len); i++) {
@@ -1202,23 +1384,42 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
break;
msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
}
+ message->header.payloadOffset = msgPayloadSize;
message->header.payloadPartSize = msgPartSize;
- message->header.payloadOffset = msgSize;
- msgSize += msgPartSize;
+ msgPayloadSize += message->header.payloadPartSize;
+ msgSize = msgPayloadSize;
msgIovLen += (msg.msg_iovlen - 1);
}
+ // iov structures are aligned to 4 bytes.
+ // Add padding to read correct footer.
+ //unsigned int padding = msgPartSize & ( 0x4-1 );
+ //if (padding) {
+ // msg.msg_iovlen++;
+ // msg.msg_iov[msg.msg_iovlen].iov_len = 0x4 - padding;
+ // msg.msg_iov[msg.msg_iovlen].iov_base = entry->writePaddingBuffer;
+ // msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ // message->header.payloadPartSize= msgPartSize;
+ // }
}
// Write optional metadata in vector buffer
- if ((msgSize >= payloadSize) &&
+ if ((msgPayloadSize >= payloadSize) &&
+ (msgMetaDataSize < metadataSize) &&
(msgPartSize < entry->maxMsgSize) &&
- (metadataSize && metadataData)) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = metadataData;
- msg.msg_iov[msg.msg_iovlen].iov_len = metadataSize;
- msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- message->header.metadataSize = metadataSize;
- msgSize += metadataSize;
+ (msg.msg_iovlen < max_msg_iov_len)) {
+ if (metadataSize && metadataData) {
+ char *buffer = payloadData;
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = &buffer[msgMetaDataSize];
+ msg.msg_iov[msg.msg_iovlen].iov_len = MIN((metadataSize - msgMetaDataSize), entry->maxMsgSize);
+ msgMetaDataPartSize = msg.msg_iov[msg.msg_iovlen].iov_len;
+ msgPartSize += msgMetaDataPartSize;
+ message->header.metadataSize = metadataSize;
+ message->header.metadataPartSize = msgMetaDataPartSize;
+ message->header.metadataOffset = msgMetaDataSize;
+ msgMetaDataSize += msgMetaDataPartSize;
+ msgSize += msgMetaDataPartSize;
+ }
}
if (msgSize >= totalMessageSize) {
message->header.isLastSegment = 0x1;
@@ -1233,17 +1434,19 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
}
void *headerData = NULL;
- size_t headerSize = 0;
+ size_t headerSize = entry->writeHeaderBufferSize;
// check if header is not part of the payload (=> headerBufferSize = 0)s
- if (entry->headerBufferSize) {
- headerData = entry->headerBuffer;
+ if (entry->writeHeaderBufferSize) {
+ headerSize = entry->writeHeaderBufferSize;
+ headerData = entry->writeHeaderBuffer;
// Encode the header, with payload size and metadata size
handle->protocol->encodeHeader(handle->protocol->handle, message,
&headerData,
&headerSize);
- entry->headerBufferSize = headerSize;
+ entry->writeHeaderBufferSize = MAX(headerSize, entry->writeHeaderBufferSize);
+ if (headerData && entry->writeHeaderBuffer != headerData) entry->writeHeaderBuffer = headerData;
}
- if (!entry->headerBufferSize) {
+ if (!entry->writeHeaderBufferSize) {
// Skip header buffer, when header is part of payload;
msg.msg_iov = &msg_iov[1];
} else if (headerSize && headerData) {
@@ -1256,7 +1459,8 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
L_ERROR("[TCP Socket] No header buffer is generated");
msg.msg_iovlen = 0;
}
- nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgPartSize, flags);
+ //nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgPartSize, flags);
+ nbytes = sendmsg(entry->fd, &msg, flags | MSG_NOSIGNAL);
// When a specific socket keeps reporting errors can indicate a subscriber
// which is not active anymore, the connection will remain until the retry
// counter exceeds the maximum retry count.
@@ -1279,20 +1483,10 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
}
}
- // Release data
- if (headerData && headerData != entry->headerBuffer) {
- free(headerData);
- }
// Note: serialized Payload is deleted by serializer
if (payloadData && (payloadData != message->payload.payload)) {
free(payloadData);
}
- if (metadataData && metadataData != entry->metaBuffer) {
- free(metadataData);
- }
- if (footerData && footerData != entry->footerBuffer) {
- free(footerData);
- }
}
}
}
@@ -1357,7 +1551,8 @@ int pubsub_tcpHandler_acceptHandler(pubsub_tcpHandler_t *handle, psa_tcp_connect
#else
struct epoll_event event;
bzero(&event, sizeof(event)); // zero the struct
- event.events = EPOLLIN | EPOLLRDHUP | EPOLLERR;
+ event.events = EPOLLRDHUP | EPOLLERR;
+ if (handle->isEndPoint) event.events |= EPOLLIN;
event.data.fd = entry->fd;
// Register Read to epoll
rc = epoll_ctl(handle->efd, EPOLL_CTL_ADD, entry->fd, &event);
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
index bb74387..a08911c 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
@@ -65,6 +65,7 @@ void pubsub_tcpHandler_setSendRetryCnt(pubsub_tcpHandler_t *handle, unsigned int
void pubsub_tcpHandler_setReceiveRetryCnt(pubsub_tcpHandler_t *handle, unsigned int count);
void pubsub_tcpHandler_setSendTimeOut(pubsub_tcpHandler_t *handle, double timeout);
void pubsub_tcpHandler_setReceiveTimeOut(pubsub_tcpHandler_t *handle, double timeout);
+void pubsub_tcpHandler_setEndPoint(pubsub_tcpHandler_t *handle, bool isEndPoint);
int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd);
int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle,
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
index 7ae65d1..218b47b 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
@@ -208,6 +208,7 @@ pubsub_tcp_topic_sender_t *pubsub_tcpTopicSender_create(
pubsub_tcpHandler_setSendRetryCnt(sender->socketHandler, (unsigned int) retryCnt);
pubsub_tcpHandler_setSendTimeOut(sender->socketHandler, timeout);
pubsub_tcpHandler_setMaxMsgSize(sender->socketHandler, (unsigned int) maxMsgSize);
+ pubsub_tcpHandler_setEndPoint(sender->socketHandler, isEndpoint);
}
//setting up tcp socket for TCP TopicSender
diff --git a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_sender.c b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_sender.c
index 1e95c0b..8c2c573 100644
--- a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_sender.c
+++ b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_topic_sender.c
@@ -601,6 +601,8 @@ static int psa_zmq_topicPublicationSend(void* handle, unsigned int msgTypeId, co
message.header.metadataSize = entry->metadataBufferSize;
message.header.payloadPartSize = payloadLength;
message.header.payloadOffset = 0;
+ message.header.metadataPartSize = entry->metadataBufferSize;
+ message.header.metadataOffset = 0;
message.header.isLastSegment = 1;
message.header.convertEndianess = 0;
diff --git a/bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/src/pubsub_wire_protocol_common.c b/bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/src/pubsub_wire_protocol_common.c
index 1d123d7..eb3755b 100644
--- a/bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/src/pubsub_wire_protocol_common.c
+++ b/bundles/pubsub/pubsub_protocol/pubsub_protocol_lib/src/pubsub_wire_protocol_common.c
@@ -192,7 +192,11 @@ celix_status_t pubsubProtocol_encodePayload(pubsub_protocol_message_t *message,
celix_status_t pubsubProtocol_encodeMetadata(pubsub_protocol_message_t *message, void **outBuffer, size_t *outLength) {
celix_status_t status = CELIX_SUCCESS;
-
+ if (outLength == NULL) {
+ status = CELIX_ILLEGAL_ARGUMENT;
+ return status;
+ }
+ *outLength = (*outLength == 0) ? 1024 : *outLength;
size_t lineMemoryLength = *outBuffer == NULL ? 1024 : *outLength;
unsigned char *line = *outBuffer == NULL ? calloc(1, lineMemoryLength) : *outBuffer;
size_t idx = 4;
diff --git a/bundles/pubsub/pubsub_protocol/pubsub_protocol_wire_v2/src/pubsub_wire_v2_protocol_impl.c b/bundles/pubsub/pubsub_protocol/pubsub_protocol_wire_v2/src/pubsub_wire_v2_protocol_impl.c
index a05e095..f87cd7c 100644
--- a/bundles/pubsub/pubsub_protocol/pubsub_protocol_wire_v2/src/pubsub_wire_v2_protocol_impl.c
+++ b/bundles/pubsub/pubsub_protocol/pubsub_protocol_wire_v2/src/pubsub_wire_v2_protocol_impl.c
@@ -46,7 +46,7 @@ celix_status_t pubsubProtocol_wire_v2_destroy(pubsub_protocol_wire_v2_t* protoco
}
celix_status_t pubsubProtocol_wire_v2_getHeaderSize(void* handle, size_t *length) {
- *length = sizeof(int) * 9 + sizeof(short) * 2; // header + sync + version = 36
+ *length = sizeof(int) * 11 + sizeof(short) * 2; // header + sync + version = 48
return CELIX_SUCCESS;
}
@@ -99,6 +99,8 @@ celix_status_t pubsubProtocol_wire_v2_encodeHeader(void *handle, pubsub_protocol
idx = pubsubProtocol_writeInt(*outBuffer, idx, convert, message->header.metadataSize);
idx = pubsubProtocol_writeInt(*outBuffer, idx, convert, message->header.payloadPartSize);
idx = pubsubProtocol_writeInt(*outBuffer, idx, convert, message->header.payloadOffset);
+ idx = pubsubProtocol_writeInt(*outBuffer, idx, convert, message->header.metadataPartSize);
+ idx = pubsubProtocol_writeInt(*outBuffer, idx, convert, message->header.metadataOffset);
idx = pubsubProtocol_writeInt(*outBuffer, idx, convert, message->header.isLastSegment);
*outLength = idx;
}
@@ -160,6 +162,8 @@ celix_status_t pubsubProtocol_wire_v2_decodeHeader(void* handle, void *data, siz
idx = pubsubProtocol_readInt(data, idx, convert, &message->header.metadataSize);
idx = pubsubProtocol_readInt(data, idx, convert, &message->header.payloadPartSize);
idx = pubsubProtocol_readInt(data, idx, convert, &message->header.payloadOffset);
+ idx = pubsubProtocol_readInt(data, idx, convert, &message->header.metadataPartSize);
+ idx = pubsubProtocol_readInt(data, idx, convert, &message->header.metadataOffset);
pubsubProtocol_readInt(data, idx, convert, &message->header.isLastSegment);
}
}
diff --git a/bundles/pubsub/pubsub_spi/include/pubsub_protocol.h b/bundles/pubsub/pubsub_spi/include/pubsub_protocol.h
index ad1a387..2c23b6f 100644
--- a/bundles/pubsub/pubsub_spi/include/pubsub_protocol.h
+++ b/bundles/pubsub/pubsub_spi/include/pubsub_protocol.h
@@ -47,11 +47,13 @@ struct pubsub_protocol_header {
* Note: this attribute is transmitted using the wire protocol, the sync word is used to determine endianess conversion */
uint32_t convertEndianess;
- /** Optional message segmentation attributes, these attributes are only used/written by the protocol admin.
+ /** pptional message segmentation attributes, these attributes are only used/written by the protocol admin.
* When message segmentation is supported by the protocol admin */
uint32_t seqNr;
uint32_t payloadPartSize;
uint32_t payloadOffset;
+ uint32_t metadataPartSize;
+ uint32_t metadataOffset;
uint32_t isLastSegment;
};
[celix] 04/11: Add maxmsg
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 78ed4319ee012645f2101e12bfafd0a2ccb65744
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Jun 29 21:28:36 2020 +0200
Add maxmsg
---
bundles/pubsub/examples/CMakeLists.txt | 1 +
bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h | 7 +++----
bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h | 1 +
bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c | 4 ++--
4 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/bundles/pubsub/examples/CMakeLists.txt b/bundles/pubsub/examples/CMakeLists.txt
index b224797..4f54917 100644
--- a/bundles/pubsub/examples/CMakeLists.txt
+++ b/bundles/pubsub/examples/CMakeLists.txt
@@ -157,6 +157,7 @@ if (BUILD_PUBSUB_PSA_TCP)
)
target_link_libraries(pubsub_subscriber2_tcp PRIVATE ${PUBSUB_CONTAINER_LIBS})
+ message("RB ${ETCD_CMD}" )
if (ETCD_CMD AND XTERM_CMD)
# Runtime starting a publish and subscriber for tcp
add_celix_runtime(pubsub_rt_tcp
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
index ff8e68f..3490f36 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
@@ -23,7 +23,7 @@
#define PSA_TCP_BASE_PORT "PSA_TCP_BASE_PORT"
#define PSA_TCP_MAX_PORT "PSA_TCP_MAX_PORT"
-#define PSA_TCP_MAX_RECV_SESSIONS "PSA_TCP_MAX_RECV_SESSIONS"
+#define PSA_TCP_MAX_MESSAGE_SIZE "PSA_TCP_MAX_MESSAGE_SIZE"
#define PSA_TCP_RECV_BUFFER_SIZE "PSA_TCP_RECV_BUFFER_SIZE"
#define PSA_TCP_TIMEOUT "PSA_TCP_TIMEOUT"
#define PSA_TCP_SUBSCRIBER_CONNECTION_TIMEOUT "PSA_TCP_SUBSCRIBER_CONNECTION_TIMEOUT"
@@ -31,8 +31,7 @@
#define PSA_TCP_DEFAULT_BASE_PORT 5501
#define PSA_TCP_DEFAULT_MAX_PORT 6000
-#define PSA_TCP_DEFAULT_MAX_RECV_SESSIONS 1
-
+#define PSA_TCP_DEFAULT_MAX_MESSAGE_SIZE 0
#define PSA_TCP_DEFAULT_RECV_BUFFER_SIZE 65 * 1024
#define PSA_TCP_DEFAULT_TIMEOUT 2000 // 2 seconds
#define PSA_TCP_SUBSCRIBER_CONNECTION_DEFAULT_TIMEOUT 250 // 250 ms
@@ -60,7 +59,7 @@
//Time-out settings are only for BLOCKING connections
#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_KEY "PUBSUB_TCP_PUBLISHER_SEND_TIMEOUT"
-#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_DEFAULT 0.0 //5.0
+#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_DEFAULT 5.0
#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_ENDPOINT_DEFAULT 0.0
#define PUBSUB_TCP_SUBSCRIBER_RCVTIMEO_KEY "PUBSUB_TCP_SUBSCRIBER_RCV_TIMEOUT"
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
index ed4581c..28df5b7 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
@@ -62,6 +62,7 @@ int pubsub_tcpHandler_listen(pubsub_tcpHandler_t *handle, char *url);
int pubsub_tcpHandler_createReceiveBufferStore(pubsub_tcpHandler_t *handle,
unsigned int maxNofBuffers,
unsigned int bufferSize);
+int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int size);
void pubsub_tcpHandler_setTimeout(pubsub_tcpHandler_t *handle, unsigned int timeout);
void pubsub_tcpHandler_setSendRetryCnt(pubsub_tcpHandler_t *handle, unsigned int count);
void pubsub_tcpHandler_setReceiveRetryCnt(pubsub_tcpHandler_t *handle, unsigned int count);
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
index 47dc888..645cdac 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
@@ -63,9 +63,7 @@ struct pubsub_tcp_topic_sender {
char *topic;
char *url;
bool isStatic;
-
bool verbose;
-
struct {
long svcId;
celix_service_factory_t factory;
@@ -196,10 +194,12 @@ pubsub_tcp_topic_sender_t *pubsub_tcpTopicSender_create(
double timeout = celix_properties_getAsDouble(topicProperties, PUBSUB_TCP_PUBLISHER_SNDTIMEO_KEY,
(!isEndpoint) ? PUBSUB_TCP_PUBLISHER_SNDTIMEO_DEFAULT :
PUBSUB_TCP_PUBLISHER_SNDTIMEO_ENDPOINT_DEFAULT);
+ long maxMsgSize = celix_properties_getAsLong(topicProperties, PSA_TCP_MAX_MESSAGE_SIZE, PSA_TCP_DEFAULT_MAX_MESSAGE_SIZE);
pubsub_tcpHandler_setThreadName(sender->socketHandler, topic, scope);
pubsub_tcpHandler_setThreadPriority(sender->socketHandler, prio, sched);
pubsub_tcpHandler_setSendRetryCnt(sender->socketHandler, (unsigned int) retryCnt);
pubsub_tcpHandler_setSendTimeOut(sender->socketHandler, timeout);
+ pubsub_tcpHandler_setMaxMsgSize(sender->socketHandler, (unsigned int) maxMsgSize);
}
//setting up tcp socket for TCP TopicSender
[celix] 07/11: Update handler
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit dbf6bdc2ebbfa70b0feaf802bc9f3195a527d333
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Aug 3 10:35:43 2020 +0200
Update handler
---
bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c | 1 +
bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c | 1 -
2 files changed, 1 insertion(+), 1 deletion(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 5099aad..20eb401 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -670,6 +670,7 @@ int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int si
if (handle != NULL) {
celixThreadRwlock_writeLock(&handle->dbLock);
handle->maxMsgSize = size;
+ handle->maxMsgSize = 4;
celixThreadRwlock_unlock(&handle->dbLock);
}
return 0;
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
index 754e769..7ae65d1 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
@@ -66,7 +66,6 @@ struct pubsub_tcp_topic_sender {
char *topic;
char *url;
bool isStatic;
- bool verbose;
struct {
long svcId;
celix_service_factory_t factory;
[celix] 06/11: Add interceptor
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 050c3962ec60fbb797bc2265656fb7bab8c85537
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Jun 29 22:15:45 2020 +0200
Add interceptor
---
.../src/pubsub_tcp_topic_receiver.c | 71 ++++++++++++++--------
.../pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c | 8 ++-
2 files changed, 51 insertions(+), 28 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c
index 71890d9..a795f92 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c
@@ -38,6 +38,7 @@
#include <uuid/uuid.h>
#include <pubsub_admin_metrics.h>
#include <pubsub_utils.h>
+#include "pubsub_interceptors_handler.h"
#include <celix_api.h>
#define MAX_EPOLL_EVENTS 16
@@ -67,6 +68,7 @@ struct pubsub_tcp_topic_receiver {
bool metricsEnabled;
pubsub_tcpHandler_t *socketHandler;
pubsub_tcpHandler_t *sharedSocketHandler;
+ pubsub_interceptors_handler_t *interceptorsHandler;
struct {
celix_thread_t thread;
@@ -164,6 +166,7 @@ pubsub_tcp_topic_receiver_t *pubsub_tcpTopicReceiver_create(celix_bundle_context
const char *staticServerEndPointUrls = NULL;
const char *staticConnectUrls = NULL;
+ pubsubInterceptorsHandler_create(ctx, scope, topic, &receiver->interceptorsHandler);
staticConnectUrls = pubsub_getEnvironmentVariableWithScopeTopic(ctx, PUBSUB_TCP_STATIC_CONNECT_URLS_FOR, topic, scope);
if (topicProperties != NULL) {
@@ -344,6 +347,7 @@ void pubsub_tcpTopicReceiver_destroy(pubsub_tcp_topic_receiver_t *receiver) {
receiver->socketHandler = NULL;
}
+ pubsubInterceptorsHandler_destroy(receiver->interceptorsHandler);
if (receiver->scope != NULL) {
free(receiver->scope);
}
@@ -553,31 +557,48 @@ processMsgForSubscriberEntry(pubsub_tcp_topic_receiver_t *receiver, psa_tcp_subs
}
if (status == CELIX_SUCCESS) {
- hash_map_iterator_t iter = hashMapIterator_construct(entry->subscriberServices);
+ const char *msgType = msgSer->msgName;
+ uint32_t msgId = message->header.msgId;
+ celix_properties_t *metadata = message->metadata.metadata;
+ bool cont = pubsubInterceptorHandler_invokePreReceive(receiver->interceptorsHandler, msgType, msgId, deSerializedMsg, &metadata);
bool release = true;
- while (hashMapIterator_hasNext(&iter)) {
- pubsub_subscriber_t *svc = hashMapIterator_nextValue(&iter);
- svc->receive(svc->handle, msgSer->msgName, msgSer->msgId, deSerializedMsg, message->metadata.metadata,
- &release);
- if (!release && hashMapIterator_hasNext(&iter)) {
- //receive function has taken ownership and still more receive function to come ..
- //deserialize again for new message
- status = msgSer->deserialize(msgSer->handle, &deSerializeBuffer, 1, &deSerializedMsg);
- if (status != CELIX_SUCCESS) {
- L_WARN("[PSA_TCP_TR] Cannot deserialize msg type %s for scope/topic %s/%s", msgSer->msgName,
- receiver->scope == NULL ? "(null)" : receiver->scope, receiver->topic);
- break;
+ if (cont) {
+ hash_map_iterator_t iter = hashMapIterator_construct(entry->subscriberServices);
+ while (hashMapIterator_hasNext(&iter)) {
+ pubsub_subscriber_t *svc = hashMapIterator_nextValue(&iter);
+ svc->receive(svc->handle,
+ msgSer->msgName,
+ msgSer->msgId,
+ deSerializedMsg,
+ message->metadata.metadata,
+ &release);
+ pubsubInterceptorHandler_invokePostReceive(receiver->interceptorsHandler,
+ msgType,
+ msgId,
+ deSerializedMsg,
+ metadata);
+ if (!release && hashMapIterator_hasNext(&iter)) {
+ //receive function has taken ownership and still more receive function to come ..
+ //deserialize again for new message
+ status = msgSer->deserialize(msgSer->handle, &deSerializeBuffer, 1, &deSerializedMsg);
+ if (status != CELIX_SUCCESS) {
+ L_WARN("[PSA_TCP_TR] Cannot deserialize msg type %s for scope/topic %s/%s",
+ msgSer->msgName,
+ receiver->scope == NULL ? "(null)" : receiver->scope,
+ receiver->topic);
+ break;
+ }
+ release = true;
}
- release = true;
}
+ if (release) {
+ msgSer->freeDeserializeMsg(msgSer->handle, deSerializedMsg);
+ }
+ if (message->metadata.metadata) {
+ celix_properties_destroy(message->metadata.metadata);
+ }
+ updateReceiveCount += 1;
}
- if (release) {
- msgSer->freeDeserializeMsg(msgSer->handle, deSerializedMsg);
- }
- if (message->metadata.metadata) {
- celix_properties_destroy(message->metadata.metadata);
- }
- updateReceiveCount += 1;
} else {
updateSerError += 1;
L_WARN("[PSA_TCP_TR] Cannot deserialize msg type %s for scope/topic %s/%s", msgSer->msgName,
@@ -644,10 +665,7 @@ static void *psa_tcp_recvThread(void *data) {
pubsub_admin_receiver_metrics_t *pubsub_tcpTopicReceiver_metrics(pubsub_tcp_topic_receiver_t *receiver) {
pubsub_admin_receiver_metrics_t *result = calloc(1, sizeof(*result));
- snprintf(result->scope,
- PUBSUB_AMDIN_METRICS_NAME_MAX,
- "%s",
- receiver->scope == NULL ? PUBSUB_DEFAULT_ENDPOINT_SCOPE : receiver->scope);
+ snprintf(result->scope, PUBSUB_AMDIN_METRICS_NAME_MAX, "%s", receiver->scope == NULL ? PUBSUB_DEFAULT_ENDPOINT_SCOPE : receiver->scope);
snprintf(result->topic, PUBSUB_AMDIN_METRICS_NAME_MAX, "%s", receiver->topic);
int msgTypesCount = 0;
@@ -671,8 +689,7 @@ pubsub_admin_receiver_metrics_t *pubsub_tcpTopicReceiver_metrics(pubsub_tcp_topi
hash_map_iterator_t iter2 = hashMapIterator_construct(entry->metrics);
while (hashMapIterator_hasNext(&iter2)) {
hash_map_t *origins = hashMapIterator_nextValue(&iter2);
- result->msgTypes[i].origins = calloc((size_t) hashMap_size(origins),
- sizeof(*(result->msgTypes[i].origins)));
+ result->msgTypes[i].origins = calloc((size_t) hashMap_size(origins), sizeof(*(result->msgTypes[i].origins)));
result->msgTypes[i].nrOfOrigins = hashMap_size(origins);
int k = 0;
hash_map_iterator_t iter3 = hashMapIterator_construct(origins);
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
index 29dc50e..754e769 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c
@@ -35,6 +35,7 @@
#include "celix_constants.h"
#include <signal.h>
#include <pubsub_utils.h>
+#include "pubsub_interceptors_handler.h"
#define FIRST_SEND_DELAY_IN_SECONDS 2
#define TCP_BIND_MAX_RETRY 10
@@ -59,6 +60,7 @@ struct pubsub_tcp_topic_sender {
bool metricsEnabled;
pubsub_tcpHandler_t *socketHandler;
pubsub_tcpHandler_t *sharedSocketHandler;
+ pubsub_interceptors_handler_t *interceptorsHandler;
char *scope;
char *topic;
@@ -144,6 +146,7 @@ pubsub_tcp_topic_sender_t *pubsub_tcpTopicSender_create(
}
sender->metricsEnabled = celix_bundleContext_getPropertyAsBool(ctx, PSA_TCP_METRICS_ENABLED,
PSA_TCP_DEFAULT_METRICS_ENABLED);
+ pubsubInterceptorsHandler_create(ctx, scope, topic, &sender->interceptorsHandler);
bool isEndpoint = false;
char *urls = NULL;
const char *ip = celix_bundleContext_getProperty(ctx, PUBSUB_TCP_PSA_IP_KEY, NULL);
@@ -312,6 +315,7 @@ void pubsub_tcpTopicSender_destroy(pubsub_tcp_topic_sender_t *sender) {
celixThreadMutex_unlock(&sender->boundedServices.mutex);
celixThreadMutex_destroy(&sender->boundedServices.mutex);
+ pubsubInterceptorsHandler_destroy(sender->interceptorsHandler);
if ((sender->socketHandler) && (sender->sharedSocketHandler == NULL)) {
pubsub_tcpHandler_destroy(sender->socketHandler);
sender->socketHandler = NULL;
@@ -533,7 +537,8 @@ psa_tcp_topicPublicationSend(void *handle, unsigned int msgTypeId, const void *i
clock_gettime(CLOCK_REALTIME, &serializationEnd);
}
- if (status == CELIX_SUCCESS /*ser ok*/) {
+ bool cont = pubsubInterceptorHandler_invokePreSend(sender->interceptorsHandler, entry->msgSer->msgName, msgTypeId, inMsg, &metadata);
+ if (status == CELIX_SUCCESS /*ser ok*/ && cont) {
pubsub_protocol_message_t message;
message.metadata.metadata = NULL;
message.payload.payload = NULL;
@@ -561,6 +566,7 @@ psa_tcp_topicPublicationSend(void *handle, unsigned int msgTypeId, const void *i
status = -1;
sendOk = false;
}
+ pubsubInterceptorHandler_invokePostSend(sender->interceptorsHandler, entry->msgSer->msgName, msgTypeId, inMsg, metadata);
if (message.metadata.metadata)
celix_properties_destroy(message.metadata.metadata);
if (serializedIoVecOutput) {
[celix] 05/11: Merge master
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 5eac987339b091de864595134f0be9241f51de5e
Merge: 78ed431 8e35298
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Jun 29 21:43:57 2020 +0200
Merge master
bundles/pubsub/examples/CMakeLists.txt | 7 +--
.../src/pubsub_psa_tcp_constants.h | 12 ++++
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 12 ++--
.../pubsub_admin_tcp/src/pubsub_tcp_handler.h | 5 +-
.../src/pubsub_tcp_topic_receiver.c | 15 +++--
.../pubsub_admin_tcp/src/pubsub_tcp_topic_sender.c | 8 ++-
.../src/pubsub_psa_udpmc_constants.h | 11 ++++
.../src/pubsub_udpmc_topic_receiver.c | 6 +-
.../src/pubsub_udpmc_topic_sender.c | 17 +++++-
.../src/pubsub_psa_websocket_constants.h | 6 ++
.../src/pubsub_websocket_topic_receiver.c | 6 +-
.../src/pubsub_psa_zmq_constants.h | 12 ++++
.../pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c | 6 +-
.../src/pubsub_zmq_topic_receiver.c | 6 +-
bundles/pubsub/pubsub_utils/include/pubsub_utils.h | 10 ++++
bundles/pubsub/pubsub_utils/src/pubsub_utils.c | 15 +++++
bundles/pubsub/test/CMakeLists.txt | 6 +-
bundles/pubsub/test/test/test_runner.cc | 44 +++++++++++++-
bundles/pubsub/test/test/tst_activator.c | 6 +-
.../{tst_activator.c => tst_endpoint_activator.c} | 67 ++++++----------------
20 files changed, 191 insertions(+), 86 deletions(-)
diff --cc bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index b404ec4,7fe8bc3..5099aad
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@@ -652,16 -642,16 +652,12 @@@ int pubsub_tcpHandler_listen(pubsub_tcp
}
//
--// Setup buffer sizes
++// Setup receive buffer size
//
--int pubsub_tcpHandler_createReceiveBufferStore(pubsub_tcpHandler_t *handle,
-- unsigned int maxNofBuffers
-- __attribute__((__unused__)),
-- unsigned int bufferSize) {
++int pubsub_tcpHandler_setReceiveBufferSize(pubsub_tcpHandler_t *handle, unsigned int size) {
if (handle != NULL) {
celixThreadRwlock_writeLock(&handle->dbLock);
-- handle->bufferSize = bufferSize;
-- handle->maxNofBuffer = maxNofBuffers;
++ handle->bufferSize = size;
celixThreadRwlock_unlock(&handle->dbLock);
}
return 0;
diff --cc bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
index 28df5b7,ed4581c..bb74387
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.h
@@@ -58,11 -58,10 +58,8 @@@ int pubsub_tcpHandler_close(pubsub_tcpH
int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url);
int pubsub_tcpHandler_disconnect(pubsub_tcpHandler_t *handle, char *url);
int pubsub_tcpHandler_listen(pubsub_tcpHandler_t *handle, char *url);
--
--int pubsub_tcpHandler_createReceiveBufferStore(pubsub_tcpHandler_t *handle,
-- unsigned int maxNofBuffers,
-- unsigned int bufferSize);
++int pubsub_tcpHandler_setReceiveBufferSize(pubsub_tcpHandler_t *handle, unsigned int size);
+int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int size);
void pubsub_tcpHandler_setTimeout(pubsub_tcpHandler_t *handle, unsigned int timeout);
void pubsub_tcpHandler_setSendRetryCnt(pubsub_tcpHandler_t *handle, unsigned int count);
void pubsub_tcpHandler_setReceiveRetryCnt(pubsub_tcpHandler_t *handle, unsigned int count);
diff --cc bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c
index eb6afbd,abc44f4..71890d9
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_topic_receiver.c
@@@ -207,14 -213,14 +213,11 @@@ pubsub_tcp_topic_receiver_t *pubsub_tcp
long retryCnt = celix_properties_getAsLong(topicProperties, PUBSUB_TCP_SUBSCRIBER_RETRY_CNT_KEY,
PUBSUB_TCP_SUBSCRIBER_RETRY_CNT_DEFAULT);
double rcvTimeout = celix_properties_getAsDouble(topicProperties, PUBSUB_TCP_SUBSCRIBER_RCVTIMEO_KEY, PUBSUB_TCP_SUBSCRIBER_RCVTIMEO_DEFAULT);
-- long sessions = celix_bundleContext_getPropertyAsLong(ctx, PSA_TCP_MAX_RECV_SESSIONS,
-- PSA_TCP_DEFAULT_MAX_RECV_SESSIONS);
-- long buffer_size = celix_bundleContext_getPropertyAsLong(ctx, PSA_TCP_RECV_BUFFER_SIZE,
++ long bufferSize = celix_bundleContext_getPropertyAsLong(ctx, PSA_TCP_RECV_BUFFER_SIZE,
PSA_TCP_DEFAULT_RECV_BUFFER_SIZE);
long timeout = celix_bundleContext_getPropertyAsLong(ctx, PSA_TCP_TIMEOUT, PSA_TCP_DEFAULT_TIMEOUT);
pubsub_tcpHandler_setThreadName(receiver->socketHandler, topic, scope);
-- pubsub_tcpHandler_createReceiveBufferStore(receiver->socketHandler, (unsigned int) sessions,
-- (unsigned int) buffer_size);
++ pubsub_tcpHandler_setReceiveBufferSize(receiver->socketHandler, (unsigned int) bufferSize);
pubsub_tcpHandler_setTimeout(receiver->socketHandler, (unsigned int) timeout);
pubsub_tcpHandler_addMessageHandler(receiver->socketHandler, receiver, processMsg);
pubsub_tcpHandler_addReceiverConnectionCallback(receiver->socketHandler, receiver, psa_tcp_connectHandler,
[celix] 01/11: Add Message segmentation
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 63fe56e1231146e446b8801e9a72f46f5bfc67e3
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Tue Jun 23 21:53:13 2020 +0200
Add Message segmentation
---
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 241 +++++++++++++--------
1 file changed, 149 insertions(+), 92 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 3bb31cd..9ef6361 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -78,6 +78,7 @@ typedef struct psa_tcp_connection_entry {
bool connected;
bool headerError;
pubsub_protocol_message_t header;
+ unsigned int maxMsgSize;
unsigned int syncSize;
unsigned int headerSize;
unsigned int headerBufferSize; // Size of headerBuffer, size = 0, no headerBuffer -> included in payload
@@ -117,6 +118,7 @@ struct pubsub_tcpHandler {
pubsub_protocol_service_t *protocol;
unsigned int bufferSize;
unsigned int maxNofBuffer;
+ unsigned int maxMsgSize;
unsigned int maxSendRetryCount;
unsigned int maxRcvRetryCount;
double sendTimeout;
@@ -331,6 +333,7 @@ pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, ch
entry->headerBufferSize = size;
handle->protocol->getSyncHeaderSize(handle->protocol->handle, &size);
entry->syncSize = size;
+ entry->maxMsgSize = (handle->maxMsgSize) ? handle->maxMsgSize : UINT32_MAX;
handle->protocol->getFooterSize(handle->protocol->handle, &size);
entry->footerSize = size;
entry->bufferSize = handle->bufferSize;
@@ -658,6 +661,20 @@ int pubsub_tcpHandler_createReceiveBufferStore(pubsub_tcpHandler_t *handle,
}
//
+// Setup receive buffer size
+//
+int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int size) {
+ if (handle != NULL) {
+ celixThreadRwlock_writeLock(&handle->dbLock);
+ handle->maxMsgSize = size;
+ celixThreadRwlock_unlock(&handle->dbLock);
+ }
+ return 0;
+}
+
+
+int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int maxMsgSize);
+//
// Setup thread timeout
//
void pubsub_tcpHandler_setTimeout(pubsub_tcpHandler_t *handle,
@@ -878,7 +895,7 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
unsigned int size = entry->header.header.payloadPartSize;
// For header less messages adjust offset and msg size;
if (!entry->headerBufferSize) {
- offset = entry->headerSize;
+ offset += entry->headerSize;
size -= offset;
}
// Read payload data from queue
@@ -923,7 +940,9 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
if (nbytes > 0) {
entry->retryCount = 0;
// Check if complete message is received
- if ((entry->bufferReadSize >= entry->header.header.payloadSize) && validMsg) {
+ if ((entry->bufferReadSize >= entry->header.header.payloadSize) &&
+ validMsg &&
+ entry->header.header.isLastSegment) {
entry->bufferReadSize = 0;
pubsub_tcpHandler_decodePayload(handle, entry);
}
@@ -1026,6 +1045,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
int nofConnToClose = 0;
if (handle) {
hash_map_iterator_t iter = hashMapIterator_construct(handle->connection_fd_map);
+ size_t max_msg_iov_len = IOV_MAX - 2;
while (hashMapIterator_hasNext(&iter)) {
psa_tcp_connection_entry_t *entry = hashMapIterator_nextValue(&iter);
if (!entry->connected) continue;
@@ -1052,6 +1072,16 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
&metadataSize);
}
message->header.metadataSize = metadataSize;
+ size_t totalMessageSize = payloadSize + metadataSize;
+
+ bool isMessageSegmentationSupported = false;
+ handle->protocol->isMessageSegmentationSupported(handle->protocol->handle, &isMessageSegmentationSupported);
+ if (((!isMessageSegmentationSupported) && (msg_iov_len > max_msg_iov_len)) ||
+ ((!isMessageSegmentationSupported) && (totalMessageSize > entry->maxMsgSize))) {
+ L_WARN("[TCP Socket] Failed to send message (fd: %d), Message segmentation is not supported\n",
+ entry->fd);
+ continue;
+ }
void *footerData = NULL;
size_t footerDataSize = 0;
@@ -1061,107 +1091,134 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
&footerDataSize);
}
+
size_t msgSize = 0;
- struct msghdr msg;
- struct iovec msg_iov[IOV_MAX];
- memset(&msg, 0x00, sizeof(struct msghdr));
- msg.msg_name = &entry->addr;
- msg.msg_namelen = entry->len;
- msg.msg_flags = flags;
- msg.msg_iov = msg_iov;
-
- // Write generic seralized payload in vector buffer
- if (payloadSize && payloadData) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = payloadData;
- msg.msg_iov[msg.msg_iovlen].iov_len = payloadSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- } else {
- // copy serialized vector into vector buffer
- for (size_t i = 0; i < MIN(msg_iov_len, IOV_MAX - 2); i++) {
+ size_t msgIovLen = 0;
+ long int nbytes = UINT32_MAX;
+ while (msgSize < totalMessageSize && nbytes > 0) {
+ struct msghdr msg;
+ struct iovec msg_iov[IOV_MAX];
+ memset(&msg, 0x00, sizeof(struct msghdr));
+ msg.msg_name = &entry->addr;
+ msg.msg_namelen = entry->len;
+ msg.msg_flags = flags;
+ msg.msg_iov = msg_iov;
+ size_t msgPartSize = 0;
+ message->header.payloadPartSize = 0;
+ message->header.payloadOffset = 0;
+ message->header.metadataSize = 0;
+ message->header.isLastSegment = 0;
+
+ // Write generic seralized payload in vector buffer
+ if (payloadSize && payloadData) {
+ char *payloadDataBuffer = payloadData;
msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = msgIoVec[i].iov_base;
- msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[i].iov_len;
+ msg.msg_iov[msg.msg_iovlen].iov_base = &payloadDataBuffer[msgSize];
+ msg.msg_iov[msg.msg_iovlen].iov_len = MIN((payloadSize - msgSize), entry->maxMsgSize);
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ message->header.payloadPartSize = msgPartSize;
+ message->header.payloadOffset = msgSize;
msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ } else {
+ // copy serialized vector into vector buffer
+ for (size_t i = 0; i < MIN(msg_iov_len, max_msg_iov_len); i++) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = msgIoVec[msgIovLen + i].iov_base;
+ msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[msgIovLen + i].iov_len;
+ if ((msgPartSize + msg.msg_iov[msg.msg_iovlen].iov_len) > entry->maxMsgSize)
+ break;
+ msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ }
+ message->header.payloadPartSize = msgPartSize;
+ message->header.payloadOffset = msgSize;
+ msgSize += msgPartSize;
+ msgIovLen += (msg.msg_iovlen - 1);
}
- }
- // Write optional metadata in vector buffer
- if (metadataSize && metadataData) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = metadataData;
- msg.msg_iov[msg.msg_iovlen].iov_len = metadataSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- }
+ // Write optional metadata in vector buffer
+ if ((msgSize < (payloadSize + metadataSize)) &&
+ (msgPartSize < entry->maxMsgSize) &&
+ (metadataSize && metadataData)) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = metadataData;
+ msg.msg_iov[msg.msg_iovlen].iov_len = metadataSize;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ message->header.metadataSize = metadataSize;
+ msgSize += metadataSize;
+ }
+ if (msgSize >= totalMessageSize) {
+ message->header.isLastSegment = 0x1;
+ }
- // Write optional footerData in vector buffer
- if (footerData && footerDataSize) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = footerData;
- msg.msg_iov[msg.msg_iovlen].iov_len = footerDataSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- }
+ // Write optional footerData in vector buffer
+ if (footerData && footerDataSize) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = footerData;
+ msg.msg_iov[msg.msg_iovlen].iov_len = footerDataSize;
+ msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ }
- void *headerData = NULL;
- size_t headerSize = 0;
- // check if header is not part of the payload (=> headerBufferSize = 0)s
- if (entry->headerBufferSize) {
- // Encode the header, with payload size and metadata size
- handle->protocol->encodeHeader(handle->protocol->handle, message,
- &headerData,
- &headerSize);
- }
- if (!entry->headerBufferSize) {
- // Skip header buffer, when header is part of payload;
- msg.msg_iov = &msg_iov[1];
- } else if (headerSize && headerData) {
- // Write header in 1st vector buffer item
- msg.msg_iov[0].iov_base = headerData;
- msg.msg_iov[0].iov_len = headerSize;
- msgSize += msg.msg_iov[0].iov_len;
- msg.msg_iovlen++;
- } else {
- L_ERROR("[TCP Socket] No header buffer is generated");
- msg.msg_iovlen = 0;
- }
- long int nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgSize, flags);
- // When a specific socket keeps reporting errors can indicate a subscriber
- // which is not active anymore, the connection will remain until the retry
- // counter exceeds the maximum retry count.
- // Btw, also, SIGSTOP issued by a debugging tool can result in EINTR error.
- if (nbytes == -1) {
- if (entry->retryCount < handle->maxSendRetryCount) {
- entry->retryCount++;
- L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
- entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
+ void *headerData = NULL;
+ size_t headerSize = 0;
+ // check if header is not part of the payload (=> headerBufferSize = 0)s
+ if (entry->headerBufferSize) {
+ // Encode the header, with payload size and metadata size
+ handle->protocol->encodeHeader(handle->protocol->handle, message,
+ &headerData,
+ &headerSize);
+ }
+ if (!entry->headerBufferSize) {
+ // Skip header buffer, when header is part of payload;
+ msg.msg_iov = &msg_iov[1];
+ } else if (headerSize && headerData) {
+ // Write header in 1st vector buffer item
+ msg.msg_iov[0].iov_base = headerData;
+ msg.msg_iov[0].iov_len = headerSize;
+ msgPartSize += msg.msg_iov[0].iov_len;
+ msg.msg_iovlen++;
} else {
- L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s",
- entry->fd, handle->maxSendRetryCount, strerror(errno));
- connFdCloseQueue[nofConnToClose++] = entry->fd;
+ L_ERROR("[TCP Socket] No header buffer is generated");
+ msg.msg_iovlen = 0;
}
- result = -1; //At least one connection failed sending
- } else if (msgSize) {
- entry->retryCount = 0;
- if (nbytes != msgSize) {
- L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
+ nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgSize, flags);
+ // When a specific socket keeps reporting errors can indicate a subscriber
+ // which is not active anymore, the connection will remain until the retry
+ // counter exceeds the maximum retry count.
+ // Btw, also, SIGSTOP issued by a debugging tool can result in EINTR error.
+ if (nbytes == -1) {
+ if (entry->retryCount < handle->maxSendRetryCount) {
+ entry->retryCount++;
+ L_ERROR(
+ "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
+ entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
+ } else {
+ L_ERROR(
+ "[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s", entry->fd, handle->maxSendRetryCount, strerror(errno));
+ connFdCloseQueue[nofConnToClose++] = entry->fd;
+ }
+ result = -1; //At least one connection failed sending
+ } else if (msgPartSize) {
+ entry->retryCount = 0;
+ if (nbytes != msgSize) {
+ L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
+ }
+ }
+ // Release data
+ if (headerData) {
+ free(headerData);
+ }
+ // Note: serialized Payload is deleted by serializer
+ if (payloadData && (payloadData != message->payload.payload)) {
+ free(payloadData);
+ }
+ if (metadataData) {
+ free(metadataData);
+ }
+ if (footerData) {
+ free(footerData);
}
}
- // Release data
- if (headerData) {
- free(headerData);
- }
- // Note: serialized Payload is deleted by serializer
- if (payloadData && (payloadData != message->payload.payload)) {
- free(payloadData);
- }
- if (metadataData) {
- free(metadataData);
- }
- if (footerData) {
- free(footerData);
- }
+
}
}
celixThreadRwlock_unlock(&handle->dbLock);
[celix] 10/11: Fix
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 4cfc6ecaf6a8055bd2c514cfc11f93426aac326b
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Aug 3 16:52:22 2020 +0200
Fix
---
.../src/pubsub_psa_tcp_constants.h | 2 +-
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 369 ++++++---------------
.../pubsub/pubsub_utils/include/pubsub_utils_url.h | 8 +-
bundles/pubsub/pubsub_utils/src/pubsub_utils_url.c | 22 +-
4 files changed, 122 insertions(+), 279 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
index 9551c5b..9f03d13 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
@@ -31,7 +31,7 @@
#define PSA_TCP_DEFAULT_BASE_PORT 5501
#define PSA_TCP_DEFAULT_MAX_PORT 6000
-#define PSA_TCP_DEFAULT_MAX_MESSAGE_SIZE 0
+#define PSA_TCP_DEFAULT_MAX_MESSAGE_SIZE UINT32_MAX
#define PSA_TCP_DEFAULT_RECV_BUFFER_SIZE 65 * 1024
#define PSA_TCP_DEFAULT_TIMEOUT 2000 // 2 seconds
#define PSA_TCP_SUBSCRIBER_CONNECTION_DEFAULT_TIMEOUT 250 // 250 ms
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index aae0406..411e9d2 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -32,17 +32,13 @@
#include <array_list.h>
#include <pthread.h>
#if defined(__APPLE__)
-#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#else
#include <sys/epoll.h>
#endif
#include <limits.h>
-#include <assert.h>
#include "ctype.h"
-#include <netdb.h>
-#include <signal.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
@@ -53,12 +49,13 @@
#define MAX_EVENTS 64
#define MAX_DEFAULT_BUFFER_SIZE 4u
-#define READ_STATE_HEADER 0u
-#define READ_STATE_PAYLOAD 1u
-#define READ_STATE_META 2u
-#define READ_STATE_FOOTER 3u
-#define READ_STATE_READY 4u
-#define READ_STATE_SYNC 5u
+#define READ_STATE_INIT 0u
+#define READ_STATE_HEADER 1u
+#define READ_STATE_PAYLOAD 2u
+#define READ_STATE_META 3u
+#define READ_STATE_FOOTER 4u
+#define READ_STATE_READY 5u
+#define READ_STATE_SYNC 6u
#if defined(__APPLE__)
#define MSG_NOSIGNAL (0)
@@ -95,7 +92,6 @@ typedef struct psa_tcp_connection_entry {
void *footerBuffer;
unsigned int bufferSize;
void *buffer;
- unsigned int bufferReadSize;
unsigned int bufferReadReadOffset;
unsigned int expectedBufferReadSize;
unsigned int msgSizeReadSize;
@@ -142,8 +138,7 @@ static inline int pubsub_tcpHandler_makeNonBlocking(pubsub_tcpHandler_t *handle,
static inline psa_tcp_connection_entry_t* pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, char *external_url, struct sockaddr_in *addr);
static inline void pubsub_tcpHandler_freeEntry(psa_tcp_connection_entry_t *entry);
static inline void pubsub_tcpHandler_releaseEntryBuffer(pubsub_tcpHandler_t *handle, int fd, unsigned int index);
-static inline int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, unsigned int offset, unsigned int size, int flag );
-static inline int pubsub_tcpHandler_readSocket_(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, int flag );
+static inline int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, int flag );
static inline void pubsub_tcpHandler_setReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int nextState);
static inline void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
@@ -264,7 +259,7 @@ int pubsub_tcpHandler_open(pubsub_tcpHandler_t *handle, char *url) {
L_ERROR("[TCP Socket] Error setsockopt (SO_RCVTIMEO) to set send timeout: %s", strerror(errno));
}
}
- struct sockaddr_in *addr = pubsub_utils_url_getInAddr(url_info->hostname, url_info->portnr);
+ struct sockaddr_in *addr = pubsub_utils_url_getInAddr(url_info->hostname, url_info->port_nr);
if (addr) {
rc = bind(fd, (struct sockaddr *) addr, sizeof(struct sockaddr));
if (rc != 0) {
@@ -342,6 +337,7 @@ pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, ch
}
if (entry->footerSize) entry->footerBuffer = calloc(sizeof(char), entry->footerSize);
if (entry->bufferSize) entry->buffer = calloc(sizeof(char), entry->bufferSize);
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
}
return entry;
}
@@ -418,11 +414,11 @@ int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
socklen_t len = sizeof(sin);
getsockname(fd, (struct sockaddr *) &sin, &len);
char *interface_url = pubsub_utils_url_get_url(&sin, NULL);
- struct sockaddr_in *addr = pubsub_utils_url_getInAddr(url_info->hostname, url_info->portnr);
+ struct sockaddr_in *addr = pubsub_utils_url_getInAddr(url_info->hostname, url_info->port_nr);
if ((rc >= 0) && addr) {
rc = connect(fd, (struct sockaddr *) addr, sizeof(struct sockaddr));
if (rc < 0 && errno != EINPROGRESS) {
- L_ERROR("[TCP Socket] Cannot connect to %s:%d: using; %s err: %s\n", url_info->hostname, url_info->portnr, interface_url,
+ L_ERROR("[TCP Socket] Cannot connect to %s:%d: using; %s err: %s\n", url_info->hostname, url_info->port_nr, interface_url,
strerror(errno));
close(fd);
} else {
@@ -449,13 +445,12 @@ int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
L_ERROR("[TCP Socket] Cannot create poll event %s\n", strerror(errno));
entry = NULL;
}
- //rc = pubsub_tcpHandler_makeNonBlocking(handle, entry->fd);
- //if (rc < 0) {
- // pubsub_tcpHandler_freeEntry(entry);
- // L_ERROR("[TCP Socket] Cannot make not blocking %s\n", strerror(errno));
- // entry = NULL;
- // }
-
+ rc = pubsub_tcpHandler_makeNonBlocking(handle, entry->fd);
+ if (rc < 0) {
+ pubsub_tcpHandler_freeEntry(entry);
+ L_ERROR("[TCP Socket] Cannot make not blocking %s\n", strerror(errno));
+ entry = NULL;
+ }
}
if ((rc >= 0) && (entry)) {
celixThreadRwlock_writeLock(&handle->dbLock);
@@ -657,6 +652,7 @@ int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int si
if (handle != NULL) {
celixThreadRwlock_writeLock(&handle->dbLock);
handle->maxMsgSize = size;
+ handle->maxMsgSize = 4;
celixThreadRwlock_unlock(&handle->dbLock);
}
return 0;
@@ -727,7 +723,7 @@ void pubsub_tcpHandler_setThreadPriority(pubsub_tcpHandler_t *handle, long prio,
if (prio > 0 && prio < 100) {
struct sched_param sch;
bzero(&sch, sizeof(struct sched_param));
- sch.sched_priority = prio;
+ sch.sched_priority = (int)prio;
pthread_setschedparam(handle->thread.thread, policy, &sch);
} else {
L_INFO("Skipping configuration of thread prio to %i and thread "
@@ -772,26 +768,8 @@ void pubsub_tcpHandler_setReceiveTimeOut(pubsub_tcpHandler_t *handle, double tim
}
static inline
-int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, unsigned int offset, unsigned int size, int flag ) {
- int expectedReadSize = size;
- int nbytes = size;
- int msgSize = 0;
- char* buffer = (char*)_buffer;
- while (nbytes > 0 && expectedReadSize > 0) {
- // Read the message header
- nbytes = recv(fd, &buffer[offset], expectedReadSize, flag | MSG_NOSIGNAL);
- // Update buffer administration
- offset += nbytes;
- expectedReadSize -= nbytes;
- msgSize += nbytes;
- }
- if (nbytes <=0) msgSize = nbytes;
- return msgSize;
-}
-
-static inline
-int pubsub_tcpHandler_readSocket_(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, int flag ) {
- int nbytes = entry->expectedBufferReadSize;
+int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, int flag ) {
+ int long nbytes = entry->expectedBufferReadSize;
char* buffer = (char*)_buffer;
while (nbytes > 0 && entry->expectedBufferReadSize > 0) {
// Read the message header
@@ -800,9 +778,6 @@ int pubsub_tcpHandler_readSocket_(pubsub_tcpHandler_t *handle, psa_tcp_connectio
entry->bufferReadReadOffset += nbytes;
entry->expectedBufferReadSize-= nbytes;
}
- if (nbytes == 0) {
- L_WARN("[TCP Socket] Failed to receive message (fd: %d), error: %s. Retry count %u of %u,", entry->fd, strerror(errno));
- }
return nbytes;
}
@@ -837,7 +812,7 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
if (entry->header.header.payloadSize) {
entry->state = READ_STATE_PAYLOAD;
entry->bufferReadReadOffset = entry->header.header.payloadOffset;
- entry->expectedBufferReadSize = entry->header.header.payloadSize;
+ entry->expectedBufferReadSize = (entry->header.header.payloadPartSize) ? entry->header.header.payloadPartSize : entry->header.header.payloadSize;
// For header less messages adjust offset and msg size;
if (!entry->headerBufferSize) {
entry->bufferReadReadOffset += entry->headerSize;
@@ -910,6 +885,7 @@ static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHand
+
//
// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
// If the message is completely reassembled true is returned and the index and size have valid values
@@ -932,182 +908,49 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
// Message buffer is to small, reallocate to make it bigger
if ((!entry->headerBufferSize) && (entry->headerSize > entry->bufferSize)) {
- handle->bufferSize = MAX(handle->bufferSize, entry->headerSize );
- if (entry->buffer) free(entry->buffer);
+ handle->bufferSize = MAX(handle->bufferSize, entry->headerSize);
+ if (entry->buffer)
+ free(entry->buffer);
entry->buffer = malloc((size_t) handle->bufferSize);
entry->bufferSize = handle->bufferSize;
}
// Read the message
- bool validMsg = false;
- char* header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
- int nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, MSG_PEEK);
- if (nbytes > 0) {
- // Check header message buffer
- if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
- // Did not receive correct header
- // skip sync word and try to read next header
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->syncSize, 0);
- if (!entry->headerError) {
- L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
- }
- entry->headerError = true;
- entry->bufferReadSize = 0;
- } else {
- // Read header message from queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, 0);
- if ((nbytes > 0) && (nbytes == entry->headerSize)) {
- entry->headerError = false;
- // For headerless message, add header to bufferReadSize;
- if (!entry->headerBufferSize)
- entry->bufferReadSize += nbytes;
- // Alloc message buffers
- if (entry->header.header.payloadSize > entry->bufferSize) {
- handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize);
- if (entry->buffer)
- free(entry->buffer);
- entry->buffer = malloc((size_t) handle->bufferSize);
- entry->bufferSize = handle->bufferSize;
- }
- if (entry->header.header.metadataSize > entry->metaBufferSize) {
- if (entry->metaBuffer) {
- free(entry->metaBuffer);
- }
- entry->metaBuffer = malloc((size_t) entry->header.header.metadataSize);
- entry->metaBufferSize = entry->header.header.metadataSize;
- L_WARN("[TCP Socket] socket: %d, url: %s, realloc read meta buffer: (%d, %d) \n", entry->fd,
- entry->url, entry->metaBufferSize, entry->header.header.metadataSize);
- }
-
- if (entry->header.header.payloadSize) {
- unsigned int offset = entry->header.header.payloadOffset;
- unsigned int size = entry->header.header.payloadPartSize;
- // For header less messages adjust offset and msg size;
- if (!entry->headerBufferSize) {
- offset = entry->headerSize;
- size -= offset;
- }
- // Read payload data from queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, offset, size, 0);
- if (nbytes > 0) {
- if (nbytes == size) {
- entry->bufferReadSize += nbytes;
- } else {
- entry->bufferReadSize = 0;
- L_ERROR("[TCP Socket] Failed to receive complete payload buffer (fd: %d) nbytes : %d = msgSize %d", entry->fd, nbytes, size);
- }
- }
- }
- if (nbytes > 0 && entry->header.header.metadataSize) {
- // Read meta data from queue
- unsigned int size = entry->header.header.metadataSize;
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0, size,0);
- if ((nbytes > 0) && (nbytes != size)) {
- L_ERROR("[TCP Socket] Failed to receive complete payload buffer (fd: %d) nbytes : %d = msgSize %d", entry->fd, nbytes, size);
- }
- }
- // Check for end of message using, footer of message. Because of streaming protocol
- if (nbytes > 0) {
- if (entry->footerSize > 0) {
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer,0, entry->footerSize,0);
- if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
- // valid footer, this means that the message is valid
- validMsg = true;
- } else {
- // Did not receive correct header
- L_ERROR("[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)", entry->header.header.seqNr, entry->fd, entry->url);
- entry->bufferReadSize = 0;
- }
- } else {
- // No Footer, then complete message is received
- validMsg = true;
- }
- }
- }
- }
- }
- if (nbytes > 0) {
- entry->retryCount = 0;
- // Check if complete message is received
- if ((entry->bufferReadSize >= entry->header.header.payloadSize) && validMsg) {
- entry->bufferReadSize = 0;
- pubsub_tcpHandler_decodePayload(handle, entry);
- }
- } else {
- if (entry->retryCount < handle->maxRcvRetryCount) {
- entry->retryCount++;
- L_WARN("[TCP Socket] Failed to receive message (fd: %d), error: %s. Retry count %u of %u,", entry->fd,
- strerror(errno), entry->retryCount, handle->maxRcvRetryCount);
- } else {
- L_ERROR("[TCP Socket] Failed to receive message (fd: %d) after %u retries! Closing connection... Error: %s",
- entry->fd, handle->maxRcvRetryCount, strerror(errno));
- nbytes = 0; //Return 0 as indicator to close the connection
- }
- }
- celixThreadRwlock_unlock(&handle->dbLock);
- return nbytes;
-}
-
-
-//
-// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
-// If the message is completely reassembled true is returned and the index and size have valid values
-//
-int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
- celixThreadRwlock_writeLock(&handle->dbLock);
- psa_tcp_connection_entry_t *entry = hashMap_get(handle->interface_fd_map, (void *) (intptr_t) fd);
- if (entry == NULL)
- entry = hashMap_get(handle->connection_fd_map, (void *) (intptr_t) fd);
- // Find FD entry
- if (entry == NULL) {
- celixThreadRwlock_unlock(&handle->dbLock);
- return -1;
- }
- // If it's not connected return from function
- if (!entry->connected) {
- celixThreadRwlock_unlock(&handle->dbLock);
- return -1;
- }
-
- // Message buffer is to small, reallocate to make it bigger
- if ((!entry->headerBufferSize) && (entry->headerSize > entry->bufferSize)) {
- handle->bufferSize = MAX(handle->bufferSize, entry->headerSize );
- if (entry->buffer) free(entry->buffer);
- entry->buffer = malloc((size_t) handle->bufferSize);
- entry->bufferSize = handle->bufferSize;
- }
- // Read the message
- long int nbytes = 0;
- char* header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
+ long int nbytes = UINT32_MAX;
+ char *header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
if (entry->state == READ_STATE_SYNC) {
- nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, header_buffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
if (nbytes > 0) {
pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
if (entry->state == READ_STATE_HEADER) {
- nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, header_buffer, MSG_PEEK);
- if (nbytes >= entry->headerSize) { // Check header message buffer
- if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
- // Did not receive correct header
- // skip sync word and try to read next header
- if (!entry->headerError) {
- L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
- }
- entry->headerError = true;
- pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_SYNC);
- } else {
- // Read header message from queue
- pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
- nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, header_buffer, 0);
- if ((nbytes > 0) && (nbytes == entry->headerSize)) {
- entry->headerError = false;
- entry->msgSizeReadSize = 0;
- // For headerless message, add header to bufferReadSize;
- if (!entry->headerBufferSize) entry->msgSizeReadSize += nbytes;
- pubsub_tcpHandler_setReadStateMachine(handle, entry);
- }
- }
- }
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, MSG_PEEK);
+ if (nbytes >= entry->headerSize) { // Check header message buffer
+ if (handle->protocol->decodeHeader(handle->protocol->handle,
+ header_buffer,
+ entry->headerSize,
+ &entry->header) != CELIX_SUCCESS) {
+ // Did not receive correct header
+ // skip sync word and try to read next header
+ if (!entry->headerError) {
+ L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
+ }
+ entry->headerError = true;
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_SYNC);
+ } else {
+ // Read header message from queue
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
+ if ((nbytes > 0) && (nbytes == entry->headerSize)) {
+ entry->headerError = false;
+ entry->msgSizeReadSize = 0;
+ // For headerless message, add header to bufferReadSize;
+ if (!entry->headerBufferSize)
+ entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
+ }
}
if (entry->state == READ_STATE_PAYLOAD) {
@@ -1121,7 +964,7 @@ int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
}
// Read payload data from queue
- nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, entry->buffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, 0);
if (nbytes > 0) {
if (nbytes >= entry->header.header.payloadPartSize) {
entry->msgSizeReadSize += nbytes;
@@ -1142,7 +985,7 @@ int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
}
// Read meta data from (queue
- nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, entry->metaBuffer,0);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0);
if ((nbytes > 0) && (nbytes >= entry->header.header.metadataSize)) {
entry->msgSizeReadSize += nbytes;
pubsub_tcpHandler_setReadStateMachine(handle, entry);
@@ -1150,7 +993,7 @@ int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
}
if (entry->state == READ_STATE_FOOTER) {
// Check for end of message using, footer of message. Because of streaming protocol
- nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, entry->footerBuffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer, 0);
if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
// valid footer, this means that the message is valid
pubsub_tcpHandler_setReadStateMachine(handle, entry);
@@ -1174,8 +1017,9 @@ int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
entry->retryCount = 0;
} else if (entry->retryCount < handle->maxRcvRetryCount) {
entry->retryCount++;
- L_WARN("[TCP Socket] Failed to receive message (fd: %d), error: %s. Retry count %u of %u,", entry->fd,
- strerror(errno), entry->retryCount, handle->maxRcvRetryCount);
+ L_WARN(
+ "[TCP Socket] Failed to receive message (fd: %d), try again. error(%d): %s, Retry count %u of %u.",
+ entry->fd, errno, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
} else {
L_ERROR("[TCP Socket] Failed to receive message (fd: %d) after %u retries! Closing connection... Error: %s",
entry->fd, handle->maxRcvRetryCount, strerror(errno));
@@ -1183,7 +1027,7 @@ int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
}
}
celixThreadRwlock_unlock(&handle->dbLock);
- return nbytes;
+ return (int)nbytes;
}
@@ -1421,8 +1265,8 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
if (entry->retryCount < handle->maxSendRetryCount) {
entry->retryCount++;
L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
- entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
+ "[TCP Socket] Failed to send message (fd: %d), try again. Retry count %u of %u, error(%d): %s.",
+ entry->fd, entry->retryCount, handle->maxSendRetryCount, errno, strerror(errno));
} else {
L_ERROR(
"[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s", entry->fd, handle->maxSendRetryCount, strerror(errno));
@@ -1560,49 +1404,48 @@ void pubsub_tcpHandler_connectionHandler(pubsub_tcpHandler_t *handle, int fd) {
//
static inline
void pubsub_tcpHandler_handler(pubsub_tcpHandler_t *handle) {
- int rc = 0;
- if (handle->efd >= 0) {
- int nof_events = 0;
- // Wait for events.
- struct kevent events[MAX_EVENTS];
- struct timespec ts = {handle->timeout / 1000, (handle->timeout % 1000) * 1000000};
- nof_events = kevent (handle->efd, NULL, 0, &events[0], MAX_EVENTS, handle->timeout ? &ts : NULL);
- if (nof_events < 0) {
- if ((errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) {
- } else
- L_ERROR("[TCP Socket] Cannot create poll wait (%d) %s\n", nof_events, strerror(errno));
- }
- for (int i = 0; i < nof_events; i++) {
- hash_map_iterator_t iter = hashMapIterator_construct(handle->interface_fd_map);
- psa_tcp_connection_entry_t *pendingConnectionEntry = NULL;
- while (hashMapIterator_hasNext(&iter)) {
- psa_tcp_connection_entry_t *entry = hashMapIterator_nextValue(&iter);
- if (events[i].ident == entry->fd)
- pendingConnectionEntry = entry;
- }
- if (pendingConnectionEntry) {
- int fd = pubsub_tcpHandler_acceptHandler(handle, pendingConnectionEntry);
- pubsub_tcpHandler_connectionHandler(handle, fd);
- } else if (events[i].filter & EVFILT_READ) {
- int rc = pubsub_tcpHandler_read(handle, events[i].ident);
- if (rc == 0) pubsub_tcpHandler_close(handle, events[i].ident);
- } else if (events[i].flags & EV_EOF) {
- int err = 0;
- socklen_t len = sizeof(int);
- rc = getsockopt(events[i].ident, SOL_SOCKET, SO_ERROR, &err, &len);
- if (rc != 0) {
- L_ERROR("[TCP Socket]:EPOLLRDHUP ERROR read from socket %s\n", strerror(errno));
- continue;
+ int rc = 0;
+ if (handle->efd >= 0) {
+ int nof_events = 0;
+ // Wait for events.
+ struct kevent events[MAX_EVENTS];
+ struct timespec ts = {handle->timeout / 1000, (handle->timeout % 1000) * 1000000};
+ nof_events = kevent (handle->efd, NULL, 0, &events[0], MAX_EVENTS, handle->timeout ? &ts : NULL);
+ if (nof_events < 0) {
+ if ((errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) {
+ } else
+ L_ERROR("[TCP Socket] Cannot create poll wait (%d) %s\n", nof_events, strerror(errno));
+ }
+ for (int i = 0; i < nof_events; i++) {
+ hash_map_iterator_t iter = hashMapIterator_construct(handle->interface_fd_map);
+ psa_tcp_connection_entry_t *pendingConnectionEntry = NULL;
+ while (hashMapIterator_hasNext(&iter)) {
+ psa_tcp_connection_entry_t *entry = hashMapIterator_nextValue(&iter);
+ if (events[i].ident == entry->fd)
+ pendingConnectionEntry = entry;
+ }
+ if (pendingConnectionEntry) {
+ int fd = pubsub_tcpHandler_acceptHandler(handle, pendingConnectionEntry);
+ pubsub_tcpHandler_connectionHandler(handle, fd);
+ } else if (events[i].filter & EVFILT_READ) {
+ rc = pubsub_tcpHandler_read(handle, events[i].ident);
+ if (rc == 0) pubsub_tcpHandler_close(handle, events[i].ident);
+ } else if (events[i].flags & EV_EOF) {
+ int err = 0;
+ socklen_t len = sizeof(int);
+ rc = getsockopt(events[i].ident, SOL_SOCKET, SO_ERROR, &err, &len);
+ if (rc != 0) {
+ L_ERROR("[TCP Socket]:EPOLLRDHUP ERROR read from socket %s\n", strerror(errno));
+ continue;
+ }
+ pubsub_tcpHandler_close(handle, events[i].ident);
+ } else if (events[i].flags & EV_ERROR) {
+ L_ERROR("[TCP Socket]:EPOLLERR ERROR read from socket %s\n", strerror(errno));
+ pubsub_tcpHandler_close(handle, events[i].ident);
+ continue;
+ }
}
- pubsub_tcpHandler_close(handle, events[i].ident);
- } else if (events[i].flags & EV_ERROR) {
- L_ERROR("[TCP Socket]:EPOLLERR ERROR read from socket %s\n", strerror(errno));
- pubsub_tcpHandler_close(handle, events[i].ident);
- continue;
- }
}
- }
- return;
}
#else
diff --git a/bundles/pubsub/pubsub_utils/include/pubsub_utils_url.h b/bundles/pubsub/pubsub_utils/include/pubsub_utils_url.h
index 87d4263..b10863c 100644
--- a/bundles/pubsub/pubsub_utils/include/pubsub_utils_url.h
+++ b/bundles/pubsub/pubsub_utils/include/pubsub_utils_url.h
@@ -28,16 +28,16 @@ typedef struct pubsub_utils_url {
char *url;
char *protocol;
char *hostname;
- unsigned int portnr;
+ unsigned int port_nr;
char *uri;
char *interface;
- unsigned int interface_portnr;
+ unsigned int interface_port_nr;
char *interface_url;
} pubsub_utils_url_t;
struct sockaddr_in *pubsub_utils_url_from_fd(int fd);
-struct sockaddr_in *pubsub_utils_url_getInAddr(const char *hostname, int port);
-char *pubsub_utils_url_generate_url(char *hostname, unsigned int portnr, char *protocol);
+struct sockaddr_in *pubsub_utils_url_getInAddr(const char *hostname, unsigned int port);
+char *pubsub_utils_url_generate_url(char *hostname, unsigned int port_nr, char *protocol);
char *pubsub_utils_url_get_url(struct sockaddr_in *inp, char *protocol);
bool pubsub_utils_url_is_multicast(char *hostname);
char *pubsub_utils_url_get_multicast_ip(char *hostname);
diff --git a/bundles/pubsub/pubsub_utils/src/pubsub_utils_url.c b/bundles/pubsub/pubsub_utils/src/pubsub_utils_url.c
index d8d518c..65a1ff2 100644
--- a/bundles/pubsub/pubsub_utils/src/pubsub_utils_url.c
+++ b/bundles/pubsub/pubsub_utils/src/pubsub_utils_url.c
@@ -56,7 +56,7 @@ struct sockaddr_in *pubsub_utils_url_from_fd(int fd) {
return inp;
}
-struct sockaddr_in *pubsub_utils_url_getInAddr(const char *hostname, int port) {
+struct sockaddr_in *pubsub_utils_url_getInAddr(const char *hostname, unsigned int port) {
struct hostent *hp;
struct sockaddr_in *inp = malloc(sizeof(struct sockaddr_in));
bzero(inp, sizeof(struct sockaddr_in)); // zero the struct
@@ -220,11 +220,11 @@ void pubsub_utils_url_parse_url(char *_url, pubsub_utils_url_t *url_info) {
maxPortnr += 1;
unsigned int minDigits = (unsigned int) atoi(portnr);
unsigned int maxDigits = (unsigned int) atoi(maxPortnr);
- url_info->portnr = pubsub_utils_url_rand_range(minDigits, maxDigits);
+ url_info->port_nr = pubsub_utils_url_rand_range(minDigits, maxDigits);
} else {
unsigned int portDigits = (unsigned int) atoi(portnr);
if (portDigits != 0)
- url_info->portnr = portDigits;
+ url_info->port_nr = portDigits;
uri = strstr(port, "/");
if ((uri) && (!url_info->uri))
url_info->uri = celix_utils_strdup(uri);
@@ -256,11 +256,11 @@ void pubsub_utils_url_parse_url(char *_url, pubsub_utils_url_t *url_info) {
maxPortnr += 1;
unsigned int minDigits = (unsigned int) atoi(portnr);
unsigned int maxDigits = (unsigned int) atoi(maxPortnr);
- url_info->interface_portnr = pubsub_utils_url_rand_range(minDigits, maxDigits);
+ url_info->interface_port_nr = pubsub_utils_url_rand_range(minDigits, maxDigits);
} else {
unsigned int portDigits = (unsigned int) atoi(portnr);
if (portDigits != 0)
- url_info->interface_portnr = portDigits;
+ url_info->interface_port_nr = portDigits;
uri = strstr(port, "/");
if ((uri) && (!url_info->uri))
url_info->uri = celix_utils_strdup(uri);
@@ -289,13 +289,13 @@ pubsub_utils_url_t *pubsub_utils_url_parse(char *url) {
free(url_info->interface);
url_info->interface = ip;
}
- struct sockaddr_in *m_sin = pubsub_utils_url_getInAddr(url_info->interface, url_info->interface_portnr);
+ struct sockaddr_in *m_sin = pubsub_utils_url_getInAddr(url_info->interface, url_info->interface_port_nr);
url_info->interface_url = pubsub_utils_url_get_url(m_sin, NULL);
free(m_sin);
pubsub_utils_url_parse_url(url_info->interface_url, &interface_url_info);
free(url_info->interface);
url_info->interface = interface_url_info.hostname;
- url_info->interface_portnr = interface_url_info.portnr;
+ url_info->interface_port_nr = interface_url_info.port_nr;
}
if (url_info->hostname) {
@@ -306,11 +306,11 @@ pubsub_utils_url_t *pubsub_utils_url_parse(char *url) {
free(url_info->hostname);
url_info->hostname = ip;
}
- struct sockaddr_in *sin = pubsub_utils_url_getInAddr(url_info->hostname, url_info->portnr);
+ struct sockaddr_in *sin = pubsub_utils_url_getInAddr(url_info->hostname, url_info->port_nr);
url_info->url = pubsub_utils_url_get_url(sin, url_info->protocol);
free(url_info->hostname);
free(sin);
- url_info->portnr = 0;
+ url_info->port_nr = 0;
url_info->hostname = NULL;
pubsub_utils_url_parse_url(url_info->url, url_info);
}
@@ -338,7 +338,7 @@ void pubsub_utils_url_free(pubsub_utils_url_t *url_info) {
url_info->hostname = NULL;
url_info->protocol = NULL;
url_info->interface = NULL;
- url_info->portnr = 0;
- url_info->interface_portnr = 0;
+ url_info->port_nr = 0;
+ url_info->interface_port_nr = 0;
free(url_info);
}
[celix] 03/11: Fix unit test
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit bec22541abb5f012ec8bf3ed2a3f55d8d6c92096
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Sun Jun 28 20:24:02 2020 +0200
Fix unit test
---
.../src/pubsub_psa_tcp_constants.h | 2 +-
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 275 +++++++++++++--------
2 files changed, 174 insertions(+), 103 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
index 302c9f6..ff8e68f 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_psa_tcp_constants.h
@@ -60,7 +60,7 @@
//Time-out settings are only for BLOCKING connections
#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_KEY "PUBSUB_TCP_PUBLISHER_SEND_TIMEOUT"
-#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_DEFAULT 5.0
+#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_DEFAULT 0.0 //5.0
#define PUBSUB_TCP_PUBLISHER_SNDTIMEO_ENDPOINT_DEFAULT 0.0
#define PUBSUB_TCP_SUBSCRIBER_RCVTIMEO_KEY "PUBSUB_TCP_SUBSCRIBER_RCV_TIMEOUT"
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 19cb0fd..b404ec4 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -53,11 +53,12 @@
#define MAX_EVENTS 64
#define MAX_DEFAULT_BUFFER_SIZE 4u
-#define READ_STATE_INIT 0u
-#define READ_STATE_HEADER 1u
-#define READ_STATE_DATA 2u
-#define READ_STATE_READY 3u
-#define READ_STATE_FIND_HEADER 4u
+#define READ_STATE_HEADER 0u
+#define READ_STATE_PAYLOAD 1u
+#define READ_STATE_META 2u
+#define READ_STATE_FOOTER 3u
+#define READ_STATE_READY 4u
+#define READ_STATE_SYNC 5u
#if defined(__APPLE__)
#define MSG_NOSIGNAL (0)
@@ -94,7 +95,9 @@ typedef struct psa_tcp_connection_entry {
void *footerBuffer;
unsigned int bufferSize;
void *buffer;
- unsigned int bufferReadSize;
+ unsigned int bufferReadReadOffset;
+ unsigned int expectedBufferReadSize;
+ unsigned int msgSizeReadSize;
unsigned int metaBufferSize;
void *metaBuffer;
struct msghdr msg;
@@ -134,29 +137,18 @@ struct pubsub_tcpHandler {
bool running;
};
-static inline int
-pubsub_tcpHandler_closeConnectionEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, bool lock);
-
+static inline int pubsub_tcpHandler_closeConnectionEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, bool lock);
static inline int pubsub_tcpHandler_closeInterfaceEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
-
static inline int pubsub_tcpHandler_makeNonBlocking(pubsub_tcpHandler_t *handle, int fd);
-
-static inline psa_tcp_connection_entry_t *
-pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, char *external_url,
- struct sockaddr_in *addr);
-
+static inline psa_tcp_connection_entry_t* pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, char *external_url, struct sockaddr_in *addr);
static inline void pubsub_tcpHandler_freeEntry(psa_tcp_connection_entry_t *entry);
-
static inline void pubsub_tcpHandler_releaseEntryBuffer(pubsub_tcpHandler_t *handle, int fd, unsigned int index);
-
-static inline int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, unsigned int offset, unsigned int size, int flag );
-
+static inline int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, int flag );
+static inline void pubsub_tcpHandler_setReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
+static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int nextState);
static inline void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
-
static inline void pubsub_tcpHandler_connectionHandler(pubsub_tcpHandler_t *handle, int fd);
-
static inline void pubsub_tcpHandler_handler(pubsub_tcpHandler_t *handle);
-
static void *pubsub_tcpHandler_thread(void *data);
//
@@ -357,6 +349,7 @@ pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, ch
entry->msg.msg_iov[entry->msg.msg_iovlen].iov_base = entry->buffer;
entry->msg.msg_iov[entry->msg.msg_iovlen].iov_len = entry->bufferSize;
entry->msg_iovlen++;
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
}
return entry;
}
@@ -469,6 +462,13 @@ int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
L_ERROR("[TCP Socket] Cannot create poll event %s\n", strerror(errno));
entry = NULL;
}
+ //rc = pubsub_tcpHandler_makeNonBlocking(handle, entry->fd);
+ //if (rc < 0) {
+ // pubsub_tcpHandler_freeEntry(entry);
+ // L_ERROR("[TCP Socket] Cannot make not blocking %s\n", strerror(errno));
+ // entry = NULL;
+ // }
+
}
if ((rc >= 0) && (entry)) {
celixThreadRwlock_writeLock(&handle->dbLock);
@@ -576,7 +576,7 @@ static inline int pubsub_tcpHandler_makeNonBlocking(pubsub_tcpHandler_t *handle,
else {
rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (rc < 0) {
- L_ERROR("[TCP Socket] Cannot set to NON_BLOCKING epoll: %s\n", strerror(errno));
+ L_ERROR("[TCP Socket] Cannot set to NON_BLOCKING: %s\n", strerror(errno));
}
}
return rc;
@@ -791,21 +791,20 @@ void pubsub_tcpHandler_setReceiveTimeOut(pubsub_tcpHandler_t *handle, double tim
}
static inline
-int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, unsigned int offset, unsigned int size, int flag ) {
- int expectedReadSize = size;
- int nbytes = size;
- int msgSize = 0;
+int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, int flag ) {
+ int nbytes = entry->expectedBufferReadSize;
char* buffer = (char*)_buffer;
- while (nbytes > 0 && expectedReadSize > 0) {
+ while (nbytes > 0 && entry->expectedBufferReadSize > 0) {
// Read the message header
- nbytes = recv(fd, &buffer[offset], expectedReadSize, flag | MSG_NOSIGNAL);
+ nbytes = recv(fd, &buffer[entry->bufferReadReadOffset], entry->expectedBufferReadSize, flag | MSG_NOSIGNAL);
// Update buffer administration
- offset += nbytes;
- expectedReadSize -= nbytes;
- msgSize += nbytes;
+ entry->bufferReadReadOffset += nbytes;
+ entry->expectedBufferReadSize-= nbytes;
}
- if (nbytes <=0) msgSize = nbytes;
- return msgSize;
+ if (nbytes == 0) {
+ L_WARN("[TCP Socket] Failed to receive message (fd: %d), error: %s. Retry count %u of %u,", entry->fd, strerror(errno));
+ }
+ return nbytes;
}
@@ -828,6 +827,88 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
}
}
+ static inline
+ void pubsub_tcpHandler_setReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry) {
+ entry->bufferReadReadOffset = 0;
+ if (entry->state == READ_STATE_SYNC) {
+ entry->expectedBufferReadSize = entry->headerSize;
+ entry->state = READ_STATE_HEADER;
+ } else if (entry->state == READ_STATE_HEADER) {
+ if (entry->header.header.payloadSize) {
+ entry->state = READ_STATE_PAYLOAD;
+ entry->bufferReadReadOffset = entry->header.header.payloadOffset;
+ entry->expectedBufferReadSize = entry->header.header.payloadSize;
+ // For header less messages adjust offset and msg size;
+ if (!entry->headerBufferSize) {
+ entry->bufferReadReadOffset += entry->headerSize;
+ entry->expectedBufferReadSize -= entry->headerSize;
+ }
+ } else if (entry->header.header.metadataSize) {
+ entry->state = READ_STATE_META;
+ entry->expectedBufferReadSize = entry->header.header.metadataSize;
+ } else if (!entry->header.header.payloadSize && !entry->header.header.metadataSize) {
+ if (entry->footerSize) {
+ entry->state = READ_STATE_FOOTER;
+ entry->expectedBufferReadSize = entry->footerSize;
+ } else if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ entry->expectedBufferReadSize = 0;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ }
+ } else if (entry->state == READ_STATE_PAYLOAD) {
+ if (entry->header.header.metadataSize) {
+ entry->state = READ_STATE_META;
+ entry->expectedBufferReadSize = entry->header.header.metadataSize;
+ } else {
+ if (entry->footerSize) {
+ entry->state = READ_STATE_FOOTER;
+ entry->expectedBufferReadSize = entry->footerSize;
+ } else if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ entry->expectedBufferReadSize = 0;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ }
+ } else if (entry->state == READ_STATE_META) {
+ if (entry->footerSize) {
+ entry->state = READ_STATE_FOOTER;
+ entry->expectedBufferReadSize = entry->footerSize;
+ } else if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ entry->expectedBufferReadSize = 0;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ } else if (entry->state == READ_STATE_FOOTER) {
+ if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ } else if (entry->state == READ_STATE_READY) {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+}
+static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int nextState){
+ entry->bufferReadReadOffset = 0;
+ if (nextState == READ_STATE_SYNC) {
+ entry->expectedBufferReadSize = entry->syncSize;
+ entry->state = nextState;
+ } else if (nextState == READ_STATE_HEADER) {
+ entry->expectedBufferReadSize = entry->headerSize;
+ entry->state = nextState;
+ }
+}
+
+
//
// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
@@ -858,35 +939,40 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
}
// Read the message
long int nbytes = 0;
- bool validMsg = false;
char* header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
+ if (entry->state == READ_STATE_SYNC) {
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
+ if (nbytes > 0) {
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
if (entry->state == READ_STATE_HEADER) {
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, MSG_PEEK);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, MSG_PEEK);
if (nbytes >= entry->headerSize) { // Check header message buffer
if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
// Did not receive correct header
// skip sync word and try to read next header
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->syncSize, 0);
if (!entry->headerError) {
L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
}
entry->headerError = true;
- entry->bufferReadSize = 0;
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_SYNC);
} else {
// Read header message from queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, 0);
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
if ((nbytes > 0) && (nbytes == entry->headerSize)) {
entry->headerError = false;
- entry->state == READ_STATE_DATA;
- // For headerless message, add header to bufferReadSize;
- if (!entry->headerBufferSize) entry->bufferReadSize += nbytes;
+ entry->msgSizeReadSize = 0;
+ // For headerless message, add header to bufferReadSize;
+ if (!entry->headerBufferSize) entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
}
}
- if (nentry->state == READ_STATE_DATA) {
-
+ if (entry->state == READ_STATE_PAYLOAD) {
// Alloc message buffers
if (entry->header.header.payloadSize > entry->bufferSize) {
handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize);
@@ -896,6 +982,17 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
entry->bufferSize = handle->bufferSize;
}
+ // Read payload data from queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, 0);
+ if (nbytes > 0) {
+ if (nbytes >= entry->header.header.payloadPartSize) {
+ entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+ }
+ }
+
+ if (entry->state == READ_STATE_META) {
if (entry->header.header.metadataSize > entry->metaBufferSize) {
if (entry->metaBuffer) {
free(entry->metaBuffer);
@@ -906,64 +1003,38 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
}
}
- if (entry->header.header.payloadSize) {
- unsigned int offset = entry->header.header.payloadOffset;
- unsigned int size = entry->header.header.payloadPartSize;
- // For header less messages adjust offset and msg size;
- if (!entry->headerBufferSize) {
- offset += entry->headerSize;
- size -= offset;
- }
- // Read payload data from queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, offset, size, 0);
- if (nbytes > 0) {
- if (nbytes == size) {
- entry->bufferReadSize += nbytes;
- } else {
- entry->bufferReadSize = 0;
- L_ERROR("[TCP Socket] Failed to receive complete payload buffer (fd: %d) nbytes : %d = msgSize %d", entry->fd, nbytes, size);
- }
- }
- }
- if (nbytes > 0 && entry->header.header.metadataSize) {
- // Read meta data from queue
- unsigned int size = entry->header.header.metadataSize;
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0, size,0);
- if ((nbytes > 0) && (nbytes != size)) {
- L_ERROR("[TCP Socket] Failed to receive complete payload buffer (fd: %d) nbytes : %d = msgSize %d", entry->fd, nbytes, size);
- }
- }
- // Check for end of message using, footer of message. Because of streaming protocol
- if (nbytes > 0) {
- if (entry->footerSize > 0) {
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer,0, entry->footerSize,0);
- if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
- // valid footer, this means that the message is valid
- validMsg = true;
- } else {
- // Did not receive correct header
- L_ERROR("[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)", entry->header.header.seqNr, entry->fd, entry->url);
- entry->bufferReadSize = 0;
- }
- } else {
- // No Footer, then complete message is received
- validMsg = true;
- }
- }
- }
+ // Read meta data from (queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0);
+ if ((nbytes > 0) && (nbytes >= entry->header.header.metadataSize)) {
+ entry->msgSizeReadSize += nbytes;
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
+ if (entry->state == READ_STATE_FOOTER) {
+ // Check for end of message using, footer of message. Because of streaming protocol
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer, 0);
+ if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
+ // valid footer, this means that the message is valid
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ } else {
+ // Did not receive correct header
+ L_ERROR("[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)", entry->header.header.seqNr, entry->fd, entry->url);
+ pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
+ }
+ }
+ if (entry->state == READ_STATE_READY) {
+ // Complete message is received
+ pubsub_tcpHandler_decodePayload(handle, entry);
+ pubsub_tcpHandler_setReadStateMachine(handle, entry);
+ }
+
if (nbytes > 0) {
entry->retryCount = 0;
- // Check if complete message is received
- if ((entry->bufferReadSize >= entry->header.header.payloadSize) &&
- validMsg &&
- entry->header.header.isLastSegment) {
- entry->bufferReadSize = 0;
- pubsub_tcpHandler_decodePayload(handle, entry);
- }
- } else {
- if (entry->retryCount < handle->maxRcvRetryCount) {
+ } else if (nbytes < 0) {
+ if ((errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) {
+ // Non blocking interrupt
+ entry->retryCount = 0;
+ } else if (entry->retryCount < handle->maxRcvRetryCount) {
entry->retryCount++;
L_WARN("[TCP Socket] Failed to receive message (fd: %d), error: %s. Retry count %u of %u,", entry->fd,
strerror(errno), entry->retryCount, handle->maxRcvRetryCount);
@@ -1143,7 +1214,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[msgIovLen + i].iov_len;
if ((msgPartSize + msg.msg_iov[msg.msg_iovlen].iov_len) > entry->maxMsgSize)
break;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
}
message->header.payloadPartSize = msgPartSize;
message->header.payloadOffset = msgSize;
@@ -1171,7 +1242,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
msg.msg_iovlen++;
msg.msg_iov[msg.msg_iovlen].iov_base = footerData;
msg.msg_iov[msg.msg_iovlen].iov_len = footerDataSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
}
void *headerData = NULL;
@@ -1196,7 +1267,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
L_ERROR("[TCP Socket] No header buffer is generated");
msg.msg_iovlen = 0;
}
- nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgSize, flags);
+ nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgPartSize, flags);
// When a specific socket keeps reporting errors can indicate a subscriber
// which is not active anymore, the connection will remain until the retry
// counter exceeds the maximum retry count.
@@ -1215,7 +1286,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
result = -1; //At least one connection failed sending
} else if (msgPartSize) {
entry->retryCount = 0;
- if (nbytes != msgSize) {
+ if (nbytes != msgPartSize) {
L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
}
}
[celix] 02/11: Update for non blocking
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 3eb8877ca9b20d8867cfc5f19fe3c17535591d82
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Sun Jun 28 08:06:27 2020 +0200
Update for non blocking
---
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 88 +++++++++++++---------
1 file changed, 52 insertions(+), 36 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 9ef6361..19cb0fd 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -53,6 +53,12 @@
#define MAX_EVENTS 64
#define MAX_DEFAULT_BUFFER_SIZE 4u
+#define READ_STATE_INIT 0u
+#define READ_STATE_HEADER 1u
+#define READ_STATE_DATA 2u
+#define READ_STATE_READY 3u
+#define READ_STATE_FIND_HEADER 4u
+
#if defined(__APPLE__)
#define MSG_NOSIGNAL (0)
#endif
@@ -77,6 +83,7 @@ typedef struct psa_tcp_connection_entry {
socklen_t len;
bool connected;
bool headerError;
+ unsigned int state;
pubsub_protocol_message_t header;
unsigned int maxMsgSize;
unsigned int syncSize;
@@ -850,45 +857,54 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
entry->bufferSize = handle->bufferSize;
}
// Read the message
+ long int nbytes = 0;
bool validMsg = false;
char* header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
- int nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, MSG_PEEK);
- if (nbytes > 0) {
- // Check header message buffer
- if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
- // Did not receive correct header
- // skip sync word and try to read next header
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->syncSize, 0);
- if (!entry->headerError) {
- L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
- }
- entry->headerError = true;
- entry->bufferReadSize = 0;
- } else {
- // Read header message from queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, 0);
- if ((nbytes > 0) && (nbytes == entry->headerSize)) {
- entry->headerError = false;
+ if (entry->state == READ_STATE_HEADER) {
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, MSG_PEEK);
+ if (nbytes >= entry->headerSize) { // Check header message buffer
+ if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
+ // Did not receive correct header
+ // skip sync word and try to read next header
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->syncSize, 0);
+ if (!entry->headerError) {
+ L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
+ }
+ entry->headerError = true;
+ entry->bufferReadSize = 0;
+ } else {
+ // Read header message from queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, 0);
+ if ((nbytes > 0) && (nbytes == entry->headerSize)) {
+ entry->headerError = false;
+ entry->state == READ_STATE_DATA;
// For headerless message, add header to bufferReadSize;
- if (!entry->headerBufferSize)
- entry->bufferReadSize += nbytes;
- // Alloc message buffers
- if (entry->header.header.payloadSize > entry->bufferSize) {
- handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize);
- if (entry->buffer)
- free(entry->buffer);
- entry->buffer = malloc((size_t) handle->bufferSize);
- entry->bufferSize = handle->bufferSize;
- }
- if (entry->header.header.metadataSize > entry->metaBufferSize) {
- if (entry->metaBuffer) {
- free(entry->metaBuffer);
- entry->metaBuffer = malloc((size_t) entry->header.header.metadataSize);
- entry->bufferSize = handle->bufferSize;
- L_WARN("[TCP Socket] socket: %d, url: %s, realloc read meta buffer: (%d, %d) \n", entry->fd,
- entry->url, entry->metaBufferSize, entry->header.header.metadataSize);
- }
- }
+ if (!entry->headerBufferSize) entry->bufferReadSize += nbytes;
+ }
+ }
+ }
+ }
+
+ if (nentry->state == READ_STATE_DATA) {
+
+ // Alloc message buffers
+ if (entry->header.header.payloadSize > entry->bufferSize) {
+ handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize);
+ if (entry->buffer)
+ free(entry->buffer);
+ entry->buffer = malloc((size_t) handle->bufferSize);
+ entry->bufferSize = handle->bufferSize;
+ }
+
+ if (entry->header.header.metadataSize > entry->metaBufferSize) {
+ if (entry->metaBuffer) {
+ free(entry->metaBuffer);
+ entry->metaBuffer = malloc((size_t) entry->header.header.metadataSize);
+ entry->metaBufferSize = entry->header.header.metadataSize;
+ L_WARN("[TCP Socket] socket: %d, url: %s, realloc read meta buffer: (%d, %d) \n", entry->fd,
+ entry->url, entry->metaBufferSize, entry->header.header.metadataSize);
+ }
+ }
if (entry->header.header.payloadSize) {
unsigned int offset = entry->header.header.payloadOffset;
[celix] 08/11: Merge branch 'master' into
feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit a35435e8df3347151aa9db6b1cd2eb6731ff1a1c
Merge: dbf6bdc 3be06db
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Aug 3 12:43:47 2020 +0200
Merge branch 'master' into feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
# Conflicts:
# bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
.github/workflows/coverity-scan.yml | 57 +++
CHANGES.md | 4 +
bundles/deployment_admin/README.md | 4 +
bundles/device_access/README.md | 4 +
.../device_access/example/base_driver/README.md | 4 +
.../example/consuming_driver/README.md | 4 +
.../example/refining_driver/README.md | 4 +
bundles/http_admin/README.md | 4 +
bundles/http_admin/civetweb/src/civetweb.c | 2 +-
bundles/logging/README.md | 4 +
bundles/logging/log_writers/README.md | 4 +
bundles/pubsub/README.md | 4 +
bundles/pubsub/examples/keys/README.md | 4 +
.../pubsub/pubsub_admin_tcp/src/pubsub_tcp_admin.c | 16 +-
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 251 ++++--------
.../src/pubsub_tcp_topic_receiver.c | 3 +
bundles/pubsub/pubsub_admin_udp_mc/README.md | 4 +
.../pubsub_admin_udp_mc/src/pubsub_udpmc_admin.c | 15 +-
.../src/pubsub_udpmc_topic_receiver.c | 5 +-
.../src/pubsub_websocket_admin.c | 15 +-
.../src/pubsub_websocket_topic_receiver.c | 3 +
.../pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c | 35 +-
.../src/pubsub_zmq_topic_receiver.c | 7 +-
.../pubsub_admin_zmq/src/pubsub_zmq_topic_sender.c | 118 +++---
.../pubsub_protocol_lib/CMakeLists.txt | 8 +-
.../pubsub_protocol_lib/gtest/CMakeLists.txt} | 15 +-
.../gtest/src/PS_WP_common_tests.cc | 57 +++
.../pubsub_protocol_lib/gtest/src/main.cc | 26 ++
.../src/pubsub_wire_protocol_common.c | 93 +++--
bundles/pubsub/pubsub_spi/CMakeLists.txt | 7 +-
.../gtest/CMakeLists.txt} | 12 +-
.../gtest/src/PubSubEndpointUtilsTestSuite.cc | 47 +++
.../pubsub/pubsub_spi/include/pubsub_endpoint.h | 16 +-
bundles/pubsub/pubsub_spi/src/pubsub_endpoint.c | 3 +-
.../pubsub/pubsub_spi/src/pubsub_endpoint_match.c | 11 +
.../src/pubsub_topology_manager.c | 452 +++++++++++++--------
.../src/pubsub_topology_manager.h | 16 +-
bundles/pubsub/test/CMakeLists.txt | 142 ++++++-
.../{ping.properties => deadlock.scope.properties} | 6 +-
...{ping.properties => deadlock.scope2.properties} | 6 +-
bundles/pubsub/test/meta_data/ping.properties | 2 +-
bundles/pubsub/test/meta_data/ping2.properties | 2 +-
bundles/pubsub/test/meta_data/pong2.properties | 2 +-
.../pubsub/test/pstm_deadlock_test/test_runner.cc | 142 +++++++
bundles/pubsub/test/test/test_endpoint_runner.cc | 4 +-
bundles/pubsub/test/test/test_runner.cc | 92 ++---
bundles/remote_services/README.md | 6 +-
bundles/remote_services/discovery_etcd/README.md | 4 +
.../remote_service_admin_dfi/README.md | 4 +
.../remote_services/remote_services_api/README.md | 4 +
bundles/remote_services/rsa_spi/README.md | 4 +
bundles/remote_services/topology_manager/README.md | 4 +
bundles/shell/remote_shell/README.md | 4 +
bundles/shell/shell/README.md | 4 +
bundles/shell/shell_tui/README.md | 4 +
bundles/shell/shell_wui/README.md | 4 +
documents/building/README.md | 4 +
documents/cmake_commands/README.md | 4 +
documents/getting_started/README.md | 4 +
.../getting_started/creating_a_simple_bundle.md | 6 +-
documents/getting_started/using_services_with_c.md | 4 +
.../getting_started/using_services_with_cxx.md | 4 +
documents/intro/README.md | 6 +-
documents/subprojects/README.md | 6 +
examples/celix-examples/README.md | 4 +
examples/celix-examples/http_example/README.md | 7 +-
.../celix-examples/services_example_c/README.md | 4 +
libs/dependency_manager/README.md | 4 +
libs/dependency_manager/TODO.md | 6 +-
libs/dependency_manager_cxx/README.md | 4 +
libs/dependency_manager_cxx/TODO.md | 6 +-
libs/etcdlib/README.md | 4 +
libs/framework/src/dm_dependency_manager_impl.c | 44 +-
libs/launcher/README.md | 4 +
libs/utils/README.md | 4 +
libs/utils/gtest/src/LogUtilsTestSuite.cc | 2 -
misc/experimental/README.md | 4 +
misc/experimental/bundles/config_admin/README.md | 4 +
misc/experimental/promise/README.md | 4 +
79 files changed, 1351 insertions(+), 570 deletions(-)
diff --cc bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 20eb401,0cbd28f..4734497
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@@ -95,13 -86,9 +93,11 @@@ typedef struct psa_tcp_connection_entr
void *footerBuffer;
unsigned int bufferSize;
void *buffer;
- unsigned int bufferReadReadOffset;
- unsigned int expectedBufferReadSize;
- unsigned int msgSizeReadSize;
+ unsigned int bufferReadSize;
unsigned int metaBufferSize;
void *metaBuffer;
+ struct msghdr msg;
+ size_t msg_iovlen; /* Number of elements in the vector. */
unsigned int retryCount;
} psa_tcp_connection_entry_t;
@@@ -343,13 -338,6 +348,9 @@@ pubsub_tcpHandler_createEntry(pubsub_tc
}
if (entry->footerSize) entry->footerBuffer = calloc(sizeof(char), entry->footerSize);
if (entry->bufferSize) entry->buffer = calloc(sizeof(char), entry->bufferSize);
+ entry->msg.msg_iov[entry->msg.msg_iovlen].iov_base = entry->buffer;
+ entry->msg.msg_iov[entry->msg.msg_iovlen].iov_len = entry->bufferSize;
+ entry->msg_iovlen++;
- pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
}
return entry;
}
@@@ -652,25 -628,16 +641,24 @@@ int pubsub_tcpHandler_listen(pubsub_tcp
}
//
-// Setup buffer sizes
+// Setup receive buffer size
//
-int pubsub_tcpHandler_createReceiveBufferStore(pubsub_tcpHandler_t *handle,
- unsigned int maxNofBuffers
- __attribute__((__unused__)),
- unsigned int bufferSize) {
+int pubsub_tcpHandler_setReceiveBufferSize(pubsub_tcpHandler_t *handle, unsigned int size) {
if (handle != NULL) {
celixThreadRwlock_writeLock(&handle->dbLock);
- handle->bufferSize = bufferSize;
- handle->maxNofBuffer = maxNofBuffers;
+ handle->bufferSize = size;
+ celixThreadRwlock_unlock(&handle->dbLock);
+ }
+ return 0;
+}
+
+//
+// Setup receive buffer size
+//
+int pubsub_tcpHandler_setMaxMsgSize(pubsub_tcpHandler_t *handle, unsigned int size) {
+ if (handle != NULL) {
+ celixThreadRwlock_writeLock(&handle->dbLock);
+ handle->maxMsgSize = size;
- handle->maxMsgSize = 4;
celixThreadRwlock_unlock(&handle->dbLock);
}
return 0;
@@@ -809,11 -775,12 +795,12 @@@ static inlin
void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry) {
if (entry->header.header.payloadSize > 0) {
-- handle->protocol->decodePayload(handle->protocol->handle, entry->buffer, entry->header.header.payloadSize, &entry->header);
++ handle->protocol->decodePayload(handle->protocol->handle, entry->buffer, entry->header.header.payloadSize, &entry->header);
}
if (entry->header.header.metadataSize > 0) {
-- handle->protocol->decodeMetadata(handle->protocol->handle, entry->metaBuffer,
++ handle->protocol->decodeMetadata(handle->protocol->handle, entry->metaBuffer,
entry->header.header.metadataSize, &entry->header);
- entry->metaBufferSize = entry->header.header.metadataSize;
++ entry->metaBufferSize = entry->header.header.metadataSize;
}
if (handle->processMessageCallback && entry->header.payload.payload != NULL && entry->header.payload.length) {
struct timespec receiveTime;
@@@ -1173,136 -1048,113 +1077,135 @@@ int pubsub_tcpHandler_write(pubsub_tcpH
handle->protocol->encodeFooter(handle->protocol->handle, message,
&footerData,
&footerDataSize);
- entry->footerSize = footerDataSize;
}
+
size_t msgSize = 0;
- struct msghdr msg;
- struct iovec msg_iov[IOV_MAX];
- memset(&msg, 0x00, sizeof(struct msghdr));
- msg.msg_name = &entry->addr;
- msg.msg_namelen = entry->len;
- msg.msg_flags = flags;
- msg.msg_iov = msg_iov;
-
- // Write generic seralized payload in vector buffer
- if (payloadSize && payloadData) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = payloadData;
- msg.msg_iov[msg.msg_iovlen].iov_len = payloadSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- } else {
- // copy serialized vector into vector buffer
- for (size_t i = 0; i < MIN(msg_iov_len, IOV_MAX - 2); i++) {
+ size_t msgIovLen = 0;
+ long int nbytes = UINT32_MAX;
+ while (msgSize < totalMessageSize && nbytes > 0) {
+ struct msghdr msg;
+ struct iovec msg_iov[IOV_MAX];
+ memset(&msg, 0x00, sizeof(struct msghdr));
+ msg.msg_name = &entry->addr;
+ msg.msg_namelen = entry->len;
+ msg.msg_flags = flags;
+ msg.msg_iov = msg_iov;
+ size_t msgPartSize = 0;
+ message->header.payloadPartSize = 0;
+ message->header.payloadOffset = 0;
+ message->header.metadataSize = 0;
+ message->header.isLastSegment = 0;
+
+ // Write generic seralized payload in vector buffer
+ if (payloadSize && payloadData) {
+ char *payloadDataBuffer = payloadData;
msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = msgIoVec[i].iov_base;
- msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[i].iov_len;
+ msg.msg_iov[msg.msg_iovlen].iov_base = &payloadDataBuffer[msgSize];
+ msg.msg_iov[msg.msg_iovlen].iov_len = MIN((payloadSize - msgSize), entry->maxMsgSize);
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ message->header.payloadPartSize = msgPartSize;
+ message->header.payloadOffset = msgSize;
msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ } else {
+ // copy serialized vector into vector buffer
+ for (size_t i = 0; i < MIN(msg_iov_len, max_msg_iov_len); i++) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = msgIoVec[msgIovLen + i].iov_base;
+ msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[msgIovLen + i].iov_len;
+ if ((msgPartSize + msg.msg_iov[msg.msg_iovlen].iov_len) > entry->maxMsgSize)
+ break;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ }
+ message->header.payloadPartSize = msgPartSize;
+ message->header.payloadOffset = msgSize;
+ msgSize += msgPartSize;
+ msgIovLen += (msg.msg_iovlen - 1);
}
- }
- // Write optional metadata in vector buffer
- if (metadataSize && metadataData) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = metadataData;
- msg.msg_iov[msg.msg_iovlen].iov_len = metadataSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- }
+ // Write optional metadata in vector buffer
+ if ((msgSize < (payloadSize + metadataSize)) &&
+ (msgPartSize < entry->maxMsgSize) &&
+ (metadataSize && metadataData)) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = metadataData;
+ msg.msg_iov[msg.msg_iovlen].iov_len = metadataSize;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ message->header.metadataSize = metadataSize;
+ msgSize += metadataSize;
+ }
+ if (msgSize >= totalMessageSize) {
+ message->header.isLastSegment = 0x1;
+ }
- // Write optional footerData in vector buffer
- if (footerData && footerDataSize) {
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = footerData;
- msg.msg_iov[msg.msg_iovlen].iov_len = footerDataSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- }
+ // Write optional footerData in vector buffer
+ if (footerData && footerDataSize) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = footerData;
+ msg.msg_iov[msg.msg_iovlen].iov_len = footerDataSize;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ }
- void *headerData = NULL;
- size_t headerSize = 0;
- // check if header is not part of the payload (=> headerBufferSize = 0)s
- if (entry->headerBufferSize) {
- // Encode the header, with payload size and metadata size
- handle->protocol->encodeHeader(handle->protocol->handle, message,
- &headerData,
- &headerSize);
- }
- if (!entry->headerBufferSize) {
- // Skip header buffer, when header is part of payload;
- msg.msg_iov = &msg_iov[1];
- } else if (headerSize && headerData) {
- // Write header in 1st vector buffer item
- msg.msg_iov[0].iov_base = headerData;
- msg.msg_iov[0].iov_len = headerSize;
- msgPartSize += msg.msg_iov[0].iov_len;
- msg.msg_iovlen++;
+ void *headerData = NULL;
+ size_t headerSize = 0;
+ // check if header is not part of the payload (=> headerBufferSize = 0)s
+ if (entry->headerBufferSize) {
- headerData = entry->headerBuffer;
+ // Encode the header, with payload size and metadata size
+ handle->protocol->encodeHeader(handle->protocol->handle, message,
+ &headerData,
+ &headerSize);
- entry->headerBufferSize = headerSize;
+ }
+ if (!entry->headerBufferSize) {
+ // Skip header buffer, when header is part of payload;
+ msg.msg_iov = &msg_iov[1];
+ } else if (headerSize && headerData) {
+ // Write header in 1st vector buffer item
+ msg.msg_iov[0].iov_base = headerData;
+ msg.msg_iov[0].iov_len = headerSize;
+ msgSize += msg.msg_iov[0].iov_len;
+ msg.msg_iovlen++;
+ } else {
+ L_ERROR("[TCP Socket] No header buffer is generated");
+ msg.msg_iovlen = 0;
+ }
+ long int nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgSize, flags);
+ // When a specific socket keeps reporting errors can indicate a subscriber
+ // which is not active anymore, the connection will remain until the retry
+ // counter exceeds the maximum retry count.
+ // Btw, also, SIGSTOP issued by a debugging tool can result in EINTR error.
+ if (nbytes == -1) {
+ if (entry->retryCount < handle->maxSendRetryCount) {
+ entry->retryCount++;
+ L_ERROR(
+ "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
+ entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
} else {
- L_ERROR("[TCP Socket] No header buffer is generated");
- msg.msg_iovlen = 0;
- }
- nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgPartSize, flags);
- // When a specific socket keeps reporting errors can indicate a subscriber
- // which is not active anymore, the connection will remain until the retry
- // counter exceeds the maximum retry count.
- // Btw, also, SIGSTOP issued by a debugging tool can result in EINTR error.
- if (nbytes == -1) {
- if (entry->retryCount < handle->maxSendRetryCount) {
- entry->retryCount++;
- L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
- entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
- } else {
- L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s", entry->fd, handle->maxSendRetryCount, strerror(errno));
- connFdCloseQueue[nofConnToClose++] = entry->fd;
- }
- result = -1; //At least one connection failed sending
- } else if (msgPartSize) {
- entry->retryCount = 0;
- if (nbytes != msgPartSize) {
- L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
- }
- }
- // Release data
- if (headerData) {
- free(headerData);
- }
- // Note: serialized Payload is deleted by serializer
- if (payloadData && (payloadData != message->payload.payload)) {
- free(payloadData);
+ L_ERROR(
+ "[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s",
+ entry->fd, handle->maxSendRetryCount, strerror(errno));
+ connFdCloseQueue[nofConnToClose++] = entry->fd;
}
- if (metadataData) {
- free(metadataData);
- }
- if (footerData) {
- free(footerData);
+ result = -1; //At least one connection failed sending
+ } else if (msgSize) {
+ entry->retryCount = 0;
+ if (nbytes != msgSize) {
+ L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
}
}
-
+ // Release data
- if (headerData && headerData != entry->headerBuffer) {
++ if (headerData) {
+ free(headerData);
+ }
+ // Note: serialized Payload is deleted by serializer
+ if (payloadData && (payloadData != message->payload.payload)) {
+ free(payloadData);
+ }
- if (metadataData && metadataData != entry->metaBuffer) {
++ if (metadataData) {
+ free(metadataData);
+ }
- if (footerData && footerData != entry->footerBuffer) {
++ if (footerData) {
+ free(footerData);
+ }
}
}
celixThreadRwlock_unlock(&handle->dbLock);
[celix] 09/11: Fix buffers
Posted by rb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rbulter pushed a commit to branch feature/add_msg_segemenation_to_tcp_admin_with_wire_v2_add_make_non_blocking_v2
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 75794733c84213d2a1c9eb63182bdb79a848db47
Author: Roy Bulter <ro...@gmail.com>
AuthorDate: Mon Aug 3 13:50:35 2020 +0200
Fix buffers
---
.../pubsub_admin_tcp/src/pubsub_tcp_handler.c | 458 ++++++++++++++++-----
1 file changed, 352 insertions(+), 106 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
index 4734497..aae0406 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_handler.c
@@ -84,7 +84,9 @@ typedef struct psa_tcp_connection_entry {
socklen_t len;
bool connected;
bool headerError;
+ unsigned int state;
pubsub_protocol_message_t header;
+ unsigned int maxMsgSize;
unsigned int syncSize;
unsigned int headerSize;
unsigned int headerBufferSize; // Size of headerBuffer, size = 0, no headerBuffer -> included in payload
@@ -94,10 +96,11 @@ typedef struct psa_tcp_connection_entry {
unsigned int bufferSize;
void *buffer;
unsigned int bufferReadSize;
+ unsigned int bufferReadReadOffset;
+ unsigned int expectedBufferReadSize;
+ unsigned int msgSizeReadSize;
unsigned int metaBufferSize;
void *metaBuffer;
- struct msghdr msg;
- size_t msg_iovlen; /* Number of elements in the vector. */
unsigned int retryCount;
} psa_tcp_connection_entry_t;
@@ -124,6 +127,7 @@ struct pubsub_tcpHandler {
pubsub_protocol_service_t *protocol;
unsigned int bufferSize;
unsigned int maxNofBuffer;
+ unsigned int maxMsgSize;
unsigned int maxSendRetryCount;
unsigned int maxRcvRetryCount;
double sendTimeout;
@@ -132,29 +136,19 @@ struct pubsub_tcpHandler {
bool running;
};
-static inline int
-pubsub_tcpHandler_closeConnectionEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, bool lock);
-
+static inline int pubsub_tcpHandler_closeConnectionEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, bool lock);
static inline int pubsub_tcpHandler_closeInterfaceEntry(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
-
static inline int pubsub_tcpHandler_makeNonBlocking(pubsub_tcpHandler_t *handle, int fd);
-
-static inline psa_tcp_connection_entry_t *
-pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, char *external_url,
- struct sockaddr_in *addr);
-
+static inline psa_tcp_connection_entry_t* pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, char *external_url, struct sockaddr_in *addr);
static inline void pubsub_tcpHandler_freeEntry(psa_tcp_connection_entry_t *entry);
-
static inline void pubsub_tcpHandler_releaseEntryBuffer(pubsub_tcpHandler_t *handle, int fd, unsigned int index);
-
static inline int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, unsigned int offset, unsigned int size, int flag );
-
+static inline int pubsub_tcpHandler_readSocket_(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* buffer, int flag );
+static inline void pubsub_tcpHandler_setReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
+static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int nextState);
static inline void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry);
-
static inline void pubsub_tcpHandler_connectionHandler(pubsub_tcpHandler_t *handle, int fd);
-
static inline void pubsub_tcpHandler_handler(pubsub_tcpHandler_t *handle);
-
static void *pubsub_tcpHandler_thread(void *data);
//
@@ -348,9 +342,6 @@ pubsub_tcpHandler_createEntry(pubsub_tcpHandler_t *handle, int fd, char *url, ch
}
if (entry->footerSize) entry->footerBuffer = calloc(sizeof(char), entry->footerSize);
if (entry->bufferSize) entry->buffer = calloc(sizeof(char), entry->bufferSize);
- entry->msg.msg_iov[entry->msg.msg_iovlen].iov_base = entry->buffer;
- entry->msg.msg_iov[entry->msg.msg_iovlen].iov_len = entry->bufferSize;
- entry->msg_iovlen++;
}
return entry;
}
@@ -458,6 +449,13 @@ int pubsub_tcpHandler_connect(pubsub_tcpHandler_t *handle, char *url) {
L_ERROR("[TCP Socket] Cannot create poll event %s\n", strerror(errno));
entry = NULL;
}
+ //rc = pubsub_tcpHandler_makeNonBlocking(handle, entry->fd);
+ //if (rc < 0) {
+ // pubsub_tcpHandler_freeEntry(entry);
+ // L_ERROR("[TCP Socket] Cannot make not blocking %s\n", strerror(errno));
+ // entry = NULL;
+ // }
+
}
if ((rc >= 0) && (entry)) {
celixThreadRwlock_writeLock(&handle->dbLock);
@@ -565,7 +563,7 @@ static inline int pubsub_tcpHandler_makeNonBlocking(pubsub_tcpHandler_t *handle,
else {
rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (rc < 0) {
- L_ERROR("[TCP Socket] Cannot set to NON_BLOCKING epoll: %s\n", strerror(errno));
+ L_ERROR("[TCP Socket] Cannot set to NON_BLOCKING: %s\n", strerror(errno));
}
}
return rc;
@@ -774,7 +772,25 @@ void pubsub_tcpHandler_setReceiveTimeOut(pubsub_tcpHandler_t *handle, double tim
}
static inline
-int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, int flag ) {
+int pubsub_tcpHandler_readSocket(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, unsigned int offset, unsigned int size, int flag ) {
+ int expectedReadSize = size;
+ int nbytes = size;
+ int msgSize = 0;
+ char* buffer = (char*)_buffer;
+ while (nbytes > 0 && expectedReadSize > 0) {
+ // Read the message header
+ nbytes = recv(fd, &buffer[offset], expectedReadSize, flag | MSG_NOSIGNAL);
+ // Update buffer administration
+ offset += nbytes;
+ expectedReadSize -= nbytes;
+ msgSize += nbytes;
+ }
+ if (nbytes <=0) msgSize = nbytes;
+ return msgSize;
+}
+
+static inline
+int pubsub_tcpHandler_readSocket_(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int fd, void* _buffer, int flag ) {
int nbytes = entry->expectedBufferReadSize;
char* buffer = (char*)_buffer;
while (nbytes > 0 && entry->expectedBufferReadSize > 0) {
@@ -811,6 +827,88 @@ void pubsub_tcpHandler_decodePayload(pubsub_tcpHandler_t *handle, psa_tcp_connec
}
}
+ static inline
+ void pubsub_tcpHandler_setReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry) {
+ entry->bufferReadReadOffset = 0;
+ if (entry->state == READ_STATE_SYNC) {
+ entry->expectedBufferReadSize = entry->headerSize;
+ entry->state = READ_STATE_HEADER;
+ } else if (entry->state == READ_STATE_HEADER) {
+ if (entry->header.header.payloadSize) {
+ entry->state = READ_STATE_PAYLOAD;
+ entry->bufferReadReadOffset = entry->header.header.payloadOffset;
+ entry->expectedBufferReadSize = entry->header.header.payloadSize;
+ // For header less messages adjust offset and msg size;
+ if (!entry->headerBufferSize) {
+ entry->bufferReadReadOffset += entry->headerSize;
+ entry->expectedBufferReadSize -= entry->headerSize;
+ }
+ } else if (entry->header.header.metadataSize) {
+ entry->state = READ_STATE_META;
+ entry->expectedBufferReadSize = entry->header.header.metadataSize;
+ } else if (!entry->header.header.payloadSize && !entry->header.header.metadataSize) {
+ if (entry->footerSize) {
+ entry->state = READ_STATE_FOOTER;
+ entry->expectedBufferReadSize = entry->footerSize;
+ } else if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ entry->expectedBufferReadSize = 0;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ }
+ } else if (entry->state == READ_STATE_PAYLOAD) {
+ if (entry->header.header.metadataSize) {
+ entry->state = READ_STATE_META;
+ entry->expectedBufferReadSize = entry->header.header.metadataSize;
+ } else {
+ if (entry->footerSize) {
+ entry->state = READ_STATE_FOOTER;
+ entry->expectedBufferReadSize = entry->footerSize;
+ } else if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ entry->expectedBufferReadSize = 0;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ }
+ } else if (entry->state == READ_STATE_META) {
+ if (entry->footerSize) {
+ entry->state = READ_STATE_FOOTER;
+ entry->expectedBufferReadSize = entry->footerSize;
+ } else if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ entry->expectedBufferReadSize = 0;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ } else if (entry->state == READ_STATE_FOOTER) {
+ if (entry->header.header.isLastSegment) {
+ entry->state = READ_STATE_READY;
+ } else {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+ } else if (entry->state == READ_STATE_READY) {
+ entry->state = READ_STATE_HEADER;
+ entry->expectedBufferReadSize = entry->headerSize;
+ }
+}
+static inline void pubsub_tcpHandler_setNextStateReadStateMachine(pubsub_tcpHandler_t *handle, psa_tcp_connection_entry_t *entry, int nextState){
+ entry->bufferReadReadOffset = 0;
+ if (nextState == READ_STATE_SYNC) {
+ entry->expectedBufferReadSize = entry->syncSize;
+ entry->state = nextState;
+ } else if (nextState == READ_STATE_HEADER) {
+ entry->expectedBufferReadSize = entry->headerSize;
+ entry->state = nextState;
+ }
+}
+
+
//
// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
@@ -836,6 +934,144 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
if ((!entry->headerBufferSize) && (entry->headerSize > entry->bufferSize)) {
handle->bufferSize = MAX(handle->bufferSize, entry->headerSize );
if (entry->buffer) free(entry->buffer);
+ entry->buffer = malloc((size_t) handle->bufferSize);
+ entry->bufferSize = handle->bufferSize;
+ }
+ // Read the message
+ bool validMsg = false;
+ char* header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
+ int nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, MSG_PEEK);
+ if (nbytes > 0) {
+ // Check header message buffer
+ if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
+ // Did not receive correct header
+ // skip sync word and try to read next header
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->syncSize, 0);
+ if (!entry->headerError) {
+ L_WARN("[TCP Socket] Failed to decode message header (fd: %d) (url: %s)", entry->fd, entry->url);
+ }
+ entry->headerError = true;
+ entry->bufferReadSize = 0;
+ } else {
+ // Read header message from queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0, entry->headerSize, 0);
+ if ((nbytes > 0) && (nbytes == entry->headerSize)) {
+ entry->headerError = false;
+ // For headerless message, add header to bufferReadSize;
+ if (!entry->headerBufferSize)
+ entry->bufferReadSize += nbytes;
+ // Alloc message buffers
+ if (entry->header.header.payloadSize > entry->bufferSize) {
+ handle->bufferSize = MAX(handle->bufferSize, entry->header.header.payloadSize);
+ if (entry->buffer)
+ free(entry->buffer);
+ entry->buffer = malloc((size_t) handle->bufferSize);
+ entry->bufferSize = handle->bufferSize;
+ }
+ if (entry->header.header.metadataSize > entry->metaBufferSize) {
+ if (entry->metaBuffer) {
+ free(entry->metaBuffer);
+ }
+ entry->metaBuffer = malloc((size_t) entry->header.header.metadataSize);
+ entry->metaBufferSize = entry->header.header.metadataSize;
+ L_WARN("[TCP Socket] socket: %d, url: %s, realloc read meta buffer: (%d, %d) \n", entry->fd,
+ entry->url, entry->metaBufferSize, entry->header.header.metadataSize);
+ }
+
+ if (entry->header.header.payloadSize) {
+ unsigned int offset = entry->header.header.payloadOffset;
+ unsigned int size = entry->header.header.payloadPartSize;
+ // For header less messages adjust offset and msg size;
+ if (!entry->headerBufferSize) {
+ offset = entry->headerSize;
+ size -= offset;
+ }
+ // Read payload data from queue
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, offset, size, 0);
+ if (nbytes > 0) {
+ if (nbytes == size) {
+ entry->bufferReadSize += nbytes;
+ } else {
+ entry->bufferReadSize = 0;
+ L_ERROR("[TCP Socket] Failed to receive complete payload buffer (fd: %d) nbytes : %d = msgSize %d", entry->fd, nbytes, size);
+ }
+ }
+ }
+ if (nbytes > 0 && entry->header.header.metadataSize) {
+ // Read meta data from queue
+ unsigned int size = entry->header.header.metadataSize;
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0, size,0);
+ if ((nbytes > 0) && (nbytes != size)) {
+ L_ERROR("[TCP Socket] Failed to receive complete payload buffer (fd: %d) nbytes : %d = msgSize %d", entry->fd, nbytes, size);
+ }
+ }
+ // Check for end of message using, footer of message. Because of streaming protocol
+ if (nbytes > 0) {
+ if (entry->footerSize > 0) {
+ nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer,0, entry->footerSize,0);
+ if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
+ // valid footer, this means that the message is valid
+ validMsg = true;
+ } else {
+ // Did not receive correct header
+ L_ERROR("[TCP Socket] Failed to decode message footer seq %d (received corrupt message, transmit buffer full?) (fd: %d) (url: %s)", entry->header.header.seqNr, entry->fd, entry->url);
+ entry->bufferReadSize = 0;
+ }
+ } else {
+ // No Footer, then complete message is received
+ validMsg = true;
+ }
+ }
+ }
+ }
+ }
+ if (nbytes > 0) {
+ entry->retryCount = 0;
+ // Check if complete message is received
+ if ((entry->bufferReadSize >= entry->header.header.payloadSize) && validMsg) {
+ entry->bufferReadSize = 0;
+ pubsub_tcpHandler_decodePayload(handle, entry);
+ }
+ } else {
+ if (entry->retryCount < handle->maxRcvRetryCount) {
+ entry->retryCount++;
+ L_WARN("[TCP Socket] Failed to receive message (fd: %d), error: %s. Retry count %u of %u,", entry->fd,
+ strerror(errno), entry->retryCount, handle->maxRcvRetryCount);
+ } else {
+ L_ERROR("[TCP Socket] Failed to receive message (fd: %d) after %u retries! Closing connection... Error: %s",
+ entry->fd, handle->maxRcvRetryCount, strerror(errno));
+ nbytes = 0; //Return 0 as indicator to close the connection
+ }
+ }
+ celixThreadRwlock_unlock(&handle->dbLock);
+ return nbytes;
+}
+
+
+//
+// Reads data from the filedescriptor which has date (determined by epoll()) and stores it in the internal structure
+// If the message is completely reassembled true is returned and the index and size have valid values
+//
+int pubsub_tcpHandler_read_(pubsub_tcpHandler_t *handle, int fd) {
+ celixThreadRwlock_writeLock(&handle->dbLock);
+ psa_tcp_connection_entry_t *entry = hashMap_get(handle->interface_fd_map, (void *) (intptr_t) fd);
+ if (entry == NULL)
+ entry = hashMap_get(handle->connection_fd_map, (void *) (intptr_t) fd);
+ // Find FD entry
+ if (entry == NULL) {
+ celixThreadRwlock_unlock(&handle->dbLock);
+ return -1;
+ }
+ // If it's not connected return from function
+ if (!entry->connected) {
+ celixThreadRwlock_unlock(&handle->dbLock);
+ return -1;
+ }
+
+ // Message buffer is to small, reallocate to make it bigger
+ if ((!entry->headerBufferSize) && (entry->headerSize > entry->bufferSize)) {
+ handle->bufferSize = MAX(handle->bufferSize, entry->headerSize );
+ if (entry->buffer) free(entry->buffer);
entry->buffer = malloc((size_t) handle->bufferSize);
entry->bufferSize = handle->bufferSize;
}
@@ -843,13 +1079,13 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
long int nbytes = 0;
char* header_buffer = (entry->headerBufferSize) ? entry->headerBuffer : entry->buffer;
if (entry->state == READ_STATE_SYNC) {
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, header_buffer, 0);
if (nbytes > 0) {
pubsub_tcpHandler_setReadStateMachine(handle, entry);
}
}
if (entry->state == READ_STATE_HEADER) {
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, MSG_PEEK);
+ nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, header_buffer, MSG_PEEK);
if (nbytes >= entry->headerSize) { // Check header message buffer
if (handle->protocol->decodeHeader(handle->protocol->handle, header_buffer, entry->headerSize, &entry->header) != CELIX_SUCCESS) {
// Did not receive correct header
@@ -862,7 +1098,7 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
} else {
// Read header message from queue
pubsub_tcpHandler_setNextStateReadStateMachine(handle, entry, READ_STATE_HEADER);
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, header_buffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, header_buffer, 0);
if ((nbytes > 0) && (nbytes == entry->headerSize)) {
entry->headerError = false;
entry->msgSizeReadSize = 0;
@@ -885,7 +1121,7 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
}
// Read payload data from queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->buffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, entry->buffer, 0);
if (nbytes > 0) {
if (nbytes >= entry->header.header.payloadPartSize) {
entry->msgSizeReadSize += nbytes;
@@ -906,7 +1142,7 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
}
// Read meta data from (queue
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->metaBuffer,0);
+ nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, entry->metaBuffer,0);
if ((nbytes > 0) && (nbytes >= entry->header.header.metadataSize)) {
entry->msgSizeReadSize += nbytes;
pubsub_tcpHandler_setReadStateMachine(handle, entry);
@@ -914,7 +1150,7 @@ int pubsub_tcpHandler_read(pubsub_tcpHandler_t *handle, int fd) {
}
if (entry->state == READ_STATE_FOOTER) {
// Check for end of message using, footer of message. Because of streaming protocol
- nbytes = pubsub_tcpHandler_readSocket(handle, entry, fd, entry->footerBuffer, 0);
+ nbytes = pubsub_tcpHandler_readSocket_(handle, entry, fd, entry->footerBuffer, 0);
if (handle->protocol->decodeFooter(handle->protocol->handle, entry->footerBuffer, entry->footerSize, &entry->header) == CELIX_SUCCESS) {
// valid footer, this means that the message is valid
pubsub_tcpHandler_setReadStateMachine(handle, entry);
@@ -1034,6 +1270,7 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
int nofConnToClose = 0;
if (handle) {
hash_map_iterator_t iter = hashMapIterator_construct(handle->connection_fd_map);
+ size_t max_msg_iov_len = IOV_MAX - 2;
while (hashMapIterator_hasNext(&iter)) {
psa_tcp_connection_entry_t *entry = hashMapIterator_nextValue(&iter);
if (!entry->connected) continue;
@@ -1055,9 +1292,11 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
void *metadataData = NULL;
size_t metadataSize = 0;
if (message->metadata.metadata) {
+ metadataData = entry->metaBuffer;
handle->protocol->encodeMetadata(handle->protocol->handle, message,
&metadataData,
&metadataSize);
+ entry->metaBufferSize = metadataSize;
}
message->header.metadataSize = metadataSize;
size_t totalMessageSize = payloadSize + metadataSize;
@@ -1074,12 +1313,13 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
void *footerData = NULL;
size_t footerDataSize = 0;
if (entry->footerSize) {
+ footerData = entry->footerBuffer;
handle->protocol->encodeFooter(handle->protocol->handle, message,
&footerData,
&footerDataSize);
+ entry->footerSize = footerDataSize;
}
-
size_t msgSize = 0;
size_t msgIovLen = 0;
long int nbytes = UINT32_MAX;
@@ -1098,33 +1338,35 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
message->header.isLastSegment = 0;
// Write generic seralized payload in vector buffer
- if (payloadSize && payloadData) {
- char *payloadDataBuffer = payloadData;
- msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = &payloadDataBuffer[msgSize];
- msg.msg_iov[msg.msg_iovlen].iov_len = MIN((payloadSize - msgSize), entry->maxMsgSize);
- msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- message->header.payloadPartSize = msgPartSize;
- message->header.payloadOffset = msgSize;
- msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
- } else {
- // copy serialized vector into vector buffer
- for (size_t i = 0; i < MIN(msg_iov_len, max_msg_iov_len); i++) {
+ if (msgSize < payloadSize) {
+ if (payloadSize && payloadData) {
+ char *payloadDataBuffer = payloadData;
msg.msg_iovlen++;
- msg.msg_iov[msg.msg_iovlen].iov_base = msgIoVec[msgIovLen + i].iov_base;
- msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[msgIovLen + i].iov_len;
- if ((msgPartSize + msg.msg_iov[msg.msg_iovlen].iov_len) > entry->maxMsgSize)
- break;
+ msg.msg_iov[msg.msg_iovlen].iov_base = &payloadDataBuffer[msgSize];
+ msg.msg_iov[msg.msg_iovlen].iov_len = MIN((payloadSize - msgSize), entry->maxMsgSize);
msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ message->header.payloadPartSize = msgPartSize;
+ message->header.payloadOffset = msgSize;
+ msgSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ } else {
+ // copy serialized vector into vector buffer
+ for (size_t i = 0; i < MIN(msg_iov_len, max_msg_iov_len); i++) {
+ msg.msg_iovlen++;
+ msg.msg_iov[msg.msg_iovlen].iov_base = msgIoVec[msgIovLen + i].iov_base;
+ msg.msg_iov[msg.msg_iovlen].iov_len = msgIoVec[msgIovLen + i].iov_len;
+ if ((msgPartSize + msg.msg_iov[msg.msg_iovlen].iov_len) > entry->maxMsgSize)
+ break;
+ msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
+ }
+ message->header.payloadPartSize = msgPartSize;
+ message->header.payloadOffset = msgSize;
+ msgSize += msgPartSize;
+ msgIovLen += (msg.msg_iovlen - 1);
}
- message->header.payloadPartSize = msgPartSize;
- message->header.payloadOffset = msgSize;
- msgSize += msgPartSize;
- msgIovLen += (msg.msg_iovlen - 1);
}
// Write optional metadata in vector buffer
- if ((msgSize < (payloadSize + metadataSize)) &&
+ if ((msgSize >= payloadSize) &&
(msgPartSize < entry->maxMsgSize) &&
(metadataSize && metadataData)) {
msg.msg_iovlen++;
@@ -1146,65 +1388,67 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
msgPartSize += msg.msg_iov[msg.msg_iovlen].iov_len;
}
- void *headerData = NULL;
- size_t headerSize = 0;
- // check if header is not part of the payload (=> headerBufferSize = 0)s
- if (entry->headerBufferSize) {
- // Encode the header, with payload size and metadata size
- handle->protocol->encodeHeader(handle->protocol->handle, message,
- &headerData,
- &headerSize);
- }
- if (!entry->headerBufferSize) {
- // Skip header buffer, when header is part of payload;
- msg.msg_iov = &msg_iov[1];
- } else if (headerSize && headerData) {
- // Write header in 1st vector buffer item
- msg.msg_iov[0].iov_base = headerData;
- msg.msg_iov[0].iov_len = headerSize;
- msgSize += msg.msg_iov[0].iov_len;
- msg.msg_iovlen++;
- } else {
- L_ERROR("[TCP Socket] No header buffer is generated");
- msg.msg_iovlen = 0;
- }
- long int nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgSize, flags);
- // When a specific socket keeps reporting errors can indicate a subscriber
- // which is not active anymore, the connection will remain until the retry
- // counter exceeds the maximum retry count.
- // Btw, also, SIGSTOP issued by a debugging tool can result in EINTR error.
- if (nbytes == -1) {
- if (entry->retryCount < handle->maxSendRetryCount) {
- entry->retryCount++;
- L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
- entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
+ void *headerData = NULL;
+ size_t headerSize = 0;
+ // check if header is not part of the payload (=> headerBufferSize = 0)s
+ if (entry->headerBufferSize) {
+ headerData = entry->headerBuffer;
+ // Encode the header, with payload size and metadata size
+ handle->protocol->encodeHeader(handle->protocol->handle, message,
+ &headerData,
+ &headerSize);
+ entry->headerBufferSize = headerSize;
+ }
+ if (!entry->headerBufferSize) {
+ // Skip header buffer, when header is part of payload;
+ msg.msg_iov = &msg_iov[1];
+ } else if (headerSize && headerData) {
+ // Write header in 1st vector buffer item
+ msg.msg_iov[0].iov_base = headerData;
+ msg.msg_iov[0].iov_len = headerSize;
+ msgPartSize += msg.msg_iov[0].iov_len;
+ msg.msg_iovlen++;
} else {
- L_ERROR(
- "[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s",
- entry->fd, handle->maxSendRetryCount, strerror(errno));
- connFdCloseQueue[nofConnToClose++] = entry->fd;
+ L_ERROR("[TCP Socket] No header buffer is generated");
+ msg.msg_iovlen = 0;
}
- result = -1; //At least one connection failed sending
- } else if (msgSize) {
- entry->retryCount = 0;
- if (nbytes != msgSize) {
- L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
+ nbytes = pubsub_tcpHandler_writeSocket(handle, entry, &msg, msgPartSize, flags);
+ // When a specific socket keeps reporting errors can indicate a subscriber
+ // which is not active anymore, the connection will remain until the retry
+ // counter exceeds the maximum retry count.
+ // Btw, also, SIGSTOP issued by a debugging tool can result in EINTR error.
+ if (nbytes == -1) {
+ if (entry->retryCount < handle->maxSendRetryCount) {
+ entry->retryCount++;
+ L_ERROR(
+ "[TCP Socket] Failed to send message (fd: %d), error: %s. try again. Retry count %u of %u, ",
+ entry->fd, strerror(errno), entry->retryCount, handle->maxSendRetryCount);
+ } else {
+ L_ERROR(
+ "[TCP Socket] Failed to send message (fd: %d) after %u retries! Closing connection... Error: %s", entry->fd, handle->maxSendRetryCount, strerror(errno));
+ connFdCloseQueue[nofConnToClose++] = entry->fd;
+ }
+ result = -1; //At least one connection failed sending
+ } else if (msgPartSize) {
+ entry->retryCount = 0;
+ if (nbytes != msgPartSize) {
+ L_ERROR("[TCP Socket] seq: %d MsgSize not correct: %d != %d (%s)\n", message->header.seqNr, msgSize, nbytes, strerror(errno));
+ }
+ }
+ // Release data
+ if (headerData && headerData != entry->headerBuffer) {
+ free(headerData);
+ }
+ // Note: serialized Payload is deleted by serializer
+ if (payloadData && (payloadData != message->payload.payload)) {
+ free(payloadData);
+ }
+ if (metadataData && metadataData != entry->metaBuffer) {
+ free(metadataData);
+ }
+ if (footerData && footerData != entry->footerBuffer) {
+ free(footerData);
}
- }
- // Release data
- if (headerData) {
- free(headerData);
- }
- // Note: serialized Payload is deleted by serializer
- if (payloadData && (payloadData != message->payload.payload)) {
- free(payloadData);
- }
- if (metadataData) {
- free(metadataData);
- }
- if (footerData) {
- free(footerData);
}
}
}
@@ -1216,6 +1460,8 @@ int pubsub_tcpHandler_write(pubsub_tcpHandler_t *handle, pubsub_protocol_message
return result;
}
+
+
//
// get interface URL
//