]> git.mxchange.org Git - flightgear.git/blobdiff - scripts/example/fgfsclient.cxx
Merge branches 'torsten/warn-atc' and 'tortsen/warn-morse'
[flightgear.git] / scripts / example / fgfsclient.cxx
index fa4012f38187f512aa4a04bfd00fe54b229f6a4f..27e8d83975d82456d8c29d0d03bdcf0e23150bc7 100644 (file)
@@ -1,5 +1,7 @@
 // $Id$
 // g++ -O2 -g -pedantic -Wall fgfsclient.cxx -o fgfsclient -lstdc++
+// USAGE: ./fgfsclient [hostname [port]]
+// Public Domain
 
 #include <errno.h>
 #include <iostream>
 #include <unistd.h>
 #include <string.h>
 
-const int maxlen = 256;
+
+const char *HOST = "localhost";
+const unsigned PORT = 5501;
+const int BUFLEN = 256;
 
 
 class FGFSSocket {
-       int             sock;
-       bool            connected;
-       unsigned        timeout;
-    public:
-                       FGFSSocket(const char *name, const unsigned port);
-                       ~FGFSSocket() { close(); };
+public:
+       FGFSSocket(const char *name, unsigned port);
+       ~FGFSSocket();
 
        int             write(const char *msg, ...);
-       const char      *read(void);
-       inline void     flush(void);
-       void            settimeout(unsigned t) { timeout = t; };
-    private:
+       const char      *read(void);
+       inline void     flush(void);
+       void            settimeout(unsigned t) { _timeout = t; }
+
+private:
        int             close(void);
+
+       int             _sock;
+       bool            _connected;
+       unsigned        _timeout;
+       char            _buffer[BUFLEN];
 };
 
 
-FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port = 5501)
-       :
-       sock(-1),
-       connected(false),
-       timeout(1)
+FGFSSocket::FGFSSocket(const char *hostname = HOST, unsigned port = PORT) :
+       _sock(-1),
+       _connected(false),
+       _timeout(1)
 {
-       sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
-       if (sock < 0)
+       _sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+       if (_sock < 0)
                throw("FGFSSocket/socket");
 
        struct hostent *hostinfo;
@@ -56,11 +63,11 @@ FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port =
        serv_addr.sin_port = htons(port);
        serv_addr.sin_addr = *(struct in_addr *)hostinfo->h_addr;
 
-       if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
+       if (connect(_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
                close();
                throw("FGFSSocket/connect");
        }
-       connected = true;
+       _connected = true;
        try {
                write("data");
        } catch (...) {
@@ -70,14 +77,20 @@ FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port =
 }
 
 
+FGFSSocket::~FGFSSocket()
+{
+       close();
+}
+
+
 int FGFSSocket::close(void)
 {
-       if (connected)
+       if (_connected)
                write("quit");
-       if (sock < 0)
+       if (_sock < 0)
                return 0;
-       int ret = ::close(sock);
-       sock = -1;
+       int ret = ::close(_sock);
+       _sock = -1;
        return ret;
 }
 
@@ -86,24 +99,24 @@ int FGFSSocket::write(const char *msg, ...)
 {
        va_list va;
        ssize_t len;
-       char buf[maxlen];
+       char buf[BUFLEN];
        fd_set fd;
        struct timeval tv;
 
        FD_ZERO(&fd);
-       FD_SET(sock, &fd);
-       tv.tv_sec = timeout;
+       FD_SET(_sock, &fd);
+       tv.tv_sec = _timeout;
        tv.tv_usec = 0;
        if (!select(FD_SETSIZE, 0, &fd, 0, &tv))
                throw("FGFSSocket::write/select: timeout exceeded");
 
        va_start(va, msg);
-       vsprintf(buf, msg, va);
+       vsnprintf(buf, BUFLEN - 2, msg, va);
        va_end(va);
        std::cout << "SEND: " << buf << std::endl;
        strcat(buf, "\015\012");
 
-       len = ::write(sock, buf, strlen(buf));
+       len = ::write(_sock, buf, strlen(buf));
        if (len < 0)
                throw("FGFSSocket::write");
        return len;
@@ -112,44 +125,43 @@ int FGFSSocket::write(const char *msg, ...)
 
 const char *FGFSSocket::read(void)
 {
-       static char buf[maxlen];
        char *p;
        fd_set fd;
        struct timeval tv;
        ssize_t len;
 
        FD_ZERO(&fd);
-       FD_SET(sock, &fd);
-       tv.tv_sec = timeout;
+       FD_SET(_sock, &fd);
+       tv.tv_sec = _timeout;
        tv.tv_usec = 0;
        if (!select(FD_SETSIZE, &fd, 0, 0, &tv)) {
-               if (timeout == 0)
+               if (_timeout == 0)
                        return 0;
                else
                        throw("FGFSSocket::read/select: timeout exceeded");
        }
 
-       len = ::read(sock, buf, maxlen - 1);
+       len = ::read(_sock, _buffer, BUFLEN - 1);
        if (len < 0)
                throw("FGFSSocket::read/read");
        if (len == 0)
                return 0;
 
-       for (p = &buf[len - 1]; p >= buf; p--)
+       for (p = &_buffer[len - 1]; p >= _buffer; p--)
                if (*p != '\015' && *p != '\012')
                        break;
        *++p = '\0';
-       return strlen(buf) ? buf : 0;
+       return strlen(_buffer) ? _buffer : 0;
 }
 
 
 inline void FGFSSocket::flush(void)
 {
-       int i = timeout;
-       timeout = 0;
+       int i = _timeout;
+       _timeout = 0;
        while (read())
                ;
-       timeout = i;
+       _timeout = i;
 }
 
 
@@ -166,15 +178,15 @@ try {
        const char *p = f.read();
        if (p)
                std::cout << "RECV: " << p << std::endl;
-       return 0;
+       return EXIT_SUCCESS;
 
 } catch (const char s[]) {
        std::cerr << "Error: " << s << ": " << strerror(errno) << std::endl;
-       return -1;
+       return EXIT_FAILURE;
 
 } catch (...) {
        std::cerr << "Error: unknown exception" << std::endl;
-       return -1;
+       return EXIT_FAILURE;
 }