2D网络游戏开发(网络篇)(七)2

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!
 
关于更多内容请访问金桥科普网站( http://popul.jqcq.com   )游戏开发栏目,如你需要游戏开发方面的书籍请参考金桥书城游戏频道( http://book.jqcq.com/category/1_70_740.html )。 如果你在阅读本篇文章时有什么好的建议请来信给我,我的 E_mail: akinggw@126.com. 如果你在使用 SDL 时有什么问题,请到金桥科普网站( http://popul.jqcq.com   )游戏开发栏目,我将详细地为你解答。
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值