Skip to content
Snippets Groups Projects

L2 sdp 251

Merged Pieter Donker requested to merge L2SDP-251 into master
All threads resolved!
35 files
+ 2649
2086
Compare changes
  • Side-by-side
  • Inline
Files
35
+ 52
36
/*
* Copyright 2020 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten,
* ASTRON Netherlands Institute for Radio Astronomy
* Licensed under the Apache License, Version 2.0 (the "License");
*
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* See ../../LICENSE.txt for more info.
*/
/* *************************************************************************
* Copyright 2020
* ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
* P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* *********************************************************************** */
/* *************************************************************************
* Author:
* . Leon Hiemstra
* . Pieter Donker
* Purpose:
* . opc-ua to ucp translator
* Description:
* . TCP communication
* *********************************************************************** */
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> // htons
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
//#include <syslog.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <stdexcept>
#include <unistd.h>
#include "tcpsocket.h"
using namespace std;
/*
TCP Server Socket listens to port p and binds to interface iface.
@@ -70,11 +81,12 @@ TCPSSocket::~TCPSSocket()
int TCPSSocket::listen(void)
{
ostringstream strs;
printf("Listening to tcp port %d\n",port);
cout << "Listening to tcp port " << port << endl;
// syslog(LOG_INFO,"Listening to tcp port %d\n",port);
if (::listen (sock, 1) < 0)
if (::listen (sock, 1) < 0) {
throw runtime_error("TCPSSocket::listen: cannot put socket into listening state");
}
/* Block until input arrives on the server socket. */
int nsock;
@@ -123,14 +135,15 @@ size_t TCPCSSocket::rx(unsigned char *buf, size_t len)
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
// No time out
if(select(sock + 1, &readfds, NULL, NULL, NULL) == -1) {
if (select(sock + 1, &readfds, NULL, NULL, NULL) == -1) {
throw runtime_error("TCPCSSocket::rx: select error");
}
if FD_ISSET(sock, &readfds) {
ret = recvfrom(sock, buf, len, 0, NULL, NULL);
if(ret == -1) {
if (ret == -1) {
throw runtime_error("TCPCSSocket::rx(): recvfrom=-1 error");
} else if(ret == 0) {
}
else if (ret == 0) {
throw runtime_error("TCPCSSocket::rx(): recvfrom=0 peer orderly shutdown");
}
nrx += ret;
@@ -157,20 +170,22 @@ size_t TCPCSSocket::_rx(unsigned char *buf, size_t len)
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
// No time out
if(select(sock + 1, &readfds, NULL, NULL, NULL) == -1) {
if (select(sock + 1, &readfds, NULL, NULL, NULL) == -1) {
throw runtime_error("TCPCSSocket::_rx: select error");
}
if FD_ISSET(sock, &readfds) {
ret = recvfrom(sock, &buf[nrx], len-nrx, 0, NULL, NULL);
if(ret == -1) {
if (ret == -1) {
throw runtime_error("TCPCSSocket::_rx(): recvfrom=-1 error");
} else if(ret == 0) {
}
else if (ret == 0) {
throw runtime_error("TCPCSSocket::_rx(): recvfrom=0 peer orderly shutdown");
}
nrx += ret;
}
if (nrx==len)
if (nrx == len) {
break;
}
} while (1);
return nrx;
}
@@ -178,8 +193,9 @@ size_t TCPCSSocket::_rx(unsigned char *buf, size_t len)
size_t TCPCSSocket::tx(const unsigned char* mes, size_t len)
{
int ntxbytes = write(sock, mes, len);
if (ntxbytes < 0)
if (ntxbytes < 0) {
throw runtime_error("TCPCSSocket::tx(): could not send message");
}
return ntxbytes;
}
Loading