Deal with buffers less hackily
This commit is contained in:
parent
9c6741a450
commit
3e02d05e25
4 changed files with 30 additions and 19 deletions
|
@ -12,7 +12,6 @@
|
|||
|
||||
#include "inet_pton.h"
|
||||
|
||||
//#define DEFAULT_IP { 192, 168, 0, 4 }
|
||||
#define DEFAULT_PORT 8888
|
||||
|
||||
enum NET_COMMANDS {
|
||||
|
@ -28,7 +27,7 @@ struct packet {
|
|||
struct {
|
||||
short x;
|
||||
short y;
|
||||
} cstick;
|
||||
} circlePad;
|
||||
|
||||
struct {
|
||||
unsigned short x;
|
||||
|
@ -38,9 +37,10 @@ struct packet {
|
|||
|
||||
extern int sock;
|
||||
extern struct sockaddr_in sain, saout;
|
||||
extern char outBuf[sizeof(struct packet)], rcvBuf[sizeof(struct packet)];
|
||||
//extern char outBuf[sizeof(struct packet)], rcvBuf[sizeof(struct packet)];
|
||||
extern struct packet outBuf, rcvBuf;
|
||||
|
||||
bool openSocket(int port);
|
||||
void sendBuf(int length);
|
||||
void sendConnectionRequest(void);
|
||||
void sendKeys(unsigned int keys, circlePosition cstick, touchPosition touch);
|
||||
void sendKeys(unsigned int keys, circlePosition circlePad, touchPosition touch);
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
int sock;
|
||||
struct sockaddr_in sain, saout;
|
||||
char outBuf[sizeof(struct packet)], rcvBuf[sizeof(struct packet)];
|
||||
//char outBuf[sizeof(struct packet)], rcvBuf[sizeof(struct packet)];
|
||||
struct packet outBuf, rcvBuf;
|
||||
|
||||
bool openSocket(int port) {
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
@ -19,23 +20,31 @@ bool openSocket(int port) {
|
|||
}
|
||||
|
||||
void sendBuf(int length) {
|
||||
sendto(sock, outBuf, length, 0, (struct sockaddr *)&saout, sizeof(saout));
|
||||
sendto(sock, (char *)&outBuf, length, 0, (struct sockaddr *)&saout, sizeof(saout));
|
||||
}
|
||||
|
||||
void sendConnectionRequest(void) {
|
||||
outBuf[0] = CONNECT;
|
||||
outBuf.command = CONNECT;
|
||||
sendBuf(1);
|
||||
}
|
||||
|
||||
void sendKeys(unsigned int keys, circlePosition cstick, touchPosition touch) {
|
||||
outBuf[0] = KEYS;
|
||||
void sendKeys(unsigned int keys, circlePosition circlePad, touchPosition touch) {
|
||||
//outBuf[0] = KEYS;
|
||||
outBuf.command = keys;
|
||||
|
||||
//memcpy(outBuf + 1, &keys, 4);
|
||||
//memcpy(outBuf + 5, &cstick, 4);
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
memcpy(&((struct packet *)outBuf)->keys, &keys, 4);
|
||||
memcpy(&((struct packet *)outBuf)->cstick, &cstick, 4);
|
||||
memcpy(&((struct packet *)outBuf)->touch, &touch, 4);
|
||||
|
||||
//#pragma GCC diagnostic push
|
||||
//#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
||||
//memcpy(&((struct packet *)outBuf)->keys, &keys, 4);
|
||||
//memcpy(&((struct packet *)outBuf)->cstick, &cstick, 4);
|
||||
//memcpy(&((struct packet *)outBuf)->touch, &touch, 4);
|
||||
//sendBuf(sizeof(struct packet));
|
||||
//#pragma GCC diagnostic pop
|
||||
|
||||
memcpy(&outBuf.keys, &keys, 4);
|
||||
memcpy(&outBuf.circlePad, &circlePad, 4);
|
||||
memcpy(&outBuf.touch, &touch, 4);
|
||||
sendBuf(sizeof(struct packet));
|
||||
#pragma GCC diagnostic pop
|
||||
}
|
||||
|
|
|
@ -38,7 +38,8 @@ extern struct sockaddr_in client_in;
|
|||
|
||||
extern int sockaddr_in_sizePtr;
|
||||
|
||||
extern char buffer[sizeof(struct packet)];
|
||||
//extern char buffer[sizeof(struct packet)];
|
||||
extern struct packet buffer;
|
||||
extern char hostName[80];
|
||||
|
||||
void initNetwork(void);
|
||||
|
|
|
@ -9,7 +9,8 @@ struct sockaddr_in client_in;
|
|||
|
||||
int sockaddr_in_sizePtr = (int)sizeof(struct sockaddr_in);
|
||||
|
||||
char buffer[sizeof(struct packet)];
|
||||
//char buffer[sizeof(struct packet)];
|
||||
struct packet buffer;
|
||||
char hostName[80];
|
||||
|
||||
void initNetwork(void) {
|
||||
|
@ -48,11 +49,11 @@ void startListening(void) {
|
|||
}
|
||||
|
||||
void sendBuffer(int length) {
|
||||
if(sendto(listener, buffer, length, 0, (struct sockaddr *)&client_in, sizeof(struct sockaddr_in)) != length) {
|
||||
if(sendto(listener, (char *)&buffer, length, 0, (struct sockaddr *)&client_in, sizeof(struct sockaddr_in)) != length) {
|
||||
error("sendto");
|
||||
}
|
||||
}
|
||||
|
||||
int receiveBuffer(int length) {
|
||||
return recvfrom(listener, buffer, length, 0, (struct sockaddr *)&client_in, &sockaddr_in_sizePtr);
|
||||
return recvfrom(listener, (char *)&buffer, length, 0, (struct sockaddr *)&client_in, &sockaddr_in_sizePtr);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue