1
0
Fork 0

- don't use static buffer in class method

- prepend underscore to class member variable names
- cosmetics
This commit is contained in:
mfranz 2006-11-22 13:47:15 +00:00
parent 3f9013ce51
commit f29a6dbf33
2 changed files with 50 additions and 42 deletions

View file

@ -136,7 +136,7 @@ int main(int argc, char **argv)
sock = fgfsconnect(hostname, port); sock = fgfsconnect(hostname, port);
if (sock < 0) if (sock < 0)
return (EXIT_FAILURE); return EXIT_FAILURE;
fgfswrite(sock, "data"); fgfswrite(sock, "data");
fgfswrite(sock, "set /controls/engines/engine[%d]/throttle %d", 0, 1); fgfswrite(sock, "set /controls/engines/engine[%d]/throttle %d", 0, 1);

View file

@ -15,34 +15,37 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
const int maxlen = 256;
const int MAXLEN = 256;
class FGFSSocket { class FGFSSocket {
int sock; public:
bool connected;
unsigned timeout;
public:
FGFSSocket(const char *name, const unsigned port); FGFSSocket(const char *name, const unsigned port);
~FGFSSocket() { close(); }; ~FGFSSocket();
int write(const char *msg, ...); int write(const char *msg, ...);
const char *read(void); const char *read(void);
inline void flush(void); inline void flush(void);
void settimeout(unsigned t) { timeout = t; }; void settimeout(unsigned t) { _timeout = t; }
private:
private:
int close(void); int close(void);
int _sock;
bool _connected;
unsigned _timeout;
char _buffer[MAXLEN];
}; };
FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port = 5501) FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port = 5501) :
: _sock(-1),
sock(-1), _connected(false),
connected(false), _timeout(1)
timeout(1)
{ {
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); _sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) if (_sock < 0)
throw("FGFSSocket/socket"); throw("FGFSSocket/socket");
struct hostent *hostinfo; struct hostent *hostinfo;
@ -57,11 +60,11 @@ FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port =
serv_addr.sin_port = htons(port); serv_addr.sin_port = htons(port);
serv_addr.sin_addr = *(struct in_addr *)hostinfo->h_addr; 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(); close();
throw("FGFSSocket/connect"); throw("FGFSSocket/connect");
} }
connected = true; _connected = true;
try { try {
write("data"); write("data");
} catch (...) { } catch (...) {
@ -71,14 +74,20 @@ FGFSSocket::FGFSSocket(const char *hostname = "localhost", const unsigned port =
} }
FGFSSocket::~FGFSSocket()
{
close();
}
int FGFSSocket::close(void) int FGFSSocket::close(void)
{ {
if (connected) if (_connected)
write("quit"); write("quit");
if (sock < 0) if (_sock < 0)
return 0; return 0;
int ret = ::close(sock); int ret = ::close(_sock);
sock = -1; _sock = -1;
return ret; return ret;
} }
@ -87,13 +96,13 @@ int FGFSSocket::write(const char *msg, ...)
{ {
va_list va; va_list va;
ssize_t len; ssize_t len;
char buf[maxlen]; char buf[MAXLEN];
fd_set fd; fd_set fd;
struct timeval tv; struct timeval tv;
FD_ZERO(&fd); FD_ZERO(&fd);
FD_SET(sock, &fd); FD_SET(_sock, &fd);
tv.tv_sec = timeout; tv.tv_sec = _timeout;
tv.tv_usec = 0; tv.tv_usec = 0;
if (!select(FD_SETSIZE, 0, &fd, 0, &tv)) if (!select(FD_SETSIZE, 0, &fd, 0, &tv))
throw("FGFSSocket::write/select: timeout exceeded"); throw("FGFSSocket::write/select: timeout exceeded");
@ -104,7 +113,7 @@ int FGFSSocket::write(const char *msg, ...)
std::cout << "SEND: " << buf << std::endl; std::cout << "SEND: " << buf << std::endl;
strcat(buf, "\015\012"); strcat(buf, "\015\012");
len = ::write(sock, buf, strlen(buf)); len = ::write(_sock, buf, strlen(buf));
if (len < 0) if (len < 0)
throw("FGFSSocket::write"); throw("FGFSSocket::write");
return len; return len;
@ -113,44 +122,43 @@ int FGFSSocket::write(const char *msg, ...)
const char *FGFSSocket::read(void) const char *FGFSSocket::read(void)
{ {
static char buf[maxlen];
char *p; char *p;
fd_set fd; fd_set fd;
struct timeval tv; struct timeval tv;
ssize_t len; ssize_t len;
FD_ZERO(&fd); FD_ZERO(&fd);
FD_SET(sock, &fd); FD_SET(_sock, &fd);
tv.tv_sec = timeout; tv.tv_sec = _timeout;
tv.tv_usec = 0; tv.tv_usec = 0;
if (!select(FD_SETSIZE, &fd, 0, 0, &tv)) { if (!select(FD_SETSIZE, &fd, 0, 0, &tv)) {
if (timeout == 0) if (_timeout == 0)
return 0; return 0;
else else
throw("FGFSSocket::read/select: timeout exceeded"); throw("FGFSSocket::read/select: timeout exceeded");
} }
len = ::read(sock, buf, maxlen - 1); len = ::read(_sock, _buffer, MAXLEN - 1);
if (len < 0) if (len < 0)
throw("FGFSSocket::read/read"); throw("FGFSSocket::read/read");
if (len == 0) if (len == 0)
return 0; return 0;
for (p = &buf[len - 1]; p >= buf; p--) for (p = &_buffer[len - 1]; p >= _buffer; p--)
if (*p != '\015' && *p != '\012') if (*p != '\015' && *p != '\012')
break; break;
*++p = '\0'; *++p = '\0';
return strlen(buf) ? buf : 0; return strlen(_buffer) ? _buffer : 0;
} }
inline void FGFSSocket::flush(void) inline void FGFSSocket::flush(void)
{ {
int i = timeout; int i = _timeout;
timeout = 0; _timeout = 0;
while (read()) while (read())
; ;
timeout = i; _timeout = i;
} }
@ -167,15 +175,15 @@ try {
const char *p = f.read(); const char *p = f.read();
if (p) if (p)
std::cout << "RECV: " << p << std::endl; std::cout << "RECV: " << p << std::endl;
return 0; return EXIT_SUCCESS;
} catch (const char s[]) { } catch (const char s[]) {
std::cerr << "Error: " << s << ": " << strerror(errno) << std::endl; std::cerr << "Error: " << s << ": " << strerror(errno) << std::endl;
return -1; return EXIT_FAILURE;
} catch (...) { } catch (...) {
std::cerr << "Error: unknown exception" << std::endl; std::cerr << "Error: unknown exception" << std::endl;
return -1; return EXIT_FAILURE;
} }