generated from AfonsoCMSousa/CPP-Template
New: Base Project.
This commit is contained in:
parent
a53ad977eb
commit
6485986a5c
@ -15,36 +15,35 @@ include_directories(${CMAKE_SOURCE_DIR}/libraries)
|
||||
|
||||
# Gather all source files (.cpp, .c)
|
||||
file(GLOB_RECURSE PROJECT_SOURCES
|
||||
${CMAKE_SOURCE_DIR}/include/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/include/*.c
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.c
|
||||
${CMAKE_SOURCE_DIR}/source/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/source/*.c
|
||||
${CMAKE_SOURCE_DIR}/include/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/include/*.c
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.c
|
||||
${CMAKE_SOURCE_DIR}/source/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/source/*.c
|
||||
)
|
||||
|
||||
# Gather all header files (.hpp, .h)
|
||||
file(GLOB_RECURSE PROJECT_HEADERS
|
||||
${CMAKE_SOURCE_DIR}/include/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/include/*.h
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.h
|
||||
${CMAKE_SOURCE_DIR}/source/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/source/*.h
|
||||
${CMAKE_SOURCE_DIR}/include/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/include/*.h
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.h
|
||||
${CMAKE_SOURCE_DIR}/source/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/source/*.h
|
||||
)
|
||||
|
||||
# Allow user to set output program name
|
||||
option(OUTPUT_NAME "Name of the output executable" "")
|
||||
if(OUTPUT_NAME STREQUAL "")
|
||||
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
||||
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
||||
else()
|
||||
set(EXECUTABLE_NAME ${OUTPUT_NAME})
|
||||
set(EXECUTABLE_NAME ${OUTPUT_NAME})
|
||||
endif()
|
||||
|
||||
# Add executable with all sources
|
||||
add_executable(${EXECUTABLE_NAME}
|
||||
${PROJECT_SOURCES}
|
||||
${PROJECT_HEADERS}
|
||||
${PROJECT_SOURCES}
|
||||
)
|
||||
|
||||
# Set output name property (for consistency)
|
||||
@ -52,22 +51,22 @@ set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAM
|
||||
|
||||
# Enable warnings and extra diagnostics
|
||||
if (MSVC)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze)
|
||||
else()
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wconversion
|
||||
-Wsign-conversion
|
||||
-Wuninitialized
|
||||
-Wunused
|
||||
-Werror=return-type
|
||||
-fsanitize=address,undefined
|
||||
-g
|
||||
)
|
||||
target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wconversion
|
||||
-Wsign-conversion
|
||||
-Wuninitialized
|
||||
-Wunused
|
||||
-Werror=return-type
|
||||
-fsanitize=address,undefined
|
||||
-g
|
||||
)
|
||||
target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined)
|
||||
endif()
|
||||
|
||||
# Optionally, enable testing
|
||||
|
||||
BIN
PlayerTracker
Executable file
BIN
PlayerTracker
Executable file
Binary file not shown.
1
compile_commands.json
Symbolic link
1
compile_commands.json
Symbolic link
@ -0,0 +1 @@
|
||||
build/compile_commands.json
|
||||
32
include/server_structs.h
Normal file
32
include/server_structs.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef SERVER_STRUCTS_H
|
||||
#define SERVER_STRUCTS_H
|
||||
|
||||
struct handshake {
|
||||
// [not used in the current Remote Telemtry version by AC]
|
||||
// In future versions it will identify the platform type of the client.
|
||||
// This will be used to adjust a specific behaviour for each platform. (eIPadDevice for now (1))
|
||||
int identifier;
|
||||
|
||||
// [not used in the current Remote Telemtry version by AC]
|
||||
// In future version this field will identify the AC Remote Telemetry version that the device expects to speak with.
|
||||
int version;
|
||||
|
||||
// This is the type of operation required by the client.
|
||||
// The following operations are now available:
|
||||
// ----------------------------------------------------------------
|
||||
// HANDSHAKE = 0 :
|
||||
// This operation identifier must be set when the client wants to start the comunication.
|
||||
//
|
||||
// SUBSCRIBE_UPDATE = 1 :
|
||||
// This operation identifier must be set when the client wants to be updated from the specific ACServer.
|
||||
//
|
||||
// SUBSCRIBE_SPOT = 2 :
|
||||
// This operation identifier must be set when the client wants to be updated from the specific ACServer just for SPOT Events (e.g.: the end of a lap).
|
||||
//
|
||||
// DISMISS = 3 :
|
||||
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
||||
int operationId;
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
#endif // SERVER_STRUCTS_H
|
||||
58
include/socket.c
Normal file
58
include/socket.c
Normal file
@ -0,0 +1,58 @@
|
||||
#include "socket.h"
|
||||
|
||||
int connect_udp_socket(const char* ip, int port) {
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket creation failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(port);
|
||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||
|
||||
if (connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
||||
perror("connection to the server failed");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
int bind_udp_socket(int sockfd, const char* ip, int port) {
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||
servaddr.sin_port = htons(port);
|
||||
|
||||
if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
||||
perror("bind failed");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int send_udp_message(int sockfd, const char *message, const char *dest_ip, int dest_port) {
|
||||
struct sockaddr_in destaddr;
|
||||
memset(&destaddr, 0, sizeof(destaddr));
|
||||
|
||||
destaddr.sin_family = AF_INET;
|
||||
destaddr.sin_port = htons(dest_port);
|
||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
||||
|
||||
int n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
||||
if (n < 0) {
|
||||
perror("sendto failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
23
include/socket.h
Normal file
23
include/socket.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
|
||||
// =========================
|
||||
// UDP SOCKET FUCNTIONS
|
||||
// =========================
|
||||
// Server hosts a UDP socket at 127.0.0.1:12000
|
||||
// Client sends a message to the server at 11000
|
||||
|
||||
int connect_udp_socket(const char* ip, int port);
|
||||
|
||||
int bind_udp_socket(int sockfd, const char* ip, int port);
|
||||
|
||||
int send_udp_message(int sockfd, const char* message, const char* dest_ip, int dest_port);
|
||||
|
||||
#endif // SOCKET_H
|
||||
|
||||
@ -1,7 +1,36 @@
|
||||
#include <ctype.h>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server_structs.h"
|
||||
#include "socket.h"
|
||||
|
||||
const int SERVER_OUT_PORT = 12000;
|
||||
const int SERVER_IN_PORT = 11000;
|
||||
const char *SERVER_IP = "127.0.0.1";
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("[+] Starting server...\n");
|
||||
int n = connect_udp_socket(SERVER_IP, SERVER_OUT_PORT);
|
||||
if (n < 0) {
|
||||
fprintf(stderr, "[-] Failed to connect to UDP socket at %s:%d\n", SERVER_IP, SERVER_OUT_PORT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TEST to see what server is sending:
|
||||
char buffer[1024];
|
||||
n = recvfrom(0, buffer, sizeof(buffer)-1, 0, NULL, NULL);
|
||||
if (n < 0) {
|
||||
perror("recvfrom failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer[n] = '\0'; // Null-terminate the received message
|
||||
printf("Received message: %s\n", buffer);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user