-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_processing.cpp
More file actions
122 lines (100 loc) · 2.66 KB
/
packet_processing.cpp
File metadata and controls
122 lines (100 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#include <string.h>
#include <unistd.h>
typedef struct Position{
int player;
int x;
int y;
}Position;
void read_UDP(Position *pXY);
void send_UDP(Position *pXY);
int main(void){
pid_t io;
Position * pXY = (Position*) malloc(sizeof(Position));
pXY->player = 0;
pXY->x = 1;
pXY->y = 1;
sleep(10);
io = fork();
if(io == 0){
send_UDP(pXY);
}
else{
pXY->player = 32;
read_UDP(pXY);
printf("Player: %d, X: %d, Y: %d", pXY->player, pXY->x, pXY->y);
}
}
void read_UDP(Position * pXY){
UDPsocket sd = 0; /* Socket descriptor */
UDPpacket * p; /* Pointer to packet memory */
int quit = 0;
if (SDLNet_Init() < 0)
{
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
/* Open a socket */
if (!(sd = SDLNet_UDP_Open(2000))) // opens UDP on port 2000
{
fprintf(stderr, "SDLNet_UDP_Open: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
/* Make space for the packet */
if (!(p = SDLNet_AllocPacket(sizeof(Position)))) //allocs a packet size, the position struct
{
fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
while (!quit)
{
/* Wait a packet. UDP_Recv returns != 0 if a packet is coming */
if (SDLNet_UDP_Recv(sd, p))
pXY = (Position*)p->data; // casting issues damnit
}
/* Clean and exit */
SDLNet_FreePacket(p);
SDLNet_Quit();
}
void send_UDP(Position *pXY){
UDPsocket sd;
IPaddress srvadd;
UDPpacket *p;
int quit = 0;
/* Initialize SDL_net */
if (SDLNet_Init() < 0)
{
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
/* Open a socket on random port */
if (!(sd = SDLNet_UDP_Open(0))) //opens port 2000
{
fprintf(stderr, "SDLNet_UDP_Open: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
/* Resolve server name */
if (SDLNet_ResolveHost(&srvadd, "142.232.112.249", 2000) == -1) // trying to send it to myself for testing purposes
{
fprintf(stderr, "SDLNet_ResolveHost");
exit(EXIT_FAILURE);
}
/* Allocate memory for the packet */
if (!(p = SDLNet_AllocPacket(512)))
{
fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
while (!quit)
{
p->address.host = srvadd.host; /* Set the destination host */
p->address.port = srvadd.port; /* And destination port */
p->len = sizeof(Position);
p->data = (Uint8*)pXY; //casting issues.. still figuring it out
quit = SDLNet_UDP_Send(sd, -1, p); /* This sets the p->channel */
}
SDLNet_FreePacket(p);
SDLNet_Quit();
}