Recently a consistent commitment to Linux under the C development, because the boss is a message out. Therefore, the main technology used is the basic background architecture of a message.
In this period of time, learned a lot, and then admire the technology of a certain message is really very powerful.
Therefore, I feel, from scratch to develop our project, to now, with my big brother to learn a lot of things.
Currently in the game of the Guild system, task system, mail system, map, mall, and so many large and small systems, are responsible for me.
Here's a little bit of what I've summed up recently.
1. Time
Linux systems have a lot of things in time. In the game, time is a very important variable, involving back and forth time synchronization, game business Countdown, Heartbeat and so on a series of function points
And so on, if you can use the variable of time flexibly, at least you should know the following functions or variables
Copy Code code as follows:
The variable is actually a long type, representing the number of seconds from one point of time (usually January 1, 1970 0:0 0 seconds). Yes, the indicator is the number of seconds.
There are other functions in the time.h file that are parameters
Copy Code code as follows:
In the time.h header file, we can also see functions that are either time_t as parameter types or return value types:
Double Difftime (time_t time1, time_t TIME0);
time_t mktime (struct TM * timeptr);
time_t Time (time_t * timer);
char * asctime (const struct TM * timeptr);
char * CTime (const time_t *timer);
Give a small example
Copy Code code as follows:
/* Time Example * *
#include <stdio.h>/* printf *
#include <time.h>/* time_t, struct TM, Difftime, TIME, mktime * *
int main ()
{
time_t timer;
struct TM Y2K;
Double seconds;
Y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; Y2k.tm_mon = 0; Y2k.tm_mday = 1;
Time (&timer); /* get current time; Same As:timer = time (NULL) * *
seconds = Difftime (Timer,mktime (&y2k));
printf ("%.f seconds since January 1," The current timezone, seconds);
return 0;
}
Below for more precise
The following two types are also used frequently
Copy Code code as follows:
struct Timeval *a_psttv, struct timezone *a_psttz
When we call
Copy Code code as follows:
Gettimeofday (&pstctx->stcurr, NULL);
Get to the current time
2. Random number
Random numbers are often used in programs, randomly coming out of a number to make something seem random.
The C language currently provides the RAND function. So how do we use it, look at the following macro
Copy Code code as follows:
#define RAND1 ((int) (double) (range) *rand ()/(rand_max+1.0))
This macro randomly gives a number between 0 and rang-1. But we want to use a sequence of his to disrupt one of our arrays, we can randomly out its subscript and then use it with a specific
Elements such as, array[0] to exchange.
3. Arrays and pointers
This topic is the permanent topic of C language. At present I commonly used the first level two pointers, is not very difficult. The operation of the data by taking the address of the specific data memory.
The conversion between arrays and pointers, and so on
Now, in particular, three functions should be noted.
Copy Code code as follows:
void * memcpy (void * destination, const void * source, size_t num);
void * Memmove (void * destination, const void * source, size_t num);
void * memset (void * ptr, int value, size_t num);
In the third three parameter sizeof must pay attention to its data block size, and data starting position
Warning
always remember to make sure the pointer is not empty before you use the pointer.
Before using arrays, be sure to pay attention to array bounds behavior.
4. Use callback function skillfully
When our system needs to call the same type of function, it is best to use the callback function, register, and then according to the function associated with the command, make a specific call, similar to C + + polymorphic behavior. Very worthwhile to use.
5, algorithm algorithm or algorithm.
Usually write code, to think more, more brain think this write good, use the specific algorithm will be better, will improve efficiency, save space and so on.
6, the background program to remember that the database is a bottleneck, so and the database when the time, to use asynchronous thinking, the data to the database, the program to do why,
To know whether or not to succeed is to be sure to come back with a data marker that tells us the program.
Today I thought so much and wrote so much. Keep on trying to refuel.