generated from AfonsoCMSousa/CPP-Template
Feature: First handshakes and Information Tradding
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
#ifndef SERVER_STRUCTS_H
|
||||
#define SERVER_STRUCTS_H
|
||||
|
||||
#include <cstdint>
|
||||
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;
|
||||
int32_t 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;
|
||||
int32_t version;
|
||||
|
||||
// This is the type of operation required by the client.
|
||||
// The following operations are now available:
|
||||
@@ -25,8 +26,28 @@ struct handshake {
|
||||
//
|
||||
// DISMISS = 3 :
|
||||
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
||||
int operationId;
|
||||
int32_t operationId;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct handshackerResponse{
|
||||
// is the name of the car that the player is driving on the AC Server
|
||||
char carName[50];
|
||||
|
||||
// is the name of the driver running on the AC Server
|
||||
char driverName[50];
|
||||
|
||||
// for now is just 4242, this code will identify different status,
|
||||
// as “NOT AVAILABLE” for connection
|
||||
int32_t identifier;
|
||||
|
||||
// for now is set to 1, this will identify the version running on the AC Server
|
||||
int32_t version;
|
||||
|
||||
// is the name of the track on the AC Server
|
||||
char trackName[50];
|
||||
|
||||
// is the track configuration on the AC Server
|
||||
char trackConfig[50];
|
||||
} __attribute__((packed));
|
||||
|
||||
#endif // SERVER_STRUCTS_H
|
||||
|
||||
+10
-4
@@ -1,6 +1,12 @@
|
||||
#include "socket.h"
|
||||
|
||||
int connect_udp_socket(const char* ip, int port) {
|
||||
// =========================
|
||||
// UDP SOCKET FUCNTIONS
|
||||
// =========================
|
||||
// @param ip: IP address to connect to
|
||||
// @param port: Port number to connect to
|
||||
// @return: Socket file descriptor, or -1 on error
|
||||
int connect_udp_socket(const char* ip, uint16_t port) {
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket creation failed");
|
||||
@@ -23,7 +29,7 @@ int connect_udp_socket(const char* ip, int port) {
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
int bind_udp_socket(int sockfd, const char* ip, int port) {
|
||||
int bind_udp_socket(int sockfd, const char* ip, uint16_t port) {
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
|
||||
@@ -40,7 +46,7 @@ int bind_udp_socket(int sockfd, const char* ip, int port) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int send_udp_message(int sockfd, const char *message, const char *dest_ip, int dest_port) {
|
||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port) {
|
||||
struct sockaddr_in destaddr;
|
||||
memset(&destaddr, 0, sizeof(destaddr));
|
||||
|
||||
@@ -48,7 +54,7 @@ int send_udp_message(int sockfd, const char *message, const char *dest_ip, int d
|
||||
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));
|
||||
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
||||
if (n < 0) {
|
||||
perror("sendto failed");
|
||||
return -1;
|
||||
|
||||
+20
-5
@@ -1,11 +1,22 @@
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
// =========================
|
||||
// UDP SOCKET FUCNTIONS
|
||||
@@ -13,11 +24,15 @@
|
||||
// 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 connect_udp_socket(const char *ip, uint16_t port);
|
||||
|
||||
int bind_udp_socket(int sockfd, const char* ip, int port);
|
||||
int bind_udp_socket(int sockfd, const char *ip, uint16_t port);
|
||||
|
||||
int send_udp_message(int sockfd, const char* message, const char* dest_ip, int dest_port);
|
||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip,
|
||||
uint16_t dest_port);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SOCKET_H
|
||||
|
||||
|
||||
Reference in New Issue
Block a user