本文整理汇总了C++中raknet::SystemAddress::GetPort方法的典型用法代码示例。如果您正苦于以下问题:C++ SystemAddress::GetPort方法的具体用法?C++ SystemAddress::GetPort怎么用?C++ SystemAddress::GetPort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类raknet::SystemAddress
的用法示例。
在下文中一共展示了SystemAddress::GetPort方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connect
void Connector::connect( RakNet::SystemAddress server ){
if (raknetInterface == nullptr){
LOG("Client is not started!");
return;
}
char * ip = new char[64]; // must be new char[]! Not char ip[], BAD_ACCESS!!
for(int i=0; i<64; i++){ ip[i] = 0; }
server.ToString(false, ip);
int result = raknetInterface->Connect(ip, server.GetPort(), RAKNET_PASSWORD, (int)strlen(RAKNET_PASSWORD));
LOG("[Connector] Connecting to %s", ip);
if(result == RakNet::CONNECTION_ATTEMPT_STARTED)
{
LOG("[Connector] Connecting to %s started.", ip);
}
else if (result == RakNet::INVALID_PARAMETER)
{
LOG("[Connector] INVALID_PARAMETER");
}
else if (result == RakNet::CANNOT_RESOLVE_DOMAIN_NAME)
{
LOG("[Connector] CANNOT_RESOLVE_DOMAIN_NAME");
}
else if (result == RakNet::ALREADY_CONNECTED_TO_ENDPOINT)
{
LOG("[Connector] ALREADY_CONNECTED_TO_ENDPOINT");
}
else if (result == RakNet::CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS)
{
LOG("[Connector] CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS");
}
else if (result == RakNet::SECURITY_INITIALIZATION_FAILED)
{
LOG("[Connector] SECURITY_INITIALIZATION_FAILED");
}
else{
LOG("[Connector] connecting atempt failed for unknown reason!!!");
}
delete [] ip;
}
示例2: main
//.........这里部分代码省略.........
break;
case ID_CLOUD_GET_RESPONSE:
{
RakNet::CloudQueryResult cloudQueryResult;
cloudClient.OnGetReponse(&cloudQueryResult, packet);
unsigned int rowIndex;
const bool wasCallToGetServers=cloudQueryResult.cloudQuery.keys[0].primaryKey=="CloudConnCount";
printf("\n");
if (wasCallToGetServers)
printf("Downloaded server list. %i servers.\n", cloudQueryResult.rowsReturned.Size());
else
printf("Downloaded client list. %i clients.\n", cloudQueryResult.rowsReturned.Size());
unsigned short connectionsOnOurServer=65535;
unsigned short lowestConnectionsServer=65535;
RakNet::SystemAddress lowestConnectionAddress;
for (rowIndex=0; rowIndex < cloudQueryResult.rowsReturned.Size(); rowIndex++)
{
RakNet::CloudQueryRow *row = cloudQueryResult.rowsReturned[rowIndex];
if (wasCallToGetServers)
{
unsigned short connCount;
RakNet::BitStream bsIn(row->data, row->length, false);
bsIn.Read(connCount);
printf("%i. Server found at %s with %i connections\n", rowIndex+1, row->serverSystemAddress.ToString(true), connCount);
unsigned short connectionsExcludingOurselves;
if (row->serverGUID==packet->guid)
connectionsExcludingOurselves=connCount-1;
else
connectionsExcludingOurselves=connCount;
// Find the lowest load server (optional)
if (packet->guid==row->serverGUID)
{
connectionsOnOurServer=connectionsExcludingOurselves;
}
else if (connectionsExcludingOurselves < lowestConnectionsServer)
{
lowestConnectionsServer=connectionsExcludingOurselves;
lowestConnectionAddress=row->serverSystemAddress;
}
}
else
{
printf("%i. Client found at %s", rowIndex+1, row->clientSystemAddress.ToString(true));
if (row->clientGUID==rakPeer->GetMyGUID())
printf(" (Ourselves)");
RakNet::BitStream bsIn(row->data, row->length, false);
RakNet::RakString clientData;
bsIn.Read(clientData);
printf(" Data: %s", clientData.C_String());
printf("\n");
}
}
// Do load balancing by reconnecting to lowest load server (optional)
if (didRebalance==false && wasCallToGetServers && cloudQueryResult.rowsReturned.Size()>0 && connectionsOnOurServer>lowestConnectionsServer)
{
printf("Reconnecting to lower load server %s\n", lowestConnectionAddress.ToString(false));
rakPeer->CloseConnection(packet->guid, true);
// Wait for the thread to close, otherwise will immediately get back ID_CONNECTION_ATTEMPT_FAILED because no free outgoing connection slots
// Alternatively, just call Startup() with 2 slots instead of 1
RakSleep(500);
rakPeer->Connect(lowestConnectionAddress.ToString(false), lowestConnectionAddress.GetPort(), 0, 0);
didRebalance=true;
}
cloudClient.DeallocateWithDefaultAllocator(&cloudQueryResult);
}
break;
case ID_CLOUD_SUBSCRIPTION_NOTIFICATION:
{
bool wasUpdated;
RakNet::CloudQueryRow cloudQueryRow;
cloudClient.OnSubscriptionNotification(&wasUpdated, &cloudQueryRow, packet, 0 );
if (wasUpdated)
printf("New client at %s\n", cloudQueryRow.clientSystemAddress.ToString(true));
else
printf("Lost client at %s\n", cloudQueryRow.clientSystemAddress.ToString(true));
cloudClient.DeallocateWithDefaultAllocator(&cloudQueryRow);
}
break;
}
}
// Any additional client processing can go here
RakSleep(30);
}
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
return 0;
}
示例3: main
int main(int argc, char **argv)
{
if (argc<8)
{
printf("Arguments: serverIP, pathToGame, gameName, patchImmediately, localPort, serverPort, fullScan");
return 0;
}
RakNet::SystemAddress TCPServerAddress=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
RakNet::AutopatcherClient autopatcherClient;
RakNet::FileListTransfer fileListTransfer;
RakNet::CloudClient cloudClient;
autopatcherClient.SetFileListTransferPlugin(&fileListTransfer);
bool didRebalance=false; // So we only reconnect to a lower load server once, for load balancing
bool fullScan = argv[7][0]=='1';
unsigned short localPort;
localPort=atoi(argv[5]);
unsigned short serverPort=atoi(argv[6]);
RakNet::PacketizedTCP packetizedTCP;
if (packetizedTCP.Start(localPort,1)==false)
{
printf("Failed to start TCP. Is the port already in use?");
return 1;
}
packetizedTCP.AttachPlugin(&autopatcherClient);
packetizedTCP.AttachPlugin(&fileListTransfer);
RakNet::RakPeerInterface *rakPeer;
rakPeer = RakNet::RakPeerInterface::GetInstance();
RakNet::SocketDescriptor socketDescriptor(localPort,0);
rakPeer->Startup(1,&socketDescriptor, 1);
rakPeer->AttachPlugin(&cloudClient);
DataStructures::List<RakNet::RakNetSocket2* > sockets;
rakPeer->GetSockets(sockets);
printf("Started on port %i\n", sockets[0]->GetBoundAddress().GetPort());
char buff[512];
strcpy(buff, argv[1]);
rakPeer->Connect(buff, serverPort, 0, 0);
printf("Connecting...\n");
char appDir[512];
strcpy(appDir, argv[2]);
char appName[512];
strcpy(appName, argv[3]);
bool patchImmediately=argc>=5 && argv[4][0]=='1';
RakNet::Packet *p;
while (1)
{
RakNet::SystemAddress notificationAddress;
notificationAddress=packetizedTCP.HasCompletedConnectionAttempt();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
{
printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
TCPServerAddress=notificationAddress;
}
notificationAddress=packetizedTCP.HasNewIncomingConnection();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
printf("ID_NEW_INCOMING_CONNECTION\n");
notificationAddress=packetizedTCP.HasLostConnection();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
printf("ID_CONNECTION_LOST\n");
notificationAddress=packetizedTCP.HasFailedConnectionAttempt();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
{
printf("ID_CONNECTION_ATTEMPT_FAILED TCP\n");
autopatcherClient.SetFileListTransferPlugin(0);
autopatcherClient.Clear();
packetizedTCP.Stop();
rakPeer->Shutdown(500,0);
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
return 0;
}
p=packetizedTCP.Receive();
while (p)
{
if (p->data[0]==ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR)
{
char buff[256];
RakNet::BitStream temp(p->data, p->length, false);
temp.IgnoreBits(8);
RakNet::StringCompressor::Instance()->DecodeString(buff, 256, &temp);
printf("ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR\n");
printf("%s\n", buff);
autopatcherClient.SetFileListTransferPlugin(0);
autopatcherClient.Clear();
packetizedTCP.Stop();
rakPeer->Shutdown(500,0);
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
return 0;
//.........这里部分代码省略.........
示例4: startAsServer
bool Connector::startAsServer(unsigned short maxPlayers)
{
server = RakNet::UNASSIGNED_SYSTEM_ADDRESS;
// raknet interface configuration
raknetInterface= RakNet::RakPeerInterface::GetInstance();
raknetInterface->SetTimeoutTime(CONNECTION_LOST_TIMEOUT, RakNet::UNASSIGNED_SYSTEM_ADDRESS);
raknetInterface->SetOccasionalPing(true);
raknetInterface->SetIncomingPassword(RAKNET_PASSWORD, strlen(RAKNET_PASSWORD));
raknetInterface->SetMaximumIncomingConnections(maxPlayers);
// socket descriptor settings
RakNet::SocketDescriptor socketDescriptors[1];
socketDescriptors[0].port = (maxPlayers>1) ? SERVER_PORT : CLIENT_PORT;
socketDescriptors[0].socketFamily=AF_INET; // IPV4
// try 10 ports
bool result = false;
for(int i=0; i<PORT_RANGE; i++)
{
result = raknetInterface->Startup(maxPlayers, socketDescriptors, 1) == RakNet::RAKNET_STARTED; // last arg is socketDescriptor count
if(result == true) {
LOG("Servers started on port %d.\n", socketDescriptors[0].port);
break;
} else {
LOG("Server failed to start on port %d. Trying next...\n", socketDescriptors[0].port);
socketDescriptors[0].port++;
continue;
}
}
if (result == false){
LOG("Server failed to start on EACH PORT!!.\n");
RakNet::RakPeerInterface::DestroyInstance(raknetInterface);
raknetInterface = nullptr;
return false;
}
// logging used socket addresses
DataStructures::List< RakNet::RakNetSocket2* > sockets;
raknetInterface->GetSockets(sockets);
/*LOG("Socket addresses used by Connector:\n==========================\n");
for (unsigned int i=0; i < sockets.Size(); i++)
{
LOG("%i. %s\n", i+1, sockets[i]->GetBoundAddress().ToString(true));
}*/
LOG("\nMy IP addresses:\n");
for (unsigned int i = 0; i < raknetInterface->GetNumberOfAddresses(); i++)
{
RakNet::SystemAddress sa = raknetInterface->GetInternalID(RakNet::UNASSIGNED_SYSTEM_ADDRESS, i);
LOG("%i. %s:%i\n", i+1, sa.ToString(false), sa.GetPort());
}
// server name
if(maxPlayers>1){
raknetInterface->SetOfflinePingResponse("Unnamed Server", strlen("Unnamed Server") + 1); // '\0' is automatically added in "hello"
}
return true;
}