File: multiplayer_client.cpp

package info (click to toggle)
seriousproton 2020.01.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 720 kB
  • sloc: cpp: 7,666; ansic: 376; php: 59; makefile: 15
file content (227 lines) | stat: -rw-r--r-- 7,253 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "multiplayer_client.h"
#include "multiplayer.h"
#include "multiplayer_internal.h"
#include "engine.h"

P<GameClient> game_client;

GameClient::GameClient(int version_number, sf::IpAddress server, int port_nr)
: version_number(version_number), server(server), port_nr(port_nr), connect_thread(&GameClient::runConnect, this)
{
    assert(!game_server);
    assert(!game_client);

    client_id = -1;
    game_client = this;
    status = ReadyToConnect;

    last_receive_time.restart();
}

GameClient::~GameClient()
{
    connect_thread.wait();
}

P<MultiplayerObject> GameClient::getObjectById(int32_t id)
{
    if (objectMap.find(id) != objectMap.end())
        return objectMap[id];
    return NULL;
}

void GameClient::update(float delta)
{
    if (status == ReadyToConnect)
    {
        status = Connecting;
        connect_thread.launch();
    }
    if (status == Disconnected || status == Connecting)
        return;

    std::vector<int32_t> delList;
    for(std::unordered_map<int32_t, P<MultiplayerObject> >::iterator i=objectMap.begin(); i != objectMap.end(); i++)
    {
        int id = i->first;
        P<MultiplayerObject> obj = i->second;
        if (!obj)
            delList.push_back(id);
    }
    for(unsigned int n=0; n<delList.size(); n++)
        objectMap.erase(delList[n]);

    socket.update();
    sf::Packet reply;
    sf::Packet packet;
    sf::TcpSocket::Status socket_status;
    while((socket_status = socket.receive(packet)) == sf::TcpSocket::Done)
    {
        last_receive_time.restart();

        command_t command;
        packet >> command;
        switch(status)
        {
        case ReadyToConnect:
        case Connecting:
        case Authenticating:
        case WaitingForPassword:
            switch(command)
            {
            case CMD_REQUEST_AUTH:
                {
                    int32_t server_version;
                    bool require_password;
                    packet >> server_version >> require_password;

		    if (server_version != 0 && server_version != version_number)
		    {
                        LOG(INFO) << "Server version " << server_version << " does not match client version " << version_number;
		    }
                    
                    if (!require_password)
                    {
                        reply.clear();
                        reply << CMD_CLIENT_SEND_AUTH << int32_t(version_number) << string("");
                        socket.send(reply);
                    }else{
                        status = WaitingForPassword;
                    }
                }
                break;
            case CMD_SET_CLIENT_ID:
                packet >> client_id;
                status = Connected;
                break;
            case CMD_ALIVE:
                // send response to calculate ping
                reply.clear();
                reply << CMD_ALIVE_RESP;
                socket.send(reply);
                break;
            default:
                LOG(ERROR) << "Unknown command from server: " << command;
            }
            break;
        case Connected:
            switch(command)
            {
            case CMD_CREATE:
                {
                    int32_t id;
                    string name;
                    packet >> id >> name;
                    for(MultiplayerClassListItem* i = multiplayerClassListStart; i; i = i->next)
                    {
                        if (i->name == name)
                        {
                            LOG(INFO) << "Created " << name << " from server replication";
                            MultiplayerObject* obj = i->func();
                            obj->multiplayerObjectId = id;
                            objectMap[id] = obj;

                            int16_t idx;
                            while(packet >> idx)
                            {
                                if (idx >= 0 && idx < int16_t(obj->memberReplicationInfo.size()))
                                    (obj->memberReplicationInfo[idx].receiveFunction)(obj->memberReplicationInfo[idx].ptr, packet);
                                else
                                    LOG(DEBUG) << "Odd index from server replication: " << idx;
                            }
                        }
                    }
                }
                break;
            case CMD_DELETE:
                {
                    int32_t id;
                    packet >> id;
                    if (objectMap.find(id) != objectMap.end() && objectMap[id])
                        objectMap[id]->destroy();
                }
                break;
            case CMD_UPDATE_VALUE:
                {
                    int32_t id;
                    int16_t idx;
                    packet >> id;
                    if (objectMap.find(id) != objectMap.end() && objectMap[id])
                    {
                        P<MultiplayerObject> obj = objectMap[id];
                        while(packet >> idx)
                        {
                            if (idx < int32_t(obj->memberReplicationInfo.size()))
                                (obj->memberReplicationInfo[idx].receiveFunction)(obj->memberReplicationInfo[idx].ptr, packet);
                        }
                    }
                }
                break;
            case CMD_SET_GAME_SPEED:
                {
                    float gamespeed;
                    packet >> gamespeed;
                    engine->setGameSpeed(gamespeed);
                }
                break;
            case CMD_SERVER_COMMAND:
                {
                    int32_t id;
                    packet >> id;
                    if (objectMap.find(id) != objectMap.end() && objectMap[id])
                    {
                        P<MultiplayerObject> obj = objectMap[id];
                        obj->onReceiveServerCommand(packet);
                    }
                }
                break;
            case CMD_ALIVE:
                // send response to calculate ping
                reply.clear();
                reply << CMD_ALIVE_RESP;
                socket.send(reply);
                break;
            default:
                LOG(ERROR) << "Unknown command from server: " << command;
            }
        case Disconnected:
            break;
        }
    }

    if (socket_status == sf::TcpSocket::Disconnected || last_receive_time.getElapsedTime().asSeconds() > no_data_disconnect_time)
    {
        socket.disconnect();
        status = Disconnected;
    }
}

void GameClient::sendPacket(sf::Packet& packet)
{
    socket.send(packet);
}

void GameClient::sendPassword(string password)
{
    if (status != WaitingForPassword)
        return;

    sf::Packet reply;
    reply << CMD_CLIENT_SEND_AUTH << int32_t(version_number) << password;
    socket.send(reply);
    
    status = Authenticating;
}

void GameClient::runConnect()
{
    if (socket.connect(server, port_nr, sf::seconds(5)) == sf::TcpSocket::Done)
    {
        LOG(INFO) << "GameClient: Connected, waiting for authentication";
        status = Authenticating;
    }else{
        LOG(INFO) << "GameClient: Failed to connect";
        status = Disconnected;
    }
    socket.setBlocking(false);
}