Changeset 4632ff7 in nscp


Ignore:
Timestamp:
08/22/11 21:43:56 (22 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
0b8df3e
Parents:
bd18eb2
Message:
  • Fixed so it works on linux (fixed issues with timeout)
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • changelog

    rbd18eb2 r4632ff7  
    1010 * Fixed so it builds on linux 
    1111 * Fixed so automagic plugin renaming add lib<LIB name> on *nix. 
     12 * Fixed so it works on linux (fixed issues with timeout) 
    1213 
    13142011-08-16 MickeM 
  • include/nscp/client/socket.hpp

    r438998b r4632ff7  
    5959 
    6060    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); 
    6263      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        } 
    6468      } 
     69      writer->stop_timer(); 
     70      writer.reset(); 
    6571    } 
    6672    virtual std::list<nscp::packet::nscp_chunk> recv(boost::posix_time::seconds timeout) { 
     73      int left = 1; 
    6774      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 
    73101      get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); 
    74102      get_socket().close(); 
    75       */ 
    76       return chunks; //nscp::packet(&buf[0], buf.size(), packet.get_payload_length()); 
     103      return chunks; 
    77104    } 
    78105  }; 
     
    108135 
    109136    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); 
    111139      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; 
    114142          return; 
    115143        } 
    116144      } 
     145      writer->stop_timer(); 
     146      writer.reset(); 
    117147    } 
    118148 
     
    120150      int left = 1; 
    121151      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); 
    123154      while (left > 0) { 
    124155        nscp::packet::nscp_chunk chunk; 
    125156        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))) { 
    127158          get_socket().close(); 
    128159          std::cout << "Timeout (sig)..." << std::endl; 
     
    130161        } 
    131162        chunk.read_signature(buf); 
    132         std::wcout << _T("---> ") << chunk.signature.to_wstring() << std::endl; 
     163        std::wcout << chunk.signature.to_wstring() << std::endl; 
    133164        buf.resize(chunk.signature.payload_length); 
    134165 
    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))) { 
    136167          get_socket().close(); 
    137168          std::cout << "Timeout (pl)..." << std::endl; 
     
    142173        left = chunk.signature.additional_packet_count; 
    143174      } 
     175      reader->stop_timer(); 
     176      reader.reset(); 
    144177 
    145178      get_socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both); 
  • include/socket/socket_helpers.hpp

    rbd18eb2 r4632ff7  
    55#include <boost/bind.hpp> 
    66#include <boost/optional.hpp> 
     7#include <boost/shared_ptr.hpp> 
     8#include <boost/enable_shared_from_this.hpp> 
    79 
    810#include <unicode_char.hpp> 
     
    117119    void set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b); 
    118120 
    119     struct timed_writer : boost::noncopyable { 
     121    struct timed_writer : public boost::enable_shared_from_this<timed_writer> { 
    120122      boost::asio::io_service &io_service; 
    121123      boost::posix_time::time_duration duration; 
     
    125127      boost::optional<boost::system::error_code> read_result; 
    126128 
    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) { 
    131134        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() { 
    135138        timer.cancel(); 
    136139      } 
    137140 
    138141      template <typename AsyncWriteStream, typename MutableBufferSequence> 
    139       void write(AsyncWriteStream& socket, MutableBufferSequence &buffer) { 
    140         async_write(socket, buffer, boost::bind(set_result, &read_result, _1)); 
    141       } 
    142  
    143       template <typename AsyncWriteStream, typename RawSocket, typename MutableBufferSequence> 
    144       bool write_and_wait(AsyncWriteStream& sock, RawSocket& rawSocket, const MutableBufferSequence& buffer) { 
    145         write(sock, 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) { 
    151154        io_service.reset(); 
    152155        while (io_service.run_one()) { 
    153156          if (read_result) { 
     157            read_result.reset(); 
    154158            return true; 
    155159          } 
     
    160164        } 
    161165      } 
     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 
    162172    }; 
    163173 
     
    186196 
    187197 
    188     struct timed_reader : boost::noncopyable { 
     198    struct timed_reader : public boost::enable_shared_from_this<timed_reader> { 
    189199      boost::asio::io_service &io_service; 
    190200      boost::posix_time::time_duration duration; 
     
    194204      boost::optional<boost::system::error_code> write_result; 
    195205 
    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) { 
    200212        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() { 
    204216        timer.cancel(); 
    205217      } 
    206218 
    207219      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) { 
    218231        io_service.reset(); 
    219232        while (io_service.run_one()) { 
    220233          if (write_result) { 
    221             std::cout << "---read---" << std::endl; 
    222             //timer.cancel(); 
     234            write_result.reset(); 
    223235            return true; 
    224236          } 
    225237          else if (timer_result) { 
    226             std::cout << "---timer (read)---" << std::endl; 
    227             //socket.close(); 
     238            socket.close(); 
    228239            return false; 
    229240          } 
    230241        } 
    231242      } 
     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 
    232248    }; 
    233249 
  • modules/NSCPClient/NSCPClient.cpp

    r438998b r4632ff7  
    250250      return nscp_result_data(NSCAPI::returnUNKNOWN, _T("SSL support not available (compiled without USE_SSL)!")); 
    251251#endif 
    252     } else 
     252    } else { 
    253253      chunks = send_nossl(con.host, con.port, con.timeout, chunks); 
     254    } 
    254255    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()))); 
    257257    } 
    258258    return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Hello")); 
    259259  } catch (nscp::nscp_exception &e) { 
     260    NSC_LOG_ERROR_STD(_T("Socket error: ") + e.getMessage()); 
    260261    return nscp_result_data(NSCAPI::returnUNKNOWN, _T("NSCP Packet error: ") + e.getMessage()); 
    261262  } catch (std::runtime_error &e) { 
    262263    NSC_LOG_ERROR_STD(_T("Socket error: ") + utf8::cvt<std::wstring>(e.what())); 
    263264    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...")); 
    265270    return nscp_result_data(NSCAPI::returnUNKNOWN, _T("Unknown error -- REPORT THIS!")); 
    266271  } 
Note: See TracChangeset for help on using the changeset viewer.