Driver: Add Servd

This commit is contained in:
Kyle Schwarz
2025-05-08 11:22:42 -04:00
parent 7eeb1b6c38
commit 4dcb944d35
18 changed files with 679 additions and 75 deletions
+13
View File
@@ -174,6 +174,19 @@ public:
VSAExtendedMessageError = VSABufferCorrupted + 5,
VSAOtherError = VSABufferCorrupted + 6,
// Servd
ServdBindError = 0x6000,
ServdNonblockError = ServdBindError + 1,
ServdTransceiveError = ServdBindError + 2,
ServdOutdatedError = ServdBindError + 3,
ServdInvalidResponseError = ServdBindError + 4,
ServdLockError = ServdBindError + 5,
ServdSendError = ServdBindError + 6,
ServdRecvError = ServdBindError + 7,
ServdPollError = ServdBindError + 8,
ServdNoDataError = ServdBindError + 9,
ServdJoinMulticastError = ServdBindError + 10,
NoErrorFound = 0xFFFFFFFD,
TooManyEvents = 0xFFFFFFFE,
Unknown = 0xFFFFFFFF
+2
View File
@@ -37,6 +37,8 @@ public:
bool readAvailable() { return readBuffer.size() > 0; }
RingBuffer& getReadBuffer() { return readBuffer; }
virtual bool enableCommunication(bool /* enable */, bool& sendMsg) { sendMsg = true; return true; }
device_eventhandler_t report;
size_t writeQueueSize = 50;
+2
View File
@@ -1012,6 +1012,8 @@ private:
* @return The size of the vsa log files on the disk
*/
std::optional<uint64_t> getVSADiskSize();
bool enableNetworkCommunication(bool enable);
};
}
+46
View File
@@ -0,0 +1,46 @@
#ifndef __SERVD_H_
#define __SERVD_H_
#ifdef __cplusplus
#include <optional>
#include <thread>
#include <unordered_set>
#include "icsneo/device/neodevice.h"
#include "icsneo/communication/driver.h"
#include "icsneo/device/founddevice.h"
#include "icsneo/platform/socket.h"
namespace icsneo {
class Servd : public Driver {
public:
static void Find(std::vector<FoundDevice>& foundDevices);
static bool Enabled();
Servd(const device_eventhandler_t& err, neodevice_t& forDevice, const std::unordered_set<std::string>& availableDrivers);
~Servd() override;
bool open() override;
bool isOpen() override;
bool close() override;
bool faa(const std::string& key, int32_t inc, int32_t& orig);
bool enableCommunication(bool enable, bool& sendMsg) override;
private:
void alive();
void read(Address&& address);
void write(Address&& address);
neodevice_t& device;
std::thread aliveThread; // makes sure the client and server are healthy
std::thread writeThread;
std::thread readThread;
Socket messageSocket;
bool opened = false;
bool comEnabled = false;
std::string driver;
};
}
#endif // __cplusplus
#endif
+208
View File
@@ -0,0 +1,208 @@
#ifndef __SOCKET_H_
#define __SOCKET_H_
#ifdef __cplusplus
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#endif
#include <string>
#include <chrono>
namespace icsneo {
#ifdef _WIN32
class WSA {
public:
WSA() {
// TODO: add error checking
WSAStartup(MAKEWORD(2, 2), &wsaData);
}
~WSA() {
WSACleanup();
}
private:
WSADATA wsaData;
};
#endif
class Address {
public:
Address() = default;
Address(const char* ip, uint16_t port)
: _ip(ip), _port(port)
{
_sockaddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &_sockaddr.sin_addr);
_sockaddr.sin_port = htons(port);
}
Address(sockaddr_in& sockaddr)
: _sockaddr(sockaddr)
{
char cip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &sockaddr.sin_addr, cip, sizeof(cip));
_ip = cip;
_port = ntohs(sockaddr.sin_port);
}
const std::string& ip() const { return _ip; }
const uint16_t& port() const { return _port; }
const sockaddr_in& sockaddr() const { return _sockaddr; }
private:
std::string _ip;
uint16_t _port;
sockaddr_in _sockaddr;
};
class Socket {
public:
#ifdef _WIN32
using SocketHandleType = SOCKET;
#else
using SocketHandleType = int;
#endif
Socket() {
#ifdef _WIN32
static WSA wsa;
#endif
mFD = socket(AF_INET, SOCK_DGRAM, 0);
}
~Socket() {
#ifdef _WIN32
closesocket(mFD);
#else
close(mFD);
#endif
}
bool set_reuse(bool value) {
int ival = value;
return ::setsockopt(mFD, SOL_SOCKET, SO_REUSEADDR, (const char*)&ival, sizeof(ival)) != -1;
}
bool set_nonblocking() {
#ifdef _WIN32
u_long nonblock = 1;
return ioctlsocket(mFD, FIONBIO, &nonblock) != SOCKET_ERROR;
#else
return fcntl(mFD, F_SETFL, fcntl(mFD, F_GETFL, 0) | O_NONBLOCK) != -1;
#endif
}
bool bind(const Address& at) {
return ::bind(mFD, (sockaddr*)&at.sockaddr(), sizeof(sockaddr_in)) != -1;
}
bool poll(const std::chrono::milliseconds& timeout, bool& in) {
#ifdef _WIN32
WSAPOLLFD pfd;
pfd.fd = mFD;
pfd.events = POLLIN;
if (::WSAPoll(&pfd, 1, static_cast<int>(timeout.count())) == SOCKET_ERROR) {
return false;
}
in = pfd.revents & POLLIN;
return true;
#else
struct pollfd pfd;
pfd.fd = mFD;
pfd.events = POLLIN;
pfd.revents = 0;
if (::poll(&pfd, 1, static_cast<int>(timeout.count())) == -1) {
return false;
}
in = pfd.revents & POLLIN;
return true;
#endif
}
bool sendto(const void* buffer, size_t size, const Address& to) {
size_t totalSent = 0;
do {
const auto sent = ::sendto(mFD, (const char*)buffer, (int)size, 0, (sockaddr*)&to.sockaddr(), sizeof(sockaddr_in));
if (sent == -1) {
return false;
}
totalSent += sent;
} while (totalSent < size);
return true;
}
bool recvfrom(void* buffer, size_t& size, Address& from) {
sockaddr_in addr;
socklen_t addLen = sizeof(addr);
const auto read = ::recvfrom(mFD, (char*)buffer, (int)size, 0, (sockaddr*)&addr, &addLen);
if (read == -1) {
return false;
}
size = read;
from = Address(addr);
return true;
}
bool recv(void* buffer, size_t& size) {
const auto read = ::recv(mFD, (char*)buffer, (int)size, 0);
if (read == -1) {
return false;
}
size = read;
return true;
}
template<typename REQ, typename RES>
bool transceive(const Address& to, REQ&& request, RES&& response, const std::chrono::milliseconds& timeout) {
if(!sendto(request.data(), request.size(), to)) {
return false;
}
bool hasData;
if(!poll(timeout, hasData)) {
return false;
}
if(!hasData) {
return false;
}
size_t responseSize = response.size();
if(!recv(response.data(), responseSize)) {
return false;
}
response.resize(responseSize);
return true;
}
bool address(Address& address) const {
sockaddr_in sin;
socklen_t len = sizeof(sin);
getsockname(mFD, (sockaddr*)&sin, &len);
address = Address(sin);
return true;
}
bool join_multicast(const std::string& interfaceIP, const std::string& multicastIP) {
ip_mreq mreq;
inet_pton(AF_INET, interfaceIP.c_str(), &mreq.imr_interface);
inet_pton(AF_INET, multicastIP.c_str(), &mreq.imr_multiaddr);
return setsockopt(mFD, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq)) == 0;
}
operator bool() const { return mFD != -1; }
operator SocketHandleType() const { return mFD; }
private:
SocketHandleType mFD;
};
} // namespace icsneo
#endif // __cplusplus
#endif // __SOCKET_H_
-8
View File
@@ -1,8 +0,0 @@
#ifndef __PLATFORM_WINDOWS_H_
#define __PLATFORM_WINDOWS_H_
#if defined _WIN32
#include "icsneo/platform/windows/windows.h"
#endif
#endif
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef __DYNAMICLIB_WINDOWS_H_
#define __DYNAMICLIB_WINDOWS_H_
#include "icsneo/platform/windows.h"
#include <windows.h>
#ifndef ICSNEOC_BUILD_STATIC
#ifdef ICSNEOC_MAKEDLL
@@ -3,7 +3,7 @@
#ifdef __cplusplus
#include "icsneo/platform/windows.h"
#include <windows.h>
#include <pcap.h>
#include <memory>
-13
View File
@@ -1,13 +0,0 @@
// Include Windows.h with as few annoying defines as possible
#define NOMINMAX
#ifndef WIN32_LEAN_AND_MEAN
#define LAM_DEFINED
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#ifdef LAM_DEFINED
#undef LAM_DEFINED
#undef WIN32_LEAN_AND_MEAN
#endif
#undef NOMINMAX