send and receive simultaneously
values were ok, EDDB has a mag derivation of ~4° *facepalm*
This commit is contained in:
parent
6c04cdf97f
commit
ace99dfe93
@ -14,17 +14,17 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// network
|
// network
|
||||||
xPlaneSocket = connectXPlane(DEST_SERVER, DEST_PORT, SRC_PORT);
|
xPlaneSocket = createSocket(SRC_PORT);
|
||||||
if (xPlaneSocket < 0) {
|
if (xPlaneSocket < 0) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
if (sendToXPlane(xPlaneSocket, "CMND", argv[1], strlen(argv[1])) < 0) {
|
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
disconnectXPlane(xPlaneSocket);
|
closeSocket(xPlaneSocket);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
35
network.c
35
network.c
@ -15,8 +15,9 @@
|
|||||||
const int DGRAM_MSG_START = 0;
|
const int DGRAM_MSG_START = 0;
|
||||||
const int DGRAM_PAYLOAD_START = DGRAM_MSG_START + DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH;
|
const int DGRAM_PAYLOAD_START = DGRAM_MSG_START + DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH;
|
||||||
|
|
||||||
int sendToXPlane(int sock, char *msg, void *payload, int lengthOfPayload) {
|
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload) {
|
||||||
char bytesToSend[SEND_BUFFER];
|
char bytesToSend[SEND_BUFFER];
|
||||||
|
struct sockaddr_in6 xPlaneAddress;
|
||||||
memset(bytesToSend, 0, SEND_BUFFER);
|
memset(bytesToSend, 0, SEND_BUFFER);
|
||||||
|
|
||||||
// msg is always 4 bytes long
|
// msg is always 4 bytes long
|
||||||
@ -32,7 +33,12 @@ int sendToXPlane(int sock, char *msg, void *payload, int lengthOfPayload) {
|
|||||||
strcpy(&bytesToSend[DGRAM_MSG_START], msg);
|
strcpy(&bytesToSend[DGRAM_MSG_START], msg);
|
||||||
memcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload, lengthOfPayload);
|
memcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload, lengthOfPayload);
|
||||||
|
|
||||||
int numBytesSend = send(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0);
|
// prepare destination address struct
|
||||||
|
xPlaneAddress.sin6_family = AF_INET6;
|
||||||
|
xPlaneAddress.sin6_port = htons(destPort);
|
||||||
|
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr);
|
||||||
|
|
||||||
|
int numBytesSend = sendto(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
|
||||||
if (numBytesSend >= 0) {
|
if (numBytesSend >= 0) {
|
||||||
return numBytesSend;
|
return numBytesSend;
|
||||||
} else {
|
} else {
|
||||||
@ -46,11 +52,6 @@ int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxL
|
|||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, 0);
|
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, 0);
|
||||||
if (recvBytes >= 0) {
|
if (recvBytes >= 0) {
|
||||||
printf("received (%d): ", recvBytes);
|
|
||||||
for (int i = 0; i < recvBytes; i++) {
|
|
||||||
printf("%02x ", buf[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
if (recvBytes >= DGRAM_MSG_LENGTH) {
|
if (recvBytes >= DGRAM_MSG_LENGTH) {
|
||||||
memcpy(msg, &buf[0], DGRAM_MSG_LENGTH);
|
memcpy(msg, &buf[0], DGRAM_MSG_LENGTH);
|
||||||
memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength);
|
memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength);
|
||||||
@ -66,9 +67,9 @@ int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxL
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int connectXPlane(char *destAddr, int destPort, int srcPort) {
|
int createSocket(int srcPort) {
|
||||||
int xPlaneSocket;
|
int xPlaneSocket;
|
||||||
struct sockaddr_in6 xPlaneAddress, localAddress;
|
struct sockaddr_in6 localAddress;
|
||||||
|
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
@ -82,7 +83,6 @@ int connectXPlane(char *destAddr, int destPort, int srcPort) {
|
|||||||
// prepare source address struct
|
// prepare source address struct
|
||||||
localAddress.sin6_family = AF_INET6;
|
localAddress.sin6_family = AF_INET6;
|
||||||
localAddress.sin6_port = htons(srcPort);
|
localAddress.sin6_port = htons(srcPort);
|
||||||
//inet_pton(AF_INET6, in6addr_any, &localAddress.sin6_addr.s6_addr);
|
|
||||||
localAddress.sin6_addr = in6addr_any;
|
localAddress.sin6_addr = in6addr_any;
|
||||||
|
|
||||||
// bind to local port
|
// bind to local port
|
||||||
@ -92,22 +92,9 @@ int connectXPlane(char *destAddr, int destPort, int srcPort) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare destination address struct
|
|
||||||
xPlaneAddress.sin6_family = AF_INET6;
|
|
||||||
xPlaneAddress.sin6_port = htons(destPort);
|
|
||||||
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr);
|
|
||||||
|
|
||||||
// connect to xPlane
|
|
||||||
//err = connect(xPlaneSocket, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
|
|
||||||
if (err) {
|
|
||||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", destAddr, destPort, errno, gai_strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return xPlaneSocket;
|
return xPlaneSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
int disconnectXPlane(int sock) {
|
void closeSocket(int sock) {
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
|
||||||
close(sock);
|
close(sock);
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
extern const int DGRAM_MSG_START;
|
extern const int DGRAM_MSG_START;
|
||||||
extern const int DGRAM_PAYLOAD_START;
|
extern const int DGRAM_PAYLOAD_START;
|
||||||
|
|
||||||
int sendToXPlane(int sock, char *msg, void *payload, int lengthOfPayload);
|
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload);
|
||||||
int recvFromXPlane(int sock, char msg[4], void *payload, int maxLength);
|
int recvFromXPlane(int sock, char msg[4], void *payload, int maxLength);
|
||||||
int connectXPlane(char *destAddr, int destPort, int srcPort);
|
int createSocket(int srcPort);
|
||||||
int disconnectXPlane(int sock);
|
void closeSocket(int sock);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user