File: RemoteServer.cpp

package info (click to toggle)
burgerspace 1.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,524 kB
  • sloc: sh: 10,939; cpp: 5,791; makefile: 240
file content (394 lines) | stat: -rw-r--r-- 10,958 bytes parent folder | download | duplicates (4)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*  $Id: RemoteServer.cpp,v 1.5 2012/10/07 21:29:24 sarrazip Exp $
    RemoteServer.cpp - UDP server

    burgerspace - A hamburger-smashing video game.
    Copyright (C) 2010 Pierre Sarrazin <http://sarrazip.com/>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "RemoteServer.h"

#include "BurgerSpaceClient.h"

#include <flatzebra/Couple.h>

#include <stdio.h>
#include <errno.h>
#include <limits.h>

#ifdef _MSC_VER
#include <io.h>  // for close()
#else
#include <unistd.h>  // for close()
#endif


using namespace std;
using namespace flatzebra;


RemoteServer::RemoteServer(BurgerSpaceClient &_client,
                                                unsigned char _serverIPAddr[4],
                                                unsigned short _serverUDPPort,
                                                Role _role)
  : client(_client),
    role(_role),
    sock(socket(AF_INET, SOCK_DGRAM, 0)),
    serverUDPPort(_serverUDPPort),
    buffer(NULL),
    acc(),
    lastPlayerCommandSent()
{
    if (serverIPAddr == NULL)
        throw EINVAL;
    if (sock < 0)
        throw errno;
    memcpy(serverIPAddr, _serverIPAddr, 4);
    buffer = new char[MAX_LEN_PACKET];

#ifdef _MSC_VER
    u_long iMode = 1;
    ioctlsocket(sock, FIONBIO, &iMode);  // non blocking mode
#endif
}


//virtual
RemoteServer::~RemoteServer()
{
    if (sock >= 0)
    {
        #ifdef _MSC_VER
        closesocket(sock);
        #else
        close(sock);
        #endif
    }

    delete [] buffer;
}


void
RemoteServer::describeClient()
{
    char s[] =
    {
        char(CLIENT_DESC_PACKET),
        char(role)
    };
    const size_t len = sizeof(s) / sizeof(s[0]);
    ssize_t numBytesSent = sendToServer(s, len);
    if (numBytesSent != ssize_t(len))
        cout << PROGRAM << ": tried to send client descriptor of " << len
                << " bytes but only " << numBytesSent << " bytes sent" << endl;
}


void
RemoteServer::requestLevelDescription()
{
    char packet = char(LEVEL_DESC_REQUEST_PACKET);
    ssize_t numBytesSent = sendToServer(&packet, 1);
    if (numBytesSent != 1)
        cout << PROGRAM << ": tried to send level description request but only " << numBytesSent << " bytes sent" << endl;
}


//virtual
void
RemoteServer::startNewGame()
{
    char s[] =
    {
        char(START_NEW_GAME_PACKET)
    };
    const size_t len = sizeof(s) / sizeof(s[0]);
    ssize_t numBytesSent = sendToServer(s, len);
    if (numBytesSent != ssize_t(len))
        cout << PROGRAM << ": tried to send start request of " << len
                << " bytes but only " << numBytesSent << " bytes sent" << endl;
}


//virtual
bool
RemoteServer::isPaused() const
{
    return false;  // No player can pause the game in client-server mode.
}


//virtual
void
RemoteServer::pauseGame()
{
    // No player can pause the game in client-server mode.
}


//virtual
void
RemoteServer::resumeGame()
{
    // No player can pause the game in client-server mode.
}


