autoPatcher.SetNetworkingSystem(rakPeer);
SetNetworkingSystem函数的原型如下:
void SetNetworkingSystem (RakPeerInterface *localSystem)
void SetNetworkingSystem (RakClientInterface *localSystem)
void SetNetworkingSystem (RakServerInterface *localSystem)
这个函数的作用就是设置使用哪个系统的实例来传输文件。
这些设置好了以后,下一步我们将在服务器端设置加载文件,在客户端设置下载文件,设置如下:
服务器端:
if (AutoPatcher::CreateFileSignature(input)==false)
printf("Unable to create file signature %s.shan", input);
if (autoPatcher.SetFileDownloadable(input, true)!=SET_FILE_DOWNLOADABLE_SUCCESS)
printf("Unable to make %s downloadablen", input);
}
printf("Enter name of second file to make downloadable or hit enter for nonen");
gets(input);
if (input[0] && autoPatcher.SetFileDownloadable(input, false)!=SET_FILE_DOWNLOADABLE_SUCCESS)
printf("Unable to make %s downloadablen", input);
前面两个if语句用于设置带签名的文件,最后一个if用于设置不带签名的下载。三个函数原型如下:
bool AutoPatcher::CreateFileSignature ( char * filename ) [static]
为filename建立一个以.sha为扩展名的签名文件。
SetFileDownloadableResult AutoPatcher::SetFileDownloadable ( char * filename,
bool checkFileSignature
)
这个函数,我们已经在前面讲解了。
然后需要在客户端设置文件的下载目录:
autoPatcher.SetDownloadedFileDirectoryPrefix(input);
函数原型如下:
void AutoPatcher::SetDownloadedFileDirectoryPrefix ( char * prefix )
就是设置文件下载到什么地方,用prefix设置,例如 c:.
现在进入循环当中,在消息检测当中,加入下面的代码:
case ID_AUTOPATCHER_REQUEST_FILE_LIST:
printf("Got a request for the file listn");
autoPatcher.SendDownloadableFileList(packet->playerId);
break;
case ID_AUTOPATCHER_FILE_LIST:
printf("n-------nGot the list of available server files.nRequesting downloads.n-------n");
autoPatcher.OnAutopatcherFileList(packet, true);
if (ShowDownloadStatus(&autoPatcher)==0)
{
rakPeer->DeallocatePacket(packet);
goto QUIT;
}
break;
case ID_AUTOPATCHER_REQUEST_FILES:
printf("Got a request for filesn");
autoPatcher.OnAutopatcherRequestFiles(packet);
break;
case ID_AUTOPATCHER_SET_DOWNLOAD_LIST:
printf("* Confirmed download listn");
autoPatcher.OnAutopatcherSetDownloadList(packet);
break;
case ID_AUTOPATCHER_WRITE_FILE:
printf("-------nGot a filen-------n");
autoPatcher.OnAutopatcherWriteFile(packet);
if (ShowDownloadStatus(&autoPatcher)==0)
{
rakPeer->DeallocatePacket(packet);
goto QUIT;
}
break;
}
autoPatcher.SendDownloadableFileList函数的原型如下:
void AutoPatcher::SendDownloadableFileList ( const PlayerID remoteSystem )
这个函数的作用就是将下载列表中的全部文件发送到以PlayerID为标致的客户端去。
.OnAutopatcherFileList的函数如下:
void AutoPatcher::OnAutopatcherFileList ( Packet * packet,
bool onlyAcceptFilesIfRequested
)
这个函数的作用就是分析服务器端的下载文件列表,看是否有我们没有的文件,或是不匹配的文件。然后以包的形式将分析结果传给服务器。
第二个参数,如果设置成真,那么就调用了这个函数以后就用GetDownloadStatus函数进行分析。如果设置成假,就马上下载文件。
autoPatcher.OnAutopatcherRequestFiles(packet);
这个函数原型如下:
void AutoPatcher::OnAutopatcherRequestFiles ( Packet * packet )
这个函数的作用就是从内存中读取文件,然后将它们发送到客户端。
autoPatcher.OnAutopatcherSetDownloadList(packet);
这个函数原型如下:
void AutoPatcher::OnAutopatcherSetDownloadList ( Packet * packet )
这个函数的作用就是服务器端同意下载下载列表上的文件。
autoPatcher.OnAutopatcherWriteFile(packet);
函数原型如下:
bool AutoPatcher::OnAutopatcherWriteFile ( Packet * packet )
这个函数的作用就是将接受到的文件全部写入到我们设置的硬盘里。
其它就没什么了,大家可以参考其源代码就在raknet的例程当中。关于程序执行的效果,请大家自己去编译。
Over! |