Network programming in online games

xiaoxiao2021-03-06  62

Talking about the "Network" programming in online games ???? In recent times, there are always many people who have completed the port model. Many times, I am very depressed, why will everyone choose to use the completion port? Or many times they actually use Windows as a network game server platform, and also open a bunch of online game cases using a Windows platform. Some brothers have always argued that "Windows? 2003? Server is very fierce, very efficient", the correctness of this sentence is not discussed, first speaks how much WINDOWS? 2003? SERVER? Reducing costs are not equal to making more money. Put a free and efficient Linux is not available, the article will give money to buy expensive? And the performance is still low? Don't worry, rest assured, I am right, let's take a look at some of IBM's researchers compare the performance of Linux and Windows. Let's take a look at the speed comparison of thread creation: ???? Of course, maybe you will say, now use the thread pool, you have created a thread when you start, then you will use it directly. Well, let's take a look at the speed of the socket. ???? Don't have a red neck, this is just a simple test, but there is a small news that Windows will increase a lot when it is transmitted for the 127.0.0.1 address of the local area, then is it? Let's take a look at the test results below: ???? Among them, the wire with the fork is to the transmission speed in the case of 127.0.0.1 this address, it is true that the small road is true, and the windows2000 is This is really a lot when the address is transmitted. So what shortcomings are there in Windows and Linux? Windows has a feature in the thread, called Thread? Creation? DECAY, what does it mean? That is, as the running time is getting longer, the speed of the thread is created and the speed of the thread is created, and the speed of the thread will be different. This is why a Linux server can restart a few years. The Windows operating system is not. I don't deny Windows. I just want to say here, the Windows system is due to the Built-IN? GUI it provides, causing the kernel to handle each system call, more or less will have some loss. ???? It seems that there is a bit of a job, these and the network have nothing to do, then we will look back to the network programming, first we will discuss the fundamental of the network, what is it? Yes IO. Receive some data, send some data, in the C / S mode, and divide the two program models of the server and client depending on the received transmission behavior. So how do you deal with these IO? Let's take a look at a few common methods: Blocking? Read-write ???? About this most common model is: while? (Size? =? Read (fd,? Buf,? Max_size,? 0)?) ProcessMessage; ???? Almost unable to handle the situation in which multiple users are connected, because in this case, the operation of Read is blocked, will always wait until there is news to arrive return. So everyone thinks about a way to make this model for multiple users, then: Multi? Threading? Blocking? IO ???? is to connect to each user, open a thread to run the loop above .

However, with the increase in the number of users, there are more and more threads, although most of the time is consumed, but the operating system has paid a considerable amount of user-level thread switching. IOCP? (I / O? COMPLETION port) ???? This is the completion of many people who have the completion of the Tao, it is indeed, under the Windows platform, it is the most perfect solution about IO, but also the efficiency, but also The occupied resources are relatively small, and write related operations in the Windows platform, it is definitely not wrong. The principle is the same as the ticket window of the station, with a fixed, limited number of windows to process all requests. Asynchronous? Io ???? So what is AIO? This is a concept that is different from the above three, or is just a definition, unlike the above, it is a structural model that can be seen. The definition of the so-called AIO is the utilization of the operating system IO bandwidth, resulting in a higher utilization of the operating system, resulting in a highly efficient IO processing structure. This is just a definition, but in fact, the implementation of AIO is varied. Generally speaking, real AIO is implemented in accordance with AIO's thoughts, we call true AIO, such as the API of the WSAAsync series under Windows, and the POSIX? AIO series of the AIO series under Windows, whose principle is to immediately Return, then let the operating system call its callback function after processing IO. Therefore, for the method of using other methods, we are called fake AIO, because fake AIO starts from the definition of AIO, the same effect is basically, the typical method is SELECT / POLL / EPOLL to cooperate with non-block IO read Fake AIO written to implement. In fact, there are many controversy about AIO, and Linux's kernel's support for AIO has also changed almost every version. There are some models that are the same as the AIO's PATCH or even the completion port, but still is still called AIO. ???? Good, talk about it, I hope everyone knows the current IO processing method, then let's take a look at how different IO processing schemes are selected according to different needs. ???? First of all, it is obviously the efficiency problem, just like just in the introduction of IOCP, IOCP is undoubtedly Windows to solve the best solution for IO issues, then linux? The choice of IO operations under Linux should be some hindle, the key is the hits between the fake AIO and POSIX series of the SELECT / POLL / EPOLL. ???? Before Linux? 2.5, POSIX? AIO has little unique advantage, so almost all Linux-based network applications use SELECT / POLL / EPOLL with non-blocking simulations to achieve efficient IO processing schemes, but In the kernel of Linux? 2.5, the support of AIO is very large, causing the possibility of using POSIX? AIO. However, the disadvantage is also obvious. Since the support of AIO in the kernel is composed of system calls that are very close to a series of operating system levels in the operating system. There is a certain problem in compatibility, so the suggestion here is that if you don't understand POSIX? AIO, it is best not to choose POSIX? AIO, but the old man use SELECT / POLL / EPOLL with non- Blocking IO processing method, although the performance difference between the two is still not very big, but I want to update the Linux kernel, POSIX? AIO will become better and better.

转载请注明原文地址:https://www.9cbs.com/read-118863.html

New Post(0)