source: trunk/FACT++/src/ConnectionUSB.h@ 16879

Last change on this file since 16879 was 16768, checked in by tbretz, 11 years ago
There is no need to make the scheduling of the async write asynchronously, so I schedule the async write now directly; this also removed the send queue, which was primarily introduced to make the tranmission to the FTM serial, but this is not needed anymore; to keep track of the send queue size and for debugging, a counter for the messages in the send buffer has been introduced
File size: 3.4 KB
Line 
1#ifndef FACT_Connection
2#define FACT_Connection
3
4#include <list>
5#include <array>
6#include <string>
7
8#include <boost/asio.hpp>
9#include <boost/function.hpp>
10#include <boost/asio/deadline_timer.hpp>
11
12#include "MessageImp.h"
13
14class ConnectionUSB : public MessageImp, public boost::asio::serial_port
15{
16private:
17 MessageImp *fLog;
18
19 std::string fAddress;
20
21 boost::asio::serial_port_base::baud_rate fBaudRate; // unisgned int
22 boost::asio::serial_port_base::character_size fCharacterSize; // unisgned int
23 boost::asio::serial_port_base::parity fParity; // unisgned int
24 boost::asio::serial_port_base::stop_bits fStopBits; // unisgned int
25 boost::asio::serial_port_base::flow_control fFlowControl; // unisgned int
26
27 enum ConnectionStatus_t
28 {
29 kDisconnected = 0,
30 kConnecting = 1,
31 kConnected = 2,
32 };
33
34protected:
35 boost::asio::deadline_timer fInTimeout;
36
37private:
38 boost::asio::deadline_timer fOutTimeout;
39 boost::asio::deadline_timer fConnectTimeout;
40
41 size_t fQueueSize;
42
43 ConnectionStatus_t fConnectionStatus;
44
45public:
46 void SetLogStream(MessageImp *log) { fLog = log; }
47 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
48
49 // -------- Abbreviations for starting async tasks ---------
50
51 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0);
52 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
53 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
54 void (ConnectionUSB::*handler)(const boost::system::error_code&));
55
56protected:
57 void CloseImp(int64_t delay=0);
58
59private:
60 void ConnectImp(const boost::system::error_code& error,
61 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
62
63 void HandleWriteTimeout(const boost::system::error_code &error);
64 void HandleSentData(const boost::system::error_code& error, size_t);
65 void HandleReconnectTimeout(const boost::system::error_code &error);
66
67 int Write(const Time &t, const std::string &txt, int qos=kInfo);
68
69 virtual void ConnectionEstablished() { }
70
71public:
72 ConnectionUSB(boost::asio::io_service& io_service, std::ostream &out);
73
74 // ------------------------ connect --------------------------
75
76 void SetEndpoint(const std::string &addr);
77
78 void Connect();
79
80 // ------------------------ close --------------------------
81 void PostClose(int64_t delay=0);
82
83 // ------------------------ write --------------------------
84 void PostMessage(const void *msg, size_t s=0);
85 void PostMessage(const std::string &cmd, size_t s=-1);
86
87 template<typename T, size_t N>
88 void PostMessage(const std::array<T, N> &msg)
89 {
90 PostMessage(msg.begin(), msg.size()*sizeof(T));
91 }
92
93 template<typename T>
94 void PostMessage(const std::vector<T> &msg)
95 {
96 PostMessage(&msg[0], msg.size()*sizeof(T));
97 }
98
99 // ------------------------ others --------------------------
100
101 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0, int = 0) { }
102 virtual void HandleTransmittedData(size_t) { }
103 virtual void HandleReadTimeout(const boost::system::error_code&) { }
104
105 int IsClosed() const { return !is_open(); }
106
107 bool IsConnected() const { return fConnectionStatus==kConnected; }
108 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
109
110 std::string URL() const { return fAddress; }
111};
112
113#endif
Note: See TracBrowser for help on using the repository browser.