Ignore:
Timestamp:
12/23/22 09:09:50 (2 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

The C++ client API can connect over TCP

Location:
src/client/c/hu/varadiistvan/xplra
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/client/c/hu/varadiistvan/xplra/XPlane.cc

    r107 r110  
    3535
    3636#include <hu/varadiistvan/scpl/io/LocalClientSocket.h>
     37#include <hu/varadiistvan/scpl/io/TCPClientSocket.h>
    3738#include <hu/varadiistvan/scpl/io/DataStream.h>
    3839
     
    5253
    5354using hu::varadiistvan::scpl::io::LocalClientSocket;
    54 using hu::varadiistvan::scpl::io::LocalConnector;
     55using hu::varadiistvan::scpl::io::TCPClientSocket;
    5556using hu::varadiistvan::scpl::io::DataStream;
    5657
     
    6061using std::string;
    6162using std::min;
     63
     64//------------------------------------------------------------------------------
     65
     66const unsigned short XPlane::defaultTCPPort;
     67
     68//------------------------------------------------------------------------------
     69
     70template <class ClientSocket>
     71void XPlane::connect(std::unique_ptr<ClientSocket> clientSocket)
     72{
     73    auto& connector = clientSocket->getConnector();
     74
     75    while (!connector.connect()) {
     76        if (connector.failed()) {
     77            throw IOException(connector.getErrorCode());
     78        }
     79        waiter.wait();
     80        if (waiter.failed()) {
     81            throw IOException(waiter.getErrorCode());
     82        }
     83    }
     84
     85#ifndef _WIN32
     86    signal(SIGPIPE, SIG_IGN);
     87#endif
     88
     89    socket = clientSocket.release();
     90    stream = new DataStream(*socket);
     91
     92    for(multiBuffers_t::iterator i = multiBuffers.begin();
     93        i!=multiBuffers.end(); ++i)
     94    {
     95        MultiBuffer* buffer = *i;
     96        buffer->reregisterInXPlane();
     97    }
     98}
    6299
    63100//------------------------------------------------------------------------------
     
    150187    unique_ptr<LocalClientSocket> clientSocket(new LocalClientSocket("xplra",
    151188                                                                     &waiter));
    152     LocalConnector& connector = clientSocket->getConnector();
    153 
    154     while (!connector.connect()) {
    155         if (connector.failed()) {
    156             throw IOException(connector.getErrorCode());
    157         }
    158         waiter.wait();
    159         if (waiter.failed()) {
    160             throw IOException(waiter.getErrorCode());
    161         }
    162     }
    163 
    164 #ifndef _WIN32
    165     signal(SIGPIPE, SIG_IGN);
    166 #endif
    167 
    168     socket = clientSocket.release();
    169     stream = new DataStream(*socket);
    170 
    171     for(multiBuffers_t::iterator i = multiBuffers.begin();
    172         i!=multiBuffers.end(); ++i)
    173     {
    174         MultiBuffer* buffer = *i;
    175         buffer->reregisterInXPlane();
    176     }
     189    connect(std::move(clientSocket));
     190}
     191
     192//------------------------------------------------------------------------------
     193
     194void XPlane::connectTCP(const std::string& address, unsigned short port)
     195{
     196    if (socket!=0) return;
     197
     198    unique_ptr<TCPClientSocket> clientSocket(new TCPClientSocket(address.c_str(),
     199                                                                 port,
     200                                                                 &waiter));
     201    connect(std::move(clientSocket));
    177202}
    178203
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r107 r110  
    3636
    3737#include <set>
     38#include <memory>
    3839
    3940#include <inttypes.h>
     
    4344namespace hu { namespace varadiistvan { namespace scpl { namespace io {
    4445
    45 class LocalClientSocket;
     46class BufferedStream;
    4647class DataStream;
    4748
     
    6970private:
    7071    /**
     72     * The default TCP port.
     73     */
     74    static const unsigned short defaultTCPPort = 0x5852;
     75
     76    /**
    7177     * Type for the set of multi-dataref buffers.
    7278     */
     
    7985
    8086    /**
    81      * The local client socket over which we are communicating with X-Plane.
    82      */
    83     scpl::io::LocalClientSocket* socket;
     87     * The client socket as a stream, over which we are communicating with X-Plane.
     88     */
     89    scpl::io::BufferedStream* socket;
    8490
    8591    /**
     
    108114
    109115    /**
    110      * Connect to the simulator.
     116     * Connect to the simulator locally.
    111117     */
    112118    void connect();
     119
     120    /**
     121     * Connect to the simulator on TCP to the given address.
     122     */
     123    void connectTCP(const std::string& address,
     124                    unsigned short port = defaultTCPPort);
    113125
    114126    /**
     
    363375    void setArray(const char* name, uint8_t type, size_t length,
    364376                  size_t offset);
     377
     378    /**
     379     * Connect with the given socket.
     380     */
     381    template <class ClientSocket>
     382    void connect(std::unique_ptr<ClientSocket> clientSocket);
    365383
    366384    friend class MultiBuffer;
Note: See TracChangeset for help on using the changeset viewer.