Chapter Seventy-Four: Change the Pea Shooter Again

On the surface, Han Hai had entered the cultivation, but in fact, he had opened the data model library, and when he observed the harvest this time, he remembered that after killing Li Yilong, there was a sound from the data model warehouse, and something had been put into the warehouse.

Han Hai searched in the warehouse for a long time, and finally found the new data model, this is a white card, the center of the card is written SYSTEM, and the upper position of the card is written with socket.io, when he sees this model, Han Hai's mood is very happy, this data model is obviously a systematic class library, and its function is also very powerful.

It's no exaggeration to say that without sockets, the Internet wouldn't exist.

That's right, socket is the nested word protocol in network transmission, this protocol is to give you a quantitative byte transmission space, responsible for the connection between computer terminals a protocol scheme.

At present, the protocol that is widely used in browsers is the http protocol developed based on TCP long connection, which is a short connection protocol, the principle is very simple, one request is returned, and then disconnected, and most of the chat software used by everyone is made of software made by TCP long connection protocol.

Now that Hanhai has a socket system library, he can write an IM system, that is, an instant messaging system, which can also solve the problem of inconvenient communication in team collaboration.

After the apocalypse, because the power facilities were destroyed by monsters or lost maintenance, all kinds of mobile terminals have lost their use, and now everyone communicates by roaring, which is very unfavorable for the spread of information, and when Han Hai has this socket system library, he can develop multi-person chat software based on his own system.

Moreover, after the development of this kind of software, Han Hai can not only use it for his own people, but also sell it to others for use, and earn money, which not only facilitates his own wealth, but also provides services to the society.

Han Hai couldn't help but smile when he thought of this, and then Han Hai created a new project, and then created 6 new folders, namely protocol, common, core, server, main, and utils, which correspond to protocol modules, general modules, core modules, service modules, main function folders, and utility folders.

This time Han Hai wrote a slightly larger project, in which the number of characters used, he estimated that it would take at least 20-300,000 characters, that is, it would consume 20-300,000 characters of energy, which shows the difficulty of the server implementation of a chat system, and after writing the server, Han Hai also needs to realize the writing of the client, which also requires a lot of character energy.

However, after the siege of the city, Han Hai's character energy reached 2 million, which is still enough for him to squander, so he doesn't have to worry about character energy at present.

Then Han Hai began to write a long time, but fortunately, he wrote a set of chat systems when he was in the company before, and now he can recall it and copy it again.

The real-time chat system written by Han Hai is mainly divided into three layers: the protocol layer, the function layer and the business layer.

The protocol layer is mainly to define the convention when the server and the client transmit data packets, and this layer is also divided into two layers, the first is the communication protocol layer, and the second is the business protocol layer.

Communication protocol layer: It is mainly a generalized encryption and decryption scheme, such as handshake, reconnection, and disconnection.

Business protocol layer: It mainly deals with the convention that a user wants to communicate with someone, and then send a message to someone, disconnect, etc.

To put it bluntly, the protocol is also a data structure, but the way they propagate is streamlined, and the code needs to be parsed step by step, just like when writing a letter, I want to tell you something, but I can't move, the first line of the letter must be written with your name, followed by what I wrote to you.

Of course, it is not as simple as writing a letter, we may add a lot of marks in front of the main message body, as I said before, the first layer uses two bytes to use the IM logo, which is a placeholder belonging to this communication system, the second layer uses four bytes to pass in a time, the third layer uses one byte to transmit a short integer 7, the business of identifying this text is to transmit messages, the fourth layer uses four bytes to put a long integer to identify the byte length of the message body, and the fifth layer can use the length of the message body just read, in order to get the message body of the fixed length.

After that, you also need to decode the message body to get the real message, but this thing is generally written on the client, and the server is only responsible for transmission.

Of course, the above description is just a chestnut, and the real application is more and more complex than these data, for example, if a user passes a message to another user, if it is handed over to another user, not only the server must identify the identity of the sender of the message body, but also identify whether the message body exists, whether it can be sent, and so on.

The functional layer is mainly used for some data processing, such as the following code:

int encodeLong(long long int src,unsigned char **ret,int &len)

{

unsigned char *buf =(unsigned char*)malloc(9);

memset(buf,0,9);

unsigned char *p = buf;

*p++|= LPUSH_FMT_LONG;

*p++|=(src>>56)&0xFF;

*p++|=(src>>48)&0xFF;

*p++|=(src>>40)&0xFF;

*p++|=(src>>32)&0xFF;

*p++|=(src>>24)&0xFF;

*p++|=(src>>16)&0xFF;

*p++|=(src>>8)&0xFF;

*p++|=(src)&0xFF;

len = 9;

*ret = buf;

return 0;

}

What this code does is encapsulate a long integer of data into a byte stream, and the first part of the byte stream is returned to the secondary pointer.

The business module is much easier to understand, for example, forwarding the message from a client, finding the corresponding user through the identification of the user ID, and then finding the corresponding client from the message registered by the user on the server, and then transmitting the message to another client through the socket.

It took Han Hai a whole half of the night to complete the writing of the server, if he hadn't written a lot of things, it would not have been possible to write so fast, if a novice writes a chat server, it will take a month from entry to start, of course, novice refers to the level of engineering graduates.

Next, it's time to write the client, the client and the server are different, the client needs a carrier, this Han Hai has already thought of it, at this time, the attack of the pea shooter can no longer break through the defense of ordinary zombies, so Han Hai is ready to change it into a communication tool, think about what kind of picture is a pea shooter on each shoulder, and then talk to its barrel.

As soon as Han Hai thought of this, he felt that he had infinite motivation, and the evil taste of being a science and engineering man was really not understandable to ordinary people.

Han Hai came back to his senses, opened the data model of the pea shooter, and then began to read for a long time, this time because he still rewrote the module of the barrel attack direction, so he had the permission to operate.

But this time Han Hai needs to overhaul the function of this module, because Han Hai wants to completely rewrite the function of the attack into a composition of listening to the sound and playing the sound, fortunately, the pea shooter is born with a hearing model, and there is also a mouth on the barrel, Han Hai only needs to sample and forward these two functional modules.

At this point, Han Hai had to use the hook technique, and he wrote a total of 2 tampering functions, because Han Hai, the prototype of the function, could see it, so the tampered function could seamlessly connect with the original function, so as not to affect the previous program.

The first to tamper with is the Pea Shooter's function of listening to sounds, from which he simply intercepts the data of each frame of sound.

The second tampering is the function of the pea shooter's vocalization, this time he needs to add the sound data he receives from the socket to the team that makes the sound.

The prototype of the Pea Shooter's sound is the standard PCM, which stands for Pulse Code Modulation.

In the optical fiber communication system, the optical fiber transmits binary optical pulse "0" and "1" codes, which are generated by the on-off modulation of the light source by the binary digital signal. A digital signal is generated by sampling, quantizing and encoding a continuously changing analog signal, which is called PCM (Pulse-code modulation), that is, pulse code modulation.

However, this kind of sample data is generally large in order of magnitude, which is not conducive to transmission, so Hanhai uses a general compression standard AAC for encoding and transmission, which stands for Advanced Audio Coding.

At present, Hanhai can roughly realize the AAC encoding steps, this compression encoding algorithm is a loss algorithm, the principle of sampling PCM data for a period of time, and then using Fourier variation codec, the sound is actually the amplitude of different frequency domains, superimposed sine waves, the encoding algorithm is actually to superimpose the time domain sine wave into a near-square sine wave, and decoding is to use the Fourier inverse transform to decode the superimposed sine wave into the original segment of the sine wave. (This is just a rough codec)

In the next step, the compressed audio can be sent to the server, and then forwarded by the server to another client, and when the client receives the audio message, it runs the tampered sound function, so as to achieve the purpose of communication.

Of course, the client must also establish a set of identifiers, otherwise there will be no way to find contacts for communication, which Han Hai has already thought of, when the model is established, Han Hai can set a digital number for each pea shooter, when someone wants to communicate, dial on the pea shooter, and the group concept established within the system is actually a chat group, and the contact can send a message in the group to communicate with everyone in the group.

Later, Han Hai also wrote a chat interface, in which he turned the green skin on the side of the pea shooter into a dynamic screen, so that users could send both voice and text. (Refer to QQIM)

At present, Han Hai has not developed the function of video chat, because the encoding of video is more than a little more complicated than sound, which is what he lacked in his previous learning, which needs him to learn H264 video encoding before it can be realized, so here he left a button, only to improve it later.

After doing this, Han Hai wrote the function of the socket connection, and then carefully observed the written code, which is a habit he has developed for a long time, no programmer dares to say that the program he wrote has no bugs, if someone said it, then he just didn't find it.

After a while, Han Hai modified a few more places, and then carefully looked at them again, until he couldn't find the problem, and then he breathed a sigh of relief.

At this time, he felt a sense of sleepiness coming, obviously a lot of code writing, consumed a lot of energy, which made him feel shocked, because there was still something to do tomorrow, thinking of this, he didn't dare to slack off, began to empty his head, really don't think about things, and then fell asleep.