//virtual
void
RemoteServer::setChefRequest(const bool desiredDirections[4], bool shootPepper)
{
    char s[] =
    {
        char(PLAYER_COMMAND_PACKET),
        char(desiredDirections[0]),
        char(desiredDirections[1]),
        char(desiredDirections[2]),
        char(desiredDirections[3]),
        char(shootPepper)
    };
    const size_t len = sizeof(s) / sizeof(s[0]);
    if (lastPlayerCommandSent.empty() || lastPlayerCommandSent.compare(0, len, s, len) != 0)
    {
        ssize_t numBytesSent = sendToServer(s, len);
        if (numBytesSent != ssize_t(len))
            cout << PROGRAM << ": tried to send player command but only " << numBytesSent << " bytes sent" << endl;

        lastPlayerCommandSent.assign(s, len);
    }
}


//virtual
void
RemoteServer::setEnemyRequest(const bool desiredDirections[4])
{
    setChefRequest(desiredDirections, false);
}


ssize_t
RemoteServer::sendToServer(const void *s, size_t len)
{
    if (s == NULL || len == 0)
    {
        assert(!"RemoteServer::sendToServer: empty packet");
        return 0;
    }

    assert(serverIPAddr != NULL);

    sockaddr_in to;
    memset(&to, '\0', sizeof(to));
    to.sin_family = AF_INET;
    memcpy(&to.sin_addr, serverIPAddr, 4);
    to.sin_port = htons(serverUDPPort);
    return sendto(sock, reinterpret_cast<const char *>(s), len, 0, (sockaddr *) &to, sizeof(to));
}


//virtual
bool
RemoteServer::update()
{
    assert(buffer != NULL);

    // Receive and interpret messages from the server, if any.

    if (sock < 0)
    {
        cout << PROGRAM << ": invalid socket" << endl;
        return false;
    }

    // Process all immediately available UDP packets:
    for (;;)
    {
        sockaddr_in from;
        memset(&from, '\0', sizeof(from));
        socklen_t length = sizeof(from);
        int flags = 0;
        #ifndef _MSC_VER
        flags = MSG_DONTWAIT;
        #endif
        ssize_t numBytesReceived = recvfrom(sock, buffer, MAX_LEN_PACKET,
                                            flags, (sockaddr *) &from, &length);
        int e = errno;
        //cerr << "RemoteServer::update: " << numBytesReceived << " " << e << endl;
        if (numBytesReceived < 0)  // if error or no packet available
        {
            #ifdef _MSC_VER

            int winsockError = WSAGetLastError();
            if (winsockError == WSAEWOULDBLOCK)  // if no packet available
                return true;
            cout << PROGRAM << ": read error on socket: " << winsockError << endl;  // see WinError.h

            #else

            if (e == EAGAIN)  // if no packet available
                return true;
            cout << PROGRAM << ": read error on socket: " << strerror(e) << endl;

            #endif

            return false;
        }
        if (numBytesReceived == 0)
            continue;
        //cerr << "RemoteServer::update: received " << numBytesReceived << " byte(s)" << endl;

        processReceivedBytes(buffer, numBytesReceived);
    }

    return true;
}


//virtual
void
RemoteServer::disconnect()
{
    char s[] =
    {
        char(DISCONNECT_PACKET)
    };
    const size_t len = sizeof(s) / sizeof(s[0]);
    ssize_t numBytesSent = sendToServer(s, len);
    if (numBytesSent != ssize_t(len))
        cout << PROGRAM << ": tried to disconnect packet but only " << numBytesSent << " bytes sent" << endl;
}


//virtual
bool
RemoteServer::saveGame(std::ostream &/*out*/)
{
    return false;  // save not supported in client-server mode
}


//virtual
int
RemoteServer::loadGame(std::istream &/*in*/)
{
    return -1;  // load not supported in client-server mode
}


