Maintenance: js_server
Fix memory leaks. Better use of whitespace.
This commit is contained in:
parent
21330d2f31
commit
0ee3fd4ab6
1 changed files with 71 additions and 79 deletions
|
@ -26,119 +26,111 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <memory>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <plib/netSocket.h>
|
#include <plib/netSocket.h>
|
||||||
|
|
||||||
#include "FlightGear_js.h"
|
#include "FlightGear_js.h"
|
||||||
|
|
||||||
void usage(char * progname)
|
void usage(char* progname)
|
||||||
{
|
{
|
||||||
printf("This is an UDP based remote joystick server.\n");
|
printf("This is an UDP based remote joystick server.\n");
|
||||||
printf("usage: %s <hostname> <port>\n", progname);
|
printf("usage: %s <hostname> <port>\n", progname);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main ( int argc, char ** argv )
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
jsJoystick * js ;
|
|
||||||
float * ax;
|
|
||||||
int port = 16759;
|
int port = 16759;
|
||||||
char * host; /* = "192.168.1.7"; */
|
char* host; /* = "192.168.1.7"; */
|
||||||
int activeaxes = 4;
|
int activeaxes = 4;
|
||||||
|
|
||||||
if( argc != 3 )
|
if (argc != 3) {
|
||||||
{
|
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
host = argv[1];
|
host = argv[1];
|
||||||
port = atoi(argv[2]);
|
port = atoi(argv[2]);
|
||||||
|
|
||||||
jsInit () ;
|
jsInit();
|
||||||
|
|
||||||
js = new jsJoystick ( 0 ) ;
|
auto js = std::make_unique<jsJoystick>(0);
|
||||||
|
|
||||||
if ( js->notWorking () )
|
if (js->notWorking()) {
|
||||||
{
|
printf("no Joystick detected... exitting\n");
|
||||||
printf ( "no Joystick detected... exitting\n" ) ;
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
printf ( "Joystick is \"%s\"\n", js->getName() ) ;
|
printf("Joystick is \"%s\"\n", js->getName());
|
||||||
|
|
||||||
int numaxes = js->getNumAxes();
|
int numaxes = js->getNumAxes();
|
||||||
ax = new float [ numaxes ] ;
|
auto ax = std::make_unique<float[]>(numaxes);
|
||||||
activeaxes = numaxes;
|
activeaxes = numaxes;
|
||||||
|
|
||||||
if( numaxes > 4 )
|
if (numaxes > 4) {
|
||||||
{
|
|
||||||
printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes);
|
printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes);
|
||||||
activeaxes = 4;
|
activeaxes = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must call this before any other net stuff
|
// Must call this before any other net stuff
|
||||||
netInit( &argc,argv );
|
netInit(&argc, argv);
|
||||||
|
|
||||||
netSocket c;
|
netSocket c;
|
||||||
|
|
||||||
if ( ! c.open( false ) ) { // open a UDP socket
|
if (!c.open(false)) { // open a UDP socket
|
||||||
printf("error opening socket\n");
|
printf("error opening socket\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
c.setBlocking( false );
|
c.setBlocking(false);
|
||||||
|
|
||||||
if ( c.connect( host, port ) == -1 ) {
|
if (c.connect(host, port) == -1) {
|
||||||
printf("error connecting to %s:%d\n", host, port);
|
printf("error connecting to %s:%d\n", host, port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char packet[256] = "Hello world!";
|
char packet[256] = "Hello world!";
|
||||||
while(1)
|
while (1) {
|
||||||
{
|
|
||||||
int b;
|
int b;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
int axis = 0;
|
int axis = 0;
|
||||||
|
|
||||||
js->read( &b, ax );
|
js->read(&b, ax.get());
|
||||||
for ( axis = 0 ; axis < activeaxes ; axis++ )
|
for (axis = 0; axis < activeaxes; axis++) {
|
||||||
{
|
int32_t axisvalue = (int32_t)(ax[axis] * 2147483647.0);
|
||||||
int32_t axisvalue = (int32_t)(ax[axis]*2147483647.0);
|
|
||||||
printf("axisval=%li\n", (long)axisvalue);
|
printf("axisval=%li\n", (long)axisvalue);
|
||||||
memcpy(packet+len, &axisvalue, sizeof(axisvalue));
|
memcpy(packet + len, &axisvalue, sizeof(axisvalue));
|
||||||
len+=sizeof(axisvalue);
|
len += sizeof(axisvalue);
|
||||||
}
|
}
|
||||||
// fill emtpy values into packes when less than 4 axes
|
// fill emtpy values into packes when less than 4 axes
|
||||||
for( ; axis < 4; axis++ )
|
for (; axis < 4; axis++) {
|
||||||
{
|
|
||||||
int32_t axisvalue = 0;
|
int32_t axisvalue = 0;
|
||||||
memcpy(packet+len, &axisvalue, sizeof(axisvalue));
|
memcpy(packet + len, &axisvalue, sizeof(axisvalue));
|
||||||
len+=sizeof(axisvalue);
|
len += sizeof(axisvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t b_l = b;
|
int32_t b_l = b;
|
||||||
memcpy(packet+len, &b_l, sizeof(b_l));
|
memcpy(packet + len, &b_l, sizeof(b_l));
|
||||||
len+=sizeof(b_l);
|
len += sizeof(b_l);
|
||||||
|
|
||||||
const char * termstr = "\0\0\r\n";
|
const char* termstr = "\0\0\r\n";
|
||||||
memcpy(packet+len, termstr, 4);
|
memcpy(packet + len, termstr, 4);
|
||||||
len += 4;
|
len += 4;
|
||||||
|
|
||||||
c.send( packet, len, 0 );
|
c.send(packet, len, 0);
|
||||||
|
|
||||||
/* give other processes a chance */
|
/* give other processes a chance */
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
Sleep ( 1 ) ;
|
Sleep(1);
|
||||||
#elif defined(sgi)
|
#elif defined(sgi)
|
||||||
sginap ( 1 ) ;
|
sginap(1);
|
||||||
#else
|
#else
|
||||||
usleep ( 200 ) ;
|
usleep(200);
|
||||||
#endif
|
#endif
|
||||||
printf(".");
|
printf(".");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0 ;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue