~ubuntu-branches/ubuntu/oneiric/mmpong/oneiric

« back to all changes in this revision

Viewing changes to lib/message.h

  • Committer: Bazaar Package Importer
  • Author(s): André Gaul
  • Date: 2009-01-09 16:39:01 UTC
  • Revision ID: james.westby@ubuntu.com-20090109163901-3k701q6yxwailed7
Tags: upstream-0.9
Import upstream version 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
  Copyright (C) 2008 Kai Hertel, André Gaul
 
3
 
 
4
        This file is part of mmpong.
 
5
 
 
6
        mmpong is free software: you can redistribute it and/or modify
 
7
        it under the terms of the GNU General Public License as published by
 
8
        the Free Software Foundation, either version 3 of the License, or
 
9
        (at your option) any later version.
 
10
 
 
11
        mmpong is distributed in the hope that it will be useful,
 
12
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
        GNU General Public License for more details.
 
15
 
 
16
        You should have received a copy of the GNU General Public License
 
17
        along with mmpong.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#ifndef __MESSAGE_HEADER__
 
21
#define __MESSAGE_HEADER__
 
22
 
 
23
#include <stdint.h>
 
24
#include <sys/time.h>
 
25
#include "game.h"
 
26
#include "dllhelper.h"
 
27
 
 
28
 
 
29
#ifdef __cplusplus
 
30
extern "C" {
 
31
#endif
 
32
 
 
33
#define NETMESSAGE_MAXLEN 256
 
34
 
 
35
#define NETMSG_SUCCESS                  0
 
36
#define NETMSG_ARGINVALID               (-1)
 
37
#define NETMSG_PARTIAL                  (-2)
 
38
#define NETMSG_END_SOCKET               (-3)
 
39
#define NETMSG_FAIL_DELIVER     (-4)
 
40
#define NETMSG_FAIL_SOCKET              (-5)
 
41
#define NETMSG_FAIL_CONVERT     (-6)
 
42
#define NETMSG_FAIL_CHECKSUM    (-7)
 
43
#define NETMSG_FAIL_SYSCRITICAL (-8)
 
44
 
 
45
enum netmessage_type {
 
46
        NETMSG_STAT= 's',       // full game state
 
47
        NETMSG_UPDT= 'u',       // game state update
 
48
        NETMSG_EXIT= 'e',       // peer exits voluntarily
 
49
        NETMSG_KICK= 'k',       // client session terminates
 
50
        NETMSG_POS=  'p'        // position update
 
51
};
 
52
 
 
53
// no padding for structures related to the net code
 
54
#pragma pack(push, 1)
 
55
 
 
56
struct netmessage_header {
 
57
        uint16_t id;
 
58
        uint16_t len;
 
59
        struct gametime_public stamp;
 
60
};
 
61
 
 
62
struct netmessage {
 
63
        struct netmessage_header hdr;
 
64
        union {
 
65
                char data[NETMESSAGE_MAXLEN -sizeof(struct netmessage_header)];
 
66
                struct game_state_full {
 
67
                        uint16_t team;
 
68
                        struct gameplay_public game;
 
69
                } full;
 
70
                struct game_state_part {        // this one might need manual adjustments to stay in sync with `struct gameplay_public`
 
71
                        struct gametime_public stamp;   // time stamp
 
72
                        struct gameball_public ball;
 
73
                        struct gamepaddle_public pad[2];
 
74
                } part;
 
75
                uint16_t position;
 
76
        } payload;
 
77
};
 
78
 
 
79
#pragma pack(pop)
 
80
 
 
81
struct netmessage_buffer {
 
82
        uint16_t sz;
 
83
        struct netmessage msg;
 
84
        uint16_t pos, len;
 
85
};
 
86
 
 
87
EXPORT int netmessage_buffer_init(struct netmessage_buffer **);
 
88
EXPORT int netmessage_buffer_flush(const int, struct netmessage_buffer *);
 
89
EXPORT int netmessage_scrambler_init(void);
 
90
EXPORT int netmessage_send(const int, const enum netmessage_type, const void *, const uint16_t, struct netmessage_buffer *);
 
91
EXPORT int netmessage_recv(const int, struct netmessage *, const uint16_t, struct netmessage_buffer *);
 
92
EXPORT int netmessage_get_hdr_stamp(const struct netmessage *, struct timeval *);
 
93
 
 
94
#ifdef __cplusplus
 
95
}
 
96
#endif
 
97
 
 
98
#endif
 
99