void
RemoteServer::processReceivedBytes(const char *s, ssize_t len)
{
    assert(s != NULL);
    assert(len > 0);

    acc.append(s, len);
    if (acc.length() < 2)
        return;  // nothing to do

    size_t origAccLen = acc.length();

    ServerPacketType type = static_cast<ServerPacketType>(* (uint16_t *) acc.data());
    //cerr << "len=" << len << ", type=" << type << ", acc=" << acc.length() << endl;

    if (type == UPDATE_LEVEL_PACKET && acc.length() >= 2 + 12)
    {
        uint16_t levelNo = * (uint16_t *) &acc[2];
        uint16_t numRows = * (uint16_t *) &acc[4];
        uint16_t numColumns = * (uint16_t *) &acc[6];
        uint16_t levelPosX = * (uint16_t *) &acc[8];
        uint16_t levelPosY = * (uint16_t *) &acc[10];
        uint16_t descLen = * (uint16_t *) &acc[12];
        string desc(acc, 14, descLen);

        if (false) cout << "processReceivedBytes/0: "
                << levelNo << " "
                << numRows << " "
                << numColumns << " "
                << int16_t(levelPosX) << " "
                << int16_t(levelPosY) << " "
                << descLen << " '"
                << desc << "'" << endl;
        assert(uint16_t(desc.length()) == numRows * numColumns);

        client.handleLevelUpdate(int(levelNo), size_t(numRows), size_t(numColumns),
                                    Couple(levelPosX, levelPosY), desc);

        acc.erase(0, 14 + descLen);
    }
    else if (type == UPDATE_SPRITE_PACKET && acc.length() >= 4)
    {
        // Expected at offset 2:
        // number of sprite updates, then array of updates.

        size_t numUpdates = * (uint16_t *) &acc[2];
        size_t reqLen = 2 + 2 + numUpdates * UPDATE_SPRITE_PACKET_LEN;
        assert(acc.length() >= reqLen);

        for (size_t i = 0; i < numUpdates; ++i)
        {
            size_t j = 2 + 2 + i * UPDATE_SPRITE_PACKET_LEN;
            uint32_t id = * (uint32_t *) &acc[j];
            uint16_t stype = * (uint16_t *) &acc[j + 4];
            uint16_t posX = * (uint16_t *) &acc[j + 6];
            uint16_t posY = * (uint16_t *) &acc[j + 8];
            uint16_t pixmapIndex = * (uint16_t *) &acc[j + 10];

            BurgerSpaceServer::SpriteType spriteType = BurgerSpaceServer::SpriteType(stype);
            if (spriteType == BurgerSpaceServer::NO_SPRITE)
                client.handleSpriteDeletion(id);
            else
                client.handleSpriteUpdate(id,
                                        spriteType,
                                        Couple(posX, posY),
                                        pixmapIndex == USHRT_MAX ? size_t(-1) : size_t(pixmapIndex));
        }

        acc.erase(0, reqLen);
    }
    else if (type == UPDATE_SCORE_PACKET && acc.length() >= 2 + 10)
    {
        uint32_t score = * (uint32_t *) &acc[2];
        uint16_t lives = * (uint16_t *) &acc[6];
        uint16_t peppers = * (uint16_t *) &acc[8];
        uint16_t level = * (uint16_t *) &acc[10];

        client.handleScoreUpdate(long(score), int(lives), int(peppers), int(level));

        acc.erase(0, 2 + 10);
    }
    else if (type == PLAY_SOUND_EFFECT_PACKET && acc.length() >= 2 + 2)
    {
        uint16_t se = * (uint16_t *) &acc[2];

        client.handleSoundEffect(BurgerSpaceServer::SoundEffect(se));

        acc.erase(0, 2 + 2);
    }
    else if (type == ROLE_ASSIGNMENT_PACKET && acc.length() >= 2 + 1)
    {
        char r = * (char *) &acc[2];
        client.handleRoleAssignment(static_cast<Role>(r));
        acc.erase(0, 2 + 1);
    }
    else
        cout << PROGRAM << ": invalid server packet of type " << type << endl;

    if (acc.length() == origAccLen)
        cout << PROGRAM << ": packet accumulator not consumed" << endl;
}