10 #include <boost/bind.hpp> 14 namespace ba = boost::asio;
15 namespace bs = boost::system;
16 namespace dummy = ba::placeholders;
18 using ba::serial_port_base;
34 return fLog->Write(t, txt, qos);
41 ba::async_read(*
this, buffers,
43 dummy::error, dummy::bytes_transferred, type, counter));
48 ba::async_write(*
this, buffers,
50 dummy::error, dummy::bytes_transferred));
64 timer.expires_from_now(boost::posix_time::milliseconds(millisec));
66 timer.async_wait(boost::bind(handler,
this, dummy::error));
74 Info(
"Closing connection to "+URL()+
".");
79 if (ec && ec!=ba::error::basic_errors::bad_descriptor)
82 msg <<
"Cancel async requests on " << URL() <<
": " << ec.message() <<
" (" << ec <<
")";
92 msg <<
"Closing " << URL() <<
": " << ec.message() <<
" (" << ec <<
")";
96 Info(
"Closed connection to "+URL()+
" succesfully.");
101 fOutTimeout.cancel();
102 fConnectTimeout.cancel();
109 ofstream fout1(
"transmitted.txt", ios::app);
110 ofstream fout2(
"received.txt", ios::app);
111 ofstream fout3(
"send.txt", ios::app);
112 fout1 <<
Time() <<
": ---" << endl;
113 fout2 <<
Time() <<
": ---" << endl;
114 fout3 <<
Time() <<
": ---" << endl;
117 if (delay<0 || IsConnecting())
123 fConnectTimeout.expires_from_now(boost::posix_time::seconds(delay));
134 if (error==ba::error::basic_errors::operation_aborted)
141 str <<
"Reconnect timeout of " << URL() <<
": " << error.message() <<
" (" << error <<
")";
151 Error(
"HandleReconnectTimeout - "+URL()+
" is already open.");
159 if (fConnectTimeout.expires_at() > ba::deadline_timer::traits_type::now())
170 if (error==ba::error::basic_errors::operation_aborted)
177 str <<
"Write timeout of " << URL() <<
": " << error.message() <<
" (" << error <<
")";
195 if (fOutTimeout.expires_at() > ba::deadline_timer::traits_type::now())
198 Error(
"fOutTimeout has expired, writing data to "+URL());
205 if (error==ba::error::basic_errors::operation_aborted)
208 if (error && error != ba::error::not_connected)
211 str <<
"Writing to " << URL() <<
": " << error.message() <<
" (" << error <<
")";
218 if (error == ba::error::not_connected)
221 msg << n <<
" bytes could not be sent to " << URL() <<
" due to missing connection.";
227 fOutTimeout.cancel();
231 msg << n <<
" bytes successfully sent to " << URL();
236 ofstream fout(
"transmitted.txt", ios::app);
237 fout <<
Time() <<
": ";
238 for (
unsigned int i=0;
i<fOutQueue.front().size();
i++)
239 fout << hex << setfill(
'0') << setw(2) << (uint32_t)fOutQueue.front()[
i];
243 HandleTransmittedData(n);
261 AsyncWrite(ba::const_buffers_1(ptr, sz));
267 max = cmd.length()+1;
269 PostMessage(cmd.c_str(), min(cmd.length()+1, max));
276 Info(
"Connecting to "+URL()+
".");
284 msg <<
"Error opening " << URL() <<
"... " << ec.message() <<
" (" << ec <<
")";
290 Info(
"Connection established.");
294 Info(
"Setting Baud Rate");
295 set_option(fBaudRate);
296 Info(
"Setting Character Size");
297 set_option(fCharacterSize);
298 Info(
"Setting Parity");
300 Info(
"Setting Sop Bits");
301 set_option(fStopBits);
302 Info(
"Setting Flow control");
303 set_option(fFlowControl);
305 catch (
const bs::system_error &erc)
307 Error(
string(
"Setting connection options: ")+erc.what());
315 ConnectionEstablished();
320 if (fConnectionStatus>=1)
321 Warn(
"Connection or connection attempt in progress. New endpoint only valid for next connection.");
323 fAddress =
"/dev/"+addr;
328 MessageImp(out), ba::serial_port(ioservice), fLog(0),
330 fCharacterSize(8), fParity(parity::none), fStopBits(stop_bits::one),
331 fFlowControl(flow_control::none),
332 fInTimeout(ioservice), fOutTimeout(ioservice), fConnectTimeout(ioservice),
void SetEndpoint(const std::string &addr)
void PostMessage(const void *msg, size_t s=0)
void CloseImp(int64_t delay=0)
The base implementation of a distributed messaging system.
void AsyncWrite(const boost::asio::const_buffers_1 &buffers)
void AsyncWait(boost::asio::deadline_timer &timer, int millisec, void(ConnectionUSB::*handler)(const boost::system::error_code &))
Adds some functionality to boost::posix_time::ptime for our needs.
void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0)
virtual void HandleReceivedData(const boost::system::error_code &, size_t, int=0, int=0)
virtual int Write(const Time &time, const std::string &txt, int qos=kMessage)
void HandleSentData(const boost::system::error_code &error, size_t)
ConnectionUSB(boost::asio::io_service &io_service, std::ostream &out)
void HandleWriteTimeout(const boost::system::error_code &error)
void HandleReconnectTimeout(const boost::system::error_code &error)
void PostClose(int64_t delay=0)
static void handler(int conn_id, char *packet, int size, int status)
int Write(const Time &t, const std::string &txt, int qos=kInfo)