mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-07-14 17:50:07 +02:00
Build and Deploy on local RHEL9 / build (push) Successful in 2m0s
Build on RHEL9 docker image / build (push) Successful in 3m34s
Build on RHEL8 docker image / build (push) Successful in 4m46s
Build and Deploy on local RHEL8 / build (push) Successful in 5m3s
Run Simulator Tests on local RHEL9 / build (push) Successful in 14m43s
Run Simulator Tests on local RHEL8 / build (push) Successful in 18m15s
* added fetch fmt server library * added first draft of matterhorn * added enum ReturnCode * added cpp TCP Interface to slsDetectorServer * added fmt to workflows * bug: added std::signal for proper handling of ctr+c * added compile option to set log level * WIP * dont use c project settings when building matterhornserver * updated logger * WIP * WIP * linked fmt to slsProjectOptions * solved merge conflict * some refactoring * cleaned up logs * added fmt to workflow * WIP * generated register defs from csv file * oops given in hex * properly added fmt as a dependency * add fmt to conda recipe * some format changes * dont use public headers of fmt * WIP * used CRTP for virtual detector * WIP * added udp functions to matterhornserver * Matterhorn in tostring * warning unused variable from other PR * fixed build * updated cmake * added Server class usable for all detectors * removed stopserver * added some more functions * wrong overload * porper cleanup of matterhorn app * PR Review * refactored directory structure * used pause insetad of sleep --------- Co-authored-by: Dhanya Thattil <dhanya.thattil@psi.ch>
64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-or-other
|
|
// Copyright (C) 2021 Contributors to the SLS Detector Package
|
|
#include "sls/ServerSocket.h"
|
|
#include "sls/ServerInterface.h"
|
|
|
|
#include "sls/DataSocket.h"
|
|
#include "sls/logger.h"
|
|
#include "sls/sls_detector_defs.h"
|
|
#include "sls/sls_detector_exceptions.h"
|
|
#include "sls/string_utils.h"
|
|
|
|
#include <arpa/inet.h>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <unistd.h>
|
|
|
|
namespace sls {
|
|
|
|
#define DEFAULT_PACKET_SIZE 1286
|
|
#define SOCKET_BUFFER_SIZE (100 * 1024 * 1024) // 100 MB
|
|
#define DEFAULT_BACKLOG 5
|
|
|
|
ServerSocket::ServerSocket(int port)
|
|
: DataSocket(socket(AF_INET, SOCK_STREAM, 0)), serverPort(port) {
|
|
|
|
struct sockaddr_in serverAddr;
|
|
serverAddr.sin_family = AF_INET;
|
|
serverAddr.sin_port = htons(port);
|
|
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (bind(getSocketId(), (struct sockaddr *)&serverAddr,
|
|
sizeof(serverAddr)) != 0) {
|
|
close();
|
|
throw SocketError(
|
|
std::string("Server ERROR: cannot bind socket with port number ") +
|
|
std::to_string(port) +
|
|
std::string(". Please check if another instance is running."));
|
|
}
|
|
if (listen(getSocketId(), DEFAULT_BACKLOG) != 0) {
|
|
close();
|
|
throw std::runtime_error("Server ERROR: cannot listen to socket");
|
|
}
|
|
}
|
|
|
|
ServerInterface ServerSocket::accept() {
|
|
lastClient = thisClient; // update from previous connection
|
|
struct sockaddr_in clientAddr;
|
|
socklen_t addr_size = sizeof clientAddr;
|
|
int newSocket =
|
|
::accept(getSocketId(), (struct sockaddr *)&clientAddr, &addr_size);
|
|
if (newSocket == -1) {
|
|
throw SocketError("Server ERROR: socket accept failed\n");
|
|
}
|
|
|
|
char tc[INET_ADDRSTRLEN]{};
|
|
inet_ntop(AF_INET, &(clientAddr.sin_addr), tc, INET_ADDRSTRLEN);
|
|
thisClient = IpAddr{tc};
|
|
// Set socket buffer size
|
|
return ServerInterface(newSocket);
|
|
}
|
|
|
|
}; // namespace sls
|