]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/ExternalNet/ExternalNet.cxx
Set SO_REUSEADDR on incoming UDP socket so other processes on the host
[flightgear.git] / src / FDM / ExternalNet / ExternalNet.cxx
index e5e450908f2e38699e720a563fd3e7d1267bb269..a18b316f42c927ec78a2b9fc9166de26055621a9 100644 (file)
 #  include <netinet/in.h>      // htonl() ntohl()
 #endif
 
+#if defined( linux )
+#  include <sys/types.h>
+#  include <sys/socket.h>
+#endif
 
 // The function htond is defined this way due to the way some
 // processors and OSes treat floating point values.  Some will raise
@@ -108,8 +112,12 @@ static void global2net( FGNetCtrls *net ) {
     net->num_wheels = FGNetCtrls::FG_MAX_WHEELS;
     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
         if ( node->getChild("brakes", i) != 0 ) {
-            net->brake[i]
-                = node->getChild("brakes", i)->getDoubleValue();
+            if ( node->getChild("parking-brake")->getDoubleValue() > 0.0 ) {
+                net->brake[i] = 1.0;
+           } else {
+                net->brake[i]
+                    = node->getChild("brakes", i)->getDoubleValue();
+            }
         } else {
             net->brake[i] = 0.0;
         }
@@ -120,6 +128,9 @@ static void global2net( FGNetCtrls *net ) {
     net->master_alt = node->getChild("master-alt")->getBoolValue();
     net->master_avionics = node->getChild("master-avionics")->getBoolValue();
 
+    net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
+    net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
+
     // cur_fdm_state->get_ground_elev_ft() is what we want ... this
     // reports the altitude of the aircraft.
     // "/environment/ground-elevation-m" reports the ground elevation
@@ -128,6 +139,16 @@ static void global2net( FGNetCtrls *net ) {
     net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
     net->speedup = fgGetInt("/sim/speed-up");
+    net->freeze = 0;
+    if ( fgGetBool("/sim/freeze/master") ) {
+        net->freeze |= 0x01;
+    }
+    if ( fgGetBool("/sim/freeze/position") ) {
+        net->freeze |= 0x02;
+    }
+    if ( fgGetBool("/sim/freeze/fuel") ) {
+        net->freeze |= 0x04;
+    }
 
     // convert to network byte order
     net->version = htonl(net->version);
@@ -158,9 +179,12 @@ static void global2net( FGNetCtrls *net ) {
     net->master_bat = htonl(net->master_bat);
     net->master_alt = htonl(net->master_alt);
     net->master_avionics = htonl(net->master_avionics);
+    htond(net->wind_speed_kt);
+    htond(net->wind_dir_deg);
     htond(net->hground);
     htond(net->magvar);
     net->speedup = htonl(net->speedup);
+    net->freeze = htonl(net->freeze);
 }
 
 
@@ -240,7 +264,7 @@ static void net2global( FGNetFDM *net ) {
                                                   net->v_wind_body_east,
                                                   net->v_wind_body_down );
 
-        fgSetDouble( "/sim/aero/alarms/stall-warning", net->stall_warning );
+        fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
                                               net->A_Y_pilot,
                                               net->A_Z_pilot );
@@ -350,6 +374,15 @@ FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
     // disable blocking
     data_server.setBlocking( false );
 
+#if defined( linux )
+    // set SO_REUSEADDR flag
+    int socket = data_server.getHandle();
+    int one = 1;
+    int result;
+    result = ::setsockopt( socket, SOL_SOCKET, SO_REUSEADDR,
+                           &one, sizeof(one) );
+#endif
+
     // allowed to read from a broadcast addr
     // data_server.setBroadcast( true );
 
@@ -377,39 +410,55 @@ void FGExternalNet::init() {
     // init method first.
     common_init();
 
-    double lon = fgGetDouble( "/position/longitude-deg" );
-    double lat = fgGetDouble( "/position/latitude-deg" );
+    double lon = fgGetDouble( "/sim/presets/longitude-deg" );
+    double lat = fgGetDouble( "/sim/presets/latitude-deg" );
+    double alt = fgGetDouble( "/sim/presets/altitude-ft" );
     double ground = fgGetDouble( "/environment/ground-elevation-m" );
-    double heading = fgGetDouble("/orientation/heading-deg");
+    double heading = fgGetDouble("/sim/presets/heading-deg");
+    double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
 
     char cmd[256];
 
     HTTPClient *http;
     sprintf( cmd, "/longitude-deg?value=%.8f", lon );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     sprintf( cmd, "/latitude-deg?value=%.8f", lat );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
+    delete http;
+
+    sprintf( cmd, "/altitude-ft?value=%.8f", alt );
+    http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     sprintf( cmd, "/ground-m?value=%.8f", ground );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
+    delete http;
+
+    sprintf( cmd, "/speed-kts?value=%.8f", speed );
+    http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     sprintf( cmd, "/heading-deg?value=%.8f", heading );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
 
-    sprintf( cmd, "/reset?value=ground" );
+    if( fgGetBool("/sim/presets/onground") ) {
+      sprintf( cmd, "/reset?value=ground" );
+    } else {
+      sprintf( cmd, "/reset?value=air" );
+    }
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );