Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEyrak Paen2021-06-18 00:27:33 +0000
committerEyrak Paen2021-06-18 00:39:27 +0000
commit1c14442fd7f9f73ef63a20523d6514a6b0e82a26 (patch)
tree1999c8c4bc3ff71ee19f9dfed2225ac71e8a9875 /runtime/org.eclipse.etrice.runtime.c
parentcfdbad8f002ffaf2f0c7640a81f6c68224db49e2 (diff)
downloadorg.eclipse.etrice-1c14442fd7f9f73ef63a20523d6514a6b0e82a26.tar.gz
org.eclipse.etrice-1c14442fd7f9f73ef63a20523d6514a6b0e82a26.tar.xz
org.eclipse.etrice-1c14442fd7f9f73ef63a20523d6514a6b0e82a26.zip
[modellib.c][runtime.c] Integrate fixes for tcp
- Functional test added for ATcpClient and ATcpServer - Additional fixes for posix etTcpSockets - Changed posix etThread error output to use etLogger to avoid crash in gradle Change-Id: Iaa1480fd1379da249d2143e6d338b8397ef0870c
Diffstat (limited to 'runtime/org.eclipse.etrice.runtime.c')
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/osal/etTcpSockets.h3
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etTcpSockets.c46
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etThread.c12
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_WIN_MinGW/etTcpSockets.c10
4 files changed, 55 insertions, 16 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/osal/etTcpSockets.h b/runtime/org.eclipse.etrice.runtime.c/src/common/osal/etTcpSockets.h
index 3bd72dede..8576b5afb 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/common/osal/etTcpSockets.h
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/osal/etTcpSockets.h
@@ -28,12 +28,13 @@
ET_EXTERN_C_BEGIN
/** the maximum number of connections per server */
+// if you change this value, please change also in tcp.room
#define MAX_CONNECTIONS 32
/** function prototype for receiving data from a socket */
typedef int (*etSocketReceiver)(void* self, int channel, int size, const int8* data);
/** function prototype for a function returning a writable buffer */
-typedef int8* (*etBufferProvider)(void* self, int* size);
+typedef int8* (*etBufferProvider)(void* self, int* size, int channel);
/**
* The public part of the server data.
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etTcpSockets.c b/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etTcpSockets.c
index 0ad6804cf..7e230d5a1 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etTcpSockets.c
+++ b/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etTcpSockets.c
@@ -18,6 +18,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/syscall.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
@@ -30,8 +31,7 @@
#define LOCAL_HOST "127.0.0.1"
#define INVALID_SOCKET -1
-#define PRINT_DEBUG(x) { printf(x); fflush(stdout); }
-
+#define PRINT_DEBUG(x) { printf("%d: %s", syscall(__NR_gettid), x); }
typedef int SOCKET;
/* implementation versions of data */
@@ -64,17 +64,30 @@ etSocketServerDataImpl;
static void readThreadFunc(void* threadData) {
etSocketConnectionDataImpl* self = (etSocketConnectionDataImpl*) threadData;
int len, retval;
- int8* buffer = (self->data.bufferProvider)(self->data.userData, &len);
+ int8* buffer = (self->data.bufferProvider)(self->data.userData, &len, self->channel);
while (ET_TRUE) {
retval = recv(self->socket, buffer, len, 0);
- if (retval<=0) {
- /* TODO: call WSAGetLastError and do error handling */
+ if (retval == 0) {
PRINT_DEBUG("connection thread: socket lost, exiting\n")
self->socket = INVALID_SOCKET;
etThread_destruct(&self->readThread);
return;
}
+ else if (retval < 0) {
+ printf("connection thread: %s\n", strerror(errno));
+ if (EINTR == errno) {
+ PRINT_DEBUG("connection thread: read interrupted\n")
+ continue;
+ }
+ else {
+ PRINT_DEBUG("connection thread: error, exiting\n")
+ self->socket = INVALID_SOCKET;
+ etThread_destruct(&self->readThread);
+ return;
+ }
+
+ }
(self->data.receiver)(self->data.userData, self->channel, retval, buffer);
}
@@ -108,13 +121,19 @@ static void listenerThreadFunc(void* threadData) {
&len);
if (self->connections[slot].socket == INVALID_SOCKET) {
- /* TODO: error handling */
PRINT_DEBUG("server: accept interrupted\n")
- break;
+ printf("server: %s\n", strerror(errno));
+ fflush(stdout);
+ if (EINTR == errno) {
+ continue;
+ }
+ else {
+ break;
+ }
}
PRINT_DEBUG("server: accepted new client, starting read thread\n")
- self->connections[slot].channel = self->nConnections++;
+ self->connections[slot].channel = slot;
etThread_construct(
&self->connections[slot].readThread,
@@ -147,6 +166,7 @@ etSocketError etCleanupSockets() {
etSocketServerData* etCreateSocketServerData() {
etSocketServerDataImpl* data = malloc(sizeof(etSocketServerDataImpl));
memset(data, 0, sizeof(etSocketServerDataImpl));
+ data->data.maxConnections = MAX_CONNECTIONS;
return &data->data;
}
@@ -191,6 +211,13 @@ etSocketError etStartListening(etSocketServerData* data, short port) {
return ETSOCKET_ERROR;
}
+ int enable = 1;
+ if (setsockopt(self->socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
+ printf("server: %s\n", strerror(errno));
+ fflush(stdout);
+ return ETSOCKET_ERROR;
+ }
+
if (bind(self->socket, (struct sockaddr*) &local, sizeof(local)) < 0) {
printf("server: %s\n", strerror(errno));
fflush(stdout);
@@ -214,6 +241,7 @@ etSocketError etStartListening(etSocketServerData* data, short port) {
etSocketError etStopSocketServer(etSocketServerData* data) {
etSocketServerDataImpl* self = (etSocketServerDataImpl*) data;
PRINT_DEBUG("server: stop\n")
+ etThread_destruct(&self->listenerThread);
close(self->socket);
return ETSOCKET_OK;
}
@@ -253,6 +281,7 @@ etSocketError etCloseServerSocket(etSocketServerData* data, int connection) {
if (self->connections[connection].socket!=INVALID_SOCKET) {
PRINT_DEBUG("server: close connection\n")
+ etThread_destruct(&self->connections[connection].readThread);
close(self->connections[connection].socket);
self->connections[connection].socket = INVALID_SOCKET;
}
@@ -267,6 +296,7 @@ etSocketError etCloseAllServerSockets(etSocketServerData* data) {
PRINT_DEBUG("server: close all connections\n")
for (i=0; i<MAX_CONNECTIONS; ++i) {
if (self->connections[i].socket!=INVALID_SOCKET) {
+ etThread_destruct(&self->connections[i].readThread);
close(self->connections[i].socket);
self->connections[i].socket = INVALID_SOCKET;
}
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etThread.c b/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etThread.c
index 8b7af981c..24e90f5e5 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etThread.c
+++ b/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_POSIX_GENERIC_GCC/etThread.c
@@ -27,6 +27,7 @@
#include <sys/unistd.h>
#include <errno.h>
#include <limits.h>
+#include <string.h>
typedef void *(*threadFunc)(void *);
void* etThread_execute(etThread* self);
@@ -52,10 +53,15 @@ void etThread_construct(
ET_MSC_LOGGER_SYNC_EXIT
}
-/* TODO: improve this using a logger */
static void etThread_handleError(int err, const char* msg) {
- errno = err;
- perror(msg);
+ char errmsg[100];
+ int ret = strerror_r(err, errmsg, sizeof(errmsg));
+ if (ret == 0) {
+ etLogger_logErrorF("%s: (%d) %s", msg, err, errmsg);
+ }
+ else {
+ etLogger_logErrorF("%s: (%d) unknown error", msg, err);
+ }
}
void etThread_start(etThread* self) {
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_WIN_MinGW/etTcpSockets.c b/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_WIN_MinGW/etTcpSockets.c
index 6f2fb1b41..09ff0b68d 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_WIN_MinGW/etTcpSockets.c
+++ b/runtime/org.eclipse.etrice.runtime.c/src/platforms/MT_WIN_MinGW/etTcpSockets.c
@@ -12,9 +12,9 @@
*
*******************************************************************************/
-#include "osal/etTcpSockets.h"
#include <winsock2.h>
#include <ctype.h>
+#include <osal/etTcpSockets.h>
#include "osal/etThread.h"
#define STACK_SIZE 0 /* let system select default size */
@@ -53,7 +53,7 @@ etSocketServerDataImpl;
static void readThreadFunc(void* threadData) {
etSocketConnectionDataImpl* self = (etSocketConnectionDataImpl*) threadData;
int len, retval;
- int8* buffer = (self->data.bufferProvider)(self->data.userData, &len);
+ int8* buffer = (self->data.bufferProvider)(self->data.userData, &len, self->channel);
while (ET_TRUE) {
retval = recv(self->socket, (char*)buffer, len, 0);
@@ -103,7 +103,7 @@ static void listenerThreadFunc(void* threadData) {
}
PRINT_DEBUG("server: accepted new client, starting read thread\n")
- self->connections[slot].channel = self->nConnections++;
+ self->connections[slot].channel = slot;
etThread_construct(
&self->connections[slot].readThread,
@@ -209,8 +209,10 @@ etSocketError etWriteServerSocket(etSocketServerData* dat, int connection, int s
etSocketServerDataImpl* self = (etSocketServerDataImpl*) dat;
int offset = 0;
- if (connection<0 || connection>MAX_CONNECTIONS || self->connections[connection].socket==INVALID_SOCKET)
+ if (connection<0 || connection>MAX_CONNECTIONS || self->connections[connection].socket==INVALID_SOCKET) {
+ PRINT_DEBUG("connection error\n");
return ETSOCKET_ERROR;
+ }
/* Note: loop required because:
* If no error occurs, send returns the total number of bytes sent, which can be less than the number

Back to the top