| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
1 |
/* |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
2 |
=========================================================================== |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
3 |
blockattack - Block Attack - Rise of the Blocks |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
4 |
Copyright (C) 2005-2012 Poul Sander |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
5 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
6 |
This program is free software: you can redistribute it and/or modify |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
7 |
it under the terms of the GNU General Public License as published by |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
8 |
the Free Software Foundation, either version 2 of the License, or |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
9 |
(at your option) any later version. |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
10 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
11 |
This program is distributed in the hope that it will be useful, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
14 |
GNU General Public License for more details. |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
15 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
16 |
You should have received a copy of the GNU General Public License |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
17 |
along with this program. If not, see http://www.gnu.org/licenses/ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
18 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
19 |
Source information and contacts persons can be found at |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
20 |
http://blockattack.sf.net |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
21 |
=========================================================================== |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
22 |
*/ |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
23 |
|
| 07383b8f |
sago007 |
2012-05-11 21:00 |
24 |
#include "BlockGame.hpp" |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
25 |
|
| 07383b8f |
sago007 |
2012-05-11 21:00 |
26 |
|
| 2f30c381 |
sago007 |
2008-09-14 13:08 |
27 |
#if NETWORK |
| 2f30c381 |
sago007 |
2008-09-14 13:08 |
28 |
|
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
29 |
//The network interface |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
30 |
class NetworkThing |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
31 |
{ |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
32 |
private: |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
33 |
ENetAddress address; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
34 |
ENetHost * server; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
35 |
ENetHost * client; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
36 |
ENetPeer *peer; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
37 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
38 |
BlockGame *bgHome, *bgAway; //Pointers to the two games, so we can call the procedures |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
39 |
bool weAreAServer, weAreAClient; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
40 |
bool weAreConnected; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
41 |
bool enemyIsReady; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
42 |
bool enemyHasStarted; //If we should have a ready button |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
43 |
bool gameHasStarted; //If the player is GameOver it might just be because the game hasn't started yet... not if this is true! |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
44 |
|
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
45 |
public: |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
46 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
47 |
Uint32 theSeed; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
48 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
49 |
NetworkThing() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
50 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
51 |
weAreAServer = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
52 |
weAreAClient = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
53 |
weAreConnected = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
54 |
enemyIsReady = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
55 |
enemyHasStarted = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
56 |
gameHasStarted = false; |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
57 |
bgAway = NULL; |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
58 |
bgHome = NULL; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
59 |
if (enet_initialize () != 0) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
60 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
61 |
fprintf (stderr, "An error occurred while initializing ENet.\n"); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
62 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
63 |
else |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
64 |
{ |
| 56241205 |
sago007 |
2012-08-05 11:15 |
65 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
66 |
cout << "Network is working!" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
67 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
68 |
//atexit (enet_deinitialize); in deconstructor |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
69 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
70 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
71 |
//This function couses us to remove the connection to the other peer, plus destroying servers |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
72 |
void ntDisconnect() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
73 |
{ |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
74 |
if(!bgHome || !bgAway) |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
75 |
return; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
76 |
strcpy(bgAway->name,player2name); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
77 |
if (weAreConnected || weAreAClient || weAreAServer) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
78 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
79 |
if (weAreAClient) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
80 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
81 |
enet_peer_disconnect (peer,0); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
82 |
SDL_Delay(20); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
83 |
enet_host_destroy(client); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
84 |
weAreAClient = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
85 |
weAreConnected = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
86 |
enemyHasStarted = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
87 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
88 |
if (weAreAServer) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
89 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
90 |
enet_host_destroy(server); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
91 |
weAreAServer = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
92 |
weAreConnected = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
93 |
enemyHasStarted = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
94 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
95 |
//If the game is running then we disconnect, we are considered the looser! |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
96 |
if ((!bgHome->isGameOver())&&(!bgAway->isGameOver())) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
97 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
98 |
bgAway->setPlayerWon(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
99 |
bgHome->SetGameOver(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
100 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
101 |
gameHasStarted = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
102 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
103 |
}; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
104 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
105 |
//Lets disconnect before we close... |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
106 |
~NetworkThing() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
107 |
{ |
| 56241205 |
sago007 |
2012-08-05 11:15 |
108 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
109 |
cout << "Network system is going down" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
110 |
ntDisconnect(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
111 |
enet_deinitialize(); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
112 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
113 |
cout << "Network system went down" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
114 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
115 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
116 |
void theGameHasStarted() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
117 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
118 |
gameHasStarted = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
119 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
120 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
121 |
//The NetworkThing needs to be able to call the models... here the pointers are set |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
122 |
void setBGpointers(BlockGame *bg1, BlockGame *bg2) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
123 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
124 |
bgHome = bg1; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
125 |
bgAway = bg2; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
126 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
127 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
128 |
//Starts a server instance |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
129 |
void startServer() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
130 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
131 |
if (!weAreAServer) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
132 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
133 |
ntDisconnect(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
134 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
135 |
/* Bind the server to the default localhost. */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
136 |
/* A specific host address can be specified by */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
137 |
/* enet_address_set_host (& address, "x.x.x.x"); */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
138 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
139 |
address.host = ENET_HOST_ANY; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
140 |
/* Bind the server to port SERVERPORT. */ |
| 43765588 |
sago007 |
2012-05-16 19:14 |
141 |
address.port = Config::getInstance()->getInt("portv4"); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
142 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
143 |
server = enet_host_create (& address /* the address to bind the server host to */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
144 |
1 /* allow up to 1 clients and/or outgoing connections */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
145 |
4 /* allow 4 channels to be used */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
146 |
0 /* assume any amount of incoming bandwidth */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
147 |
0 /* assume any amount of outgoing bandwidth */); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
148 |
if (server == NULL) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
149 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
150 |
fprintf (stderr, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
151 |
"An error occurred while trying to create an ENet server host.\n"); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
152 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
153 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
154 |
else |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
155 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
156 |
weAreAServer = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
157 |
enemyIsReady = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
158 |
gameHasStarted = false; |
| 56241205 |
sago007 |
2012-08-05 11:15 |
159 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
160 |
cout << "Server is listening on port " << SERVERPORT << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
161 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
162 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
163 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
164 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
165 |
void connectToServer(string server) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
166 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
167 |
ENetAddress address; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
168 |
ENetEvent event; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
169 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
170 |
enet_address_set_host (& address, server.c_str()); |
| 43765588 |
sago007 |
2012-05-16 19:14 |
171 |
address.port = Config::getInstance()->getInt("portv4"); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
172 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
173 |
ntDisconnect(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
174 |
client = enet_host_create (NULL /* create a client host */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
175 |
1 /* only allow 1 outgoing connection */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
176 |
4 /* allow 4 channels to be used */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
177 |
0 /* Unlimited downstream bandwidth */, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
178 |
0 /* Unlimted upstream bandwidth */); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
179 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
180 |
if (client == NULL) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
181 |
{ |
| 56241205 |
sago007 |
2012-08-05 11:15 |
182 |
cerr << "Error: An error occurred while trying to create an ENet client host." << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
183 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
184 |
else |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
185 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
186 |
/* Initiate the connection, allocating the four channels 0 and 1 and 2 and 3. */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
187 |
peer = enet_host_connect (client, & address, 4,0); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
188 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
189 |
if (peer == NULL) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
190 |
{ |
| 56241205 |
sago007 |
2012-08-05 11:15 |
191 |
cerr << "Error: No available peers for initiating an ENet connection." << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
192 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
193 |
else if (enet_host_service (client, & event, 2000) > 0 && |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
194 |
event.type == ENET_EVENT_TYPE_CONNECT) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
195 |
{ |
| 56241205 |
sago007 |
2012-08-05 11:15 |
196 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
197 |
cout << "We are connected!" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
198 |
enemyIsReady = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
199 |
weAreAClient = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
200 |
weAreConnected = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
201 |
gameHasStarted = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
202 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
203 |
else |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
204 |
{ |
| 43765588 |
sago007 |
2012-05-16 19:14 |
205 |
cout << "Server " << server.c_str() << ":" << address.port << " didn't answer in time" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
206 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
207 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
208 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
209 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
210 |
bool isConnected() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
211 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
212 |
return (weAreConnected||weAreAServer); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
213 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
214 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
215 |
bool isConnectedToPeer() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
216 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
217 |
return weAreConnected; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
218 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
219 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
220 |
//This function must be called in the game loop: |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
221 |
void updateNetwork() |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
222 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
223 |
//Now we must send our own board: |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
224 |
if (weAreConnected) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
225 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
226 |
//cout << "Creating package" << endl; |
| 80b830cd |
sago007 |
2012-08-19 17:51 |
227 |
Uint32 tick; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
228 |
Sint32 action; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
229 |
Sint32 paramsize; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
230 |
string param; |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
231 |
while(bgHome->GotAction(tick,action,param)) |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
232 |
{ |
| 610d0b15 |
sago007 |
2012-05-19 18:13 |
233 |
if(action == ACTION_NEW || action == ACTION_NEWTT || action == ACTION_NEWVS) |
| 610d0b15 |
sago007 |
2012-05-19 18:13 |
234 |
continue; //Do not send start messages |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
235 |
paramsize = param.length(); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
236 |
char *tmpPacket = (char*)malloc(sizeof(Sint32)*3+param.length()+1); |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
237 |
if(!tmpPacket) |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
238 |
{ |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
239 |
cerr << "Failed to alloc memoroy" << endl; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
240 |
exit(-3); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
241 |
} |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
242 |
memcpy(tmpPacket,&tick,sizeof(Sint32)); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
243 |
memcpy(tmpPacket+sizeof(Sint32),&action,sizeof(Sint32)); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
244 |
memcpy(tmpPacket+sizeof(Sint32)*2,¶msize,sizeof(Sint32)); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
245 |
memcpy(tmpPacket+sizeof(Sint32)*3,param.c_str(),paramsize+1); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
246 |
ENetPacket * packet = enet_packet_create (tmpPacket, |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
247 |
sizeof(Sint32)*3+param.length()+1, |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
248 |
ENET_PACKET_FLAG_RELIABLE); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
249 |
//Now lets send the package |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
250 |
if (weAreAServer) |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
251 |
enet_host_broadcast (server, 0, packet); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
252 |
else if (weAreAClient) |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
253 |
enet_peer_send (peer, 0, packet); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
254 |
//cout << "Package sent" << endl; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
255 |
free(tmpPacket); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
256 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
257 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
258 |
//See if we are game over and in that case notify the other player |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
259 |
if ((gameHasStarted)&&(bgHome->isGameOver())) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
260 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
261 |
ENetPacket * packet = enet_packet_create("G",2,ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
262 |
if (weAreAServer) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
263 |
enet_host_broadcast (server, 1, packet); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
264 |
else if (weAreAClient) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
265 |
enet_peer_send (peer, 1, packet); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
266 |
gameHasStarted=false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
267 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
268 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
269 |
//Now lets see if we have something to throw at the opponent. |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
270 |
Uint8 x,y,type; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
271 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
272 |
if (gameHasStarted) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
273 |
while (bgAway->popGarbage(&x,&y,&type)) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
274 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
275 |
//if there are garbage to drop |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
276 |
if (type==1) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
277 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
278 |
x=255; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
279 |
y=255; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
280 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
281 |
Uint16 g = ((Uint16)x)*256+(Uint16)y; //A 16-bit int containing everything |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
282 |
ENetPacket * packet = enet_packet_create(&g,2,ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
283 |
if (weAreAServer) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
284 |
enet_host_broadcast (server, 2, packet); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
285 |
else if (weAreAClient) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
286 |
enet_peer_send (peer, 2, packet); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
287 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
288 |
cout << "We send a garbage block: " << (int)x << "," << (int)y << "==" << g << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
289 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
290 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
291 |
ENetEvent event; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
292 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
293 |
/* Wait up to 0 milliseconds for an event. */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
294 |
while (((weAreAClient)&&(enet_host_service (client, & event, 0) > 0))||((weAreAServer)&&(enet_host_service (server, & event, 0) > 0))) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
295 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
296 |
switch (event.type) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
297 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
298 |
case ENET_EVENT_TYPE_CONNECT: |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
299 |
printf ("A new client connected from %x:%u.\n", |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
300 |
event.peer -> address.host, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
301 |
event.peer -> address.port); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
302 |
weAreConnected = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
303 |
/* Store any relevant client information here. */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
304 |
//event.peer -> data = "Client"; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
305 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
306 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
307 |
ENetPacket * namePacket = enet_packet_create(bgHome->name,sizeof(char[30]),ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
308 |
//if(weAreAServer) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
309 |
enet_host_broadcast (server, 3, namePacket); |
| 43765588 |
sago007 |
2012-05-16 19:14 |
310 |
ENetPacket * answerPacket = enet_packet_create("version4",sizeof("version4"),ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
311 |
enet_host_broadcast (server, 3, answerPacket); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
312 |
theSeed = time(0)/4; |
| 610d0b15 |
sago007 |
2012-05-19 18:13 |
313 |
bgHome->NewVsGame(50,100,bgAway,SDL_GetTicks()); |
| 610d0b15 |
sago007 |
2012-05-19 18:13 |
314 |
bgAway->NewVsGame(xsize-500,100,bgHome,SDL_GetTicks()); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
315 |
bgHome->putStartBlocks(theSeed); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
316 |
ENetPacket * timePacket = enet_packet_create(&theSeed,sizeof(theSeed),ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
317 |
enet_host_broadcast (server, 3, timePacket); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
318 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
319 |
cout << "We send the seed: " << theSeed << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
320 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
321 |
break; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
322 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
323 |
case ENET_EVENT_TYPE_RECEIVE: |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
324 |
/*printf ("A packet of length %u containing %s was received from %s on channel %u.\n", |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
325 |
event.packet -> dataLength, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
326 |
event.packet -> data, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
327 |
event.peer -> data, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
328 |
event.channelID);*/ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
329 |
//cout << "Package recieved" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
330 |
if (event.channelID==0) //Unreliable (only boardPacks) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
331 |
{ |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
332 |
Sint32 tick; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
333 |
Sint32 action; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
334 |
string param; |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
335 |
memcpy(&tick,event.packet->data,sizeof(Sint32)); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
336 |
memcpy(&action,event.packet->data+sizeof(Sint32),sizeof(Sint32)); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
337 |
param = (const char*) (event.packet->data+sizeof(Sint32)*3); |
| 07383b8f |
sago007 |
2012-05-11 21:00 |
338 |
bgAway->PerformAction(tick,action,param); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
339 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
340 |
if (event.channelID==1) //reliable (used for GameOver notifications only!) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
341 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
342 |
if ((bgHome->isGameOver())&&(!bgHome->GetIsWinner())&&(gameHasStarted)) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
343 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
344 |
bgHome->setDraw(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
345 |
bgAway->setDraw(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
346 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
347 |
else |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
348 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
349 |
bgHome->setPlayerWon(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
350 |
bgAway->SetGameOver(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
351 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
352 |
gameHasStarted=false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
353 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
354 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
355 |
if (event.channelID==2) //reliable (used for Garbage only!) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
356 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
357 |
Uint16 g; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
358 |
memcpy(&g,event.packet->data,sizeof(Uint16)); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
359 |
Uint8 x = (g/256); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
360 |
Uint8 y = (g%256); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
361 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
362 |
cout << "Recieved Garbage: " << (int)x << "," << (int)y << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
363 |
if ((x==255)&&(y==255)) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
364 |
bgHome->CreateGreyGarbage(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
365 |
else |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
366 |
bgHome->CreateGarbage(x,y); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
367 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
368 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
369 |
if (event.channelID==3) //We have reacieved a name or a version number |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
370 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
371 |
if (event.packet->dataLength==sizeof(char[30])) //We have reveived a name |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
372 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
373 |
strcpy(bgAway->name,(const char*)event.packet->data); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
374 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
375 |
cout << "The enemy name is: " << bgAway->name << " Length: " << sizeof(char[30])<< endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
376 |
if (weAreAClient) //We have just recieved the servers name and must send our own |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
377 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
378 |
ENetPacket * answerPacket = enet_packet_create(bgHome->name,sizeof(char[30]),ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
379 |
enet_peer_send (peer, 3, answerPacket); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
380 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
381 |
} |
| 50258544 |
sago007 |
2012-05-10 19:11 |
382 |
else if (event.packet->dataLength==sizeof("version4")) //We have recieved aversion number |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
383 |
{ |
| 50258544 |
sago007 |
2012-05-10 19:11 |
384 |
if (0!=strcmp((const char*)event.packet->data,"version4")) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
385 |
{ |
| 56241205 |
sago007 |
2012-08-05 11:15 |
386 |
cerr << "Error: Incompatible version: " << event.packet->data << "!=" << "version4" << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
387 |
ntDisconnect(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
388 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
389 |
if (weAreAClient) //We will send our version number |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
390 |
{ |
| 50258544 |
sago007 |
2012-05-10 19:11 |
391 |
ENetPacket * answerPacket = enet_packet_create("version4",sizeof("version4"),ENET_PACKET_FLAG_RELIABLE); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
392 |
enet_peer_send (peer, 3, answerPacket); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
393 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
394 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
395 |
else if (event.packet->dataLength==sizeof(Uint32)) //We have recieved a seed |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
396 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
397 |
memcpy(&theSeed,event.packet->data,sizeof(Uint32)); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
398 |
bgHome->putStartBlocks(theSeed); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
399 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
400 |
cout << "We recieved a seed: " << theSeed << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
401 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
402 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
403 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
404 |
/* Clean up the packet now that we're done using it. */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
405 |
enet_packet_destroy (event.packet); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
406 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
407 |
break; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
408 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
409 |
case ENET_EVENT_TYPE_DISCONNECT: |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
410 |
//printf ("%s disconected.\n", event.peer -> data); |
| 56241205 |
sago007 |
2012-08-05 11:15 |
411 |
if(verboseLevel) |
| 56241205 |
sago007 |
2012-08-05 11:15 |
412 |
cout << event.peer -> data << " disconnected." << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
413 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
414 |
/* Reset the peer's client information. */ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
415 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
416 |
event.peer -> data = NULL; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
417 |
//weAreConnected = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
418 |
if ((!bgHome->isGameOver())&&(!bgAway->isGameOver())) |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
419 |
{ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
420 |
bgHome->setPlayerWon(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
421 |
bgAway->SetGameOver(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
422 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
423 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
424 |
ntDisconnect(); //When we will disconnect! |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
425 |
break; |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
426 |
|
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
427 |
case ENET_EVENT_TYPE_NONE: |
| b4f1d3cf |
sago007 |
2012-08-20 21:28 |
428 |
break; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
429 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
430 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
431 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
432 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
433 |
} |
| 89c4a3a6 |
sago007 |
2008-08-29 14:32 |
434 |
}; //NetworkThing |
| 2f30c381 |
sago007 |
2008-09-14 13:08 |
435 |
|
| 2f30c381 |
sago007 |
2008-09-14 13:08 |
436 |
#endif |
|
|
1970-01-01 00:00 |
437 |
|