generated from AfonsoCMSousa/CPP-Template
New: Base Project.
This commit is contained in:
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user