etlist: multiple packets received until a timeout

This commit is contained in:
Radegast 2012-09-03 00:11:28 +02:00
parent a89d06c8fd
commit fe18c88311
6 changed files with 256 additions and 190 deletions

View file

@ -5,6 +5,6 @@ project(etlist)
Find_Package(Boost REQUIRED COMPONENTS system program_options) # Specifying 'asio' doesn't work Find_Package(Boost REQUIRED COMPONENTS system program_options) # Specifying 'asio' doesn't work
Find_Package(Threads) # asio dependency Find_Package(Threads) # asio dependency
add_executable(etlist connection.cpp main.cpp) add_executable(etlist main.cpp connection.cpp etparser.cpp)
target_link_libraries(etlist ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(etlist ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

View file

@ -20,175 +20,81 @@
#include "connection.h" #include "connection.h"
Connection::Connection(std::string server_name, int server_port /*=27960*/, Connection::Connection(boost::asio::io_service& io_service,
std::string message /*=getstatus*/) std::string server_name /*=etlegacy.com*/,
: socket_(io_service_), resolver_(io_service_), deadline_(io_service_) int server_port /*=27960*/,
std::string message /*=getstatus*/,
float timeout /*=1.5*/)
: io_service_(io_service), socket_(io_service, udp::v4()), timer_(io_service)
{ {
udp::resolver resolver(io_service_);
udp::resolver::query query(udp::v4(), server_name, udp::resolver::query query(udp::v4(), server_name,
boost::lexical_cast<std::string>(server_port)); boost::lexical_cast<std::string>(server_port));
udp::endpoint receiver_endpoint = *resolver_.resolve(query); receiver_endpoint_ = *resolver.resolve(query);
socket_.open(udp::v4()); socket_.async_send_to(boost::asio::buffer(wrap_message(message)),
socket_.send_to(boost::asio::buffer(wrap_message(message), 1024), receiver_endpoint_,
receiver_endpoint); boost::bind(&Connection::HandleSend, this));
// Set to positive infinity so that there's no action until a specific // How long should we wait for the server to respond
// deadline is set. timer_.expires_from_now(boost::posix_time::seconds(timeout));
deadline_.expires_at(boost::posix_time::pos_infin); timer_.async_wait(boost::bind(&Connection::close, this));
// Start the persistent actor that checks for deadline expiry.
check_deadline();
} }
/* void Connection::close()
{
socket_.close();
}
/**
* @brief Wraps messages into the Quake III protocol format * @brief Wraps messages into the Quake III protocol format
*/ */
std::string Connection::wrap_message(std::string message) std::string Connection::wrap_message(std::string message)
{ {
// NOTE: master server doesn't react to a message terminated with 0xfa // NOTE: master server doesn't react to a message terminated with 0xfa
return std::string(4, 0xff) + message + std::string(1, 0xfa); return std::string(4, 0xff) + message;
} }
std::size_t Connection::ReceiveMessage( std::string Connection::get_response()
const boost::asio::mutable_buffer& buffer,
boost::posix_time::time_duration timeout,
boost::system::error_code& ec)
{ {
deadline_.expires_from_now(timeout); return response_;
ec = boost::asio::error::would_block;
std::size_t length = 0;
// Start the asynchronous operation itself. The handle_receive function
// used as a callback will update the ec and length variables.
socket_.async_receive(boost::asio::buffer(buffer),
boost::bind(&Connection::handle_receive, _1, _2,
&ec, &length));
// Block until the asynchronous operation has completed.
do
io_service_.run_one();
while (ec == boost::asio::error::would_block);
return length;
} }
void Connection::handle_receive( /**
const boost::system::error_code& ec, std::size_t length, * @brief Calls itself after every received packet.
boost::system::error_code *out_ec, std::size_t *out_length) * @note It hangs after the last received packet until it is stopped.
*/
void Connection::HandleReceive(const boost::system::error_code& error,
size_t bytes_recvd)
{ {
*out_ec = ec; if (!error && bytes_recvd > 0)
*out_length = length;
}
void Connection::check_deadline()
{
// Check whether the deadline has passed. We compare the deadline against
// the current time since a new asynchronous operation may have moved the
// deadline before this actor had a chance to run.
if (deadline_.expires_at() <= boost::asio::deadline_timer::traits_type::now())
{ {
// The deadline has passed. The outstanding asynchronous operation needs socket_.async_receive_from(
// to be cancelled so that the blocked receive() function will return. boost::asio::buffer(data_, max_length), receiver_endpoint_,
// boost::bind(&Connection::HandleReceive, this,
// Please note that cancel() has portability issues on some versions of boost::asio::placeholders::error,
// Microsoft Windows, and it may be necessary to use close() instead. boost::asio::placeholders::bytes_transferred));
// Consult the documentation for cancel() for further information.
socket_.cancel();
// There is no longer an active deadline. The expiry is set to positive // std::cout.write(data_, bytes_recvd) << std::endl;
// infinity so that the actor takes no action until a new deadline is set. response_ += data_;
deadline_.expires_at(boost::posix_time::pos_infin);
} }
else if (error)
// Put the actor back to sleep. {
deadline_.async_wait(boost::bind(&Connection::check_deadline, this)); std::cout << "Receive error: " << error.message() << std::endl;
}
// io_service will quit when it has no more work to do
} }
void Connection::ParseMessage(std::string recv_msg) /**
* @brief Calls HandleReceive for the first packet.
*
* Separate from the HandleReceive method to avoid adding whitespace to response_
*/
void Connection::HandleSend()
{ {
// recv_msg.erase(recv_msg.find('\0'), recv_msg.npos); socket_.async_receive_from(
size_t headerEnd = recv_msg.find('\n'); boost::asio::buffer(data_, max_length), receiver_endpoint_,
boost::bind(&Connection::HandleReceive, this,
// Omit OOB from the packet name boost::asio::placeholders::error,
std::cout << "Parsing " << boost::asio::placeholders::bytes_transferred));
recv_msg.substr(4, headerEnd - 4) << " packet.... "; }
std::map<std::string, std::string> recv_tokens;
std::string key, value;
size_t tokenStart = 0;
size_t tokenEnd = 0;
for (;; )
{
/*
* Search for a key
*/
tokenStart = recv_msg.find('\\', tokenEnd++);
tokenEnd = recv_msg.find('\\', ++tokenStart);
// No more keys
if (tokenStart == std::string::npos)
{
break;
}
// Key without a value
if (tokenEnd == std::string::npos)
{
key = recv_msg.substr(tokenStart,
recv_msg.length() - tokenStart);
recv_tokens[key] = "";
std::cout << "Warning: adding a key with empty value." << std::endl;
break;
}
key = recv_msg.substr(tokenStart, tokenEnd - tokenStart);
/*
* Search for a value
*/
tokenStart = recv_msg.find('\\', tokenEnd++);
tokenEnd = recv_msg.find('\\', ++tokenStart);
// No more values
if (tokenStart == std::string::npos)
{
break;
}
// Value is not at the end
if (tokenEnd != std::string::npos)
{
value = recv_msg.substr(tokenStart, tokenEnd - tokenStart);
}
else
{
// Last value
value = recv_msg.substr(tokenStart, recv_msg.length() - tokenStart);
}
/*
* Store key->value pair in a map
*/
recv_tokens[key] = value;
// FIXME: This should not happen, but it does. Why?
if (tokenStart >= recv_msg.length() || tokenEnd >= recv_msg.length())
{
break;
}
}
/*
* Display key->value pairs
*/
std::cout << recv_tokens.size() << " variables paired" << std::endl << std::endl;
std::map <std::string, std::string>::iterator it;
for (it = recv_tokens.begin(); it != recv_tokens.end(); ++it)
{
std::cout << std::setw(22) << it->first << ": " << it->second <<
std::endl;
}
}

View file

@ -17,15 +17,15 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>. * along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef CONNECTION_H
#define CONNECTION_H
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <map>
#include <iomanip> // using 'setw'
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/deadline_timer.hpp> #include <boost/asio/deadline_timer.hpp>
#include <boost/program_options.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
@ -34,24 +34,27 @@ using boost::asio::ip::udp;
class Connection class Connection
{ {
public: public:
Connection(std::string server_name = "etlegacy.com", Connection(boost::asio::io_service& io_service,
std::string server_name = "etlegacy.com",
int server_port = 27960, int server_port = 27960,
std::string message = "getstatus"); std::string message = "getstatus",
float timeout = 1.5);
std::size_t ReceiveMessage(const boost::asio::mutable_buffer& buffer, void close();
boost::posix_time::time_duration timeout, std::string get_response();
boost::system::error_code& ec);
void ParseMessage(std::string recv_msg);
private: private:
boost::asio::io_service io_service_; boost::asio::deadline_timer timer_;
boost::asio::ip::udp::socket socket_; boost::asio::io_service &io_service_;
boost::asio::deadline_timer deadline_; udp::socket socket_;
boost::asio::ip::udp::resolver resolver_; udp::endpoint receiver_endpoint_;
enum { max_length = 2048 };
char data_[max_length];
std::string response_;
void HandleReceive(const boost::system::error_code& error, size_t bytes_recvd);
void HandleSend();
std::string wrap_message(std::string message); std::string wrap_message(std::string message);
void check_deadline();
static void handle_receive(
const boost::system::error_code& ec, std::size_t length,
boost::system::error_code *out_ec, std::size_t *out_length);
}; };
#endif // CONNECTION_H

125
etlist/etparser.cpp Normal file
View file

@ -0,0 +1,125 @@
/*
* ET: Legacy
* Copyright (C) 2012 Jan Simek <mail@etlegacy.com>
*
* This file is part of ET: Legacy - http://www.etlegacy.com
*
* ET: Legacy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ET: Legacy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <map>
#include <iomanip> // using 'setw'
#include "etparser.h"
ETParser::ETParser()
{
}
ETParser::~ETParser()
{
}
void ETParser::ParseMessage(std::string recv_msg)
{
// recv_msg.erase(recv_msg.find('\0'), recv_msg.npos);
// Omit OOB from the packet name
size_t headerEnd = recv_msg.find('\n');
std::cout << "Parsing " <<
recv_msg.substr(4, headerEnd - 4) << " packet.... ";
std::map<std::string, std::string> recv_tokens;
std::string key, value;
size_t tokenStart = 0;
size_t tokenEnd = 0;
for (;; )
{
/*
* Search for a key
*/
tokenStart = recv_msg.find('\\', tokenEnd++);
tokenEnd = recv_msg.find('\\', ++tokenStart);
// No more keys
if (tokenStart == std::string::npos)
{
break;
}
// Key without a value
if (tokenEnd == std::string::npos)
{
key = recv_msg.substr(tokenStart,
recv_msg.length() - tokenStart);
recv_tokens[key] = "";
std::cout << "Warning: adding a key with empty value." << std::endl;
break;
}
key = recv_msg.substr(tokenStart, tokenEnd - tokenStart);
/*
* Search for a value
*/
tokenStart = recv_msg.find('\\', tokenEnd++);
tokenEnd = recv_msg.find('\\', ++tokenStart);
// No more values
if (tokenStart == std::string::npos)
{
break;
}
// Value is not at the end
if (tokenEnd != std::string::npos)
{
value = recv_msg.substr(tokenStart, tokenEnd - tokenStart);
}
else
{
// Last value
value = recv_msg.substr(tokenStart, recv_msg.length() - tokenStart);
}
/*
* Store key->value pair in a map
*/
recv_tokens[key] = value;
// FIXME: This should not happen, but it does. Why?
if (tokenStart >= recv_msg.length() || tokenEnd >= recv_msg.length())
{
break;
}
}
/*
* Display key->value pairs
*/
std::cout << recv_tokens.size() << " variables paired" << std::endl << std::endl;
std::map <std::string, std::string>::iterator it;
for (it = recv_tokens.begin(); it != recv_tokens.end(); ++it)
{
std::cout << std::setw(22) << it->first << ": " << it->second <<
std::endl;
}
}

