-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathServer.h
More file actions
376 lines (278 loc) · 9.07 KB
/
Server.h
File metadata and controls
376 lines (278 loc) · 9.07 KB
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
//
// server.hpp
// Project Spitfire
//
// Copyright (c) 2013 Daizee (rensiadz at gmail dot com)
//
// This file is part of Spitfire.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SPITFIRE_SERVER_HPP
#define SPITFIRE_SERVER_HPP
//#include <iostream>
//#include <list>
//#include <string>
//#include <cctype>
//#include "sfsql.h"
#include "structs.h"
#include "funcs.h"
#include "connection.h"
#include <set>
#include <lua/lua.hpp>
#include <lua/lauxlib.h>
#include <boost/thread.hpp>
#include <asio.hpp>
// #include <iostream>
// #include <list>
// #include <string>
// #include <cctype>
#include <boost/noncopyable.hpp>
class Map;
class Client;
class City;
class Alliance;
class AllianceCore;
class sockets;
#include "Poco/Data/Common.h"
//#include "Poco/Data/SQLite/Connector.h"
#include "Poco/Data/MySQL/Connector.h"
#include "Poco/Data/MySQL/MySQLException.h"
#include "Poco/Data/SessionPool.h"
#include "Poco/Data/RecordSet.h"
#include "Poco/Exception.h"
#include "Poco/Logger.h"
#include "Poco/PatternFormatter.h"
#include "Poco/FormattingChannel.h"
#include "Poco/ConsoleChannel.h"
#include "Poco/FileChannel.h"
#include "Poco/Message.h"
using Poco::Logger;
using Poco::PatternFormatter;
using Poco::FormattingChannel;
using Poco::ConsoleChannel;
using Poco::FileChannel;
using Poco::Message;
using Poco::Data::RecordSet;
using namespace std;
using namespace Poco::Data;
/// The top-level class of the Spitfire server.
class Server
{
public:
Server();
~Server();
/// Run the server's io_service loop.
void run();
/// Stop the server.
void stop();
/// Start the first asynchronous operation for the connection.
void start(connection_ptr c);
/// Stop the specified connection.
void stop(connection_ptr c);
/// Stop all connections.
void stop_all();
/// Handle completion of an asynchronous accept operation.
void handle_accept(const asio::error_code& e);
/// Handle a request to stop the server.
void handle_stop();
/// The io_service used to perform asynchronous operations.
asio::io_service io_service_;
/// The signal_set is used to register for process termination notifications.
asio::signal_set signals_;
/// Acceptor used to listen for incoming connections.
asio::ip::tcp::acceptor acceptor_;
/// The connection list which owns all live connections.
std::set<connection_ptr> connections_;
/// The next connection to be accepted.
connection_ptr new_connection_;
/// The handler for all incoming requests.
request_handler request_handler_;
string sqlhost, sqluser, sqlpass, bindaddress, bindport;
string dbmaintable;
string dbservertable;
uint32_t serverstatus;
uint16_t mapsize;
FormattingChannel* pFCConsole;
FormattingChannel* pFCFile;
Logger * consoleLogger;
Logger * fileLogger;
SessionPool * accountpool;
SessionPool * serverpool;
//Lua
lua_State *L;
bool Init();
uint32_t maxplayers, currentplayersonline;
bool TimerThreadRunning;
void TimerThread();
void SaveData();
string servername;
Map * map;
bool ConnectSQL();
bool InitSockets();
Client * NewClient();
Client * GetClient(int accountid);
Client * GetClientByParent(int accountid);
Client * GetClientByName(string name);
Client * GetClientByCastle(int32_t castleid);
int32_t GetClientIndex(int32_t accountid);
void CloseClient(int id);
void CloseClient(Client * client, int typecode = 1, string message = "Connection Closed");
City * AddPlayerCity(Client * client, int tileid, uint32_t castleid);
City * AddNpcCity(int tileid);
void MassMessage(string str, bool nosender = false, bool tv = false, bool all = false);
void SendMessage(Client * client, string str, bool nosender = false, bool tv = false, bool all = false);
void Shutdown();
list<Client*> players;
bool savethreadrunning;
sockets * skts;
// void SendObject(uint32_t s, amf3object & object)
// {
// try
// {
// if (serverstatus == 0)
// return;
// if ((s == 0) || (m_clients[s]))
// return;
// char buffer[15000];
// int length = 0;
// amf3writer * writer;
//
// writer = new amf3writer(buffer+4);
//
// writer->Write(object);
//
// (*(int*)buffer) = length = writer->position;
// ByteSwap(*(int*)buffer);
//
// //m_clients[s]->socket->write(buffer, length+4);
// delete writer;
// }
// catch (std::exception& e)
// {
// std::cerr << "exception: " << __FILE__ << " @ " << __LINE__ << "\n";
// std::cerr << e.what() << "\n";
// }
// }
void SendObject(connection * s, amf3object & object)
{
if (s == 0)
return;
SendObject(*s, object);
}
void SendObject(connection & s, amf3object & object)
{
try
{
if (serverstatus == 0)
return;
char buffer[15000];
int length = 0;
amf3writer * writer;
writer = new amf3writer(buffer+4);
writer->Write(object);
(*(int*)buffer) = length = writer->position;
ByteSwap(*(int*)buffer);
s.write(buffer, length+4);
delete writer;
}
catch (std::exception& e)
{
std::cerr << "exception: " << __FILE__ << " @ " << __LINE__ << "\n";
std::cerr << e.what() << "\n";
}
}
vector<City*> m_city;// npc cities
AllianceCore * m_alliances;
bool ParseChat(Client * client, char * str);
int16_t GetRelation(int32_t client1, int32_t client2);
uint64_t ltime;
uint64_t armycounter;
string m_itemxml;
stItemConfig m_items[DEF_MAXITEMS];
int m_itemcount;
stRarityGamble m_gambleitems;
list<stMarketEntry> m_marketbuy;
list<stMarketEntry> m_marketsell;
static bool comparebuy(stMarketEntry first, stMarketEntry second);
static bool comparesell(stMarketEntry first, stMarketEntry second);
void AddMarketEntry(stMarketEntry me, int8_t type);
#define DEF_TIMEDARMY 1
#define DEF_TIMEDBUILDING 2
#define DEF_TIMEDRESEARCH 3
stItemConfig * GetItem(string name);
double GetPrestigeOfAction(int8_t action, int8_t id, int8_t level, int8_t thlevel);
void MassDisconnect();
void AddTimedEvent(stTimedEvent & te);
stBuildingConfig m_buildingconfig[35][10];
stBuildingConfig m_researchconfig[25][10];
stBuildingConfig m_troopconfig[20];
list<stTimedEvent> armylist;
list<stTimedEvent> buildinglist;
list<stTimedEvent> researchlist;
queue<stPacketOut> m_packetout;
int64_t m_heroid;
int64_t m_cityid;
int32_t m_allianceid;
vector<int32_t> m_deletedhero;
vector<int32_t> m_deletedcity;
int RandomStat();
Hero * CreateRandomHero(int innlevel);
static bool compareprestige(stClientRank first, stClientRank second);
static bool comparehonor(stClientRank first, stClientRank second);
static bool comparetitle(stClientRank first, stClientRank second);
static bool comparepop(stClientRank first, stClientRank second);
static bool comparecities(stClientRank first, stClientRank second);
void SortPlayers();
list<stClientRank> m_prestigerank;
list<stClientRank> m_honorrank;
list<stClientRank> m_titlerank;
list<stClientRank> m_populationrank;
list<stClientRank> m_citiesrank;
list<stHeroRank> m_herorankstratagem;
list<stHeroRank> m_herorankpower;
list<stHeroRank> m_herorankmanagement;
list<stHeroRank> m_herorankgrade;
void SortHeroes();
static bool comparestratagem(stHeroRank first, stHeroRank second);
static bool comparepower(stHeroRank first, stHeroRank second);
static bool comparemanagement(stHeroRank first, stHeroRank second);
static bool comparegrade(stHeroRank first, stHeroRank second);
list<stCastleRank> m_castleranklevel;
list<stCastleRank> m_castlerankpopulation;
void SortCastles();
static bool comparepopulation(stCastleRank first, stCastleRank second);
static bool comparelevel(stCastleRank first, stCastleRank second);
list<stSearchClientRank> m_searchclientranklist;
list<stSearchHeroRank> m_searchheroranklist;
list<stSearchCastleRank> m_searchcastleranklist;
list<stSearchAllianceRank> m_searchallianceranklist;
void * DoRankSearch(string key, int8_t type, void * subtype, int16_t page, int16_t pagesize);
void CheckRankSearchTimeouts(uint64_t time);
amf3object CreateError(string cmd, int32_t id, string message)
{
amf3object obj;
obj["cmd"] = cmd;
obj["data"] = amf3object();
amf3object & data = obj["data"];
data["errorMsg"] = message;
data["packageId"] = 0.0f;
data["ok"] = id;
return obj;
}
};
#endif // SPITFIRE_SERVER_HPP