File size: 4,034 Bytes
00e6582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 "client.h"
#include "shared.h"

void set_non_blocking(int fd) {
    int flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}

void network_update(struct _Seafarer_Engine_Network_Client* SENC) {
    StatePacket state_pkt;

    while (1) {
        // Peek at the stream to see how many bytes are currently waiting
        int bytes_available = recv(SENC->sock_fd, &state_pkt, sizeof(StatePacket), MSG_PEEK);

        // Handle server disconnection
        if (bytes_available == 0) {
            printf("Server disconnected gracefully.\n");
            exit(0);
        }

        // If there isn't a full StatePacket ready yet, don't touch it!
        // Break and wait until the next frame.
        if (bytes_available < (int)sizeof(StatePacket)) {
            break;
        }

        // Since we peeked and know a full packet is ready, read it for real
        int bytes_read = 0;
        while (bytes_read < sizeof(StatePacket)) {
            int ret = recv(SENC->sock_fd, ((char*)&state_pkt) + bytes_read, sizeof(StatePacket) - bytes_read, 0);
            if (ret <= 0) {
                printf("Server disconnected during stream read.\n");
                exit(0);
            }
            bytes_read += ret;
        }

        // Apply the updated world state
        if (state_pkt.type == MSG_STATE) {
            printf("Updating State\n");
            memcpy(SENC->local_players, state_pkt.players, sizeof(SENC->local_players));
        }
    }
}

void handle_input(struct _Seafarer_Engine_Network_Client* SENC,unsigned char key, int x, int y) {
    Packet move_pkt;
    move_pkt.type = MSG_MOVE;
    move_pkt.x = 0.0f;
    move_pkt.y = 0.0f;

    float speed = 0.05f;

    switch (key) {
        case 'w': move_pkt.y = speed; break;
        case 's': move_pkt.y = -speed; break;
        case 'a': move_pkt.x = -speed; break;
        case 'd': move_pkt.x = speed; break;
        case 27: exit(0); // ESC key
    }

    // Send input request to the server instead of moving locally
    if (move_pkt.x != 0.0f || move_pkt.y != 0.0f) {
        send(SENC->sock_fd, &move_pkt, sizeof(Packet), 0);
    }
}


int Connect_IPv4(struct _Seafarer_Engine_Network_Client* SENC,const char* IPv4_Address, unsigned short PORT, const char* Username) {
    struct sockaddr_in serv_addr;
    if ((SENC->sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        printf("\n Socket creation error \n");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    if (inet_pton(AF_INET, IPv4_Address, &serv_addr.sin_addr) <= 0) {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }

    if (connect(SENC->sock_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("\nConnection Failed \n");
        return -1;
    }

    printf("Connection successful!\n");
    // Send the join packet containing the username right away
    // Send the join packet containing the username right away
    Packet join_pkt;
    join_pkt.type = MSG_JOIN;
    strncpy(join_pkt.username, Username, MAX_NAME);
    send(SENC->sock_fd, &join_pkt, sizeof(Packet), 0);

    // 1. Read the welcome packet synchronously (blocking) FIRST
    Packet welcome_pkt;
    int bytes_received = 0;
    while (bytes_received < sizeof(Packet)) {
        int ret = recv(SENC->sock_fd, ((char*)&welcome_pkt) + bytes_received, sizeof(Packet) - bytes_received, 0);
        if (ret <= 0) {
            perror("Failed to receive welcome packet from server");
            return -1;
        }
        bytes_received += ret;
    }

    if (welcome_pkt.type == MSG_WELCOME) {
        SENC->my_id = welcome_pkt.player_id;
        printf("Connected successfully! Assigned Player ID: %d\n", SENC->my_id);
    } else {
        printf("Unexpected packet type received during handshake: %d\n", welcome_pkt.type);
        return -1;
    }

    // 2. NOW it is safe to turn on non-blocking mode for our game loops!
    set_non_blocking(SENC->sock_fd);
    return 1;
}