* Add a "pretty"-printing function for printing games to a terminal (through std::cout)
* switch to using IPv4 instead of IPv6 (for testing) * Use correct buffersize for retrieval of a command/method (5 bytes/chars right now, ought to be NUL-terminated) git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1641 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
e0872c90af
commit
771d1efa9e
|
@ -58,11 +58,61 @@ int main(int argc, char* argv[])
|
|||
return 1;
|
||||
}
|
||||
|
||||
std::string str_repeat(const std::string& src, unsigned int count)
|
||||
{
|
||||
std::string tmp;
|
||||
tmp.reserve(src.length() * count);
|
||||
for (unsigned int i = 0; i != count; ++i)
|
||||
{
|
||||
tmp += src;
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline std::string to_string (const T& t)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << t;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void printGame(boost::shared_ptr<GAMESTRUCT> game)
|
||||
{
|
||||
std::string gameName(game->name);
|
||||
std::string gameSize(to_string(game->desc.dwSize));
|
||||
std::string gameFlags(to_string(game->desc.dwFlags));
|
||||
std::string gameHost(game->desc.host);
|
||||
std::string gameMaxPlayers(to_string(game->desc.dwMaxPlayers));
|
||||
std::string gameCurrentPlayers(to_string(game->desc.dwCurrentPlayers));
|
||||
std::string gameUser1(to_string(game->desc.dwUser1));
|
||||
std::string gameUser2(to_string(game->desc.dwUser2));
|
||||
std::string gameUser3(to_string(game->desc.dwUser3));
|
||||
std::string gameUser4(to_string(game->desc.dwUser4));
|
||||
|
||||
unsigned int targetLength = std::max(std::max(std::max(std::max(std::max(std::max(std::max(std::max(std::max(gameName.length() + 2, gameSize.length()), gameFlags.length()), gameHost.length() + 2), gameMaxPlayers.length()), gameCurrentPlayers.length()), gameUser1.length()), gameUser2.length()), gameUser3.length()), gameUser4.length());
|
||||
|
||||
std::cout << "Game info:\n"
|
||||
<< "+------------------" << str_repeat("-", targetLength) << "-+\n"
|
||||
<< "| Name | \"" << gameName << "\"" << str_repeat(" ", targetLength - gameName.length() - 2) << " |\n"
|
||||
<< "| Size | " << str_repeat(" ", targetLength - gameSize.length()) << gameSize << " |\n"
|
||||
<< "| Flags | " << str_repeat(" ", targetLength - gameFlags.length()) << gameFlags << " |\n"
|
||||
<< "| Host | \"" << gameHost << "\"" << str_repeat(" ", targetLength - gameHost.length() - 2) << " |\n"
|
||||
<< "| MaxPlayers | " << str_repeat(" ", targetLength - gameMaxPlayers.length()) << gameMaxPlayers << " |\n"
|
||||
<< "| CurrentPlayers | " << str_repeat(" ", targetLength - gameCurrentPlayers.length()) << gameCurrentPlayers << " |\n"
|
||||
<< "| User1 | " << str_repeat(" ", targetLength - gameUser1.length()) << gameUser1 << " |\n"
|
||||
<< "| User2 | " << str_repeat(" ", targetLength - gameUser2.length()) << gameUser2 << " |\n"
|
||||
<< "| User3 | " << str_repeat(" ", targetLength - gameUser3.length()) << gameUser3 << " |\n"
|
||||
<< "| User4 | " << str_repeat(" ", targetLength - gameUser4.length()) << gameUser4 << " |\n"
|
||||
<< "+------------------" << str_repeat("-", targetLength) << "-+" << std::endl;
|
||||
}
|
||||
|
||||
int start()
|
||||
{
|
||||
boost::asio::io_service io_service;
|
||||
|
||||
boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), lobbyPort));
|
||||
boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), lobbyPort));
|
||||
|
||||
for (unsigned int count = 0; count != 10; ++count)
|
||||
{
|
||||
|
@ -77,8 +127,7 @@ int start()
|
|||
|
||||
boost::array<char, 5> buffer;
|
||||
|
||||
size_t reply_length = boost::asio::read(socket, boost::asio::buffer(buffer, buffer.size() - 1));
|
||||
*(buffer.end() - 1) = 0;
|
||||
size_t reply_length = boost::asio::read(socket, boost::asio::buffer(buffer));
|
||||
|
||||
std::cout << "Received: " << buffer.data() << std::endl;
|
||||
|
||||
|
@ -95,7 +144,7 @@ int start()
|
|||
reply_length = boost::asio::read(socket, boost::asio::buffer(newGameData.get(), sizeof(GAMESTRUCT)));
|
||||
|
||||
if (lobbyDev)
|
||||
std::cout << "Game name: " << newGameData->name << "; host name: " << newGameData->desc.host << "; players/max: " << newGameData->desc.dwCurrentPlayers << "/" << newGameData->desc.dwMaxPlayers << std::endl;
|
||||
printGame(newGameData);
|
||||
}
|
||||
else if (buffer == cmdListGames)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue