官方网址:http://www.jenkinssoftware.com/raknet/manual/tutorialsample3.html

Tutorial code sample 3
Remote procedure call setup. 

The target of this exercise was to add the following features to sample 2:

  1. When the client successfully connects, send a custom user message.
  2. The first byte of custom messages should always start after the enumeration ID_USER_PACKET_ENUM, defined in MessageIdentifiers.h. Otherwise, RakNet would confuse its own identifiers with your game identifiers.
  3. Read the custom message in our packet processing loop. The native RakString type can read and write to the BitStream class. You would not be able to do this using std::string.

New code over sample 2 is in bold.

#include <stdio.h>
#include <string.h>
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h" // MessageID
#define MAX_CLIENTS 10
#define SERVER_PORT 60000 enum GameMessages
{
ID_GAME_MESSAGE_1=ID_USER_PACKET_ENUM+1
};
int main(void)
{
char str[512]; RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
bool isServer;
RakNet::Packet *packet; printf("(C) or (S)erver?\n");
gets(str); if ((str[0]=='c')||(str[0]=='C'))
{
RakNet::SocketDescriptor sd;
peer->Startup(1,&sd, 1);
isServer = false;
} else {
RakNet::SocketDescriptor sd(SERVER_PORT,0);
peer->Startup(MAX_CLIENTS, &sd, 1);
isServer = true;
} if (isServer)
{
printf("Starting the server.\n");
// We need to let the server accept incoming connections from the clients
peer->SetMaximumIncomingConnections(MAX_CLIENTS);
} else {
printf("Enter server IP or hit enter for 127.0.0.1\n");
gets(str);
if (str[0]==0){
strcpy(str, "127.0.0.1");
}
printf("Starting the client.\n");
peer->Connect(str, SERVER_PORT, 0,0); } while (1)
{
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
{
switch (packet->data[0])
{
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
printf("Another client has disconnected.\n");
break;
case ID_REMOTE_CONNECTION_LOST:
printf("Another client has lost the connection.\n");
break;
case ID_REMOTE_NEW_INCOMING_CONNECTION:
printf("Another client has connected.\n");
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
{
printf("Our connection request has been accepted.\n"); // Use a BitStream to write a custom user message
// Bitstreams are easier to use than sending casted structures, and handle endian swapping automatically
RakNet::BitStream bsOut;
bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
bsOut.Write("Hello world");
peer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
}

break;
case ID_NEW_INCOMING_CONNECTION:
printf("A connection is incoming.\n");
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
printf("The server is full.\n");
break;
case ID_DISCONNECTION_NOTIFICATION:
if (isServer){
printf("A client has disconnected.\n");
} else {
printf("We have been disconnected.\n");
}
break;
case ID_CONNECTION_LOST:
if (isServer){
printf("A client lost the connection.\n");
} else {
printf("Connection lost.\n");
}
break;

case ID_GAME_MESSAGE_1:
{
RakNet::RakString rs;
RakNet::BitStream bsIn(packet->data,packet->length,false);
bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
bsIn.Read(rs);
printf("%s\n", rs.C_String());
}
break;

default:
printf("Message with identifier %i has arrived.\n", packet->data[0]);
break;
}
}
} RakNet::RakPeerInterface::DestroyInstance(peer); return 0;
}

最新文章

  1. C#后台调用公网接口(GET, POST)
  2. percona-toolkit中在线ddl
  3. 转:logBack.xml配置路径
  4. java三种实现线程的方法比较
  5. linux之间连接—使用SSH
  6. 伴随ListView、RecyclerView、ScrollView滚动滑入滑出小图标--第三方开源--FloatingActionButton
  7. 手写归并排序(MergeSort)
  8. windows下Qt5.1 for android开发环境配置(PS:Qt5.2出来了哈,稳定)
  9. HDU3591找零,背包
  10. jquery滚动到指定元素,模仿锚点
  11. c# 迭代器 与 集合 IEnumerable.GetEnumerator 方法
  12. POJ 1155 - TELE 树型DP(泛化背包转移)..
  13. WP 开发中.xaml 与.xaml.cs
  14. java abstract类和abstract方法
  15. java通过JDBC链接SQLServer2012【转载!!!超详细】
  16. jQuery实现web页面固定列表搜索
  17. JSP页面输出九九乘法表--JSP基础
  18. 常用的user32说明
  19. 分布式系统监视zabbix讲解十一之zabbix升级--技术流ken
  20. Vue 移动端常用tap事件封装

热门文章

  1. java+jsp+servlet实现分页
  2. Sensitive directory/file Integrity Monitoring and Checking
  3. BZOJ2730: [HNOI2012]矿场搭建
  4. 个人整理的一些web前端面试题
  5. 在UP Board 上搭建M——L服务器
  6. PHPCMS导航栏当前栏目选中方法
  7. A*寻路算法探究
  8. TCP学习之一:TCP网络编程概念
  9. 利用JS实现点击按钮后图片自动切换
  10. 如何安装 第三方 Go 离线包? (GOPATH、 go install)