本文整理汇总了C++中raknet::RakPeerInterface::Shutdown方法的典型用法代码示例。如果您正苦于以下问题:C++ RakPeerInterface::Shutdown方法的具体用法?C++ RakPeerInterface::Shutdown怎么用?C++ RakPeerInterface::Shutdown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类raknet::RakPeerInterface
的用法示例。
在下文中一共展示了RakPeerInterface::Shutdown方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PingRakNetServer
unsigned int PingRakNetServer(const char *addr, unsigned short port)
{
RakNet::Packet *packet;
bool done = false;
RakNet::TimeMS time = PING_UNREACHABLE;
RakNet::SocketDescriptor socketDescriptor{0, ""};
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
peer->Startup(1, &socketDescriptor, 1);
if (!peer->Ping(addr, port, false))
return time;
RakNet::TimeMS start = RakNet::GetTimeMS();
while (!done)
{
RakNet::TimeMS now = RakNet::GetTimeMS();
if (now - start >= PING_UNREACHABLE)
break;
packet = peer->Receive();
if (!packet)
continue;
switch (packet->data[0])
{
case ID_DISCONNECTION_NOTIFICATION:
case ID_CONNECTION_LOST:
done = true;
break;
case ID_CONNECTED_PING:
case ID_UNCONNECTED_PONG:
{
RakNet::BitStream bsIn(&packet->data[1], packet->length, false);
bsIn.Read(time);
time = now - time;
done = true;
break;
}
default:
break;
}
peer->DeallocatePacket(packet);
}
peer->Shutdown(0);
RakNet::RakPeerInterface::DestroyInstance(peer);
return time > PING_UNREACHABLE ? PING_UNREACHABLE : time;
}
示例2: main
//.........这里部分代码省略.........
printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
sysAddrZero=p->systemAddress;
}
else if (p->data[0]==ID_DISCONNECTION_NOTIFICATION)
printf("ID_DISCONNECTION_NOTIFICATION\n");
else if (p->data[0]==ID_CONNECTION_LOST)
printf("ID_CONNECTION_LOST\n");
else if (p->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
printf("ID_CONNECTION_ATTEMPT_FAILED\n");
rakPeer->DeallocatePacket(p);
p=rakPeer->Receive();
#endif
}
if (kbhit())
{
ch=getch();
if (ch=='s')
{
printf("Enter application directory\n");
Gets(str, sizeof(str));
if (str[0]==0)
strcpy(str, "C:/Temp");
directoryDeltaTransfer.SetApplicationDirectory(str);
printf("This directory will be prefixed to upload and download subdirectories.\n");
}
else if (ch=='a')
{
printf("Enter uploads subdirectory\n");
Gets(str, sizeof(str));
directoryDeltaTransfer.AddUploadsFromSubdirectory(str);
printf("%i files for upload.\n", directoryDeltaTransfer.GetNumberOfFilesForUpload());
}
else if (ch=='d')
{
char subdir[256];
char outputSubdir[256];
printf("Enter remote subdirectory to download from.\n");
printf("This directory may be any uploaded directory, or a subdir therein.\n");
Gets(subdir,sizeof(subdir));
printf("Enter subdirectory to output to.\n");
Gets(outputSubdir,sizeof(outputSubdir));
unsigned short setId;
setId=directoryDeltaTransfer.DownloadFromSubdirectory(subdir, outputSubdir, true, sysAddrZero, &transferCallback, HIGH_PRIORITY, 0, 0);
if (setId==(unsigned short)-1)
printf("Download failed. Host unreachable.\n");
else
printf("Downloading set %i\n", setId);
}
else if (ch=='c')
{
directoryDeltaTransfer.ClearUploads();
printf("Uploads cleared.\n");
}
else if (ch=='o')
{
char host[256];
printf("Enter host IP: ");
Gets(host,sizeof(host));
if (host[0]==0)
strcpy(host, "127.0.0.1");
unsigned short remotePort;
printf("Enter host port: ");
Gets(str, sizeof(str));
if (str[0]==0)
remotePort=60000;
else
remotePort=atoi(str);
#ifdef USE_TCP
tcp1.Connect(host,remotePort,false);
#else
rakPeer->Connect(host, remotePort, 0, 0);
#endif
printf("Connecting.\n");
}
else if (ch=='q')
{
printf("Bye!\n");
#ifdef USE_TCP
tcp1.Stop();
#else
rakPeer->Shutdown(1000,0);
#endif
break;
}
}
// Keeps the threads responsive
RakSleep(0);
}
#ifdef USE_TCP
#else
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
#endif
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: main
//.........这里部分代码省略.........
serverAddress=p->systemAddress;
}
else if (p->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
printf("ID_CONNECTION_ATTEMPT_FAILED\n");
else if (p->data[0]==ID_NO_FREE_INCOMING_CONNECTIONS)
printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
else 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);
}
else if (p->data[0]==ID_AUTOPATCHER_FINISHED)
{
printf("ID_AUTOPATCHER_FINISHED with server time %f\n", autopatcherClient.GetServerDate());
double srvDate=autopatcherClient.GetServerDate();
FILE *fp = fopen("srvDate", "wb");
fwrite(&srvDate,sizeof(double),1,fp);
fclose(fp);
}
else if (p->data[0]==ID_AUTOPATCHER_RESTART_APPLICATION)
printf("Launch \"AutopatcherClientRestarter.exe autopatcherRestart.txt\"\nQuit this application immediately after to unlock files.\n");
rakPeer->DeallocatePacket(p);
p=rakPeer->Receive();
}
#endif
if (kbhit())
ch=getch();
else
ch=0;
if (ch=='q')
break;
else if (ch=='r')
{
#ifdef USE_TCP
packetizedTCP.Connect(buff,60000,false);
#else
rakPeer->Connect(buff, 60000, 0, 0);
#endif
}
else if (ch=='d')
{
#ifdef USE_TCP
packetizedTCP.CloseConnection(serverAddress);
#else
rakPeer->CloseConnection(serverAddress, true);
#endif
}
else if (ch=='p' || (serverAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS && patchImmediately==true))
{
patchImmediately=false;
char restartFile[512];
strcpy(restartFile, appDir);
strcat(restartFile, "/autopatcherRestart.txt");
double lastUpdateDate;
FILE *fp = fopen("srvDate", "rb");
if (fp)
{
fread(&lastUpdateDate, sizeof(lastUpdateDate), 1, fp);
fclose(fp);
}
else
lastUpdateDate=0;
if (autopatcherClient.PatchApplication(appName, appDir, lastUpdateDate, serverAddress, &transferCallback, restartFile, argv[0]))
{
printf("Patching process starting.\n");
}
else
{
printf("Failed to start patching.\n");
}
}
else if (ch=='c')
{
autopatcherClient.Clear();
printf("Autopatcher cleared.\n");
}
RakSleep(30);
}
// Dereference so the destructor doesn't crash
autopatcherClient.SetFileListTransferPlugin(0);
#ifdef USE_TCP
packetizedTCP.Stop();
#else
rakPeer->Shutdown(500,0);
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
#endif
return 1;
}
示例5: main
int main()
{
RakNet::Packet *packet;
RakNet::RakPeerInterface *rakPeer;
bool isConnected=false;
rakPeer=RakNet::RakPeerInterface::GetInstance();
char command[512];
printf("This sample demonstrates connecting to the command console.\n");
printf("using the RakNet transport protocol\n");
printf("It's the equivalent of a secure telnet client\n");
printf("See the 'CommandConsoleServer' project.\n");
printf("Difficulty: Intermediate\n\n");
printf("RakNet secure command console.\n");
printf("Commands:\n");
printf("/Connect\n");
printf("/Disconnect\n");
printf("/Quit\n");
printf("Any other command goes to the remote console\n");
while (1)
{
if (kbhit())
{
Gets(command,sizeof(command));
if (stricmp(command, "/quit")==0)
{
printf("Goodbye.\n");
rakPeer->Shutdown(500, 0);
return 0;
}
else if (stricmp(command, "/disconnect")==0)
{
if (isConnected)
{
rakPeer->Shutdown(500, 0);
isConnected=false;
printf("Disconnecting.\n");
}
else
{
printf("Not currently connected.\n");
}
}
else if (stricmp(command, "/connect")==0)
{
if (isConnected)
{
printf("Disconnect first.\n");
}
else
{
char ip[128];
char remotePort[64];
char password[512];
char localPort[64];
printf("Enter remote IP: ");
do {
Gets(ip, sizeof(ip));
} while(ip[0]==0);
printf("Enter remote port: ");
do {
Gets(remotePort,sizeof(remotePort));
} while(remotePort[0]==0);
printf("Enter local port (enter for 0): ");
Gets(localPort,sizeof(localPort));
if (localPort[0]==0)
{
strcpy(localPort, "0");
}
printf("Enter console password (enter for none): ");
Gets(password,sizeof(password));
RakNet::SocketDescriptor socketDescriptor((int) atoi(localPort),0);
if (rakPeer->Startup(1, &socketDescriptor, 1)==RakNet::RAKNET_STARTED)
{
int passwordLen;
if (password[0])
passwordLen=(int) strlen(password)+1;
else
passwordLen=0;
if (rakPeer->Connect(ip, (int) atoi(remotePort), password, passwordLen)==RakNet::CONNECTION_ATTEMPT_STARTED)
printf("Connecting...\nNote: if the password is wrong the other system will ignore us.\n");
else
{
printf("Connect call failed.\n");
rakPeer->Shutdown(0, 0);
}
}
else
printf("Initialize call failed.\n");
}
}
else
{
if (isConnected)
{
RakNet::BitStream str;
str.Write((unsigned char) ID_TRANSPORT_STRING);
str.Write(command, (int) strlen(command)+1);
//.........这里部分代码省略.........
示例6: main
//.........这里部分代码省略.........
case ID_CONNECTION_REQUEST_ACCEPTED:
printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
break;
case ID_NEW_INCOMING_CONNECTION:
printf("ID_NEW_INCOMING_CONNECTION from %s\n", packet->systemAddress.ToString());
break;
case ID_DISCONNECTION_NOTIFICATION:
printf("ID_DISCONNECTION_NOTIFICATION\n");
break;
case ID_CONNECTION_LOST:
printf("ID_CONNECTION_LOST\n");
break;
case ID_ADVERTISE_SYSTEM:
// The first conditional is needed because ID_ADVERTISE_SYSTEM may be from a system we are connected to, but replying on a different address.
// The second conditional is because AdvertiseSystem also sends to the loopback
if (rakPeer->GetSystemAddressFromGuid(packet->guid)==RakNet::UNASSIGNED_SYSTEM_ADDRESS &&
rakPeer->GetMyGUID()!=packet->guid)
{
printf("Connecting to %s\n", packet->systemAddress.ToString(true));
rakPeer->Connect(packet->systemAddress.ToString(false), packet->systemAddress.GetPort(),0,0);
}
break;
case ID_SND_RECEIPT_LOSS:
case ID_SND_RECEIPT_ACKED:
{
uint32_t msgNumber;
memcpy(&msgNumber, packet->data+1, 4);
DataStructures::List<Replica3*> replicaListOut;
replicaManager.GetReplicasCreatedByMe(replicaListOut);
unsigned int idx;
for (idx=0; idx < replicaListOut.Size(); idx++)
{
((SampleReplica*)replicaListOut[idx])->NotifyReplicaOfMessageDeliveryStatus(packet->guid,msgNumber, packet->data[0]==ID_SND_RECEIPT_ACKED);
}
}
break;
}
}
if (kbhit())
{
ch=getch();
if (ch=='q' || ch=='Q')
{
printf("Quitting.\n");
quit=true;
}
if (ch=='c' || ch=='C')
{
printf("Objects created.\n");
if (topology==SERVER||topology==CLIENT)
{
replicaManager.Reference(new ClientCreatible_ClientSerialized);
replicaManager.Reference(new ServerCreated_ClientSerialized);
replicaManager.Reference(new ClientCreatible_ServerSerialized);
replicaManager.Reference(new ServerCreated_ServerSerialized);
}
else
{
// for (int i=0; i < 20; i++)
replicaManager.Reference(new P2PReplica);
}
}
if (ch=='r' || ch=='R')
{
DataStructures::List<Replica3*> replicaListOut;
replicaManager.GetReplicasCreatedByMe(replicaListOut);
unsigned int idx;
for (idx=0; idx < replicaListOut.Size(); idx++)
{
((SampleReplica*)replicaListOut[idx])->RandomizeVariables();
}
}
if (ch=='d' || ch=='D')
{
printf("My objects destroyed.\n");
DataStructures::List<Replica3*> replicaListOut;
// The reason for ClearPointers is that in the sample, I don't track which objects have and have not been allocated at the application level. So ClearPointers will call delete on every object in the returned list, which is every object that the application has created. Another way to put it is
// A. Send a packet to tell other systems to delete these objects
// B. Delete these objects on my own system
replicaManager.GetReplicasCreatedByMe(replicaListOut);
replicaManager.BroadcastDestructionList(replicaListOut, RakNet::UNASSIGNED_SYSTEM_ADDRESS);
for (unsigned int i=0; i < replicaListOut.Size(); i++)
RakNet::OP_DELETE(replicaListOut[i], _FILE_AND_LINE_);
}
}
RakSleep(30);
for (int i=0; i < 4; i++)
{
if (rakPeer->GetInternalID(RakNet::UNASSIGNED_SYSTEM_ADDRESS,0).GetPort()!=SERVER_PORT+i)
rakPeer->AdvertiseSystem("255.255.255.255", SERVER_PORT+i, 0,0,0);
}
}
rakPeer->Shutdown(100,0);
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
}
示例7: getExtendedData
ServerExtendedData getExtendedData(const char *addr, unsigned short port)
{
ServerExtendedData data;
RakNet::SocketDescriptor socketDescriptor = {0, ""};
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
peer->Startup(1, &socketDescriptor, 1);
stringstream sstr;
sstr << TES3MP_VERSION;
sstr << TES3MP_PROTO_VERSION;
std::string msg;
if (peer->Connect(addr, port, sstr.str().c_str(), (int)(sstr.str().size()), nullptr, 0, 3, 500, 0) != RakNet::CONNECTION_ATTEMPT_STARTED)
msg = "Connection attempt failed.\n";
int queue = 0;
while (queue == 0)
{
for (RakNet::Packet *packet = peer->Receive(); packet; peer->DeallocatePacket(
packet), packet = peer->Receive())
{
switch (packet->data[0])
{
case ID_CONNECTION_ATTEMPT_FAILED:
{
msg = "Connection failed.\n"
"Either the IP address is wrong or a firewall on either system is blocking\n"
"UDP packets on the port you have chosen.";
queue = -1;
break;
}
case ID_INVALID_PASSWORD:
{
msg = "Connection failed.\n"
"The client or server is outdated.\n";
queue = -1;
break;
}
case ID_CONNECTION_REQUEST_ACCEPTED:
{
msg = "Connection accepted.\n";
queue = 1;
break;
}
case ID_DISCONNECTION_NOTIFICATION:
throw runtime_error("ID_DISCONNECTION_NOTIFICATION.\n");
case ID_CONNECTION_BANNED:
throw runtime_error("ID_CONNECTION_BANNED.\n");
case ID_CONNECTION_LOST:
throw runtime_error("ID_CONNECTION_LOST.\n");
default:
printf("Connection message with identifier %i has arrived in initialization.\n", packet->data[0]);
}
}
}
puts(msg.c_str());
if (queue == -1) // connection is failed
return data;
{
RakNet::BitStream bs;
bs.Write((unsigned char) (ID_USER_PACKET_ENUM + 1));
peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
RakNet::Packet *packet;
bool done = false;
while (!done)
{
for (packet = peer->Receive(); packet; peer->DeallocatePacket(packet), packet = peer->Receive())
{
if (packet->data[0] == (ID_USER_PACKET_ENUM + 1))
{
RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(1);
size_t length = 0;
bs.Read(length);
for (size_t i = 0; i < length; i++)
{
RakNet::RakString str;
bs.Read(str);
data.players.emplace_back(str.C_String());
}
bs.Read(length);
for (size_t i = 0; i < length; i++)
{
RakNet::RakString str;
bs.Read(str);
data.plugins.emplace_back(str.C_String());
}
done = true;
}
}
}
peer->Shutdown(0);
RakSleep(10);
//.........这里部分代码省略.........
示例8: main
//.........这里部分代码省略.........
printf("Enter IP to ban. You can use * as a wildcard\n");
Gets(message, sizeof(message));
server->AddToBanList(message);
printf("IP %s added to ban list.\n", message);
continue;
}
// Message now holds what we want to broadcast
char message2[2048];
// Append Server: to the message so clients know that it ORIGINATED from the server
// All messages to all clients come from the server either directly or by being
// relayed from other clients
message2[0] = 0;
const static char prefix[] = "Server: ";
strncpy(message2, prefix, sizeof(message2));
strncat(message2, message, sizeof(message2) - strlen(prefix) - 1);
// message2 is the data to send
// strlen(message2)+1 is to send the null terminator
// HIGH_PRIORITY doesn't actually matter here because we don't use any other priority
// RELIABLE_ORDERED means make sure the message arrives in the right order
// We arbitrarily pick 0 for the ordering stream
// RakNet::UNASSIGNED_SYSTEM_ADDRESS means don't exclude anyone from the broadcast
// true means broadcast the message to everyone connected
server->Send(message2, (const int)strlen(message2) + 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
// Get a packet from either the server or the client
for (p = server->Receive(); p; server->DeallocatePacket(p), p = server->Receive())
{
// We got a packet, get the identifier with our handy function
packetIdentifier = GetPacketIdentifier(p);
// Check if this is a network message packet
switch (packetIdentifier)
{
case ID_DISCONNECTION_NOTIFICATION:
// Connection lost normally
printf("ID_DISCONNECTION_NOTIFICATION from %s\n", p->systemAddress.ToString(true));;
break;
case ID_NEW_INCOMING_CONNECTION:
// Somebody connected. We have their IP now
printf("ID_NEW_INCOMING_CONNECTION from %s with GUID %s\n", p->systemAddress.ToString(true), p->guid.ToString());
clientID = p->systemAddress; // Record the player ID of the client
printf("Remote internal IDs:\n");
for (int index = 0; index < MAXIMUM_NUMBER_OF_INTERNAL_IDS; index++)
{
RakNet::SystemAddress internalId = server->GetInternalID(p->systemAddress, index);
if (internalId != RakNet::UNASSIGNED_SYSTEM_ADDRESS)
{
printf("%i. %s\n", index + 1, internalId.ToString(true));
}
}
break;
case ID_INCOMPATIBLE_PROTOCOL_VERSION:
printf("ID_INCOMPATIBLE_PROTOCOL_VERSION\n");
break;
case ID_CONNECTED_PING:
case ID_UNCONNECTED_PING:
printf("Ping from %s\n", p->systemAddress.ToString(true));
break;
case ID_CONNECTION_LOST:
// Couldn't deliver a reliable packet - i.e. the other system was abnormally
// terminated
printf("ID_CONNECTION_LOST from %s\n", p->systemAddress.ToString(true));;
break;
default:
// The server knows the static data of all clients, so we can prefix the message
// With the name data
printf("%s\n", p->data);
// Relay the message. We prefix the name for other clients. This demonstrates
// That messages can be changed on the server before being broadcast
// Sending is the same as before
sprintf(message, "%s", p->data);
server->Send(message, (const int)strlen(message) + 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, p->systemAddress, true);
break;
}
}
}
server->Shutdown(300);
// We're done with the network
RakNet::RakPeerInterface::DestroyInstance(server);
}
示例9: StartClient
//.........这里部分代码省略.........
RPC3_REGISTER_FUNCTION(rpc3Inst, UpdateCharacter);
RPC3_REGISTER_FUNCTION(rpc3Inst, UpdateCharacterVelocity);
RPC3_REGISTER_FUNCTION(rpc3Inst, SyncClients);
RPC3_REGISTER_FUNCTION(rpc3Inst, ReceiveID);
RPC3_REGISTER_FUNCTION(rpc3Inst, RemoveCharacter);
//Create the basic connection with help of RakNet
SocketDescriptor socketDescriptor(0,0);
socketDescriptor.socketFamily=AF_INET;
pClient->Startup(1,&socketDescriptor, 1);
pClient->Ping( "255.255.255.255", 50000, true, 0 );
// Holds packets
Packet* p;
//Make sure the RPC3 is attached as a plugin, else it won't work
pClient->AttachPlugin(rpc3Inst);
char message[2048];
// Loop for input
while (Session::threading)
{
// This sleep keeps RakNet responsive
RakSleep(30);
#ifdef _WIN32
Sleep(30);
#else
usleep(30 * 1000);
#endif
// Get a packet from either the server or the client
for (p=pClient->Receive(); p; pClient->DeallocatePacket(p), p=pClient->Receive())
{
switch (p->data[0])
{
case ID_DISCONNECTION_NOTIFICATION:
printf("ID_DISCONNECTION_NOTIFICATION\n");
break;
case ID_ALREADY_CONNECTED:
printf("ID_ALREADY_CONNECTED\n");
break;
case ID_CONNECTION_ATTEMPT_FAILED:
printf("Connection attempt failed\n");
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
break;
case ID_UNCONNECTED_PONG:
// Found the server
pClient->Connect(p->systemAddress.ToString(false),p->systemAddress.GetPort(),0,0,0);
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
// This tells the client they have connected
printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
Game::getSingletonPtr()->CreateCharacter("PlayerHost");
//Game::getSingletonPtr()->SetPlayer(
break;
case ID_NEW_INCOMING_CONNECTION:
{
printf("New incoming connection");
break;
}
case ID_RPC_REMOTE_ERROR:
{
// Recipient system returned an error
switch (p->data[1])
{
case RakNet::RPC_ERROR_NETWORK_ID_MANAGER_UNAVAILABLE:
printf("RPC_ERROR_NETWORK_ID_MANAGER_UNAVAILABLE\n");
break;
case RakNet::RPC_ERROR_OBJECT_DOES_NOT_EXIST:
printf("RPC_ERROR_OBJECT_DOES_NOT_EXIST\n");
break;
case RakNet::RPC_ERROR_FUNCTION_INDEX_OUT_OF_RANGE:
printf("RPC_ERROR_FUNCTION_INDEX_OUT_OF_RANGE\n");
break;
case RakNet::RPC_ERROR_FUNCTION_NOT_REGISTERED:
printf("RPC_ERROR_FUNCTION_NOT_REGISTERED\n");
break;
case RakNet::RPC_ERROR_FUNCTION_NO_LONGER_REGISTERED:
printf("RPC_ERROR_FUNCTION_NO_LONGER_REGISTERED\n");
break;
case RakNet::RPC_ERROR_CALLING_CPP_AS_C:
printf("RPC_ERROR_CALLING_CPP_AS_C\n");
break;
case RakNet::RPC_ERROR_CALLING_C_AS_CPP:
printf("RPC_ERROR_CALLING_C_AS_CPP\n");
break;
}
printf("Function: %s", p->data+2);
}
}
}
}
//When we are done as a client, close everything and delete RPC3
Session::threading = false;
pClient->Shutdown(20,0);
RakNet::RakPeerInterface::DestroyInstance(pClient);
delete rpc3Inst;
}
示例10: run
//.........这里部分代码省略.........
std::cout << "Capture open fail" << std::endl;
return;
}
capture.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
capture.set(CV_CAP_PROP_FOURCC, CV_FOURCC('X', '2', '6', '4'));
cv::namedWindow("RemoteVideo", 1);
while (1) {
const time_t t = time(NULL);
struct tm* current_time = localtime(&t);
std::cout << "current time is " << current_time->tm_sec << std::endl;
cv::Mat frame;
capture.read(frame);
cv::imshow("MyVideo", frame);
packet = rakPeer->Receive();
if (packet) {
RakNet::BitStream bitStream(packet->data, packet->length, false);
bitStream.Read(typeId);
switch (typeId) {
case ID_CONNECTION_REQUEST_ACCEPTED:
{
std::cout << "ID_CONNECTION_REQUEST_ACCEPTED" << ip << std::endl;
address = packet->systemAddress;
connected = true;
break;
}
case ID_NEW_INCOMING_CONNECTION:
{
std::cout << "ID_NEW_INCOMING_CONNECTION" << ip << std::endl;
address = packet->systemAddress;
connected = true;
break;
}
case ID_DISCONNECTION_NOTIFICATION:
{
connected = false;
break;
}
case ID_CONNECTION_LOST:
{
connected = false;
break;
}
case ID_USER_PACKET_ENUM:
{
int cols, rows, type, channels, size;
bitStream.Read(cols);
bitStream.Read(rows);
bitStream.Read(type);
bitStream.Read(channels);
bitStream.Read(size);
char* data = new char[size];
bitStream.Read(data, size);
cv::Mat mat(cols, rows, type, (uchar*)data);
cv::imshow("RemoteVideo", mat.reshape(channels, rows));
delete data;
break;
}
default:
break;
}
rakPeer->DeallocatePacket(packet);
}
if (connected) {
int size = frame.total()*frame.elemSize();
RakNet::BitStream sendStream;
sendStream.Write((RakNet::MessageID)ID_USER_PACKET_ENUM);
sendStream.Write(frame.cols);
sendStream.Write(frame.rows);
sendStream.Write((int)frame.type());
sendStream.Write(frame.channels());
sendStream.Write(size);
sendStream.Write((const char *)frame.data, size);
rakPeer->Send(&sendStream, LOW_PRIORITY, UNRELIABLE_SEQUENCED, 0, address, false);
//rakPeer->Send(&sendStream, IMMEDIATE_PRIORITY, UNRELIABLE_SEQUENCED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
key = cvWaitKey(10);
if (char(key) == 27) {
break;
}
}
rakPeer->Shutdown(300);
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
cv::destroyAllWindows();
}