mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-09 11:30:43 +02:00
initial fixing
This commit is contained in:
parent
94fcf52e64
commit
8f64449117
@ -19,3 +19,17 @@ target_link_libraries(result
|
||||
set_target_properties(result PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
|
||||
)
|
||||
|
||||
add_executable(udp udp.cpp)
|
||||
target_link_libraries(udp
|
||||
slsDetectorShared
|
||||
slsSupportLib
|
||||
pthread
|
||||
rt
|
||||
)
|
||||
|
||||
set_target_properties(udp PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
|
||||
)
|
||||
|
||||
|
||||
|
13
sample/udp.cpp
Normal file
13
sample/udp.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "UdpSocket.h"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
int main(){
|
||||
std::cout << "HEJ\n";
|
||||
sls::UdpSocket s(50010, 1024);
|
||||
|
||||
while(true){
|
||||
std::cout << "Got: " << s.ReceivePacket() << " bytes\n";
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
}
|
56
slsSupportLib/include/UdpSocket.h
Normal file
56
slsSupportLib/include/UdpSocket.h
Normal file
@ -0,0 +1,56 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <errno.h>
|
||||
#include <iostream>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
class UdpSocket {
|
||||
int port;
|
||||
size_t packet_size;
|
||||
size_t buffer_size;
|
||||
int fd = -1;
|
||||
|
||||
public:
|
||||
UdpSocket(int port, size_t packet_size)
|
||||
: port(port), packet_size(packet_size) {
|
||||
const char *hostname = 0; /* wildcard */
|
||||
const std::string portname = std::to_string(port);
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_protocol = 0;
|
||||
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
|
||||
struct addrinfo *res = 0;
|
||||
if (getaddrinfo(hostname, portname.c_str(), &hints, &res)){
|
||||
throw RuntimeError("Failed getaddinfo");
|
||||
}
|
||||
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if(fd==-1){
|
||||
throw RuntimeError("Failed creating socket");
|
||||
}
|
||||
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
|
||||
throw RuntimeError("Failed to bind socket");
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
}
|
||||
|
||||
int ReceivePacket() {
|
||||
char buffer[549];
|
||||
struct sockaddr_storage src_addr;
|
||||
socklen_t src_addr_len = sizeof(src_addr);
|
||||
ssize_t count = recvfrom(fd, buffer, sizeof(buffer), 0,
|
||||
(struct sockaddr *)&src_addr, &src_addr_len);
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sls
|
Loading…
x
Reference in New Issue
Block a user