Changeset 4632ff7 in nscp
- Timestamp:
- 08/22/11 21:43:56 (22 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 0b8df3e
- Parents:
- bd18eb2
- Files:
-
- 4 edited
-
changelog (modified) (1 diff)
-
include/nscp/client/socket.hpp (modified) (5 diffs)
-
include/socket/socket_helpers.hpp (modified) (6 diffs)
-
modules/NSCPClient/NSCPClient.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
changelog
rbd18eb2 r4632ff7 10 10 * Fixed so it builds on linux 11 11 * Fixed so automagic plugin renaming add lib<LIB name> on *nix. 12 * Fixed so it works on linux (fixed issues with timeout) 12 13 13 14 2011-08-16 MickeM -
include/nscp/client/socket.hpp
r438998b r4632ff7 59 59 60 60 virtual void send(std::list<nscp::packet::nscp_chunk> &chunks, boost::posix_time::seconds timeout) { 61 socket_helpers::io::timed_writer writer(get_io_service(), timeout); 61 boost::shared_ptr<socket_helpers::io::timed_writer> writer(new socket_helpers::io::timed_writer(get_io_service())); 62 writer->start_timer(timeout); 62 63 BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, chunks) { 63 writer.write_and_wait(*socket_, get_socket(), boost::asio::buffer(chunk.to_buffer())); 64 if (!writer->write_and_wait(*socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) { 65 std::cout << "FaILED TO SEND DATA..." << std::endl; 66 return; 67 } 64 68 } 69 writer->stop_timer(); 70 writer.reset(); 65 71 } 66 72 virtual std::list<nscp::packet::nscp_chunk> recv(boost::posix_time::seconds timeout) { 73 int left = 1; 67 74 std::list<nscp::packet::nscp_chunk> chunks; 68 /* 69 socket_helpers::io::timed_reader reader(get_io_service(), timeout); 70 std::vector<char> buf(sizeof(nscp::data::signature_packet)); 71 reader.read_and_wait(*socket_, boost::asio::buffer(buf)); 72 std::cout << "read: " << strEx::format_buffer(buf) << std::endl; 75 boost::shared_ptr<socket_helpers::io::timed_reader> reader(new socket_helpers::io::timed_reader(get_io_service())); 76 reader->start_timer(timeout); 77 while (left > 0) { 78 nscp::packet::nscp_chunk chunk; 79 std::vector<char> buf(sizeof(nscp::data::signature_packet)); 80 if (!reader->read_and_wait(*socket_, get_socket(), boost::asio::buffer(buf))) { 81 get_socket().close(); 82 std::cout << "Timeout (sig)..." << std::endl; 83 return chunks; 84 } 85 chunk.read_signature(buf); 86 std::wcout << chunk.signature.to_wstring() << std::endl; 87 buf.resize(chunk.signature.payload_length); 88 89 if (!reader->read_and_wait(*socket_, get_socket(), boost::asio::buffer(buf))) { 90 get_socket().close(); 91 std::cout << "Timeout (pl)..." << std::endl; 92 return chunks; 93 } 94 chunk.read_payload(buf); 95 chunks.push_back(chunk); 96 left = chunk.signature.additional_packet_count; 97 } 98 reader->stop_timer(); 99 reader.reset(); 100 73 101 get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); 74 102 get_socket().close(); 75 */ 76 return chunks; //nscp::packet(&buf[0], buf.size(), packet.get_payload_length()); 103 return chunks; 77 104 } 78 105 }; … … 108 135 109 136 virtual void send(std::list<nscp::packet::nscp_chunk> &chunks, boost::posix_time::seconds timeout) { 110 socket_helpers::io::timed_writer writer(get_io_service(), timeout); 137 boost::shared_ptr<socket_helpers::io::timed_writer> writer(new socket_helpers::io::timed_writer(get_io_service())); 138 writer->start_timer(timeout); 111 139 BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, chunks) { 112 if (!writer .write_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) {113 get_socket().close();140 if (!writer->write_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(chunk.to_buffer()))) { 141 std::cout << "FaILED TO SEND DATA..." << std::endl; 114 142 return; 115 143 } 116 144 } 145 writer->stop_timer(); 146 writer.reset(); 117 147 } 118 148 … … 120 150 int left = 1; 121 151 std::list<nscp::packet::nscp_chunk> chunks; 122 socket_helpers::io::timed_reader reader(get_io_service(), timeout); 152 boost::shared_ptr<socket_helpers::io::timed_reader> reader(new socket_helpers::io::timed_reader(get_io_service())); 153 reader->start_timer(timeout); 123 154 while (left > 0) { 124 155 nscp::packet::nscp_chunk chunk; 125 156 std::vector<char> buf(sizeof(nscp::data::signature_packet)); 126 if (!reader .read_and_wait(*ssl_socket_, boost::asio::buffer(buf))) {157 if (!reader->read_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(buf))) { 127 158 get_socket().close(); 128 159 std::cout << "Timeout (sig)..." << std::endl; … … 130 161 } 131 162 chunk.read_signature(buf); 132 std::wcout << _T("---> ") <<chunk.signature.to_wstring() << std::endl;163 std::wcout << chunk.signature.to_wstring() << std::endl; 133 164 buf.resize(chunk.signature.payload_length); 134 165 135 if (!reader .read_and_wait(*ssl_socket_, boost::asio::buffer(buf))) {166 if (!reader->read_and_wait(*ssl_socket_, get_socket(), boost::asio::buffer(buf))) { 136 167 get_socket().close(); 137 168 std::cout << "Timeout (pl)..." << std::endl; … … 142 173 left = chunk.signature.additional_packet_count; 143 174 } 175 reader->stop_timer(); 176 reader.reset(); 144 177 145 178 get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); -
include/socket/socket_helpers.hpp
rbd18eb2 r4632ff7 5 5 #include <boost/bind.hpp> 6 6 #include <boost/optional.hpp> 7 #include <boost/shared_ptr.hpp> 8 #include <boost/enable_shared_from_this.hpp> 7 9 8 10 #include <unicode_char.hpp> … … 117 119 void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b); 118 120 119 struct timed_writer : boost::noncopyable{121 struct timed_writer : public boost::enable_shared_from_this<timed_writer> { 120 122 boost::asio::io_service &io_service; 121 123 boost::posix_time::time_duration duration; … … 125 127 boost::optional<boost::system::error_code> read_result; 126 128 127 timed_writer(boost::asio::io_service &io_service, boost::posix_time::time_duration duration) 128 : io_service(io_service) 129 , timer(io_service) 130 { 129 timed_writer(boost::asio::io_service& io_service) : io_service(io_service), timer(io_service) {} 130 ~timed_writer() { 131 timer.cancel(); 132 } 133 void start_timer(boost::posix_time::time_duration duration) { 131 134 timer.expires_from_now(duration); 132 timer.async_wait(boost::bind( set_result, &timer_result, _1));133 } 134 ~timed_writer() {135 timer.async_wait(boost::bind(&timed_writer::set_result, shared_from_this(), &timer_result, _1)); 136 } 137 void stop_timer() { 135 138 timer.cancel(); 136 139 } 137 140 138 141 template <typename AsyncWriteStream, typename MutableBufferSequence> 139 void write(AsyncWriteStream& s ocket, MutableBufferSequence &buffer) {140 async_write(s ocket, buffer, boost::bind(set_result, &read_result, _1));141 } 142 143 template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence>144 bool write_and_wait(AsyncWriteStream& s ock, RawSocket& rawSocket, const MutableBufferSequence& buffer) {145 write(s ock, buffer);146 return wait( rawSocket);147 } 148 149 template <typename RawSocket>150 bool wait( RawSocket& socket) {142 void write(AsyncWriteStream& stream, MutableBufferSequence &buffer) { 143 async_write(stream, buffer, boost::bind(&timed_writer::set_result, shared_from_this(), &read_result, _1)); 144 } 145 146 template <typename AsyncWriteStream, typename Socket, typename MutableBufferSequence> 147 bool write_and_wait(AsyncWriteStream& stream, Socket& socket, const MutableBufferSequence& buffer) { 148 write(stream, buffer); 149 return wait(socket); 150 } 151 152 template<typename Socket> 153 bool wait(Socket& socket) { 151 154 io_service.reset(); 152 155 while (io_service.run_one()) { 153 156 if (read_result) { 157 read_result.reset(); 154 158 return true; 155 159 } … … 160 164 } 161 165 } 166 167 void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code ec) { 168 if (!ec) 169 a->reset(ec); 170 } 171 162 172 }; 163 173 … … 186 196 187 197 188 struct timed_reader : boost::noncopyable{198 struct timed_reader : public boost::enable_shared_from_this<timed_reader> { 189 199 boost::asio::io_service &io_service; 190 200 boost::posix_time::time_duration duration; … … 194 204 boost::optional<boost::system::error_code> write_result; 195 205 196 timed_reader(boost::asio::io_service &io_service, boost::posix_time::time_duration duration) 197 : io_service(io_service) 198 , timer(io_service) 199 { 206 timed_reader(boost::asio::io_service &io_service) : io_service(io_service), timer(io_service) {} 207 ~timed_reader() { 208 timer.cancel(); 209 } 210 211 void start_timer(boost::posix_time::time_duration duration) { 200 212 timer.expires_from_now(duration); 201 timer.async_wait(boost::bind( set_result, &timer_result, _1));202 } 203 ~timed_reader() {213 timer.async_wait(boost::bind(&timed_reader::set_result, shared_from_this(), &timer_result, _1)); 214 } 215 void stop_timer() { 204 216 timer.cancel(); 205 217 } 206 218 207 219 template <typename AsyncWriteStream, typename MutableBufferSequence> 208 void read(AsyncWriteStream& socket, const MutableBufferSequence &buffers) { 209 async_read(socket, buffers, boost::bind(set_result, &write_result, _1)); 210 } 211 212 template <typename AsyncWriteStream, typename MutableBufferSequence> 213 bool read_and_wait(AsyncWriteStream& sock, const MutableBufferSequence& buffers) { 214 read(sock, buffers); 215 return wait(); 216 } 217 bool wait() { 220 void read(AsyncWriteStream& stream, const MutableBufferSequence &buffers) { 221 async_read(stream, buffers, boost::bind(&timed_reader::set_result, shared_from_this(), &write_result, _1)); 222 } 223 224 template <typename AsyncWriteStream, typename Socket, typename MutableBufferSequence> 225 bool read_and_wait(AsyncWriteStream& stream, Socket& socket, const MutableBufferSequence& buffers) { 226 read(stream, buffers); 227 return wait(socket); 228 } 229 template <typename Socket> 230 bool wait(Socket& socket) { 218 231 io_service.reset(); 219 232 while (io_service.run_one()) { 220 233 if (write_result) { 221 std::cout << "---read---" << std::endl; 222 //timer.cancel(); 234 write_result.reset(); 223 235 return true; 224 236 } 225 237 else if (timer_result) { 226 std::cout << "---timer (read)---" << std::endl; 227 //socket.close(); 238 socket.close(); 228 239 return false; 229 240 } 230 241 } 231 242 } 243 void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code ec) { 244 if (!ec) 245 a->reset(ec); 246 } 247 232 248 }; 233 249 -
modules/NSCPClient/NSCPClient.cpp
r438998b r4632ff7 250 250 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("SSL support not available (compiled without USE_SSL)!")); 251 251 #endif 252 } else 252 } else { 253 253 chunks = send_nossl(con.host, con.port, con.timeout, chunks); 254 } 254 255 BOOST_FOREACH(nscp::packet::nscp_chunk &chunk, chunks) { 255 std::cout << "Found chunk: " << strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size()) << std::endl; 256 256 NSC_DEBUG_MSG_STD(_T("Found chunk: ") + utf8::cvt<std::wstring>(strEx::format_buffer(chunk.payload.c_str(), chunk.payload.size()))); 257 257 } 258 258 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Hello")); 259 259 } catch (nscp::nscp_exception &e) { 260 NSC_LOG_ERROR_STD(_T("Socket error: ") + e.getMessage()); 260 261 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("NSCP Packet error: ") + e.getMessage()); 261 262 } catch (std::runtime_error &e) { 262 263 NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 263 264 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 264 } catch (...) { 265 } catch (std::exception &e) { 266 NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); 267 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 268 } catch (...) { 269 NSC_LOG_ERROR_STD(_T("Unknown exception...")); 265 270 return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Unknown error -- REPORT THIS!")); 266 271 }
Note: See TracChangeset
for help on using the changeset viewer.








