File: Client.h

package info (click to toggle)
between 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 97; perl: 67
file content (200 lines) | stat: -rw-r--r-- 4,346 bytes parent folder | download | duplicates (3)
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
#ifndef CLIENT_INCLUDED
#define CLIENT_INCLUDED


#include "WebRequest.h"
#include "BlockGrid.h"


// request types:
enum RequestType { 
    NONE,
    GET_SERVER_URL,
    GET_ID,
    CREATE,
    JOIN_FRIEND,
    IS_PARTNER_READY,
    JOIN_STRANGER,
    REPORT_ALIVE,
    POST_MOVE,
    GET_CHANGES };


class QueuedRequest {
        
    public:

        QueuedRequest( RequestType inType, const char *inMethod, 
                       const char *inURL,
                       const char *inBody )
                : mType( inType ),
                  mMethod( stringDuplicate( inMethod ) ),
                  mURL( stringDuplicate( inURL ) ),
                  mBody( NULL ) {
             
            if( inBody != NULL ) {
                mBody = stringDuplicate( inBody );
                }
            }
        

        ~QueuedRequest() {
            delete [] mMethod;
            delete [] mURL;
            if( mBody != NULL ) {
                delete [] mBody;
                }
            }
        
        RequestType mType;
        
        char *mMethod;
        char *mURL;
        char *mBody;
    };



            


// encapsulates all game network client operations
class Client {
        

    public:

        // creates client and starts to request a player_id from the server, 
        // if needed
        Client();
        

        ~Client();
        

        // perform another step of any pending network operations
        // returns true if work remains to be done
        // returns false if client is done with all pending work
        char step();
        

        // must return true before starting any operations below
        char isClientReady();
        

        
        // these calls start operations
        
        void createGame();
        
        // NULL to join game with stranger
        void joinGame( char *inGameID );
        
        
        // posts dirty columns to server
        // returns true if some were dirty and a post to the server resulted
        char postMove( BlockGrid *inGrid );


        // gets changes and adds them to inGrid
        void getChanges( BlockGrid *inGrid );
        



        // result not destroyed by caller
        // NULL if not set yet
        char *getGameID();
        
        // useful for detecting a zombie or disconnected partner
        unsigned long getSecondsSincePartnerActed();
        
        

        char isGameReady();


        // gets a random, yet constant-per-game (and same as partner)
        // world index
        // game must be ready before this call works
        int getSharedWorldIndex( int mNumWorlds );
        
        
        
        char isError();
        
        void clearError();
        
        // destroyed internally
        char *getErrorString();
        

        // frees static data for this class
        // must be called at app exit
        static void staticFree();
        

    protected:
        // URL where we fetch server URL from
        char *mServerFinderURL;

        // only fetch once per application instance
        static char *sServerURL;
        
        char *mPlayerID;

        char *mGameID;
        
        char *mGamePasscode;
        
        char *mPlayerNumber;

        // self-reversing transform
        int mBlockTransform[16];
        
        // set from comma-delimited list
        void setBlockTransformFromString( char *inTransformString );
        
        char *transformColumnString( char *inColumnString );
        

        
        char mGameReady;
        
        unsigned long mTimeLastCheckedReady;
        unsigned long mTimeBetweenChecks;
        
        
        // we report our alive status when we're otherwise idle
        unsigned long mTimeLastReportedAlive;
        unsigned long mTimeBetweenReports;
        

        unsigned long mSecondsSincePartnerActed;
        
        
        char *mLastStateSeen;
        
        BlockGrid *mGridToUpdate;
        

        char mError;
        char *mErrorString;

        // inErrorMessage copied internally
        // must be destroyed by caller
        void setError( const char *inErrorMessage );
        
        


        RequestType mCurrentType;
        WebRequest *mCurrentRequest;
        
        SimpleVector<QueuedRequest*> mQueuedRequests;
        
    };



#endif