Changeset 465866c in nscp for include/nsca/client/nsca_client_protocol.hpp
- Timestamp:
- 06/05/12 07:35:30 (12 months ago)
- Branches:
- master, 0.4.1, 0.4.2
- Children:
- 742b035
- Parents:
- 695f240
- File:
-
- 1 edited
-
include/nsca/client/nsca_client_protocol.hpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
include/nsca/client/nsca_client_protocol.hpp
raf05fa1 r465866c 99 99 } 100 100 } 101 102 103 /*104 #pragma once105 106 #include <boost/shared_ptr.hpp>107 #include <boost/asio.hpp>108 109 #include <socket/socket_helpers.hpp>110 111 #include <nsca/nsca_packet.hpp>112 #include <nsca/nsca_enrypt.hpp>113 114 using boost::asio::ip::tcp;115 116 namespace nsca {117 118 class socket : public boost::noncopyable {119 private:120 boost::shared_ptr<tcp::socket> socket_;121 boost::asio::io_service &io_service_;122 nsca_encrypt crypt_inst;123 int time;124 public:125 typedef boost::asio::basic_socket<tcp,boost::asio::stream_socket_service<tcp> > basic_socket_type;126 127 public:128 socket(boost::asio::io_service &io_service) : io_service_(io_service), time(0) {129 socket_.reset(new tcp::socket(io_service_));130 }131 ~socket() {132 if (socket_)133 socket_->close();134 socket_.reset();135 }136 137 virtual void connect(std::string host, std::string port) {138 NSC_DEBUG_MSG(_T("Connecting to: ") + utf8::cvt<std::wstring>(host) + _T(" (") + utf8::cvt<std::wstring>(port) + _T(")"));139 tcp::resolver resolver(io_service_);140 tcp::resolver::query query(host, port);141 142 tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);143 tcp::resolver::iterator end;144 145 boost::system::error_code error = boost::asio::error::host_not_found;146 while (error && endpoint_iterator != end) {147 tcp::resolver::endpoint_type ep = *endpoint_iterator;148 socket_->close();149 socket_->connect(*endpoint_iterator++, error);150 NSC_DEBUG_MSG(_T("Connected to: ") + utf8::cvt<std::wstring>(ep.address().to_string()));151 }152 if (error) {153 NSC_DEBUG_MSG(_T("Failed to connect to:") + utf8::to_unicode(host));154 throw boost::system::system_error(error);155 }156 }157 158 159 virtual void shutdown() {160 NSC_DEBUG_MSG(_T("Ending socket (gracefully)"));161 // Initiate graceful connection closure.162 boost::system::error_code ignored_ec;163 if (socket_)164 socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);165 };166 virtual void close() {167 if (socket_)168 socket_->close();169 socket_.reset();170 };171 172 virtual void send_nsca(const nsca::packet &packet, const boost::posix_time::seconds timeout) {173 if (!socket_ || !socket_->is_open()) {174 NSC_LOG_ERROR_STD(_T("Socket was closed when trying to send data..."));175 return;176 }177 std::string buffer = crypt_inst.get_rand_buffer(packet.get_packet_length());178 packet.get_buffer(buffer, time);179 crypt_inst.encrypt_buffer(buffer);180 NSC_DEBUG_MSG(_T("Sending data: ") + strEx::itos(buffer.size()));181 write_with_timeout(buffer, timeout);182 }183 virtual bool recv_iv(std::string password, int encryption_method, boost::posix_time::seconds timeout) {184 if (!socket_ || !socket_->is_open()) {185 NSC_LOG_ERROR_STD(_T("Socket was closed when trying to read data..."));186 return false;187 }188 unsigned int len = nsca::length::iv::get_packet_length();189 std::vector<char> buf(len);190 if (!read_with_timeout(buf, timeout)) {191 NSC_LOG_ERROR_STD(_T("Failed to read IV from server (using ") + strEx::itos(encryption_method) + _T(", ") + strEx::itos(len) + _T(")."));192 return false;193 }194 nsca::iv_packet iv_packet(std::string(buf.begin(), buf.end()));195 std::string iv = iv_packet.get_iv();196 time = iv_packet.get_time();197 NSC_DEBUG_MSG(_T("Encrypting using: ") + utf8::cvt<std::wstring>(nsca::nsca_encrypt::helpers::encryption_to_string(encryption_method)) + _T(", password '") + utf8::cvt<std::wstring>(password) + _T("'"));198 crypt_inst.encrypt_init(password, encryption_method, iv);199 return true;200 }201 virtual bool read_with_timeout(std::vector<char> &buf, boost::posix_time::seconds timeout) {202 return socket_helpers::io::read_with_timeout(*socket_, *socket_, boost::asio::buffer(buf), timeout);203 }204 virtual void write_with_timeout(std::string &buf, boost::posix_time::seconds timeout) {205 socket_helpers::io::write_with_timeout(*socket_, *socket_, boost::asio::buffer(buf), timeout);206 }207 };208 }209 */
Note: See TracChangeset
for help on using the changeset viewer.








