本文整理汇总了C++中raknet::RakPeerInterface::Send方法的典型用法代码示例。如果您正苦于以下问题:C++ RakPeerInterface::Send方法的具体用法?C++ RakPeerInterface::Send怎么用?C++ RakPeerInterface::Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类raknet::RakPeerInterface
的用法示例。
在下文中一共展示了RakPeerInterface::Send方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: packAndSendGameState
void packAndSendGameState(RakNet::RakPeerInterface& _krPeer, map<RakNet::NetworkID, Entity*>& _players,
const Circle* _kpFoodModel)
{
RakNet::BitStream stream;
stream.Write((RakNet::MessageID) MessageIdentifiers::ID_SNAKE_STATES);
stream.Write(_players.size());
for (map<RakNet::NetworkID, Entity*>::iterator playerIter = _players.begin();
playerIter != _players.end(); playerIter++)
{
Entity* pSnake = playerIter->second;
stream.Write(playerIter->first);
stream.Write(pSnake->getSingleComponent<Componentizer<unsigned int> >()->getValue());
vector<Vector2> segmentPositions = pSnake->getSingleComponent<CSnakeModel>()->getSegmentPositions();
stream.Write(segmentPositions.size());
for (unsigned int uiSegmentIndex = 0; uiSegmentIndex < segmentPositions.size(); uiSegmentIndex++)
{
stream.Write(segmentPositions[uiSegmentIndex]);
}
}
if (_kpFoodModel == NULL)
{
stream.Write(false);
}
else
{
stream.Write(true);
stream.Write(_kpFoodModel->getPosition());
}
_krPeer.Send(&stream, IMMEDIATE_PRIORITY, UNRELIABLE, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
示例2: 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;
//.........这里部分代码省略.........
示例3: main
//.........这里部分代码省略.........
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);
rakPeer->Send(&str, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
}
else
{
printf("You must be connected to send commands.\n");
}
}
}
packet = rakPeer->Receive();
if (packet)
{
switch (packet->data[0])
{
case ID_DISCONNECTION_NOTIFICATION:
printf("The server disconnected us.\n");
isConnected=false;
break;
case ID_CONNECTION_BANNED:
printf("We are banned from this server.\n");
isConnected=false;
break;
case ID_CONNECTION_ATTEMPT_FAILED:
printf("Connection attempt failed.\nThe password was wrong or there is no responsive machine at that IP/port.\n");
isConnected=false;
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
printf("Server is full.\n");
isConnected=false;
break;
case ID_CONNECTION_LOST:
printf("We lost the connection.\n");
isConnected=false;
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
printf("Connection accepted.\n");
isConnected=true;
break;
case ID_TRANSPORT_STRING:
printf("%s", packet->data+1);
break;
}
rakPeer->DeallocatePacket(packet);
}
// This sleep keeps RakNet responsive
#ifdef _WIN32
Sleep(30);
#else
usleep(30 * 1000);
#endif
}
return 0;
}
示例4: 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);
//.........这里部分代码省略.........
示例5: main
//.........这里部分代码省略.........
printf("%i. %s\n", i + 1, systems[i].ToString(true));
}
continue;
}
if (strcmp(message, "ban") == 0)
{
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));
}
}
示例6: HandleNetworking
//.........这里部分代码省略.........
// Registry stuff
HKEY hKey = 0;
DWORD dwType = 0;
// Get the size of our buffer
DWORD dwBufSize = sizeof( guid );
// Define the subkey
const wchar_t* subkey = L"Software\\Microsoft\\Cryptography";
// Open the registry
if( RegOpenKey( HKEY_LOCAL_MACHINE, subkey, &hKey ) == ERROR_SUCCESS )
{
// We're looking for a string
dwType = REG_SZ;
// Read to our buffer
if( !RegQueryValueEx( hKey, L"MachineGuid", 0, &dwType, (BYTE *)guid, &dwBufSize ) == ERROR_SUCCESS )
{
// Fall back to a null GUID
isGuidNull = true;
}
else
{
// Convert the wide char buffer into a char buffer
wcstombs( guid_net, guid, 255 );
}
}
else
{
// Fall back to a null GUID
isGuidNull = true;
}
}
// Initialize a bitstream
RakNet::BitStream bsOut;
// Send our user data to the server
{
// Packet ID
bsOut.Write( (RakNet::MessageID)ID_SC4_CONNECTION_DATA );
// Nickname
bsOut.Write( "Stormeus" );
// Unique GUID, or a null one
if( isGuidNull )
bsOut.Write( 0 );
else
{
#ifdef DEBUG
MessageBoxA( NULL, guid_net, "SC4Multi -- GUID", MB_ICONINFORMATION | MB_OK );
#endif
bsOut.Write( guid_net );
}
// City taken
bsOut.Write( "Unknown City" );
}
// Priority shipping to the server
peer->Send
(
&bsOut,
HIGH_PRIORITY,
RELIABLE_ORDERED,
0,
packet->systemAddress,
false
);
break;
}
case ID_NEW_INCOMING_CONNECTION:
MessageBoxA( NULL, "Incoming connection", "SC4Multi", MB_OK | MB_ICONINFORMATION );
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
MessageBoxA( NULL, "Server is full", "SC4Multi", MB_OK | MB_ICONINFORMATION );
break;
case ID_DISCONNECTION_NOTIFICATION:
MessageBoxA( NULL, "We disconnected", "SC4Multi", MB_OK | MB_ICONINFORMATION );
break;
case ID_CONNECTION_LOST:
MessageBoxA( NULL, "We lost connection", "SC4Multi", MB_OK | MB_ICONINFORMATION );
break;
}
}
}
// Destroy this instance
RakNet::RakPeerInterface::DestroyInstance( peer );
// And we're done here.
MessageBoxA( NULL, "Destroyed RakNet instance.", "SC4Multi -- Networking Debug", MB_OK | MB_ICONEXCLAMATION );
return 1;
}
示例7: 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();
}