本文整理汇总了C++中sf::TcpSocket::receive方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpSocket::receive方法的具体用法?C++ TcpSocket::receive怎么用?C++ TcpSocket::receive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sf::TcpSocket
的用法示例。
在下文中一共展示了TcpSocket::receive方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendRegisteringToken
void Client::sendRegisteringToken(const std::string& name, const std::string& password, sf::TcpSocket& socket)
{
sf::Packet packet;
packet << TransferType::REGISTERING << name << password;
if(socket.send(packet) != sf::Socket::Done)
throw std::runtime_error("sending packet failed.");
// Receive the server response
socket.receive(packet);
TransferType response;
packet >> response;
switch(response)
{
case TransferType::USERNAME_NOT_AVAILABLE:
throw std::runtime_error("the username " + name + " is not available.");
case TransferType::FAILED_TO_REGISTER:
throw std::runtime_error("the server failed to register your account.");
case TransferType::ACKNOWLEDGE:
return;
default:
throw std::runtime_error("unidentified server response.");
}
}
示例2: recv_dummy_or_inc_ctr
void recv_dummy_or_inc_ctr()
{
std::size_t dummy;
if(_sckt.receive(&dummy, 0, dummy) == sf::Socket::Disconnected)
{
_ctr += 1;
}
else
{
reset_ctr();
}
}
示例3: update
void update()
{
if(!busy) { ssvu::lo("ManagedSocket") << "Update failed - not busy" << std::endl; return; }
std::this_thread::sleep_for(std::chrono::milliseconds(1));
sf::Packet packet;
for(int i{0}; i < 5; ++i)
{
if(busy && socket.receive(packet) == sf::Socket::Done) onPacketReceived(packet);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
示例4: receiveTime
sf::Socket::Status NETSERVER::receiveTime(sf::TcpSocket &p_socket, char* p_buffer, const unsigned int p_limit, size_t &p_received)
{
sf::SocketSelector selector;
selector.add(p_socket);
if (selector.wait(sf::seconds(2)))
{
return p_socket.receive(p_buffer, p_limit, p_received);
}
else
{
return sf::Socket::NotReady;
}
} // receiveTime()
示例5: if
bfbc2::server_info bfbc2::QueryServerInfo(sf::TcpSocket &socket) {
server_info serverInfo;
std::cout << "Sending serverInfo query..." << std::endl;
if (socket.send(serverInfo_QueryPacket, sizeof(serverInfo_QueryPacket)) == sf::Socket::Status::Done) {
std::cout << "Success! Waiting for response..." << std::endl;
char buffer[PACKETSIZE];
std::size_t received = 0;
// receive response
if (socket.receive(buffer, sizeof(buffer), received) == sf::Socket::Status::Done) {
if (received > 0) {
std::cout << "Success! Processing..." << std::endl;
// skip header.
int index = 8;
// get number of WORDs in packet, then store them for parsing.
uint32_t numofwords = (static_cast<uint32_t>(buffer[index]) ) +
(static_cast<uint32_t>(buffer[index + 1]) << 8) +
(static_cast<uint32_t>(buffer[index + 2]) << 16) +
(static_cast<uint32_t>(buffer[index + 3]) << 24);
// skip numwords
index += 4;
// extract the content of each word.
std::vector<std::string> content;
for (uint32_t i = 0; i < numofwords; i++) {
content.push_back(GetWordContent(buffer, index));
}
// checks to see if we have the correct results
if (!content.empty() && content.size() == 24) {
//
// CHECKING FOR OK RESPONSE (.at(0)) & & IF ACCEPTINGPLAYERS )
//
if (content.at(0) == "OK") {
if (content.at(12) == "AcceptingPlayers") {
serverInfo.ip = socket.getRemoteAddress();
serverInfo.name = content.at(1);
serverInfo.playersonline = atoi(content.at(2).c_str());
serverInfo.maxplayers = atoi(content.at(3).c_str());
serverInfo.gamemode = content.at(4);
std::string mapstring = content.at(5);
// change map string to lowercase, as the map filename is stored in the server .cfg,
// and the server admin can make it upper or lower case.
std::transform(mapstring.begin(), mapstring.end(), mapstring.begin(), ::tolower);
std::string map;
if (mapstring.find("001") != std::string::npos) { map = "Panama Canal"; }
else if (mapstring.find("mp_002") != std::string::npos) { map = "Valparaiso"; }
else if (mapstring.find("003") != std::string::npos) { map = "Laguna Alta"; }
else if (mapstring.find("004") != std::string::npos) { map = "Isla Inocentes"; }
else if (mapstring.find("mp_005") != std::string::npos) { map = "Atacama Desert"; }
else if (mapstring.find("006") != std::string::npos) { map = "Arica Harbor"; }
else if (mapstring.find("007") != std::string::npos) { map = "White Pass"; }
else if (mapstring.find("008") != std::string::npos) { map = "Nelson Bay"; }
else if (mapstring.find("009") != std::string::npos) { map = "Laguna Presa"; }
else if (mapstring.find("012") != std::string::npos) { map = "Port Valdez"; }
else if (mapstring.find("oasis") != std::string::npos) { map = "Oasis"; }
else if (mapstring.find("sp_005") != std::string::npos) { map = "Heavy Metal"; }
else if (mapstring.find("sp_002") != std::string::npos) { map = "Cold War"; }
else if (mapstring.find("harvest") != std::string::npos) { map = "Harvest Day"; }
serverInfo.map = map;
}
}
}
}
} // socket.receive
} // socket.send
// if send, receive, or content check fails anywhere, server_info.ip will return sf::IpAddress::None
return serverInfo;
}
示例6: atoi
std::vector<bfbc2::player_info> bfbc2::QueryPlayerInfo(sf::TcpSocket &socket) {
std::cout << "Sending playerInfo query..." << std::endl;
std::vector<player_info> players;
if (socket.send(listPlayer_QueryPacket, sizeof(listPlayer_QueryPacket)) == sf::Socket::Status::Done) {
std::cout << "Success! Waiting for response..." << std::endl;
char buffer[PACKETSIZE];
std::size_t received = 0;
// receive response
if (socket.receive(buffer, sizeof(buffer), received) == sf::Socket::Status::Done) {
if (received > 0) {
std::cout << "Success! Processing..." << std::endl;
// skip header
int index = 8;
uint32_t numofwords = (static_cast<uint32_t>(buffer[index]) ) +
(static_cast<uint32_t>(buffer[index + 1]) << 8) +
(static_cast<uint32_t>(buffer[index + 2]) << 16) +
(static_cast<uint32_t>(buffer[index + 3]) << 24);
// skip numofwords
index += 4;
// extract the content of each word.
std::vector<std::string> content;
for (uint32_t i = 0; i < numofwords; i++) {
content.push_back(GetWordContent(buffer, index));
}
// hIndex is used for parsing.
int hIndex = 0;
// check for content. we need at least 9 entries for the
// response and the categories, so if it didn't at least fill those,
// i'll know there was a kind of error.
if (content.at(hIndex) == "OK" && content.size() >= 9) {
// skip forward +2, 1 for response, other for numofcategories
// i know the number, so no need to pull.
hIndex += 2;
// next is the actual categories, 9 of them.
// skipping & not parsing the category names, already know them.
hIndex += 9;
// next is the player count on the server, read then skip
int playerc = atoi(content.at(hIndex).c_str());
hIndex++;
// parse players and their category info
for (int pc = 0; pc < playerc; pc++) {
player_info p;
p.clantag = content.at(hIndex++);
p.name = content.at(hIndex++);
p.guid = content.at(hIndex++);
p.teamid = atoi(content.at(hIndex++).c_str());
p.squadid = atoi(content.at(hIndex++).c_str());
p.kills = atoi(content.at(hIndex++).c_str());
p.deaths = atoi(content.at(hIndex++).c_str());
p.score = atoi(content.at(hIndex++).c_str());
p.ping = atoi(content.at(hIndex++).c_str());
players.push_back(p);
}
}
}
} // socket.receive
} // socket.send
return players;
}