]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/input_output/FGfdmSocket.cpp
PAtch by Andreas Gaeb to eliminate NaN's in the location code
[flightgear.git] / src / FDM / JSBSim / input_output / FGfdmSocket.cpp
index 7d2178433b85a7589d01e3deaa407c57dd2718f2..d1edb67394876a630d64a7fd39311f8437d78143 100644 (file)
@@ -6,7 +6,7 @@
  Purpose:      Encapsulates a socket
  Called by:    FGOutput, et. al.
 
- ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
 
  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free Software
@@ -38,24 +38,33 @@ HISTORY
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-#include "FGfdmSocket.h"
+#include <iostream>
+#include <iomanip>
 #include <cstring>
+#include <cstdio>
+#include "FGfdmSocket.h"
+#include "string_utilities.h"
+
+using std::cout;
+using std::cerr;
+using std::endl;
+using std::string;
 
 namespace JSBSim {
 
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGfdmSocket.cpp,v 1.27 2010/05/13 03:07:59 jberndt Exp $";
 static const char *IdHdr = ID_FDMSOCKET;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-FGfdmSocket::FGfdmSocket(string address, int port, int protocol)
+FGfdmSocket::FGfdmSocket(const string& address, int port, int protocol)
 {
-  sckt = sckt_in = size = 0;
+  sckt = sckt_in = 0;
   connected = false;
 
-  #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
+  #if defined(_MSC_VER) || defined(__MINGW32__)
     WSADATA wsaData;
     int wsaReturnCode;
     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
@@ -63,12 +72,14 @@ FGfdmSocket::FGfdmSocket(string address, int port, int protocol)
     else cout << "Winsock DLL not initialized ..." << endl;
   #endif
 
-  if (address.find_first_not_of("0123456789.",0) != address.npos) {
+  if (!is_number(address)) {
     if ((host = gethostbyname(address.c_str())) == NULL) {
       cout << "Could not get host net address by name..." << endl;
     }
   } else {
-    if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
+    unsigned int ip;
+    ip = inet_addr(address.c_str());
+    if ((host = gethostbyaddr((char*)&ip, address.size(), PF_INET)) == NULL) {
       cout << "Could not get host net address by number..." << endl;
     }
   }
@@ -104,12 +115,12 @@ FGfdmSocket::FGfdmSocket(string address, int port, int protocol)
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-FGfdmSocket::FGfdmSocket(string address, int port)
+FGfdmSocket::FGfdmSocket(const string& address, int port)
 {
-  sckt = sckt_in = size = 0;
+  sckt = sckt_in = 0;
   connected = false;
 
-  #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
+  #if defined(_MSC_VER) || defined(__MINGW32__)
     WSADATA wsaData;
     int wsaReturnCode;
     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
@@ -121,7 +132,7 @@ FGfdmSocket::FGfdmSocket(string address, int port)
   cout << "Host name...   " << address << ",  Port...  " << port << "." << endl;
   cout << "Host name... (char)  " << address.c_str() << "." << endl;
 
-  if (address.find_first_not_of("0123456789.",0) != address.npos) {
+  if (!is_number(address)) {
     if ((host = gethostbyname(address.c_str())) == NULL) {
       cout << "Could not get host net address by name..." << endl;
     }
@@ -158,11 +169,10 @@ FGfdmSocket::FGfdmSocket(string address, int port)
 
 FGfdmSocket::FGfdmSocket(int port)
 {
-  size = 0;
   connected = false;
   unsigned long NoBlock = true;
 
-  #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
+  #if defined(_MSC_VER) || defined(__MINGW32__)
     WSADATA wsaData;
     int wsaReturnCode;
     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
@@ -180,7 +190,7 @@ FGfdmSocket::FGfdmSocket(int port)
     if (bind(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
       cout << "Successfully bound to socket for input on port " << port << endl;
       if (listen(sckt, 5) >= 0) { // successful listen()
-        #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
+        #if defined(_MSC_VER) || defined(__MINGW32__)
           ioctlsocket(sckt, FIONBIO, &NoBlock);
           sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
         #else
@@ -207,10 +217,6 @@ FGfdmSocket::~FGfdmSocket()
 {
   if (sckt) shutdown(sckt,2);
   if (sckt_in) shutdown(sckt_in,2);
-
-  #ifdef __BORLANDC__
-    WSACleanup();
-  #endif
   Debug(1);
 }
 
@@ -221,19 +227,18 @@ string FGfdmSocket::Receive(void)
   char buf[1024];
   int len = sizeof(struct sockaddr_in);
   int num_chars=0;
-  int total_chars = 0;
   unsigned long NoBlock = true;
-  string data = ""; // todo: should allocate this with a standard size as a
+  string data;      // todo: should allocate this with a standard size as a
                     // class attribute and pass as a reference?
 
   if (sckt_in <= 0) {
-    #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
+    #if defined(_MSC_VER) || defined(__MINGW32__)
       sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
     #else
       sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
     #endif
     if (sckt_in > 0) {
-      #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
+      #if defined(_MSC_VER) || defined(__MINGW32__)
          ioctlsocket(sckt_in, FIONBIO,&NoBlock);
       #else
          ioctl(sckt_in, FIONBIO, &NoBlock);
@@ -243,9 +248,8 @@ string FGfdmSocket::Receive(void)
   }
 
   if (sckt_in > 0) {
-    while ((num_chars = recv(sckt_in, buf, 1024, 0)) > 0) {
-      data += string(buf).substr(0,num_chars);
-      total_chars += num_chars;
+    while ((num_chars = recv(sckt_in, buf, sizeof buf, 0)) > 0) {
+      data.append(buf, num_chars);
     }
 
 #if defined(_MSC_VER)
@@ -262,12 +266,12 @@ string FGfdmSocket::Receive(void)
 #endif
   }
 
-  return data.substr(0, total_chars);
+  return data;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-int FGfdmSocket::Reply(string text)
+int FGfdmSocket::Reply(const string& text)
 {
   int num_chars_sent=0;
 
@@ -292,71 +296,58 @@ void FGfdmSocket::Close(void)
 
 void FGfdmSocket::Clear(void)
 {
-  buffer = "";
-  size = 0;
+  buffer.str(string());
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGfdmSocket::Clear(string s)
+void FGfdmSocket::Clear(const string& s)
 {
-  buffer = s + " ";
-  size = buffer.size();
+  Clear();
+  buffer << s << ' ';
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGfdmSocket::Append(const char* item)
 {
-  if (size == 0) buffer += string(item);
-  else buffer += string(",") + string(item);
-  size++;
+  if (buffer.tellp() > 0) buffer << ',';
+  buffer << item;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGfdmSocket::Append(double item)
 {
-  char s[25];
-
-  sprintf(s,"%12.7f",item);
-
-  if (size == 0) buffer += string(s);
-  else buffer += string(",") + string(s);
-  size++;
+  if (buffer.tellp() > 0) buffer << ',';
+  buffer << std::setw(12) << std::setprecision(7) << item;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGfdmSocket::Append(long item)
 {
-  char s[25];
-
-  sprintf(s,"%12ld",item);
-
-  if (size == 0) buffer += string(s);
-  else buffer += string(",") + string(s);
-  size++;
+  if (buffer.tellp() > 0) buffer << ',';
+  buffer << std::setw(12) << item;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGfdmSocket::Send(void)
 {
-  buffer += string("\n");
-  if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
+  buffer << '\n';
+  string str = buffer.str();
+  if ((send(sckt,str.c_str(),str.size(),0)) <= 0) {
     perror("send");
-  } else {
   }
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGfdmSocket::Send(char *data, int length)
+void FGfdmSocket::Send(const char *data, int length)
 {
   if ((send(sckt,data,length,0)) <= 0) {
     perror("send");
-  } else {
   }
 }