refactored code in server.cpp

This commit is contained in:
Gunter Labes 2009-02-01 12:42:07 +00:00
parent 14b5bb32ba
commit 029a6d1921

View file

@ -36,6 +36,7 @@
#include "simple_wml.hpp"
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <algorithm>
#include <cassert>
#include <cerrno>
@ -562,30 +563,35 @@ void server::run() {
char* buf_ptr = new char [buf.size()];
memcpy(buf_ptr, &buf[0], buf.size());
simple_wml::string_span compressed_buf(buf_ptr, buf.size());
boost::scoped_ptr<simple_wml::document> data_ptr;
try {
simple_wml::document data(compressed_buf); // might throw a simple_wml::error
data.take_ownership_of_buffer(buf_ptr);
std::vector<char>().swap(buf);
data_ptr.reset(new simple_wml::document(compressed_buf)); // might throw a simple_wml::error
data_ptr->take_ownership_of_buffer(buf_ptr);
const clock_t after_parsing = get_cpu_time(sample);
process_data(sock, data);
bandwidth_type->set_type(data.root().first_child().to_string());
if(sample) {
const clock_t after_processing = get_cpu_time(sample);
metrics_.record_sample(data.root().first_child(),
after_parsing - before_parsing,
after_processing - after_parsing);
}
} catch (simple_wml::error& e) {
// Most likely the error happened in scenario data so just give a server
// message instead of send_error() which would kick the client out.
lobby_.send_server_message(std::string("Invalid WML received: '"
+ e.message + "'. You might have to quit.").c_str(), sock);
delete buf_ptr;
send_error(sock, ("Invalid WML received: " + e.message).c_str());
delete [] buf_ptr;
continue;
} catch(...) {
delete [] buf_ptr;
throw;
}
simple_wml::document& data = *data_ptr;
std::vector<char>().swap(buf);
const clock_t after_parsing = get_cpu_time(sample);
process_data(sock, data);
bandwidth_type->set_type(data.root().first_child().to_string());
if(sample) {
const clock_t after_processing = get_cpu_time(sample);
metrics_.record_sample(data.root().first_child(),
after_parsing - before_parsing,
after_processing - after_parsing);
}
}
metrics_.no_requests();
@ -593,7 +599,7 @@ void server::run() {
} catch(config::error& e) {
WRN_CONFIG << "Warning: error in received data: " << e.message << "\n";
} catch(simple_wml::error& e) {
WRN_CONFIG << "Warning: error in received data\n";
WRN_CONFIG << "Warning: error in received data: " << e.message << "\n";
} catch(network::error& e) {
if (e.message == "shut down") {
LOG_SERVER << "Try to disconnect all users...\n";