socket read with timeout, logging
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
r
|
||||||
|
build
|
||||||
|
old
|
||||||
|
dump
|
||||||
22
Makefile
Normal file
22
Makefile
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
target = r
|
||||||
|
CXX = g++
|
||||||
|
CXX_FLAGS := -std=c++14 -Iloguru
|
||||||
|
LD_FLAGS := -lpthread -ldl
|
||||||
|
BUILD := ./build
|
||||||
|
OBJS := $(BUILD)/client.o $(BUILD)/loguru.o
|
||||||
|
|
||||||
|
all: $(target)
|
||||||
|
|
||||||
|
$(BUILD)/%.o: %.cc
|
||||||
|
@mkdir -p $(BUILD)
|
||||||
|
g++ -c $< -o $@ $(CXX_FLAGS)
|
||||||
|
|
||||||
|
$(BUILD)/loguru.o:
|
||||||
|
@mkdir -p $(BUILD)
|
||||||
|
g++ -c loguru/loguru.cpp -Iloguru -o $@ $(CXX_FLAGS)
|
||||||
|
|
||||||
|
r: $(OBJS)
|
||||||
|
g++ -o $@ $^ $(LD_FLAGS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD) r
|
||||||
134
client.cc
Normal file
134
client.cc
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <fstream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <csignal>
|
||||||
|
#include <loguru.hpp>
|
||||||
|
|
||||||
|
const char * HOST = "192.168.30.89";
|
||||||
|
const uint32_t PORT = 9999;
|
||||||
|
const size_t MAX_EVENT_SIZE = 33484; // in bytes
|
||||||
|
const size_t MIN_EVENT_SIZE = 204; // in bytes
|
||||||
|
const bool verbose = true;
|
||||||
|
|
||||||
|
int sock = 0;
|
||||||
|
bool running = true;
|
||||||
|
int open_socket();
|
||||||
|
void signalHandler( int signum );
|
||||||
|
|
||||||
|
bool bad_data = true;
|
||||||
|
|
||||||
|
int main(int argc, char const* argv[])
|
||||||
|
{
|
||||||
|
signal(SIGINT, signalHandler);
|
||||||
|
int ret = open_socket();
|
||||||
|
if (ret != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
char char_buffer[MAX_EVENT_SIZE];
|
||||||
|
uint32_t * wordData = (uint32_t*) char_buffer;
|
||||||
|
ssize_t nread;
|
||||||
|
|
||||||
|
fd_set select_fds; // fd's used by select
|
||||||
|
struct timeval timeout; // Time value for time out
|
||||||
|
//Timeout set for 0 sec + 200 millisec
|
||||||
|
timeout.tv_sec = 0;
|
||||||
|
timeout.tv_usec = 200*1000;
|
||||||
|
std::chrono::duration<int, std::milli> polling_period(2);
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
// Setup the descriptor set for select()
|
||||||
|
FD_ZERO(&select_fds);
|
||||||
|
FD_SET(sock, &select_fds);
|
||||||
|
if ( select(sock + 1, &select_fds, NULL, NULL, &timeout) == 0 ){
|
||||||
|
std::this_thread::sleep_for(polling_period);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// read in first 4 bytes
|
||||||
|
nread = recv(sock, char_buffer, 4, 0);
|
||||||
|
LOG_IF_F(INFO, verbose, "read in %u bytes: 0x%08X", nread, wordData[0]);
|
||||||
|
// if not A5 marker, go to next iteration (read in 4 more bytes)
|
||||||
|
if ((char_buffer[0] & 0xFF) != 0xA5){
|
||||||
|
bad_data = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bad_data = false;
|
||||||
|
size_t frameSize = 4 * (wordData[0] >> 8);
|
||||||
|
nread = recv(sock, char_buffer, frameSize - 4, 0);
|
||||||
|
// nread + 4 must be between MIN_EVENT_SIZE and MAX_EVENT_SIZE, and equal to frameSize.
|
||||||
|
if ((nread != frameSize - 4) || (frameSize < MIN_EVENT_SIZE)|| (frameSize > MAX_EVENT_SIZE)){
|
||||||
|
bad_data = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// ok, this seems to be good data
|
||||||
|
LOG_IF_F(INFO, verbose, "next %u bytes (expecting %u): 0x%08X",
|
||||||
|
nread, frameSize - 4, wordData[0]);
|
||||||
|
uint8_t board_id = (wordData[0] >> 24) & 0xFF;
|
||||||
|
uint8_t version = wordData[0] & 0xFF;
|
||||||
|
uint16_t spill_id = (wordData[1] & 0x00FFFF00) >> 8;
|
||||||
|
uint8_t channel_count = (wordData[1]) & 0x000000FF;
|
||||||
|
if (verbose) {
|
||||||
|
std::cout << "board " << static_cast<uint32_t>(board_id) <<
|
||||||
|
", version " << static_cast<uint32_t>(version) <<
|
||||||
|
", spill " << static_cast<uint32_t>(spill_id) <<
|
||||||
|
", channel_count " << static_cast<uint32_t>(channel_count)<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((board_id > 4) || (board_id < 1) || (version != 1) || (channel_count > 8) || (channel_count < 1)) {
|
||||||
|
bad_data = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(sock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void signalHandler( int signum ) {
|
||||||
|
std::cout << "Interrupt signal (" << signum << ") received, exiting..."
|
||||||
|
<< std::endl;
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int open_socket(){
|
||||||
|
struct sockaddr_in serv_addr;
|
||||||
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||||
|
std::cerr << "Cannot create socket" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&serv_addr, '0', sizeof(serv_addr));
|
||||||
|
serv_addr.sin_family = AF_INET;
|
||||||
|
serv_addr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
if(inet_pton(AF_INET, HOST, &serv_addr.sin_addr)<=0) {
|
||||||
|
std::cerr << "Invalid address or Address not supported" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||||
|
std::cerr << "Cannot connect to " << HOST << ":" << PORT << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user