1
0
Fork 0

Fixes for OpenBSD.

Took the opportunity to switch to the more portable call 'fctnl(... O_NONBLOCK ...)' instead of 'ioctl(... FIONBIO ...)' for all UNIX-like OSes.
This commit is contained in:
Bertrand Coconnier 2020-10-31 14:29:22 +01:00
parent bb0b69bc41
commit 53fac332c1

View file

@ -40,10 +40,15 @@ INCLUDES
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <WS2tcpip.h>
#elif defined(__OpenBSD__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#endif
#include <iomanip>
#include <cstring>
@ -95,7 +100,7 @@ FGfdmSocket::FGfdmSocket(const string& address, int port, int protocol)
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
if (!is_number(address))
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
hints.ai_flags = AI_ADDRCONFIG;
else
hints.ai_flags = AI_NUMERICHOST;
@ -189,13 +194,14 @@ FGfdmSocket::FGfdmSocket(int port, int protocol)
<< " input socket on port " << port << endl << endl;
if (Protocol == ptTCP) {
unsigned long NoBlock = true;
if (listen(sckt, 5) >= 0) { // successful listen()
#if defined(_MSC_VER) || defined(__MINGW32__)
u_long NoBlock = 1;
ioctlsocket(sckt, FIONBIO, &NoBlock);
sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
#else
ioctl(sckt, FIONBIO, &NoBlock);
int flags = fcntl(sckt, F_GETFL, 0);
fcntl(sckt, F_SETFL, flags | O_NONBLOCK);
sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
#endif
connected = true;
@ -229,7 +235,6 @@ string FGfdmSocket::Receive(void)
char buf[1024];
int len = sizeof(struct sockaddr_in);
int num_chars=0;
unsigned long NoBlock = true;
string data; // todo: should allocate this with a standard size as a
// class attribute and pass as a reference?
@ -241,9 +246,11 @@ string FGfdmSocket::Receive(void)
#endif
if (sckt_in > 0) {
#if defined(_MSC_VER) || defined(__MINGW32__)
u_long NoBlock = 1;
ioctlsocket(sckt_in, FIONBIO, &NoBlock);
#else
ioctl(sckt_in, FIONBIO, &NoBlock);
int flags = fcntl(sckt_in, F_GETFL, 0);
fcntl(sckt_in, F_SETFL, flags | O_NONBLOCK);
#endif
send(sckt_in, "Connected to JSBSim server\nJSBSim> ", 35, 0);
}