I decided to write a small server-side tutorial. In fact, I am just a beginner. This article also serves as a learning record for myself. If you think it is good, please give it a compliment. Remember, this is just a beginner's tutorial.
It is a server, but it is more like a mobile game server framework, because you are just a beginner. In more detail, you are just an intern in a small company, there is nothing to do at ordinary times. Now I am playing around with it. Hey hey, I can say that I have no experience programming for the game server. So this tutorial can be said to be a very bad tutorial, there are a lot of mistakes in it, and they contain immature design ideas (to put it bluntly, there is no design idea at all, and there is no idea about extensibility or other advanced things, of course, I will not, A little tragic). Please give me some corrections and I will learn it with an empty mind. The purpose of writing these articles is naturally to promote learning. There are various introductions in it, finally, the Code does not contain all things. This is just a small tutorial for beginners to entertain themselves!
Where can we start from? First, it indicates that the programming environment is linux and only C ++ is used.
Overall Program thinking:
A main thread is running the main logic, and other threads are responsible for other tasks (such as gateway gate thread and log writing thread) communication (here is the simplest and worst Message Queue). The database is redis. For details about redis, refer to redis. io, which belongs to the KEY-VALUE Type database, so the speed is extremely fast.
First, write the overall framework of the program. Including Thread, Game (main logic Thread), and GameApp (just to enable the Thread ).
Write the Application class as the parent class of the GameApp class:
# Ifndef _ APPLICATION_H __# define _ APPLICATION_H _ class Application {public: Application (){}~ Application () {}// the program initializes virtual int init (int argc, char ** argv) {return 0;} // The program mainly executes the virtual int main (int argc, char ** argv) {return 0;} // The program exits and cleans up virtual void retire () {}// The subclass executes init, main, retire function int run (int argc, char ** argv) {if (init (argc, argv )! = 0) {return-1;} main (argc, argv); retire (); return 0;} protected: // used to receive the exit signal void exit_waiting () {sigset_t sset; sigemptyset (& sset); sigaddset (& sset, SIGQUIT); sigaddset (& sset, SIGTERM); sigaddset (& sset, SIGQUIT); sigprocmask (SIG_BLOCK, & sset, NULL ); int sig = 0; for (;) {int ret = sigwait (& sset, & sig); if (ret = 0) {break ;}};# endif
Well, this is just the beginning. It will be written tomorrow. Good night.