The popularity of the network has made server programs widely used. So how do we design strong servers using Delphi?
Some people say that if you want to design a server, you must use VC to design it. In fact, what this person said makes sense, because if you want to use Delphi to design a server, if you want to design an efficient server, don’t use the big ones brought by Delphi. Some controls (it is better not to use the Delphi control), why? I will tell you below. In this way, you use APIs to design servers, which is not much different from VCs.
Use Delphi to design server programs. The specific choice is whether to use form message mode or complete port mode, which depends on the number of user connections. If your user connections are less than 1,000 and the amount of data processed is not large, you can use the message mode of the form to develop the server. If it is greater than 1,000, it is best to use the completion port to develop the server. I suggest that everyone is best to use the complete port mode, because you cannot ensure that the number of users does not change. At the same time, if your server runs for a period of time, it is best to make a WIN service program, which can ensure later Less maintenance.
Now introduce what you need to pay attention to when developing a Delphi server:
1 Do not use String variables in your program
This was also discovered during the actual development process. When I first developed, I used String variables to develop programs in order to be simpler, but the program always had problems after running for a period of time. Later, I was not clear about the reason after checking it. , I went online to search for information and found that someone introduced that I should not use String to make variables. The problem of modifying all my programs into arrays is basically solved.
2 Use fast encryption algorithms such as XOR encryption or DES encryption
The server must be encrypted when passing it to the client, but what type of encryption algorithm should be used? Don't use algorithms that require a lot of operations, such as RSA, and other algorithms. It is best to use XOR encryption or DES transposition encryption algorithms. This mainly meets the requirements of ordinary encrypted ciphertexts and ensures the computing speed of the server. You can also use RSA to encrypt ciphertexts, but this will cause the server to process slowly, and if you encounter a lot of processing, it is easy for the server to reject the server.
3 Use the original ADO function to connect to the database
Server programs are usually combined with databases. When using Delphi development, ADO controls are usually used to make them, but if you study the ADO manual, you will find that the server does not actually need controls to complete data operations. It can be done directly using the corresponding ADO functions. Mainly because server programs and databases are usually relatively simple operations and are not very complicated. So just use the original ADO mode. This also reduces the problems caused by ADO controls.
4. Use more “pools”
During the design process of the server, you must support a large number of variables. If you do not use the concept of pooling, your program will waste a lot of time in the process of creating and releasing variables. And it is prone to problems. Try not to create and release variables during the design process. If the variables that can be considered are created at the beginning of the run. This can speed up the program's running speed and reduce conflicts. How to use the pool technology? I will consider writing an introduction to it if I have time in the future.
5 Proficient in using pointers
If you are not familiar with pointer operation, you will almost never be able to design an efficient server. If you want to truly understand the concept of pointer, it will be a big plus for designing a server.
Here is an example. If you use Recv to receive data into a Buffer, you need to perform decryption operations. You can use the following method:
var
a,b:array [1..8] of byte;
i :integer;
ResultBuffer :array [1..Max] of byte;
Begin
for i := 1 to Sizeof(Buffer) div 8 do
Begin
move(Buffer[(i-1)*8+1],a,8);
Des(a,b,true); //DES encryption and decryption processing is used here
move(b,ResultBuffer[(i-1)*8+1],8);
end;
end
Let’s take a look at the above code and the idea is very clear, which is to use the DES decryption algorithm to decrypt it into b, and then put it back into the ResultBuffer.
If you are proficient in pointers, your efficiency will be greatly improved
var
a,b:Pbyte;
i :integer;
ResultBuffer :array [1..Max] of byte;
Begin
for i := 1 to Sizeof(Buffer) div 8 do
Begin
a := @Buffer[(i-1)*8+1];
b := @ResultBuffer[(i-1)*8+1]
Des(a^,b^,true); //DES encryption and decryption processing is used here
end;
end
Let’s take a look at the above code. Is there a process of missing two Copy data? This is the efficiency brought by pointers.
6 Use WSASend, WSARecv and other WinSocket 2 functions more often, do not use Send, Recv functions
This mainly depends on what system your server is running on. If it is running on a WIN system, it is best to use the functions of the WSA system, because Microsoft has optimized them all after all.
7. Use thread pool operations rationally
An efficient server must use thread pooling technology. It is reasonable to use many threads and what kind of data is needed for threads to process. I personally think that if you want to use thread pooling technology, you must deal with the most time-consuming operations, such as database query operations.
8 If the server uses the concept of "pool", there is another problem. How to efficiently allocate pools?
I use pools in a lot of ways, such as thread pools, data pools, etc. When data arrives, how to allocate the pool? I won’t tell you here, and I will write a special article about the pool in the future. Detailed description of how to use the pool. You can also think about it yourself.
9 Using efficient string manipulation functions
Because the server must run a large number of strings, it will be time-consuming if you use the functions provided by Delphi to operate. Therefore, here we recommend that you use QStrings.pas string to operate the function set, which I believe will be helpful to you.
10 Optimize your SQL query statements
On the one hand, you can optimize SQL query statements to improve operational efficiency, and on the other hand, you can also use stored procedures to improve operational efficiency. (You need to look at the content of the database for this knowledge, and I won’t talk about how to optimize it here.)
The above introduction is my practical experience, and it may not be all right. I hope everyone can help. If there is a better approach, it can also be discussed.