本文整理汇总了C++中raknet::RakPeerInterface::SetMaximumIncomingConnections方法的典型用法代码示例。如果您正苦于以下问题:C++ RakPeerInterface::SetMaximumIncomingConnections方法的具体用法?C++ RakPeerInterface::SetMaximumIncomingConnections怎么用?C++ RakPeerInterface::SetMaximumIncomingConnections使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类raknet::RakPeerInterface
的用法示例。
在下文中一共展示了RakPeerInterface::SetMaximumIncomingConnections方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(void)
{
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
bool isServer;
RakNet::Packet *packet;
RakNet::SocketDescriptor sd( SERVER_PORT,0 );
peer->Startup( MAX_CLIENTS, &sd, 1 );
isServer = true;
printf( "Starting the server.\n" );
// We need to let the server accept incoming connections from the clients
peer->SetMaximumIncomingConnections( MAX_CLIENTS );
while (1)
{
for( packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive() )
{
handlePacket( packet, peer );
}
}
RakNet::RakPeerInterface::DestroyInstance( peer );
return 0;
}
示例2: main
int main(void)
{
RakNet::RakPeerInterface *rakPeer = RakNet::RakPeerInterface::GetInstance();
RakNet::SocketDescriptor sd(60000,0);
rakPeer->Startup(128,&sd,1);
rakPeer->SetMaximumIncomingConnections(128);
RakNet::TelnetTransport tt;
TestCommandServer(&tt, 23, rakPeer); // Uncomment to use Telnet as a client. Telnet uses port 23 by default.
// RakNet::RakNetTransport2 rt2;
// rakPeer->AttachPlugin(&rt2);
// TestCommandServer(&rt2, 60000,rakPeer); // Uncomment to use RakNet as a client
return 1;
}
示例3: main
int main(void)
{
char ch;
#ifdef USE_TCP
RakNet::PacketizedTCP tcp1;
#else
RakNet::RakPeerInterface *rakPeer;
#endif
// directoryDeltaTransfer is the main plugin that does the work for this sample.
RakNet::DirectoryDeltaTransfer directoryDeltaTransfer;
// The fileListTransfer plugin is used by the DirectoryDeltaTransfer plugin and must also be registered (you could use this yourself too if you wanted, of course).
RakNet::FileListTransfer fileListTransfer;
// Read files in parts, rather than the whole file from disk at once
RakNet::IncrementalReadInterface iri;
directoryDeltaTransfer.SetDownloadRequestIncrementalReadInterface(&iri, 1000000);
#ifdef USE_TCP
tcp1.AttachPlugin(&directoryDeltaTransfer);
tcp1.AttachPlugin(&fileListTransfer);
#else
rakPeer = RakNet::RakPeerInterface::GetInstance();
rakPeer->AttachPlugin(&directoryDeltaTransfer);
rakPeer->AttachPlugin(&fileListTransfer);
// Get download progress notifications. Handled by the plugin.
rakPeer->SetSplitMessageProgressInterval(100);
#endif
directoryDeltaTransfer.SetFileListTransferPlugin(&fileListTransfer);
printf("This sample demonstrates the plugin to incrementally transfer compressed\n");
printf("deltas of directories. In essence, it's a simple autopatcher.\n");
printf("Unlike the full autopatcher, it has no dependencies. It is suitable for\n");
printf("patching from non-dedicated servers at runtime.\n");
printf("Difficulty: Intermediate\n\n");
printf("Enter listen port. Enter for default. If running two instances on the\nsame computer, use 0 for the client.\n");
unsigned short localPort;
char str[256];
Gets(str, sizeof(str));
if (str[0]==0)
localPort=60000;
else
localPort=atoi(str);
RakNet::SocketDescriptor socketDescriptor(localPort,0);
#ifdef USE_TCP
bool b=tcp1.Start(localPort,8);
RakAssert(b);
#else
if (rakPeer->Startup(8,&socketDescriptor, 1)!=RakNet::RAKNET_STARTED)
{
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
printf("RakNet initialize failed. Possibly duplicate port.\n");
return 1;
}
rakPeer->SetMaximumIncomingConnections(8);
#endif
printf("Commands:\n");
printf("(S)et application directory.\n");
printf("(A)dd allowed uploads from subdirectory.\n");
printf("(D)ownload from subdirectory.\n");
printf("(C)lear allowed uploads.\n");
printf("C(o)nnect to another system.\n");
printf("(Q)uit.\n");
RakNet::SystemAddress sysAddrZero=RakNet::UNASSIGNED_SYSTEM_ADDRESS;
RakNet::TimeMS nextStatTime = RakNet::GetTimeMS() + 1000;
RakNet::Packet *p;
while (1)
{
/*
if (//directoryDeltaTransfer.GetNumberOfFilesForUpload()>0 &&
RakNet::GetTimeMS() > nextStatTime)
{
// If sending, periodically show connection stats
char statData[2048];
RakNetStatistics *statistics = rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0));
// if (statistics->messagesOnResendQueue>0 || statistics->internalOutputQueueSize>0)
if (rakPeer->GetSystemAddressFromIndex(0)!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
{
StatisticsToString(statistics, statData, 2);
printf("%s\n", statData);
}
nextStatTime=RakNet::GetTimeMS()+5000;
}
*/
// Process packets
#ifdef USE_TCP
p=tcp1.Receive();
#else
p=rakPeer->Receive();
#endif
#ifdef USE_TCP
RakNet::SystemAddress sa;
sa=tcp1.HasNewIncomingConnection();
//.........这里部分代码省略.........
示例4: main
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, sizeof(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, sizeof(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");
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;
default:
printf("Message with identifier %i has arrived.\n", packet->data[0]);
break;
}
}
}
RakNet::RakPeerInterface::DestroyInstance(peer);
return 0;
}
示例5: main
int main(int argc, char **argv)
{
const char *DEFAULT_SERVER_ADDRESS="test.dnsalias.net";
const unsigned short DEFAULT_SERVER_PORT=60000;
const char *serverAddress;
unsigned short serverPort;
#ifndef _DEBUG
// Only use DEFAULT_SERVER_ADDRESS for debugging
if (argc<2)
{
PrintHelp();
return false;
}
#endif
if (argc<2) serverAddress=DEFAULT_SERVER_ADDRESS;
else serverAddress=argv[1];
if (argc<3) serverPort=DEFAULT_SERVER_PORT;
else serverPort=atoi(argv[2]);
// ---- RAKPEER -----
RakNet::RakPeerInterface *rakPeer;
rakPeer=RakNet::RakPeerInterface::GetInstance();
static const unsigned short clientLocalPort=0;
RakNet::SocketDescriptor sd(clientLocalPort,0); // Change this if you want
RakNet::StartupResult sr = rakPeer->Startup(1,&sd,1); // Change this if you want
rakPeer->SetMaximumIncomingConnections(0); // Change this if you want
if (sr!=RakNet::RAKNET_STARTED)
{
printf("Startup failed. Reason=%i\n", (int) sr);
return 1;
}
RakNet::CloudClient cloudClient;
rakPeer->AttachPlugin(&cloudClient);
RakNet::ConnectionAttemptResult car = rakPeer->Connect(serverAddress, serverPort, 0, 0);
if (car==RakNet::CANNOT_RESOLVE_DOMAIN_NAME)
{
printf("Cannot resolve domain name\n");
return 1;
}
printf("Connecting to %s...\n", serverAddress);
bool didRebalance=false; // So we only reconnect to a lower load server once, for load balancing
RakNet::Packet *packet;
while (1)
{
for (packet=rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet=rakPeer->Receive())
{
switch (packet->data[0])
{
case ID_CONNECTION_LOST:
printf("Lost connection to server.\n");
return 1;
case ID_CONNECTION_ATTEMPT_FAILED:
printf("Failed to connect to server at %s.\n", packet->systemAddress.ToString(true));
return 1;
case ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY:
case ID_OUR_SYSTEM_REQUIRES_SECURITY:
case ID_PUBLIC_KEY_MISMATCH:
case ID_INVALID_PASSWORD:
case ID_CONNECTION_BANNED:
// You won't see these unless you modified CloudServer
printf("Server rejected the connection.\n");
return 1;
case ID_INCOMPATIBLE_PROTOCOL_VERSION:
printf("Server is running an incompatible RakNet version.\n");
return 1;
case ID_NO_FREE_INCOMING_CONNECTIONS:
printf("Server has no free connections\n");
return 1;
case ID_IP_RECENTLY_CONNECTED:
printf("Recently connected. Retrying.");
rakPeer->Connect(serverAddress, serverPort, 0, 0);
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
printf("Connected to server.\n");
UploadInstanceToCloud(&cloudClient, packet->guid);
GetClientSubscription(&cloudClient, packet->guid);
GetServers(&cloudClient, packet->guid);
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;
//.........这里部分代码省略.........
示例6: main
int main()
{
char str[512];
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
bool isServer;
RakNet::Packet *packet;
printf("(C)lient 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");
peer->SetMaximumIncomingConnections(MAX_CLIENTS);
}
else
{
printf("Enter server IP or hit enter for default: 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 dosconnected\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");
{
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_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;
//.........这里部分代码省略.........
示例7: main
int main(int argc, char **argv)
{
// Avoids the Error: Got a packet bigger than 'max_allowed_packet' bytes
printf("Important: Requires that you first set the DB schema and the max packet size on the server.\n");
printf("See DependentExtensions/AutopatcherMySQLRepository/readme.txt\n");
// // MySQL is extremely slow in AutopatcherMySQLRepository::GetFilePart
printf("WARNING: MySQL is an order of magnitude slower than PostgreSQL.\nRecommended you use AutopatcherServer_PostgreSQL instead.");
printf("Server starting... ");
RakNet::AutopatcherServer autopatcherServer;
// RakNet::FLP_Printf progressIndicator;
RakNet::FileListTransfer fileListTransfer;
// So only one thread runs per connection, we create an array of connection objects, and tell the autopatcher server to use one thread per item
static const int workerThreadCount=4; // Used for checking patches only
static const int sqlConnectionObjectCount=32; // Used for both checking patches and downloading
RakNet::AutopatcherMySQLRepository connectionObject[sqlConnectionObjectCount];
RakNet::AutopatcherRepositoryInterface *connectionObjectAddresses[sqlConnectionObjectCount];
for (int i=0; i < sqlConnectionObjectCount; i++)
connectionObjectAddresses[i]=&connectionObject[i];
// fileListTransfer.AddCallback(&progressIndicator);
autopatcherServer.SetFileListTransferPlugin(&fileListTransfer);
// MySQL is extremely slow in AutopatcherMySQLRepository::GetFilePart so running tho incremental reads in a thread allows multiple concurrent users to read
// Without this, only one user would be sent files at a time basically
fileListTransfer.StartIncrementalReadThreads(sqlConnectionObjectCount);
autopatcherServer.SetMaxConurrentUsers(MAX_INCOMING_CONNECTIONS); // More users than this get queued up
RakNet::AutopatcherServerLoadNotifier_Printf loadNotifier;
autopatcherServer.SetLoadManagementCallback(&loadNotifier);
#ifdef USE_TCP
RakNet::PacketizedTCP packetizedTCP;
if (packetizedTCP.Start(LISTEN_PORT,MAX_INCOMING_CONNECTIONS)==false)
{
printf("Failed to start TCP. Is the port already in use?");
return 1;
}
packetizedTCP.AttachPlugin(&autopatcherServer);
packetizedTCP.AttachPlugin(&fileListTransfer);
#else
RakNet::RakPeerInterface *rakPeer;
rakPeer = RakNet::RakPeerInterface::GetInstance();
RakNet::SocketDescriptor socketDescriptor(LISTEN_PORT,0);
rakPeer->Startup(MAX_INCOMING_CONNECTIONS,&socketDescriptor, 1);
rakPeer->SetMaximumIncomingConnections(MAX_INCOMING_CONNECTIONS);
rakPeer->AttachPlugin(&autopatcherServer);
rakPeer->AttachPlugin(&fileListTransfer);
#endif
printf("started.\n");
printf("Enter database password:\n");
char password[128];
char username[256];
strcpy(username, "root");
Gets(password,sizeof(password));
if (password[0]==0)
strcpy(password,"aaaa");
char db[256];
printf("Enter DB schema: ");
// To create the schema, go to the command line client and type create schema autopatcher;
// You also have to add
// max_allowed_packet=128M
// Where 128 is the maximum size file in megabytes you'll ever add
// to MySQL\MySQL Server 5.1\my.ini in the [mysqld] section
// Be sure to restart the service after doing so
Gets(db,sizeof(db));
if (db[0]==0)
strcpy(db,"autopatcher");
for (int conIdx=0; conIdx < sqlConnectionObjectCount; conIdx++)
{
if (!connectionObject[conIdx].Connect("localhost", username, password, db, 0, NULL, 0))
{
printf("Database connection failed.\n");
return 1;
}
}
printf("Database connection suceeded.\n");
printf("Starting threads\n");
autopatcherServer.StartThreads(workerThreadCount, sqlConnectionObjectCount, connectionObjectAddresses);
printf("System ready for connections\n");
printf("(D)rop database\n(C)reate database.\n(A)dd application\n(U)pdate revision.\n(R)emove application\n(Q)uit\n");
char ch;
RakNet::Packet *p;
while (1)
{
#ifdef USE_TCP
RakNet::SystemAddress notificationAddress;
notificationAddress=packetizedTCP.HasCompletedConnectionAttempt();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
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");
p=packetizedTCP.Receive();
while (p)
//.........这里部分代码省略.........
示例8: _tmain
int _tmain(int argc, _TCHAR* argv[])
{
// Create networking interfaces
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
RakNet::Packet *packet;
// Initialize a socket descriptor
RakNet::SocketDescriptor sd( 5187, 0 );
// Start a server with max. 5 clients
peer->Startup( 5, &sd, 1 );
printf( "Starting the server...\n" );
// Set the maximum number of clients
peer->SetMaximumIncomingConnections( 5 );
// Listen
printf( "Listening...\n" );
while( 1 )
{
for( packet = peer->Receive(); packet; peer->DeallocatePacket( packet ), packet = peer->Receive() )
{
switch( packet->data[0] )
{
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:
printf("A client has disconnected.\n");
break;
case ID_CONNECTION_LOST:
printf("A client lost the connection.\n");
break;
case ID_SC4_CONNECTION_DATA:
{
printf( "An SC4Multi connection was made:\n" );
// Create several RakNet strings for reading the bistream
RakNet::RakString username, guid, city;
// Read the packet into the bitstream
RakNet::BitStream bsIn(packet->data,packet->length,false);
// Ignore the packet ID, which we already know
bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
// Read the bitstream into our variables
bsIn.Read( username );
bsIn.Read( guid );
bsIn.Read( city );
// Print the data
printf
(
"\tUsername | %s\n"
"\tGUID | %s\n"
"\tCity | %s\n",
username.C_String(),
guid.C_String(),
city.C_String()
);
break;
}
default:
printf("Message with identifier %i has arrived.\n", packet->data[0]);
break;
}
}
}
// Shut down
RakNet::RakPeerInterface::DestroyInstance( peer );
return 0;
}
示例9: main
int main(void)
{
char ch;
RakNet::SocketDescriptor sd;
sd.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
char ip[128];
static const int SERVER_PORT=12345;
// ReplicaManager3 requires NetworkIDManager to lookup pointers from numbers.
NetworkIDManager networkIdManager;
// Each application has one instance of RakPeerInterface
RakNet::RakPeerInterface *rakPeer;
// The system that performs most of our functionality for this demo
ReplicaManager3Sample replicaManager;
printf("Demonstration of ReplicaManager3.\n");
printf("1. Demonstrates creating objects created by the server and client.\n");
printf("2. Demonstrates automatic serialization data members\n");
printf("Difficulty: Intermediate\n\n");
printf("Start as (c)lient, (s)erver, (p)eer? ");
ch=getche();
rakPeer = RakNet::RakPeerInterface::GetInstance();
if (ch=='c' || ch=='C')
{
topology=CLIENT;
sd.port=0;
}
else if (ch=='s' || ch=='S')
{
topology=SERVER;
sd.port=SERVER_PORT;
}
else
{
topology=P2P;
sd.port=SERVER_PORT;
while (IRNS2_Berkley::IsPortInUse(sd.port,sd.hostAddress,sd.socketFamily, SOCK_DGRAM)==true)
sd.port++;
}
// Start RakNet, up to 32 connections if the server
rakPeer->Startup(32,&sd,1);
rakPeer->AttachPlugin(&replicaManager);
replicaManager.SetNetworkIDManager(&networkIdManager);
rakPeer->SetMaximumIncomingConnections(32);
printf("\nMy GUID is %s\n", rakPeer->GetMyGUID().ToString());
printf("\n");
if (topology==CLIENT)
{
printf("Enter server IP: ");
Gets(ip, sizeof(ip));
if (ip[0]==0)
strcpy(ip, "127.0.0.1");
rakPeer->Connect(ip,SERVER_PORT,0,0,0);
printf("Connecting...\n");
}
printf("Commands:\n(Q)uit\n'C'reate objects\n'R'andomly change variables in my objects\n'D'estroy my objects\n");
// Enter infinite loop to run the system
RakNet::Packet *packet;
bool quit=false;
while (!quit)
{
for (packet = rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet = rakPeer->Receive())
{
switch (packet->data[0])
{
case ID_CONNECTION_ATTEMPT_FAILED:
printf("ID_CONNECTION_ATTEMPT_FAILED\n");
quit=true;
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
quit=true;
break;
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);
//.........这里部分代码省略.........
示例10: main
int main(int argc, char** argv) {
RakNet::RakPeerInterface *server = RakNet::RakPeerInterface::GetInstance();
RakNet::Packet* p;
unsigned char packetIdentifier;
RakNet::SystemAddress clientID = RakNet::UNASSIGNED_SYSTEM_ADDRESS;
RakNet::RakNetStatistics *rss;
RakNet::SocketDescriptor socketDescriptors[2];
char portstring[30];
socketDescriptors[0].port = SERVER_PORT;
socketDescriptors[0].socketFamily = AF_INET; // Test out IPV4
socketDescriptors[1].port = SERVER_PORT;
socketDescriptors[1].socketFamily = AF_INET6; // Test out IPV6
if (server->Startup(4, socketDescriptors, 2) != RakNet::RAKNET_STARTED) {
std::cout << "IPV6 not supported, starting with IPV4-only interface..." << std::endl;
if (server->Startup(4, socketDescriptors, 1) != RakNet::RAKNET_STARTED) {
std::cerr << "Error creating server, exiting..." << std::endl;
exit(-1);
}
}
std::cout << "Server started successfully. Listening on port " << SERVER_PORT << std::endl;
server->SetMaximumIncomingConnections(20);
server->SetOccasionalPing(true);
server->SetUnreliableTimeout(1000);
DataStructures::List<RakNet::RakNetSocket2*> sockets;
server->GetSockets(sockets);
std::cout << "Socket addresses used by RakNet:" << std::endl;
for (unsigned int i = 0; i < sockets.Size(); i++) {
printf("%i. %s\n", i + 1, sockets[i]->GetBoundAddress().ToString(true));
}
char message[2048];
// Loop for input
while (1)
{
// This sleep keeps RakNet responsive
RakSleep(30);
if (kbhit())
{
// Notice what is not here: something to keep our network running. It's
// fine to block on gets or anything we want
// Because the network engine was painstakingly written using threads.
Gets(message, sizeof(message));
if (strcmp(message, "quit") == 0)
{
puts("Quitting.");
break;
}
if (strcmp(message, "stat") == 0)
{
rss = server->GetStatistics(server->GetSystemAddressFromIndex(0));
StatisticsToString(rss, message, 2);
printf("%s", message);
printf("Ping %i\n", server->GetAveragePing(server->GetSystemAddressFromIndex(0)));
continue;
}
if (strcmp(message, "ping") == 0)
{
server->Ping(clientID);
continue;
}
if (strcmp(message, "pingip") == 0)
{
printf("Enter IP: ");
Gets(message, sizeof(message));
printf("Enter port: ");
Gets(portstring, sizeof(portstring));
if (portstring[0] == 0)
strcpy(portstring, "1234");
server->Ping(message, atoi(portstring), false);
continue;
}
if (strcmp(message, "kick") == 0)
{
server->CloseConnection(clientID, true, 0);
continue;
}
if (strcmp(message, "getconnectionlist") == 0)
{
printf("Connections:\n");
RakNet::SystemAddress systems[10];
unsigned short numConnections = 10;
server->GetConnectionList((RakNet::SystemAddress*) &systems, &numConnections);
for (int i = 0; i < numConnections; i++)
{
printf("%i. %s\n", i + 1, systems[i].ToString(true));
}
//.........这里部分代码省略.........
示例11: main
int main(int argc, char **argv)
{
printf("Server starting... ");
RakNet::AutopatcherServer autopatcherServer;
// RakNet::FLP_Printf progressIndicator;
RakNet::FileListTransfer fileListTransfer;
static const int workerThreadCount=4; // Used for checking patches only
static const int sqlConnectionObjectCount=32; // Used for both checking patches and downloading
AutopatcherPostgreRepository2_WithXDelta connectionObject[sqlConnectionObjectCount];
RakNet::AutopatcherRepositoryInterface *connectionObjectAddresses[sqlConnectionObjectCount];
for (int i=0; i < sqlConnectionObjectCount; i++)
connectionObjectAddresses[i]=&connectionObject[i];
// fileListTransfer.AddCallback(&progressIndicator);
autopatcherServer.SetFileListTransferPlugin(&fileListTransfer);
// PostgreSQL is fast, so this may not be necessary, or could use fewer threads
// This is used to read increments of large files concurrently, thereby serving users downloads as other users read from the DB
fileListTransfer.StartIncrementalReadThreads(sqlConnectionObjectCount);
autopatcherServer.SetMaxConurrentUsers(MAX_INCOMING_CONNECTIONS); // More users than this get queued up
RakNet::AutopatcherServerLoadNotifier_Printf loadNotifier;
autopatcherServer.SetLoadManagementCallback(&loadNotifier);
#ifdef USE_TCP
RakNet::PacketizedTCP packetizedTCP;
if (packetizedTCP.Start(LISTEN_PORT,MAX_INCOMING_CONNECTIONS)==false)
{
printf("Failed to start TCP. Is the port already in use?");
return 1;
}
packetizedTCP.AttachPlugin(&autopatcherServer);
packetizedTCP.AttachPlugin(&fileListTransfer);
#else
RakNet::RakPeerInterface *rakPeer;
rakPeer = RakNet::RakPeerInterface::GetInstance();
RakNet::SocketDescriptor socketDescriptor(LISTEN_PORT,0);
rakPeer->Startup(MAX_INCOMING_CONNECTIONS,&socketDescriptor, 1);
rakPeer->SetMaximumIncomingConnections(MAX_INCOMING_CONNECTIONS);
rakPeer->AttachPlugin(&autopatcherServer);
rakPeer->AttachPlugin(&fileListTransfer);
#endif
printf("started.\n");
printf("Enter database password:\n");
char connectionString[256],password[128];
char username[256];
strcpy(username, "postgres");
gets(password);
if (password[0]==0) strcpy(password, "aaaa");
strcpy(connectionString, "user=");
strcat(connectionString, username);
strcat(connectionString, " password=");
strcat(connectionString, password);
for (int conIdx=0; conIdx < sqlConnectionObjectCount; conIdx++)
{
if (connectionObject[conIdx].Connect(connectionString)==false)
{
printf("Database connection failed.\n");
return 1;
}
}
printf("Database connection suceeded.\n");
printf("Starting threads\n");
// 4 Worker threads, which is CPU intensive
// A greater number of SQL connections, which read files incrementally for large downloads
autopatcherServer.StartThreads(workerThreadCount,sqlConnectionObjectCount, connectionObjectAddresses);
autopatcherServer.CacheMostRecentPatch(0);
// autopatcherServer.SetAllowDownloadOfOriginalUnmodifiedFiles(false);
printf("System ready for connections\n");
// https://code.google.com/p/xdelta/downloads/list
printf("Optional: Enter path to xdelta.exe: ");
Gets(PATH_TO_XDELTA_EXE, sizeof(PATH_TO_XDELTA_EXE));
if (PATH_TO_XDELTA_EXE[0]==0)
strcpy(PATH_TO_XDELTA_EXE, "c:/xdelta3-3.0.6-win32.exe");
if (PATH_TO_XDELTA_EXE[0])
{
printf("Enter working directory to store temporary files: ");
Gets(WORKING_DIRECTORY, sizeof(WORKING_DIRECTORY));
if (WORKING_DIRECTORY[0]==0)
GetTempPath(MAX_PATH, WORKING_DIRECTORY);
if (WORKING_DIRECTORY[strlen(WORKING_DIRECTORY)-1]=='\\' || WORKING_DIRECTORY[strlen(WORKING_DIRECTORY)-1]=='/')
WORKING_DIRECTORY[strlen(WORKING_DIRECTORY)-1]=0;
}
printf("(D)rop database\n(C)reate database.\n(A)dd application\n(U)pdate revision.\n(R)emove application\n(Q)uit\n");
char ch;
RakNet::Packet *p;
while (1)
{
#ifdef USE_TCP
RakNet::SystemAddress notificationAddress;
notificationAddress=packetizedTCP.HasCompletedConnectionAttempt();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
notificationAddress=packetizedTCP.HasNewIncomingConnection();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
printf("ID_NEW_INCOMING_CONNECTION\n");
notificationAddress=packetizedTCP.HasLostConnection();
if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
//.........这里部分代码省略.........
示例12: main
int main(void)
{
// Pointers to the interfaces of our server and client.
// Note we can easily have both in the same program
RakNet::RakPeerInterface *client;
RakNet::RakPeerInterface *server;
bool b;
char str[256];
char serverPort[30], clientPort[30];
RakNet::TimeMS quitTime;
// Holds packets
RakNet::Packet* p;
printf("A client / server sample showing how clients can broadcast offline packets\n");
printf("to find active servers.\n");
printf("Difficulty: Beginner\n\n");
printf("Instructions:\nRun one or more servers on the same port.\nRun a client and it will get pongs from those servers.\n");
printf("Run as (s)erver or (c)lient?\n");
Gets(str, sizeof(str));
if (str[0]=='s' || str[0]=='S')
{
client=0;
server=RakNet::RakPeerInterface::GetInstance();
// A server
printf("Enter the server port\n");
Gets(serverPort,sizeof(serverPort));
if (serverPort[0]==0)
strcpy(serverPort, "60001");
printf("Starting server.\n");
// The server has to be started to respond to pings.
RakNet::SocketDescriptor socketDescriptor(atoi(serverPort),0);
socketDescriptor.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
b = server->Startup(2, &socketDescriptor, 1)==RakNet::RAKNET_STARTED;
server->SetMaximumIncomingConnections(2);
if (b)
printf("Server started, waiting for connections.\n");
else
{
printf("Server failed to start. Terminating.\n");
exit(1);
}
}
else
{
client=RakNet::RakPeerInterface::GetInstance();
server=0;
// Get our input
printf("Enter the client port to listen on, or 0\n");
Gets(clientPort,sizeof(clientPort));
if (clientPort[0]==0)
strcpy(clientPort, "60000");
printf("Enter the port to ping\n");
Gets(serverPort,sizeof(serverPort));
if (serverPort[0]==0)
strcpy(serverPort, "60001");
RakNet::SocketDescriptor socketDescriptor(atoi(clientPort),0);
socketDescriptor.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
client->Startup(1, &socketDescriptor, 1);
// Connecting the client is very simple. 0 means we don't care about
// a connectionValidationInteger, and false for low priority threads
// All 255's mean broadcast
client->Ping("255.255.255.255", atoi(serverPort), false);
printf("Pinging\n");
}
printf("How many seconds to run this sample for?\n");
Gets(str, sizeof(str));
if (str[0]==0)
{
printf("Defaulting to 5 seconds\n");
quitTime = RakNet::GetTimeMS() + 5000;
}
else
quitTime = RakNet::GetTimeMS() + atoi(str) * 1000;
// Loop for input
while (RakNet::GetTimeMS() < quitTime)
{
if (server)
p = server->Receive();
else
p = client->Receive();
if (p==0)
{
RakSleep(30);
continue;
}
if (server)
server->DeallocatePacket(p);
else
{
if (p->data[0]==ID_UNCONNECTED_PONG)
{
//.........这里部分代码省略.........
示例13: run
void Video::run(const char* ip) {
unsigned int maxConnectionsAllowed = 1;
unsigned int maxPlayersPerServer = 1;
unsigned short serverPort = 7000;
RakNet::RakPeerInterface *rakPeer = RakNet::RakPeerInterface::GetInstance();
RakNet::SocketDescriptor socketDescriptor(serverPort, 0);
if (rakPeer->Startup(maxConnectionsAllowed, &socketDescriptor, 1) != RakNet::RAKNET_STARTED) {
std::cerr << "Startup fail:" << std::endl;
RakNet::RakPeerInterface::DestroyInstance(rakPeer);
return;
}
rakPeer->SetMaximumIncomingConnections(maxPlayersPerServer);
RakNet::Packet *packet;
unsigned char typeId;
bool connected = false;
char key;
RakNet::SystemAddress address;
if (ip) {
std::cout << "Connect: " << ip << std::endl;
rakPeer->Connect(ip, serverPort, 0, 0);
}
cv::namedWindow("MyVideo", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);
cv::createTrackbar("AlphaTrackbar", "MyVideo", &alpha_slider, 100);
capture.open(0);
if (!capture.isOpened()) {
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;
//.........这里部分代码省略.........