Changeset 20088
- Timestamp:
- 04/21/21 00:19:54 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/gtcdust.cc
r19713 r20088 13 13 #include "StateMachineDim.h" 14 14 #include "StateMachineAsio.h" 15 #include "Connection .h"15 #include "ConnectionSSL.h" 16 16 #include "LocalControl.h" 17 17 #include "Configuration.h" … … 32 32 // ------------------------------------------------------------------------ 33 33 34 class ConnectionGTC : public Connection 34 class ConnectionGTC : public ConnectionSSL 35 35 { 36 36 uint16_t fInterval; … … 45 45 Time fLastReception; 46 46 47 boost::asio::streambuf fBuffer; 47 // boost::asio::streambuf fBuffer; 48 boost::array<char, 4096> fArray; 48 49 string fData; 49 50 … … 54 55 void ProcessAnswer(string s) 55 56 { 57 std::stringstream ss; 58 pt::ptree tree; 56 59 try 57 60 { 58 std::stringstream ss;59 61 ss << s; 60 61 pt::ptree tree;62 62 pt::read_json(ss, tree); 63 63 64 } 65 catch (std::exception const& e) 66 { 67 Error(string("Parsing JSON failed: ")+e.what()); 68 } 64 69 // {"pm25":{"Value":"13.9000","Date":"2019-10-03 10:31:40"}} 70 try 71 { 65 72 66 73 const auto &pm25 = tree.get_child("pm25"); … … 80 87 catch (std::exception const& e) 81 88 { 82 Error(string(" Parsing JSON failed: ")+e.what());89 Error(string("Illformated report: ")+e.what()); 83 90 } 84 91 } … … 92 99 // the connection being closed by the server. 93 100 // I do not understand why bytes_received is 0 then. 94 // 95 // Extract whatever is left in the buffer 96 istream is(&fBuffer); 97 string buffer; 98 getline(is, buffer); 99 fData += buffer; 100 101 if (fIsVerbose) 102 Out() << "EOF|" << buffer << endl; 103 104 if (err==ba::error::eof) 101 if (err==ba::error::eof || err==ba::ssl::error::stream_truncated) 105 102 { 103 if (fIsVerbose) 104 Out() << "EOF" << endl; 105 106 106 // Does the message contain a header? 107 107 const size_t p1 = fData.find("\r\n\r\n"); … … 113 113 114 114 PostClose(false); 115 116 115 return; 117 116 } … … 119 118 // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category)) 120 119 // 125: Operation canceled 121 if (err && err!=ba::error::eof &&// Connection closed by remote host120 if (err && // Connection closed by remote host 122 121 err!=ba::error::basic_errors::not_connected && // Connection closed by remote host 123 122 err!=ba::error::basic_errors::operation_aborted) // Connection closed by us … … 133 132 fLastReception = Time(); 134 133 135 istream is(&fBuffer); 136 137 string buffer; 138 if (!getline(is, buffer, '\n')) 139 { 140 Fatal("Received message does not contain \\n... closing connection."); 141 PostClose(false); 142 return; 143 } 144 134 const string buffer(fArray.data(), bytes_received); 145 135 if (fIsVerbose) 146 136 Out() << bytes_received << "|" << buffer << endl; 147 148 137 fData += buffer; 149 fData += '\n'; 150 151 StartReadLine(); 152 } 153 154 void StartReadLine() 138 139 StartRead(); 140 } 141 142 void StartRead() 155 143 { 156 144 // The last report (payload) is simply termined by the connection being closed by the server. 157 async_read_until(*this, fBuffer, '\n',158 boost::bind(&ConnectionGTC::HandleRead, this,159 dummy::error, dummy::bytes_transferred));145 stream->async_read_some(ba::buffer(fArray), 146 boost::bind(&ConnectionGTC::HandleRead, this, 147 dummy::error, dummy::bytes_transferred)); 160 148 } 161 149 … … 204 192 } 205 193 206 if ( !is_open())194 if (IsClosed()) 207 195 { 208 196 // For example: Here we could schedule a new accept if we … … 228 216 { 229 217 Request(); 230 StartRead Line();218 StartRead(); 231 219 } 232 220 233 221 public: 234 ConnectionGTC(ba::io_service& ioservice, MessageImp &imp) : Connection (ioservice, imp()),222 ConnectionGTC(ba::io_service& ioservice, MessageImp &imp) : ConnectionSSL(ioservice, imp()), 235 223 fIsVerbose(true), fLastReport(Time::none), fLastReception(Time::none), fKeepAlive(ioservice) 236 224 { … … 241 229 { 242 230 fIsVerbose = b; 243 Connection ::SetVerbose(b);231 ConnectionSSL::SetVerbose(b); 244 232 } 245 233
Note:
See TracChangeset
for help on using the changeset viewer.