2D online game development (network) (7) 1

xiaoxiao2021-04-05  255

2D online game development (network) (7)

Author: akinggw

In the previous chapter, we explain how to transfer information between servers and clients, today, we will explain how to transfer files between them. This feature is very important, for example, in online games, in order to keep the game novel, the game needs to update the data each time.

And our content today is to explain how to transfer files between clients and servers.

The name of this system is called AutoPatcher, which is not included in the Raknet library file. Is a separate class, behind, we will explain its use.

The main work of the AutoPatcher system is to transfer some of them before two and more than two systems, or have changed files. Its main job is to transfer files, compress these transport files, ensure file transfer security and file operations. It does not provide a basic connection or user interface (you can use the functionality in the RAKNET). Autopatcher is usually used in commercial games.

Each system needs to establish an AutoPATCher class instance. The AutoPatcher class contains four files autopatcher.cpp, autopatcher.h, downloadablefiledescriptor.cpp, and downloadablefilescriptor.h. Reebred again, AutoPatcher is not included in the Raknet library file, so it is a bit different when debugging.

These files are included in the Source / AutoPatcher directory, when compiling, you want to include all files under this folder. These files may need to be modified in the actual game programming.

Next, it is to tell AutoPATCHER which files can be downloaded. This matter can be set with the setFileDownloadable (Char * filename, Bool CheckFilesignature) function. The function of this function is to read files into memory. If the file is large, you need to compress it, and finally add these downloadable files to a central list. The first parameter of the function is the path to the file. The second parameter is a BOOL value. The purpose of this parameter is used for digital signatures, and it is used to detect whether the file is modified during transmission. If you set up a true, you will have one of our files XX.SHA to describe our files. You can call the CreateFileSignature function to create this signature file. How to sign the signature detection failure, setFileDownloadable returns set_file_downloadable_file_no_signature_file or set_file_downloadable_file_signature_check_failed.

If these systems have been connected, if you want to download, you can call the RequestDownloadableFileList function to create a download system; if you only want to update, you can call SendDownloadableFileList to create your update system.

When you get the first byte of your packet :: data contains the following package ID:

ID_AUTOPATCHER_FILE_LIST

ID_AUTOPATCHER_REQUEST_FILES

ID_AUTOPATCHER_SET_DOWNLOAD_LIST

ID_AUTOPATCHER_WRITE_FILE

The entire package will be sent to the corresponding function:

Void onautopatcherfilelist (packet * packet, bool onlyacceptfilesifrequested);

Void onautopatcherRequestFiles (Packet * packet); void onautopatchersetdownloadLOADLIST (Packet * packet);

Void onautopatcherwritefile (packet * packet);

Below, let's take a look at how the code is implemented.

First, you need to set a parameter in your project because AutoPatcher uses Zlib compressed packets when compressed files. Therefore, when you compile file, you should load it. I am using DEV C , so I need to download a zlib.devpak, then install it, in the project settings, contain their library files:

LIB / RAKNET.A

LIB / libws2_32.a

LIB / libz.a

If you use VC, you also need the same operation.

After the project is set, you need to include the header file of AutoPatcher in your code:

#include "autopatcher.h"

#include "autopatcher.cpp"

#include "DownloadableFileDescriptor.h"

#include "DownloadableFileDescriptor.cpp"

This example is under the example file of the Raknet.

Then define an AutoPatcher instance:

AutoPATCHER AutoPATCHER;

You need to write a function to analyze the information you downloaded:

Unsigned int showdownloadstatus (AutoPATCHER * AutoPATCHER)

{

Char filename [256];

UNSIGNED NUMBEROFDOWNLOADINGFILES;

Unsigned FileLength, CompressedFileLength;

Bool dataiscompressed;

Numberofdownloadfiles = AutoPATCHER-> GetDownloadStatus (FileName, & FileLength, & Dataiscompressed, & CompressedFileLength);

IF (NumberofDownloadingFiles> 0)

{

Printf ("N% i how many files in the download list n", NumberofdownLoadingFiles;

Printf ("Current File:% S (% I bytes) N", FileName, FileLength;

IF (dataiscompressed == false)

Printf ("File No Compressed Transmission .N");

Else

Printf ("file is compressed into% i byte n", compressedFileLength;

}

Else

PRINTF ("" "There is no downloadable file N");

Return NumberofdownLoadingFiles;

}

This function finally returns the number of files we want to download.

Among them, the GETDOWNLOADSTATUS function prototype is as follows:

Unsigned int AutoPATCHER :: getDownloadStatus (char * fileename,

Unsigned * filelength,

Bool * filedataiscompressed,

Unsigned * compressedFileLength

)

This function returns how many files in the download list.

The first parameter filename, the currently downloaded file; the second parameter fileLength, the length of the currently downloaded file;

The third parameter fileDataLScompressed, whether the file is compressed;

The fourth parameter compressedFilelength, if the file is compressed, returns its size.

This function returns the number of files in the download list.

We use the peer to peer mode in this example, which is the advantage of making it simple, regarding C / S, is still further studied.

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

New Post(0)