当前位置: 首页 > 工具软件 > Raknet > 使用案例 >

raknet android,RakNet入门介绍

邵捷
2023-12-01

#include "../RakNet/src/RakPeerInterface.h"

#include "../RakNet/src/RakNetTypes.h"

#include "../RakNet/src/MessageIdentifiers.h"

#include "../RakNet/src/BitStream.h"

#include "../RakNet/src/RakString.h"

#include "../RakNet/src/StringCompressor.h"

int StartServer();

int StartClient(void* param);

//测试下RakNet

void main(int argc, char* argv[]) {

PTHREAD pThread = 0;

RakNet::RakPeerInterface* peer = RakNet::RakPeerInterface::GetInstance();

if (!peer)

{

LogUtils::e(LOG_TAG, "main get peer failed.");

system("pause");

return;

}

LogUtils::i(LOG_TAG, "(C)客服端 (S)服务器?");

char str = getchar();

if (str == 'c')

{

StartClient(NULL);

//PlatformAbstract::CreateThread(pThread, (THREADPROCTYPE)&StartClient, NULL);

LogUtils::e(LOG_TAG, "::main 客服端已经建立。");

}

else

{

StartServer();

//PlatformAbstract::CreateThread(pThread, (THREADPROCTYPE)&StartServer, NULL);

LogUtils::e(LOG_TAG, "::main 服务器已经建立。");

}

system("pause");

}

这里是一个测试方法 可以直接放到测试工程中测试

int StartServer() {

//给服务器端创建一个实例

RakNet::RakPeerInterface* pPeer = RakNet::RakPeerInterface::GetInstance();

if (NULL == pPeer)

{

LogUtils::e(LOG_TAG, "TestFunc get pPeer failed.");

return -1;

}

else

{

LogUtils::e(LOG_TAG, "::TestFunc ---------MyChatServer Init Success(C)-----------");

}

RakNet::Packet* pPacket;

LogUtils::i(LOG_TAG, "::TestFunc Start Server .....");

//启动服务器

RakNet::SocketDescriptor sd = RakNet::SocketDescriptor(6000, 0);

RakAssert(RakNet::RAKNET_STARTED == pPeer->Startup(1, &sd, 1));

//设置最大链接数

pPeer->SetMaximumIncomingConnections(1);

auto boundAddress = pPeer->GetMyBoundAddress();

const char* sAddr = boundAddress.ToString();

LogUtils::e(LOG_TAG, "::StartServer sAddr: %s", sAddr);

//

while (1)

{

for (pPacket = pPeer->Receive(); pPacket; pPeer->DeallocatePacket(pPacket), pPacket = pPeer->Receive())

{

LogUtils::e(LOG_TAG, "StartServer pPacket type: %d", pPacket->data[0]);

switch (pPacket->data[0])

{

case ID_REMOTE_DISCONNECTION_NOTIFICATION: {

LogUtils::e(LOG_TAG, "StartServer Another client has disconnected");

}break;

case ID_REMOTE_CONNECTION_LOST: {

LogUtils::i(LOG_TAG, "StartServer ID_REMOTE_CONNECTION_LOST");

}break;

case ID_REMOTE_NEW_INCOMING_CONNECTION: {

LogUtils::i(LOG_TAG, "StartServer ID_REMOTE_CONNECTION_LOST");

RakNet::BitStream bs;

bs.Write < RakNet::MessageID>(ServerConnectBack);

RakNet::StringCompressor::Instance()->EncodeString(pPacket->systemAddress.ToString(true), 255, &bs);

}break;

//client端调用Connect后收到的server响应

case ID_CONNECTION_REQUEST_ACCEPTED: {

LogUtils::i(LOG_TAG, "StartServer ID_CONNECTION_REQUEST_ACCEPTED");

RakNet::BitStream bsOut;

bsOut.Write((RakNet::MessageID)ID_USER_PACKET_ENUM + 1);

bsOut.Write("Server Say Hello.");

pPeer->Send(&bsOut, HIGH_PRIORITY, UNRELIABLE_SEQUENCED, 0, pPeer->GetMyBoundAddress(), false);

}break;

//server端收到来自client的Connect

case ID_NEW_INCOMING_CONNECTION: {

LogUtils::e(LOG_TAG, "StartServer ID_NEW_INCOMING_CONNECTION");

}break;

case ID_NO_FREE_INCOMING_CONNECTIONS: {

LogUtils::e(LOG_TAG, "StartServer ID_NO_FREE_INCOMING_CONNECTIONS");

}break;

case ID_DISCONNECTION_NOTIFICATION: {

LogUtils::e(LOG_TAG, "StartServer ID_DISCONNECTION_NOTIFICATION");

}break;

case ID_CONNECTION_LOST: {

LogUtils::e(LOG_TAG, "StartServer ID_CONNECTION_LOST");

}break;

case ID_CONNECTION_ATTEMPT_FAILED: {

LogUtils::e(LOG_TAG, "StartServer ID_CONNECTION_ATTEMPT_FAILED");

}break;

//用户自定义数据包

case ID_USER_PACKET_ENUM + 1:

{

LogUtils::e(LOG_TAG, "StartServer recv ID_USER_PACKET_ENUM + 1: %d", ID_USER_PACKET_ENUM + 1);

RakNet::RakString rs1, rs2;

RakNet::BitStream bsIn(pPacket->data, pPacket->length, false);

bsIn.IgnoreBytes(sizeof(RakNet::MessageID));

//bsIn.Read(rs); //提取字符串

RakNet::StringCompressor::Instance()->DecodeString(&rs1, 255, &bsIn);

RakNet::StringCompressor::Instance()->DecodeString(&rs2, 255, &bsIn);

LogUtils::e(LOG_TAG, "::StartServer Recv User Data: %s %s %s", pPacket->systemAddress.ToString(true, '|'), rs1.C_String(), rs2.C_String());

}

break;

default: {

LogUtils::i(LOG_TAG, "::TestFunc Message with identifier %i has arrived", pPacket->data[0]);

}break;

}

}

sleep(100);

}

//

RakNet::RakPeerInterface::DestroyInstance(pPeer);

}

