Deal with buffers less hackily

This commit is contained in:
CTurt 2014-12-08 17:34:52 +00:00
parent 9c6741a450
commit 3e02d05e25
4 changed files with 30 additions and 19 deletions

View file

@ -12,7 +12,6 @@
#include "inet_pton.h" #include "inet_pton.h"
//#define DEFAULT_IP { 192, 168, 0, 4 }
#define DEFAULT_PORT 8888 #define DEFAULT_PORT 8888
enum NET_COMMANDS { enum NET_COMMANDS {
@ -28,7 +27,7 @@ struct packet {
struct { struct {
short x; short x;
short y; short y;
} cstick; } circlePad;
struct { struct {
unsigned short x; unsigned short x;
@ -38,9 +37,10 @@ struct packet {
extern int sock; extern int sock;
extern struct sockaddr_in sain, saout; 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); bool openSocket(int port);
void sendBuf(int length); void sendBuf(int length);
void sendConnectionRequest(void); void sendConnectionRequest(void);
void sendKeys(unsigned int keys, circlePosition cstick, touchPosition touch); void sendKeys(unsigned int keys, circlePosition circlePad, touchPosition touch);

View file

@ -2,7 +2,8 @@
int sock; int sock;
struct sockaddr_in sain, saout; 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) { bool openSocket(int port) {
sock = socket(AF_INET, SOCK_DGRAM, 0); sock = socket(AF_INET, SOCK_DGRAM, 0);
@ -19,23 +20,31 @@ bool openSocket(int port) {
} }
void sendBuf(int length) { 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) { void sendConnectionRequest(void) {
outBuf[0] = CONNECT; outBuf.command = CONNECT;
sendBuf(1); sendBuf(1);
} }
void sendKeys(unsigned int keys, circlePosition cstick, touchPosition touch) { void sendKeys(unsigned int keys, circlePosition circlePad, touchPosition touch) {
outBuf[0] = KEYS; //outBuf[0] = KEYS;
outBuf.command = keys;
//memcpy(outBuf + 1, &keys, 4); //memcpy(outBuf + 1, &keys, 4);
//memcpy(outBuf + 5, &cstick, 4); //memcpy(outBuf + 5, &cstick, 4);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing" //#pragma GCC diagnostic push
memcpy(&((struct packet *)outBuf)->keys, &keys, 4); //#pragma GCC diagnostic ignored "-Wstrict-aliasing"
memcpy(&((struct packet *)outBuf)->cstick, &cstick, 4); //memcpy(&((struct packet *)outBuf)->keys, &keys, 4);
memcpy(&((struct packet *)outBuf)->touch, &touch, 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)); sendBuf(sizeof(struct packet));
#pragma GCC diagnostic pop
} }

View file

@ -38,7 +38,8 @@ extern struct sockaddr_in client_in;
extern int sockaddr_in_sizePtr; 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]; extern char hostName[80];
void initNetwork(void); void initNetwork(void);

View file

@ -9,7 +9,8 @@ struct sockaddr_in client_in;
int sockaddr_in_sizePtr = (int)sizeof(struct sockaddr_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]; char hostName[80];
void initNetwork(void) { void initNetwork(void) {
@ -48,11 +49,11 @@ void startListening(void) {
} }
void sendBuffer(int length) { 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"); error("sendto");
} }
} }
int receiveBuffer(int length) { 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);
} }