35
etlist/etparser.h Normal file
View file

@ -0,0 +1,35 @@
/*
* ET: Legacy
* Copyright (C) 2012 Jan Simek <mail@etlegacy.com>
*
* This file is part of ET: Legacy - http://www.etlegacy.com
*
* ET: Legacy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ET: Legacy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ETPARSER_H
#define ETPARSER_H
class ETParser
{
public:
ETParser();
virtual ~ETParser();
void SplitIntoParts(std::string msg_to_split);
void ParseMessage(std::string recv_msg);
private:
};
#endif // ETPARSER_H

View file

@ -26,6 +26,7 @@
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include "connection.h" #include "connection.h"
#include "etparser.h"
using boost::asio::ip::udp; using boost::asio::ip::udp;
@ -33,7 +34,7 @@ int main(int argc, char *argv[])
{ {
try try
{ {
/** /*
* Program options * Program options
*/ */
boost::program_options::options_description desc("[OPTIONS]"); boost::program_options::options_description desc("[OPTIONS]");
@ -47,6 +48,9 @@ int main(int argc, char *argv[])
("message,m", ("message,m",
boost::program_options::value<std::string>()->default_value("getstatus"), boost::program_options::value<std::string>()->default_value("getstatus"),
"message to be sent") "message to be sent")
("timeout,t",
boost::program_options::value<float>()->default_value(1.5),
"seconds to wait for the server to respond")
("raw,r", "don't parse the server response") ("raw,r", "don't parse the server response")
; ;
boost::program_options::variables_map var_map; boost::program_options::variables_map var_map;
@ -65,44 +69,37 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
/** /*
* Send the request * Send the request
*/ */
Connection client(var_map["server"].as<std::string>(), boost::asio::io_service io_service;
var_map["port"].as<unsigned int>(), Connection client(io_service, var_map["server"].as<std::string>(),
var_map["message"].as<std::string>()); var_map["port"].as<unsigned int>(),
var_map["message"].as<std::string>(),
var_map["timeout"].as<float>());
io_service.run();
char data[1024]; /*
boost::system::error_code ec; * Parse the response
std::size_t n = client.ReceiveMessage(boost::asio::buffer(data), */
boost::posix_time::seconds(10), ETParser parser;
ec);
if (ec) if (var_map.count("raw"))
{ {
std::cout << "Receive error: " << ec.message() << std::endl; std::cout << client.get_response() << std::endl;
} }
else else
{ {
if (var_map.count("raw")) parser.ParseMessage(client.get_response());
{
std::cout.write(data, n);
}
else
{
client.ParseMessage(std::string(data, n));
}
} }
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << "Exception caught: " << e.what() << std::endl; std::cerr << "Exception caught: " << e.what() << std::endl;
// return 1;
} }
catch (...) catch (...)
{ {
std::cerr << "Exception of unknown type!" << std::endl; std::cerr << "Exception of unknown type!" << std::endl;
// return 1;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;