int StartClient(void* param) {

//获取RakNetPeer接口

RakNet::RakPeerInterface* peer = RakNet::RakPeerInterface::GetInstance();

if (!peer)

{

LogUtils::e(LOG_TAG, "::StartClient get peer failed.");

return 0;

}

RakNet::SocketDescriptor sd = RakNet::SocketDescriptor(0, 0);

RakAssert(RakNet::RAKNET_STARTED == peer->Startup(1, &sd, 1));

LogUtils::i(LOG_TAG, "::StartClient get peer success");

RakAssert(RakNet::CONNECTION_ATTEMPT_STARTED == peer->Connect("127.0.0.1", 6000, 0, 0, 0, 0));

while (peer->IsActive())

{

//开始发包了

LogUtils::i(LOG_TAG, "::StartClient %d 发包", GetTickCount());

RakNet::BitStream bIn;

bIn.Write<:messageid>(ID_USER_PACKET_ENUM + 1);

RakNet::StringCompressor::Instance()->EncodeString("Client Say Hello", 255, &bIn);

RakNet::StringCompressor::Instance()->EncodeString("Client Say NMH", 255, &bIn);

//注意broadcast参数 如果是true,则发广播,systemidentifier表示不发送地址,一般使用常量 RakNet::UNASSIGNED_SYSTEM_ADDRESS

//如果是false,则往systemidentifier指定的ip和端口处发送

//如果发包是有序的,则channel指定了信道,同一信道的具有递增的排序值,避免有多种有序消息的排序值相互干扰

uint32_t sendrs = peer->Send(&bIn, HIGH_PRIORITY, UNRELIABLE_SEQUENCED, 0, RakNet::SystemAddress("127.0.0.1", 6000), false);

LogUtils::e(LOG_TAG, "StartClient sendres: %d", sendrs);

Sleep(1000);

}

peer->Shutdown(1000);

RakNet::RakPeerInterface::DestroyInstance(peer);

peer = NULL;

LogUtils::e(LOG_TAG, "Disconnect from Server. StartClient peer closed");

return 0;

}

 类似资料: