1 #ifndef ORG_EEROS_SOCKET_CLIENT_HPP_ 2 #define ORG_EEROS_SOCKET_CLIENT_HPP_ 11 #include <sys/socket.h> 21 template < u
int32_t BufInLen,
typename inT, u
int32_t BufOutLen,
typename outT >
24 SocketClient(std::string serverIP, uint16_t port,
double period = 0.01,
double timeout = 1.0) : read1({0}), read2({0}), read3({0}) {
26 this->period = period;
27 this->timeout = timeout;
28 this->serverIP = serverIP;
30 read_ptr.store(&read1);
31 send_ptr.store(&send1);
48 return *read_ptr.load();
52 auto p = send_ptr.load();
53 if (p == &send1) send1 = data;
54 else if (p == &send2) send2 = data;
55 else if (p == &send3) send3 = data;
63 log.
info() <<
"SocketClient thread started";
66 struct sockaddr_in servAddr;
67 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
68 if (sockfd < 0)
throw Fault(
"ERROR opening socket");
70 auto server = gethostbyname(serverIP.c_str());
72 throw Fault(
"Server ip not found");
74 bzero((
char *) &servAddr,
sizeof(servAddr));
75 servAddr.sin_family = AF_INET;
76 bcopy((
char *)server->h_addr,(
char *)&servAddr.sin_addr.s_addr, server->h_length);
77 servAddr.sin_port = htons(port);
79 using seconds = std::chrono::duration<double, std::chrono::seconds::period>;
80 auto next_cycle = std::chrono::steady_clock::now() + seconds(period);
81 while (connect(sockfd, (
struct sockaddr *) &servAddr,
sizeof(servAddr)) < 0) {
82 std::this_thread::sleep_until(next_cycle);
83 next_cycle += seconds(period);
85 log.
info() <<
"Client connected to ip=" << serverIP;
86 inT b_write[BufInLen]; outT b_read[BufOutLen];
87 bool connected =
true;
90 std::this_thread::sleep_until(next_cycle);
93 std::array<inT, BufInLen> &sendValue = getNextSendBuffer();
94 for(
int i = 0; i < BufInLen; i++) b_write[i] = sendValue[i];
96 int n = write(sockfd, b_write, BufInLen *
sizeof(inT));
98 log.
trace() <<
"error = " << std::strerror(errno);
103 size_t count = BufOutLen *
sizeof(outT);
104 uint8_t* ptr = (uint8_t *)b_read;
105 auto endTime = std::chrono::steady_clock::now() + seconds(timeout);
106 while (connected && count) {
107 if (std::chrono::steady_clock::now() > endTime) {
108 log.
trace() <<
"error = socket read timed out";
112 n = read(sockfd, ptr, count);
114 log.
trace() <<
"error = " << std::strerror(errno);
120 std::array<outT, BufOutLen> &readValue = getNextReceiveBuffer();
121 for(
int i = 0; i < BufOutLen; i++) readValue[i] = b_read[i];
124 next_cycle += seconds(period);
128 std::array<outT, BufOutLen> &readValue = getNextReceiveBuffer();
129 for(
int i = 0; i < BufOutLen; i++) readValue[i] = 0;
135 std::array<outT, BufOutLen>& getNextReceiveBuffer() {
136 auto p = read_ptr.load();
137 if (p == &read1)
return read2;
138 else if (p == &read2)
return read3;
139 else if (p == &read3)
return read1;
142 std::array<inT, BufInLen>& getNextSendBuffer() {
143 auto p = send_ptr.load();
144 if (p == &send1)
return send2;
145 else if (p == &send2)
return send3;
146 else if (p == &send3)
return send1;
150 read_ptr.store(&getNextReceiveBuffer());
151 send_ptr.store(&getNextSendBuffer());
155 std::string serverIP;
159 struct hostent *server;
162 std::array<outT, BufOutLen> read1, read2, read3;
163 std::array<inT, BufInLen> send1, send2, send3;
164 std::atomic< std::array<outT, BufOutLen>* > read_ptr;
165 std::atomic< std::array<inT, BufInLen>* > send_ptr;
169 template < u
int32_t BufInLen,
typename inT >
172 SocketClient(std::string serverIP, uint16_t port,
double period = 0.01) {
174 this->period = period;
175 this->serverIP = serverIP;
177 send_ptr.store(&send1);
194 auto p = send_ptr.load();
195 if (p == &send1) send1 = data;
196 else if (p == &send2) send2 = data;
197 else if (p == &send3) send3 = data;
202 log.
info() <<
"SocketClient thread started";
205 struct sockaddr_in servAddr;
206 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
207 if (sockfd < 0)
throw Fault(
"ERROR opening socket");
209 auto server = gethostbyname(serverIP.c_str());
210 if (server == NULL) {
211 throw Fault(
"Server ip not found");
213 bzero((
char *) &servAddr,
sizeof(servAddr));
214 servAddr.sin_family = AF_INET;
215 bcopy((
char *)server->h_addr,(
char *)&servAddr.sin_addr.s_addr, server->h_length);
216 servAddr.sin_port = htons(port);
218 while (connect(sockfd, (
struct sockaddr *) &servAddr,
sizeof(servAddr)) < 0) ;
219 log.
info() <<
"Client connected to ip=" << serverIP;
220 inT b_write[BufInLen];
221 using seconds = std::chrono::duration<double, std::chrono::seconds::period>;
222 auto next_cycle = std::chrono::steady_clock::now() + seconds(period);
223 bool connected =
true;
226 std::this_thread::sleep_until(next_cycle);
229 std::array<inT, BufInLen> &sendValue = getNextSendBuffer();
230 for(
int i = 0; i < BufInLen; i++) b_write[i] = sendValue[i];
232 int n = write(sockfd, b_write, BufInLen *
sizeof(inT));
234 log.
trace() <<
"error = " << std::strerror(errno);
239 next_cycle += seconds(period);
245 std::array<inT, BufInLen>& getNextSendBuffer() {
246 auto p = send_ptr.load();
247 if (p == &send1)
return send2;
248 else if (p == &send2)
return send3;
249 else if (p == &send3)
return send1;
253 send_ptr.store(&getNextSendBuffer());
257 std::string serverIP;
260 struct hostent *server;
263 std::array<inT, BufInLen> send1, send2, send3;
264 std::atomic< std::array<inT, BufInLen>* > send_ptr;
270 #endif // ORG_EEROS_SOCKET_CLIENT_HPP_ virtual void setSendBuffer(std::array< inT, BufInLen > &data)
Definition: SocketClient.hpp:51
bool newData
Definition: SocketClient.hpp:59
SocketClient(std::string serverIP, uint16_t port, double period=0.01)
Definition: SocketClient.hpp:172
virtual bool isRunning()
Definition: SocketClient.hpp:189
virtual std::array< outT, BufOutLen > & getReceiveBuffer()
Definition: SocketClient.hpp:47
Definition: Config.hpp:14
virtual void setSendBuffer(std::array< inT, BufInLen > &data)
Definition: SocketClient.hpp:193
virtual void stop()
Definition: SocketClient.hpp:185
virtual ~SocketClient()
Definition: SocketClient.hpp:35
Definition: Thread.hpp:12
LogEntry info()
Definition: Logger.hpp:27
volatile bool running
Definition: Executor.cpp:25
virtual void stop()
Definition: SocketClient.hpp:39
virtual void join()
Definition: Thread.cpp:29
eeros::logger::Logger log
Definition: Thread.hpp:26
SocketClient(std::string serverIP, uint16_t port, double period=0.01, double timeout=1.0)
Definition: SocketClient.hpp:24
Definition: SocketClient.hpp:22
virtual ~SocketClient()
Definition: SocketClient.hpp:181
virtual bool isRunning()
Definition: SocketClient.hpp:43
void sigPipeHandler(int signum)
Definition: SocketServer.cpp:6
LogEntry trace()
Definition: Logger.hpp:28
outT readbuffer
Definition: SocketClient.hpp:58