Changeset 20088


Ignore:
Timestamp:
04/21/21 00:19:54 (2 months ago)
Author:
tbretz
Message:
Updated, server uses SSL connection now.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/gtcdust.cc

    r19713 r20088  
    1313#include "StateMachineDim.h"
    1414#include "StateMachineAsio.h"
    15 #include "Connection.h"
     15#include "ConnectionSSL.h"
    1616#include "LocalControl.h"
    1717#include "Configuration.h"
     
    3232// ------------------------------------------------------------------------
    3333
    34 class ConnectionGTC : public Connection
     34class ConnectionGTC : public ConnectionSSL
    3535{
    3636    uint16_t fInterval;
     
    4545    Time fLastReception;
    4646
    47     boost::asio::streambuf fBuffer;
     47//    boost::asio::streambuf fBuffer;
     48    boost::array<char, 4096> fArray;
    4849    string fData;
    4950
     
    5455    void ProcessAnswer(string s)
    5556    {
     57        std::stringstream ss;
     58        pt::ptree tree;
    5659        try
    5760        {
    58             std::stringstream ss;
    5961            ss << s;
    60 
    61             pt::ptree tree;
    6262            pt::read_json(ss, tree);
    6363
     64        }
     65        catch (std::exception const& e)
     66        {
     67            Error(string("Parsing JSON failed: ")+e.what());
     68        }
    6469            // {"pm25":{"Value":"13.9000","Date":"2019-10-03 10:31:40"}}
     70        try
     71        {
    6572
    6673            const auto &pm25 = tree.get_child("pm25");
     
    8087        catch (std::exception const& e)
    8188        {
    82             Error(string("Parsing JSON failed: ")+e.what());
     89            Error(string("Illformated report: ")+e.what());
    8390        }
    8491    }
     
    9299            // the connection being closed by the server.
    93100            // 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)
    105102            {
     103                if (fIsVerbose)
     104                    Out() << "EOF" << endl;
     105
    106106                // Does the message contain a header?
    107107                const size_t p1 = fData.find("\r\n\r\n");
     
    113113
    114114                PostClose(false);
    115 
    116115                return;
    117116            }
     
    119118            // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
    120119            // 125: Operation canceled
    121             if (err && err!=ba::error::eof &&                     // Connection closed by remote host
     120            if (err &&                                            // Connection closed by remote host
    122121                err!=ba::error::basic_errors::not_connected &&    // Connection closed by remote host
    123122                err!=ba::error::basic_errors::operation_aborted)  // Connection closed by us
     
    133132        fLastReception = Time();
    134133
    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);
    145135        if (fIsVerbose)
    146136            Out() << bytes_received << "|" << buffer << endl;
    147 
    148137        fData += buffer;
    149         fData += '\n';
    150 
    151         StartReadLine();
    152     }
    153 
    154     void StartReadLine()
     138
     139        StartRead();
     140    }
     141
     142    void StartRead()
    155143    {
    156144        // 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));
    160148    }
    161149
     
    204192        }
    205193
    206         if (!is_open())
     194        if (IsClosed())
    207195        {
    208196            // For example: Here we could schedule a new accept if we
     
    228216    {
    229217        Request();
    230         StartReadLine();
     218        StartRead();
    231219    }
    232220
    233221public:
    234     ConnectionGTC(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
     222    ConnectionGTC(ba::io_service& ioservice, MessageImp &imp) : ConnectionSSL(ioservice, imp()),
    235223        fIsVerbose(true), fLastReport(Time::none), fLastReception(Time::none), fKeepAlive(ioservice)
    236224    {
     
    241229    {
    242230        fIsVerbose = b;
    243         Connection::SetVerbose(b);
     231        ConnectionSSL::SetVerbose(b);
    244232    }
    245233
Note: See TracChangeset for help on using the changeset viewer.