Debian Codegraph @

README.DEV

raw
1
2 PVPGN 2.x DEVELOPERS GUIDE
3
4 Please read this file before asking any questions !
5
6 0. About this document
7 =======================
8
9 This document is intended to be read by all of you out there wanting to do
10 development or testing for/on PvPGN.
11
12 1. Why ?
13 =========
14
15 You want to start coding for PvPGN ? Why ? What do you expect to get from it ?
16 Answer yourself this questions and then go to the next paragraph.
17
18 2. History
19 ===========
20
21 PvPGN has started as a game server emulation project, taking the excelent's
22 bnetd project source and working on it. While initially it has started as
23 a War3 emulation patch over bnetd it become a lot more than that (lots of
24 new features, lots of code changes organizing). Because of the code roots
25 you will notice a lot of things still carrying the "bnetd" word (most notable
26 examples are the main servers program file called "bnetd" or the main server
27 configuration file called bnetd.conf). We considered that as a sign of respect
28 from us to the bnetd coders we keep their names on the code they written (but
29 on the new code of course we may name them different).
30
31 Arround the time of pvpgn 1.7.9 release we have branched the codes and started
32 to work on pvpgn 2.x, the next big version, a C++ based version.
33
34 3. Objective
35 =============
36
37 PvPGN's main objective is to support many gaming network protocols so that
38 users can setup LAN parties and Internet gaming communities. As such it
39 focuses mainly on those protocols that are both popular and where the
40 client vendor (game maker) has NOT released any kind of server software that
41 you can run to achieve the goal above (like Battle.Net, Westwood Online, etc).
42
43 The current status is almost complete support for Battle.Net (all games) and
44 limited Westwood Online support. We also offer the possibility to connect to
45 PvPGN using non-gaming related protocols like IRC and telnet for better
46 interoperability regarding the chat feature of the server (one of the main
47 features as a gaming server).
48
49 4. Layout of files
50 ===================
51
52 Note: Starting here on you may find lots of terms and wors which may sound
53 "strange" to you and for this reason we have included a glossary of terms
54 in the end of this file.
55
56 The PvPGN project consists of a main server (called "bnetd") and various
57 other programs and (little) servers (ex. bnchat, d2cs, d2dbs etc...).
58
59 PvPGN follows the bnetd's layout of files:
60 ./bin -> used to store binaries (after compilation)
61 ./conf -> configuration files (many of them templates)
62 ./files -> various files needed for clients connecting
63 ./man -> outdated man pages :(
64 ./sbin -> same as ./bin
65 ./scripts -> various scripts for testing/managing pvpgn
66 ./src -> the source main directory
67 ./src/bnetd -> source files used only by the main server
68 ./src/common -> source files used in common by different programs
69 ./src/compat -> source files concerning portability of code
70 ...
71
72 PvPGN uses automake/autoconf build and platform portability system. This is
73 the main build method for UNIX/POSIX builds. For Win32 we offer the option to
74 build using mingw tools.
75
76 5. Coding Style
77 ================
78
79 a. General
80
81 Because PvPGN is developed mainly on POSIX/Linux systems there are some
82 things specific to this type of platform (like the line ending of all
83 text/source files to be "Unix-style").
84
85 One thing which is overlooked by newbie coders is the "esthetical" side of the
86 code. It may not be so important to many people (which code on the idea "if it
87 works then its good") but for us, coding on PvPGN is VERY important. When you
88 are coding for PvPGN PLEASE try to make your code look similar to already
89 written code (this includes identing, identificator names, etc...). Keeping
90 the code look "the same" makes its reading a lot more easier so, finding
91 bugs easier so coding better.
92
93 Other overlooked aspect for newbie coders is code replication. Please DONT
94 copy and paste code arround !!! If you need to copy common functionality from
95 some place, think about making some API of that functionalilty, put it in
96 some classes and use it from both places. I repeat, DONT replicate code.
97
98 When allocating memory (or some other kind of resource like a file, etc)
99 inside a function always free it (release the resource) in the same function
100 before its exit (exceptions: the function returns the allocated memory/resource in
101 which case the calling function should take care of the allocated memory;
102 or the allocated memory is cached/stored somewhere to be used later, in which
103 case you must make sure it will be free()d when not needed anymore).
104
105 In the startup code of any "external" function (function which may be called
106 from other modules then the one containing it or a public method ) please
107 check ALL the input parameters (you will notice how PvPGN already does that
108 in such functions). In case of errors detected in such cases (and not only)
109 code should "throw" an exception (see section 5.d).
110
111 When developing code you should compile always with "all warnings" enabled
112 and try to fix them (some warnings uncover real bugs), like for gcc use
113 "-Wall". When running/testing the codes you should use a memory debugger like
114 valgrind. For more details about how to run pvpgn check Appendix B.
115
116 To allocate/free memory use ONLY new/delete calls (this makes sure that out
117 of memory condition is detected and nicely handled because in that case
118 "new" automatically "throws" std::bad_alloc exception).
119
120 b. Indenting
121
122 PvPGN indentation should be done ONLY with "tabs" (exception see below), the
123 tab size is 8 "spaces".
124
125 We indent a level after if, while, do-while, for, case (but NOT from switch),
126 class/struct/union (but NOT the access modifier public/protected/private) and
127 a starting brace. The position of the starting brace relative to the keywords
128 from above is at the same level as the keyword.
129
130 We except to indent after switch, namespace braces and the access modifiers
131 because otherwise things will look "too indented" and make codes less readable.
132
133 c. Functions (methods/constructors) and their parameters
134
135 Function definitions should have the type returned by the function on it's own
136 line than the function name and it's parameter list. Like in:
137
138 const std::string&
139 MyClass::getString()
140 {
141 }
142
143 Function parameters should be passed by reference for "aggregate" types (like
144 class/struct/union). This reference usually is const, ie "const Type& param".
145 Accessors should return references to aggregate members (this is safe because
146 we presume the object's life time exceeds the reference cacher's use time, if
147 not the calling codes can just cache a copy of the object).
148
149 Functions should never have their bodies in the .h file (exception, class and
150 function templates that NEED to be in the .h file completely).
151
152 d. Exceptions and error handling
153
154 "Errors are not always exceptions". Keep this in mind!
155
156 But generally, errors are exceptions. In the common sense and in turn, in the
157 codes. In case of errors (that is errors that shouldnt happen too often, not
158 common case errors) the codes should throw an exception. Exceptions allows for
159 a very clean and versatile way to detach the place of error detection to the
160 place of error handling (in C you detect an error in some low-level codes and
161 you report it in the return value and this is checked and propagated by checks
162 to the higher level codes that will probably handle the error). Also with C++
163 exceptions the "normal" code flow it's NOT disrupted and polluted by trying to
164 handle errors everywhere you might hit one. Exceptions basically offer an
165 alternative SLOW code path. It is slow (much slower than normal code flow) and
166 this is why it should be used to signal relatively "rare" events (thus called
167 exceptions) and not something "common".
168
169 But using exceptions you might be carefull with them to not "leak" resources.
170 The problem is that if you allocate some resource in some codes and then you
171 might get an exception (not even from your codes but indirectly from codes you
172 call) you need to make sure the resources aquired are released BEFORE the
173 exception propagates out of your code scope. This is _easily_ done with C++
174 and the RAII principle
175 (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization). The
176 rule is simple, ALL resource aquisitions should be done using some kind of
177 wrappers that release the resource in their destructor. This also makes codes
178 a lot more simple when exiting code scope on non-error cases because you do
179 NOT need to explicitely release the "wrapped" resource as it will release
180 automatically when leaving the code scope.
181
182 You are allowed ONLY to throw objects, NEVER throw basic types. All the
183 exceptions you throw HAVE to be derived from std::exception (thus providing
184 the .what() method to get a C-string of the description of the error). It is
185 best however, you derivate from std::runtime_error & co. You MAY create your
186 own exception class (with the limitations from above) IF AND ONLY IF your
187 exception represents an error that NEEDS to be handled differentely (ie
188 catched separately from other exceptions). All such user defined exception
189 classes should be defined NESTED in the class that throws them.
190
191 Catching exceptions should ONLY be done using the "const reference" catching
192 scheme like in:
193 } catch (const std::exception& ex) {
194
195 This allows for 2 important things to happen:
196 - it allows proper polymorphic mechanisms for exceptions (so with the example
197 above ANY exception derived from std::exception is caught and handled as it
198 should be)
199 - it behaves nicely on out of memory condition ie an ISO C++98 compliant
200 compiler even when out of memory condition is reached has to provide ENOUGH
201 memory to create a temporary object that (and this is what you do when
202 throwing an exception to be caught with reference, you throw like
203 throw MyException(); whcih means it creates a temporary object MyException()
204 and that object reference is passed to the handlers)
205
206 e. Namespaces
207
208 All pvpgn codes will belong to the "pvpgn" namespace. Specific application
209 codes will belong to a namespace inside "pvpgn" like "pvpgn::bnetd". Common
210 codes need to still be just in "pvpgn" so all other codes will "see" them.
211
212 Never ever use a "using" keyword inside a .h file. Inside .cpp files you
213 _may_ use them.
214
215 Some types, classes, etc are better to be declared inside some other class
216 (like the exceptions, see the above section or smart pointers for the class).
217
218 "static" keyword to declare "local" variables and functions it's not allowed.
219 Instead of it, use unnamed namespaces. A ISO C++98 compliant compiler has to
220 provide for each separate translation unit an unique name for the unnamed
221 namespace that could be declared in that translation unit. As such, instead of
222
223 ------------------------
224 static int myvar;
225 static int myfunc(int);
226
227 extern int externfunc()
228 {
229 }
230
231 static int myfunc()
232 {
233 }
234 ------------------------
235
236 Make it
237
238 ------------------------
239 namespace
240 {
241
242 int myvar;
243 int myfunc(int);
244
245 }
246
247 extern int externfunc()
248 {
249 }
250
251 namespace
252 {
253
254 int myfunc(int)
255 {
256 }
257
258 }
259 ------------------------
260
261 f. Identifiers
262
263 Class names should start with a capital and all words composing it should too
264 (Java-like style, like in "MyStuff"). All methods should start with a lowercase
265 letter and then all words composing it with uppercase. Accessor methods should
266 start with get/set and then the name of the member accessed with the case
267 rules already mentioned.
268
269 g. Include order
270
271 In .cpp files the first include should be "common/setup_before.h" followed by
272 the coresponding header of the cpp file (ie in clan.cpp after setup_before.h
273 you include clan.h).
274
275 Then you include (this applies to headers too not only .cpp now) the headers
276 of standard C++ lib, then the headers from C90 as offered by standard C++
277 (ex. <cstring> etc), then the POSIX/win32/system dependant headers.
278
279 h. Code Flow
280
281 <to be written, not sure how it will be after the complete C++ conversion>
282
283 Appendix A. Glossary of terms
284 ===============================
285
286 * autoupdate: the feature of Battle.Net servers to send a (MPQ, see MPQ) file
287 to the client which after downloading it, it is used to update the client
288
289 * connection class: when a connection is established to a bnet listening
290 address the client sends an initial byte which tells the server of what class
291 of connection the following packets will be; classes of connections determine
292 what packets can go through that connection.
293
294 * MPQ: a format archive which is used by Blizzard clients and Battle.Net
295 servers. This files are used for containing verious files (sound/graphics in
296 clients, AI scripts, update patches etc...)
297
298 * versioncheck: also know as vcheck and sometimes just vc ; a client
299 verification system used by Battle.Net servers to identify client version and
300 "purity". Based on this the server may accept/refuse connection or ask for
301 upgrade (see autoupdate).
302
303 Appendix B. How to run PvPGN for debugging
304 ===========================================
305
306 It is very helpfull in finding out memory coruption bugs as soon as possible
307 so while developing codes or just when running a server it is good that you
308 use some memory coruption run-time debuggers. I dont know about Win32 users
309 but on Unix/Linux there are some good options.
310
311 1. valgrind (http://valgrind.kde.org)
312
313 Valgrind is not very portable (only x86, Linux and very recently FreeBSD),
314 also it slows down the debugged codes (it acts like a CPU emulator so it
315 has to do that) but I have yet to find out a better debugging tool for what
316 he does. Valgrind is so cool that recently many OSS projects use it for
317 finding out bugs in their codes. For more information you can check out their
318 web page. I will focus on valgrind with PvPGN.
319
320 After you have compiled and installed valgrind (it's easy, ./configure, make,
321 make install) you will use it by running PvPGN like this:
322
323 $ valgrind --tool=memcheck --num-callers=10 /path/to/bnetd -f 2> valg.out
324
325 "num-callers" makes valgrind record backtraces with 10 entries and is usually
326 needed with PvPGN which has not very small backtrace path :)
327
328 Another option you might want to use is "--leak-check=yes" and probably
329 "--leak-resolution=high". This options make valgrind even slower but they
330 will give memory leak information when PvPGN exits.
331
332 I encourage EVERYONE to use it if available (that is if you run PvPGN on
333 a supported platform). Only very big servers won't be able to do it because
334 there is no hardware powerfull enough to run a big server with valgrind (but
335 big means over 500 users online). You should test it with your server and if
336 it does not make your bnetd go over 90% CPU then you should be fine. If you
337 cannot run valgrind for any reason or if you are hunting for some bugs
338 valgrind cannot find (yes, valgrind is superb but there is a class of bugs,
339 especially overflows which valgrind can't help you with) you should then try
340 the next debugging tool.
341
342 2. Deleaker (http://deleaker.com)
343
344 For developers on Windows and uses Visual Studio, Deleaker is an extension for Visual Studio that helps you detect and localize resource leaks in memory, GDI and USER objects, handles. It integrates into Visual Studio's debugging functionality and displays useful information to trace leaks back to their source code.
345 Deleaker also comes with a standalone program for developers using other compilers.
346
347 Appendix C. How to generate and use "core" files
348 =================================================
349
350 This appendix is for Unix users. I dont know if other platforms have similar
351 features, that when the program crashes unexpectedly the OS would dump the
352 contents of the memory of the crashing process into a disk file for later
353 inspection.
354
355 First make sure that PvPGN has been compiled with debugging ("-g" parameter
356 to gcc) and with no optimizations ("-O0" parameter to gcc). PvPGN default
357 build process puts "-g -O2" so you need to edit Makefile file before compile
358 and change it to "-g -O0". Then something like "make clean; make".
359
360 On Unix/Linux to be able to get core dumps you first need to make sure your
361 core file size limit is set acordingly. Use "ulimit -c" for querying and
362 setting this limit (I recommend setting it to "unlimited"). After that when
363 you start PvPGN make sure you are in a directory where you have write access
364 (so the OS when it wants to do the core dump it will be allowed to do so).
365 The last thing to do is when starting PvPGN make sure it starts in FOREGROUND,
366 example : /path/to/bnetd -f . If you did all this then when PvPGN will crash
367 you will get a core dump. On linux this is a file called "core", on *BSD it's
368 called <processname>.core (for bnetd that means it's called bnetd.core).
369
370 Now that you got a core file it is time to use it to identify what happened
371 wrong in the crashing process. We use gdb (the GNU debugger, should be
372 available on all Unices) to do this. Run gdb like this:
373 $ gdb /path/to/bnetd /path/to/corefile
374
375 Then gdb should startup, print out a lot of messages and stop after printing
376 a file and line number and quoting some C code where the crash has happened.
377 You can find out a lot more information than this. Run gdb's command "bt full"
378 and it will display a full backtrace of the moment of the crash. The backtrace
379 will contain how the functions were called along the way (their parameters),
380 and also any local variables. If you do not know what to do next from here
381 contact a PvPGN developer and give him exactly that backtrace dump, he should
382 know more.