summaryrefslogtreecommitdiff
path: root/src/networking/packets.c
blob: cc7ef006039c0d47133b466b7790daf2ea276371 (plain)
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
123
124
125
126
127
128
129
#include <libultraship.h>
#include <SDL2/SDL_net.h>
#include "networking.h"
#include "code_800029B0.h"

void handleJoinPacket(const char* data) {
    printf("Join packet received: %s\n", data);
    // Handle join logic here
}

void handleLeavePacket(const char* data) {
    printf("Leave packet received: %s\n", data);
    // Handle leave logic here
}

void handleMessagePacket(const char* data) {
    printf("%s\n", data);
    // Handle message logic here
}

void handleMessageNumberPacket(const char* data) {
    printf("%s\n", data);
    // Handle message logic here
}

void handle_start_game() {
    gNetwork.playersLoaded = true;
    gIsGamePaused = false;
}

#define BUFFER_SIZE 1024
void send_data_packet(TCPsocket socket, int type, const uint8_t* payload, size_t payload_size) {
    // uint8_t buffer[BUFFER_SIZE];
    // size_t packet_size = sizeof(type) + payload_size;
    // if (packet_size > BUFFER_SIZE) {
    //     fprintf(stderr, "Packet size exceeds buffer size\n");
    //     return;
    // }

    // memcpy(buffer, &type, sizeof(type));
    // memcpy(buffer + sizeof(type), payload, payload_size);

    // int len = SDLNet_TCP_Send(socket, buffer, packet_size);
    // if (len < packet_size) {
    //     fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
    // }
}

void send_str_packet(TCPsocket socket, uint8_t type, const char* payload) {
    char buffer[BUFFER_SIZE];
    int offset = 0;

    // Copy the integer type into the buffer
    buffer[offset] = type;
    offset += sizeof(uint8_t);

    // Copy the payload into the buffer
    int size = strlen(payload);
    if (offset + size >= BUFFER_SIZE) {
        fprintf(stderr, "Payload is too large to fit in buffer\n");
        return;
    }

    *(uint16_t*) (buffer + offset) = size;
    offset += sizeof(uint16_t);

    memcpy(buffer + offset, payload, size);
    offset += size;

    // Send the buffer through the socket
    int len = SDLNet_TCP_Send(socket, buffer, offset);
    if (len < offset) {
        fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
    }
}

// void send_packet(TCPsocket socket, uint8_t type, const char *payload, uint16_t size) {
//     // Ensure the buffer is large enough to hold the type, colon, and payload
//     if (sizeof(int) + 1 + size > BUFFER_SIZE) {
//         fprintf(stderr, "Sending data too big for the buffer\n");
//         return;
//     }

//     char buffer[BUFFER_SIZE];
//     int offset = 0;

//     // Copy the type into the buffer
//     buffer[offset] = type;
//     offset += sizeof(uint8_t);

//     // Copy the payload into the buffer
//     memcpy(buffer + offset, payload, size);
//     offset += size;

//     // Send the buffer through the socket
//     int len = SDLNet_TCP_Send(socket, buffer, offset);
//     if (len < offset) {
//         fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
//     }
// }

void send_int_packet(TCPsocket socket, uint8_t type, uint32_t payload, uint16_t size) {
    // Ensure the buffer is large enough to hold the type, colon, and payload
    if (sizeof(int) + 1 + size > BUFFER_SIZE) {
        fprintf(stderr, "Sending data too big for the buffer\n");
        return;
    }

    char buffer[BUFFER_SIZE];
    int offset = 0;

    // Write the packet type into the buffer
    buffer[offset] = type;
    offset += sizeof(uint8_t);

    // Write the data size into the buffer
    *(uint16_t*) (buffer + offset) = size;
    offset += sizeof(uint16_t);

    // Write the payload into the buffer
    *(uint32_t*) (buffer + offset) = payload;
    offset += sizeof(uint32_t);

    // Send the buffer through the socket
    int len = SDLNet_TCP_Send(socket, buffer, offset);
    if (len < offset) {
        fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
    }
}