]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/input_output/FGfdmSocket.cpp
latest changes for JSBSim (1.0 prerelease)
[flightgear.git] / src / FDM / JSBSim / input_output / FGfdmSocket.cpp
index c9db20f9486d9e7fc62169ae7e635c1a9b5261e6..7fa7ae59fbc7678d646c93adf943030f68bd4de5 100644 (file)
@@ -9,20 +9,20 @@
  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
 
  This program is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free Software
+ the terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later
  version.
 
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
  details.
 
- You should have received a copy of the GNU General Public License along with
+ You should have received a copy of the GNU Lesser General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  Place - Suite 330, Boston, MA  02111-1307, USA.
 
- Further information about the GNU General Public License can also be found on
+ Further information about the GNU Lesser General Public License can also be found on
  the world wide web at http://www.gnu.org.
 
 FUNCTIONAL DESCRIPTION
@@ -32,12 +32,14 @@ This class excapsulates a socket for simple data writing
 HISTORY
 --------------------------------------------------------------------------------
 11/08/99   JSB   Created
+11/08/07   HDW   Added Generic Socket Send
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
 #include "FGfdmSocket.h"
+#include <cstring>
 
 namespace JSBSim {
 
@@ -48,12 +50,66 @@ static const char *IdHdr = ID_FDMSOCKET;
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
+FGfdmSocket::FGfdmSocket(string address, int port, int protocol)
+{
+  sckt = sckt_in = size = 0;
+  connected = false;
+
+  #if defined(_MSC_VER) || defined(__MINGW32__)
+    WSADATA wsaData;
+    int wsaReturnCode;
+    wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
+    if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
+    else cout << "Winsock DLL not initialized ..." << endl;
+  #endif
+
+  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) {
+      cout << "Could not get host net address by number..." << endl;
+    }
+  }
+
+  if (host != NULL) {
+    if (protocol == ptUDP) {  //use udp protocol
+       sckt = socket(AF_INET, SOCK_DGRAM, 0);
+       cout << "Creating UDP socket on port " << port << endl;
+    }
+    else { //use tcp protocol
+       sckt = socket(AF_INET, SOCK_STREAM, 0);
+       cout << "Creating TCP socket on port " << port << endl;
+    }
+
+    if (sckt >= 0) {  // successful
+      memset(&scktName, 0, sizeof(struct sockaddr_in));
+      scktName.sin_family = AF_INET;
+      scktName.sin_port = htons(port);
+      memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
+      int len = sizeof(struct sockaddr_in);
+      if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
+        cout << "Successfully connected to socket for output ..." << endl;
+        connected = true;
+      } else {                // unsuccessful
+        cout << "Could not connect to socket for output ..." << endl;
+      }
+    } else {          // unsuccessful
+      cout << "Could not create socket for FDM output, error = " << errno << endl;
+    }
+  }
+  Debug(0);
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
 FGfdmSocket::FGfdmSocket(string address, int port)
 {
   sckt = sckt_in = size = 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);
@@ -61,7 +117,11 @@ FGfdmSocket::FGfdmSocket(string address, int port)
     else cout << "Winsock DLL not initialized ..." << endl;
   #endif
 
-  if (address.find_first_not_of("0123456789.",0) != address.npos) {
+  cout << "... Socket Configuration Sanity Check ..." << endl;
+  cout << "Host name...   " << address << ",  Port...  " << port << "." << endl;
+  cout << "Host name... (char)  " << address.c_str() << "." << endl;
+
+  if (!is_number(address)) {
     if ((host = gethostbyname(address.c_str())) == NULL) {
       cout << "Could not get host net address by name..." << endl;
     }
@@ -82,13 +142,13 @@ FGfdmSocket::FGfdmSocket(string address, int port)
       memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
       int len = sizeof(struct sockaddr_in);
       if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
-        cout << "Successfully connected to socket ..." << endl;
+        cout << "Successfully connected to socket for output ..." << endl;
         connected = true;
       } else {                // unsuccessful
-        cout << "Could not connect to socket ..." << endl;
+        cout << "Could not connect to socket for output ..." << endl;
       }
     } else {          // unsuccessful
-      cout << "Could not create socket for FDM, error = " << errno << endl;
+      cout << "Could not create socket for FDM output, error = " << errno << endl;
     }
   }
   Debug(0);
@@ -102,7 +162,7 @@ FGfdmSocket::FGfdmSocket(int port)
   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);
@@ -116,12 +176,11 @@ FGfdmSocket::FGfdmSocket(int port)
     memset(&scktName, 0, sizeof(struct sockaddr_in));
     scktName.sin_family = AF_INET;
     scktName.sin_port = htons(port);
-//    memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
     int len = sizeof(struct sockaddr_in);
     if (bind(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
-      cout << "Successfully bound to socket ..." << endl;
+      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
@@ -133,10 +192,10 @@ FGfdmSocket::FGfdmSocket(int port)
       }
       connected = true;
     } else {                // unsuccessful
-      cerr << "Could not bind to socket ..." << endl;
+      cerr << "Could not bind to socket for input ..." << endl;
     }
   } else {          // unsuccessful
-    cerr << "Could not create socket for FDM, error = " << errno << endl;
+    cerr << "Could not create socket for FDM input, error = " << errno << endl;
   }
 
   Debug(0);
@@ -146,14 +205,8 @@ FGfdmSocket::FGfdmSocket(int port)
 
 FGfdmSocket::~FGfdmSocket()
 {
-  #ifndef macintosh
   if (sckt) shutdown(sckt,2);
   if (sckt_in) shutdown(sckt_in,2);
-  #endif
-
-  #ifdef __BORLANDC__
-    WSACleanup();
-  #endif
   Debug(1);
 }
 
@@ -170,13 +223,13 @@ string FGfdmSocket::Receive(void)
                     // 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);
@@ -293,6 +346,16 @@ void FGfdmSocket::Send(void)
   }
 }
 
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGfdmSocket::Send(char *data, int length)
+{
+  if ((send(sckt,data,length,0)) <= 0) {
+    perror("send");
+  } else {
+  }
+}
+
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 //    The bitmasked value choices are as follows:
 //    unset: In this case (the default) JSBSim would only print