1

Topic: The synchronous client on boost:: asio

Kind time of days! Actually, the client is written and works, it would be desirable to specify separate details. The client synchronous since it is used as transport for modbus-tcp and to fence synchronism through asynchronous calls it somehow strange but if the need forces, it is necessary. Made synchronous read/write with  +  at  and a method for waiting of arrival of the data. It would be desirable to receive recommendations about possible magnification of high-speed performance of the code since one request/answer occupies in  33 msec, and if in the project, say, one thousand variables () it already 33  that is unacceptable. Now the code. A method for connection and reconnection. In ' to ' very approximate interval of expended time registers. bool try_connect (const std::string& ip, uint32_t port, int32_t n_times, uint32_t* to = NULL) {std:: stringstream ss; ss <<port; tcp:: resolver:: query query (ip, ss.str ()); tcp:: resolver:: iterator iter = tcp:: : resolver (*m_io).resolve (query); for (int32_t i = 0; i <n_times; i ++) {try {m_sock-> close (); m_sock-> connect (*iter);} catch (std::exception& e) {boost:: this_thread:: sleep (boost:: posix_time:: millisec (5 * (i+1))); if (to! = NULL) *to + = 5 * (i + 1); continue;} if (m_sock-> is_open ()) return true;} return false;} the Method of waiting of the data. The profiler from VS says that on this method 80 % of time leave. bool wait_for_data (uint32_t size_in_bytes, uint32_t timeout, bool try_reconnect) {if (size_in_bytes == 0) return true; uint32_t to = 0u; if (! m_sock-> is_open ()) {if (try_reconnect) {if (! try_connect (m_ip, m_port, 3u, &to)) return false;} } if (! m_sock-> is_open ()) return false; if (bytes_available (*m_sock)> = size_in_bytes) return true; while (to <= timeout) {if (bytes_available (*m_sock)> = size_in_bytes) return true; boost:: this_thread:: sleep (boost:: posix_time:: millisec (1)); to ++;} return false;} the Amount of the data ready for reading: size_t bytes_available (boost::asio::ip::tcp::socket& s) {boost:: asio:: socket_base:: bytes_readable command (true); s.io_control (command); return command.get ();} Met often such implementation : void configure_socket_to (boost::asio::ip::tcp::socket& socket, uint32_t ms) {#if defined (WIN32) int32_t timeout = ms; setsockopt (socket.native (), SOL_SOCKET, SO_RCVTIMEO, (const char *) &timeout, sizeof (timeout)); setsockopt (socket.native (), SOL_SOCKET, SO_SNDTIMEO, (const char *) &timeout, sizeof (timeout)); #else struct timeval tv; tv.tv_sec = 0; tv.tv_usec = ms; setsockopt (socket.native (), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)); setsockopt (socket.native (), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof (tv)); #endif} At me this code did not earn at least on Windows. Well and actually data reading: bool receive (void* out, const size_t sz) {if (! m_sock-> is_open ()) if (! try_connect ()) return false;//- - connected-//if (bytes_available (*m_sock) <sz) if (! wait_for_data (sz, m_to, true)) return false; //- - have enough data in port-//try {m_sock-> read_some (boost:: asio:: buffer (out, sz));} catch (std::exception& e) {std:: cerr <<e.what () <<std:: endl; return false;} return true;} I Will be glad to any remarks and sentences. Thanks!

2

Re: The synchronous client on boost:: asio

Hello, plastictown, you wrote: P> the Client synchronous since it is used as transport for modbus-tcp and to fence synchronism through asynchronous calls it somehow strange but if the need forces, it is necessary.

3

Re: The synchronous client on boost:: asio

Hello, antropolog, you wrote: P>> the Client synchronous since it is used as transport for modbus-tcp and to fence synchronism through asynchronous calls it somehow strange but if the need forces, it is necessary. A> * I the artist, I so see * It generally my first experience with networks, therefore it is possible all the same on business?

4

Re: The synchronous client on boost:: asio

Hello, plastictown, you wrote: P> * I the artist, I so see P> * It generally my first experience with networks, therefore it is possible all the same on business? The network is an asynchronous environment. Start with it from the very beginning.  in the synchronous calls leave as a prerogative of the uppermost layer (i.e. that who uses yours ) is better.