Add support for Visual Studio 2015 on Windows

This commit is contained in:
Nick Little 2018-07-07 12:50:10 -05:00
parent d4be3b8a25
commit 40ff2c6cd9
6 changed files with 400 additions and 94 deletions

206
anet.c
View File

@ -29,19 +29,52 @@
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#ifndef _MSC_VER
#include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <netdb.h> #include <netdb.h>
#include <errno.h>
#include <stdarg.h> #define CONCAT(A, B) A ## B
#include <stdio.h>
#define anetError() errno
#define anetErrorIs(A) (errno == CONCAT(E, A))
#define ERROR_DATA_FORMAT "%s"
#define anetErrorData() strerror(errno)
#else
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define CONCAT(A, B) A ## B
#define close(S) do { shutdown(S, SD_SEND); closesocket(S); } while (0)
#define inet_aton(A, B) inet_pton(AF_INET, A, B)
#define setsockopt(A, B, C, D, E) setsockopt(A, B, C, (const char *)(D), E)
#define anetError() WSAGetLastError()
#define anetErrorIs(A) (WSAGetLastError() == CONCAT(WSAE, A))
#define ERROR_DATA_FORMAT "%i"
#define anetErrorData() WSAGetLastError()
typedef int socklen_t;
#endif
#include "anet.h" #include "anet.h"
@ -55,21 +88,46 @@ static void anetSetError(char *err, const char *fmt, ...)
va_end(ap); va_end(ap);
} }
int anetInit(char *err)
{
#ifdef _MSC_VER
// Initialize Winsock
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
anetSetError(err, "WSAStartup failed: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR;
}
#endif
return ANET_OK;
}
int anetNonBlock(char *err, int fd) int anetNonBlock(char *err, int fd)
{ {
#ifndef _MSC_VER
int flags; int flags;
/* Set the socket nonblocking. /* Set the socket nonblocking.
* Note that fcntl(2) for F_GETFL and F_SETFL can't be * Note that fcntl(2) for F_GETFL and F_SETFL can't be
* interrupted by a signal. */ * interrupted by a signal. */
if ((flags = fcntl(fd, F_GETFL)) == -1) { if ((flags = fcntl(fd, F_GETFL)) == -1) {
anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno)); anetSetError(err, "fcntl(F_GETFL): " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno)); anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
#else
u_long iMode = 1;
if (ioctlsocket(fd, FIONBIO, &iMode) != 0) {
anetSetError(err, "ioctlsocket(FIONBIO, 0): " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR;
}
#endif
return ANET_OK; return ANET_OK;
} }
@ -78,7 +136,7 @@ int anetTcpNoDelay(char *err, int fd)
int yes = 1; int yes = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1)
{ {
anetSetError(err, "setsockopt TCP_NODELAY: %s", strerror(errno)); anetSetError(err, "setsockopt TCP_NODELAY: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
return ANET_OK; return ANET_OK;
@ -88,7 +146,7 @@ int anetSetSendBuffer(char *err, int fd, int buffsize)
{ {
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize)) == -1) if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize)) == -1)
{ {
anetSetError(err, "setsockopt SO_SNDBUF: %s", strerror(errno)); anetSetError(err, "setsockopt SO_SNDBUF: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
return ANET_OK; return ANET_OK;
@ -98,7 +156,7 @@ int anetTcpKeepAlive(char *err, int fd)
{ {
int yes = 1; int yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) { if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) {
anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno)); anetSetError(err, "setsockopt SO_KEEPALIVE: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
return ANET_OK; return ANET_OK;
@ -126,14 +184,14 @@ int anetResolve(char *err, char *host, char *ipbuf)
static int anetCreateSocket(char *err, int domain) { static int anetCreateSocket(char *err, int domain) {
int s, on = 1; int s, on = 1;
if ((s = socket(domain, SOCK_STREAM, 0)) == -1) { if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
anetSetError(err, "creating socket: %s", strerror(errno)); anetSetError(err, "creating socket: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
/* Make sure connection-intensive things like the redis benckmark /* Make sure connection-intensive things like the redis benckmark
* will be able to close/open sockets a zillion of times */ * will be able to close/open sockets a zillion of times */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno)); anetSetError(err, "setsockopt SO_REUSEADDR: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
return s; return s;
@ -167,11 +225,11 @@ static int anetTcpGenericConnect(char *err, char *addr, int port, int flags)
return ANET_ERR; return ANET_ERR;
} }
if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) { if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
if (errno == EINPROGRESS && if ((anetErrorIs(INPROGRESS) || anetErrorIs(WOULDBLOCK)) &&
flags & ANET_CONNECT_NONBLOCK) flags & ANET_CONNECT_NONBLOCK)
return s; return s;
anetSetError(err, "connect: %s", strerror(errno)); anetSetError(err, "connect: " ERROR_DATA_FORMAT, anetErrorData());
close(s); close(s);
return ANET_ERR; return ANET_ERR;
} }
@ -188,42 +246,6 @@ int anetTcpNonBlockConnect(char *err, char *addr, int port)
return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONBLOCK); return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONBLOCK);
} }
int anetUnixGenericConnect(char *err, char *path, int flags)
{
int s;
struct sockaddr_un sa;
if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
return ANET_ERR;
sa.sun_family = AF_LOCAL;
strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
if (flags & ANET_CONNECT_NONBLOCK) {
if (anetNonBlock(err,s) != ANET_OK)
return ANET_ERR;
}
if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) {
if (errno == EINPROGRESS &&
flags & ANET_CONNECT_NONBLOCK)
return s;
anetSetError(err, "connect: %s", strerror(errno));
close(s);
return ANET_ERR;
}
return s;
}
int anetUnixConnect(char *err, char *path)
{
return anetUnixGenericConnect(err,path,ANET_CONNECT_NONE);
}
int anetUnixNonBlockConnect(char *err, char *path)
{
return anetUnixGenericConnect(err,path,ANET_CONNECT_NONBLOCK);
}
/* Like read(2) but make sure 'count' is read before to return /* Like read(2) but make sure 'count' is read before to return
* (unless error or EOF condition is encountered) */ * (unless error or EOF condition is encountered) */
int anetRead(int fd, char *buf, int count) int anetRead(int fd, char *buf, int count)
@ -256,7 +278,7 @@ int anetWrite(int fd, char *buf, int count)
static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) { static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) {
if (bind(s,sa,len) == -1) { if (bind(s,sa,len) == -1) {
anetSetError(err, "bind: %s", strerror(errno)); anetSetError(err, "bind: " ERROR_DATA_FORMAT, anetErrorData());
close(s); close(s);
return ANET_ERR; return ANET_ERR;
} }
@ -265,7 +287,7 @@ static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) {
* the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1); * the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
* which will thus give us a backlog of 512 entries */ * which will thus give us a backlog of 512 entries */
if (listen(s, 511) == -1) { if (listen(s, 511) == -1) {
anetSetError(err, "listen: %s", strerror(errno)); anetSetError(err, "listen: " ERROR_DATA_FORMAT, anetErrorData());
close(s); close(s);
return ANET_ERR; return ANET_ERR;
} }
@ -294,33 +316,15 @@ int anetTcpServer(char *err, int port, char *bindaddr)
return s; return s;
} }
int anetUnixServer(char *err, char *path, mode_t perm)
{
int s;
struct sockaddr_un sa;
if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
return ANET_ERR;
memset(&sa,0,sizeof(sa));
sa.sun_family = AF_LOCAL;
strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR)
return ANET_ERR;
if (perm)
chmod(sa.sun_path, perm);
return s;
}
static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) { static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {
int fd; int fd;
while(1) { while(1) {
fd = accept(s,sa,len); fd = accept(s,sa,len);
if (fd == -1) { if (fd == -1) {
if (errno == EINTR) if (anetErrorIs(INTR))
continue; continue;
else { else {
anetSetError(err, "accept: %s", strerror(errno)); anetSetError(err, "accept: " ERROR_DATA_FORMAT, anetErrorData());
return ANET_ERR; return ANET_ERR;
} }
} }
@ -341,6 +345,61 @@ int anetTcpAccept(char *err, int s, char *ip, int *port) {
return fd; return fd;
} }
#ifndef _MSC_VER
int anetUnixGenericConnect(char *err, char *path, int flags)
{
int s;
struct sockaddr_un sa;
if ((s = anetCreateSocket(err, AF_LOCAL)) == ANET_ERR)
return ANET_ERR;
sa.sun_family = AF_LOCAL;
strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
if (flags & ANET_CONNECT_NONBLOCK) {
if (anetNonBlock(err, s) != ANET_OK)
return ANET_ERR;
}
if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
if (anetErrorIs(INPROGRESS) &&
flags & ANET_CONNECT_NONBLOCK)
return s;
anetSetError(err, "connect: " ERROR_DATA_FORMAT, anetErrorData());
close(s);
return ANET_ERR;
}
return s;
}
int anetUnixConnect(char *err, char *path)
{
return anetUnixGenericConnect(err, path, ANET_CONNECT_NONE);
}
int anetUnixNonBlockConnect(char *err, char *path)
{
return anetUnixGenericConnect(err, path, ANET_CONNECT_NONBLOCK);
}
int anetUnixServer(char *err, char *path, mode_t perm)
{
int s;
struct sockaddr_un sa;
if ((s = anetCreateSocket(err, AF_LOCAL)) == ANET_ERR)
return ANET_ERR;
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_LOCAL;
strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
if (anetListen(err, s, (struct sockaddr*)&sa, sizeof(sa)) == ANET_ERR)
return ANET_ERR;
if (perm)
chmod(sa.sun_path, perm);
return s;
}
int anetUnixAccept(char *err, int s) { int anetUnixAccept(char *err, int s) {
int fd; int fd;
struct sockaddr_un sa; struct sockaddr_un sa;
@ -350,6 +409,7 @@ int anetUnixAccept(char *err, int s) {
return fd; return fd;
} }
#endif
int anetPeerToString(int fd, char *ip, int *port) { int anetPeerToString(int fd, char *ip, int *port) {
struct sockaddr_in sa; struct sockaddr_in sa;

9
anet.h
View File

@ -39,16 +39,19 @@
#define AF_LOCAL AF_UNIX #define AF_LOCAL AF_UNIX
#endif #endif
int anetInit(char *err);
int anetTcpConnect(char *err, char *addr, int port); int anetTcpConnect(char *err, char *addr, int port);
int anetTcpNonBlockConnect(char *err, char *addr, int port); int anetTcpNonBlockConnect(char *err, char *addr, int port);
int anetUnixConnect(char *err, char *path);
int anetUnixNonBlockConnect(char *err, char *path);
int anetRead(int fd, char *buf, int count); int anetRead(int fd, char *buf, int count);
int anetResolve(char *err, char *host, char *ipbuf); int anetResolve(char *err, char *host, char *ipbuf);
int anetTcpServer(char *err, int port, char *bindaddr); int anetTcpServer(char *err, int port, char *bindaddr);
int anetUnixServer(char *err, char *path, mode_t perm);
int anetTcpAccept(char *err, int serversock, char *ip, int *port); int anetTcpAccept(char *err, int serversock, char *ip, int *port);
#ifndef _MSC_VER
int anetUnixConnect(char *err, char *path);
int anetUnixNonBlockConnect(char *err, char *path);
int anetUnixServer(char *err, char *path, mode_t perm);
int anetUnixAccept(char *err, int serversock); int anetUnixAccept(char *err, int serversock);
#endif
int anetWrite(int fd, char *buf, int count); int anetWrite(int fd, char *buf, int count);
int anetNonBlock(char *err, int fd); int anetNonBlock(char *err, int fd);
int anetTcpNoDelay(char *err, int fd); int anetTcpNoDelay(char *err, int fd);

View File

@ -34,15 +34,47 @@
#include <pthread.h> #include <pthread.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <math.h> #include <math.h>
#include <sys/time.h>
#include <signal.h> #include <signal.h>
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/socket.h>
#define OPEN_FILE_MODE O_RDONLY
#define CONCAT(A, B) A ## B
#define sockError() errno
#define sockErrorIs(A) (errno == CONCAT(E, A))
#else
#include <io.h>
#include <winsock2.h>
typedef intptr_t ssize_t;
#define M_PI 3.14159265358979323846264338327950288
#define STDIN_FILENO 0
#define OPEN_FILE_MODE (O_RDONLY | O_BINARY)
#define CONCAT(A, B) A ## B
#define sockError() WSAGetLastError()
#define sockErrorIs(A) (WSAGetLastError() == CONCAT(WSAE, A))
#define WSAEAGAIN WSAEWOULDBLOCK
#endif
#include "rtl-sdr.h" #include "rtl-sdr.h"
#include "anet.h" #include "anet.h"
@ -245,6 +277,7 @@ int getTermRows();
/* ============================= Utility functions ========================== */ /* ============================= Utility functions ========================== */
static long long mstime(void) { static long long mstime(void) {
#ifndef _MSC_VER
struct timeval tv; struct timeval tv;
long long mst; long long mst;
@ -252,6 +285,12 @@ static long long mstime(void) {
mst = ((long long)tv.tv_sec)*1000; mst = ((long long)tv.tv_sec)*1000;
mst += tv.tv_usec/1000; mst += tv.tv_usec/1000;
return mst; return mst;
#else
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
return (ft.dwHighDateTime * 0x100000000LL + ft.dwLowDateTime) / 10000 - 11644473600000LL;
#endif
} }
/* =============================== Initialization =========================== */ /* =============================== Initialization =========================== */
@ -422,7 +461,11 @@ void readDataFromFile(void) {
/* When --ifile and --interactive are used together, slow down /* When --ifile and --interactive are used together, slow down
* playing at the natural rate of the RTLSDR received. */ * playing at the natural rate of the RTLSDR received. */
pthread_mutex_unlock(&Modes.data_mutex); pthread_mutex_unlock(&Modes.data_mutex);
#ifndef _MSC_VER
usleep(5000); usleep(5000);
#else
Sleep(5);
#endif
pthread_mutex_lock(&Modes.data_mutex); pthread_mutex_lock(&Modes.data_mutex);
} }
@ -889,10 +932,6 @@ char *fs_str[8] = {
/* 7 */ "Value 7 is not assigned" /* 7 */ "Value 7 is not assigned"
}; };
/* ME message type to description table. */
char *me_str[] = {
};
char *getMEDescription(int metype, int mesub) { char *getMEDescription(int metype, int mesub) {
char *mename = "Unknown"; char *mename = "Unknown";
@ -1805,7 +1844,12 @@ void interactiveShowData(void) {
progress[time(NULL)%3] = '.'; progress[time(NULL)%3] = '.';
progress[3] = '\0'; progress[3] = '\0';
#ifdef _MSC_VER
system("cls");
#else
printf("\x1b[H\x1b[2J"); /* Clear the screen */ printf("\x1b[H\x1b[2J"); /* Clear the screen */
#endif
printf( printf(
"Hex Flight Altitude Speed Lat Lon Track Messages Seen %s\n" "Hex Flight Altitude Speed Lat Lon Track Messages Seen %s\n"
"--------------------------------------------------------------------------------\n", "--------------------------------------------------------------------------------\n",
@ -1909,20 +1953,29 @@ void modesInitNet(void) {
memset(Modes.clients,0,sizeof(Modes.clients)); memset(Modes.clients,0,sizeof(Modes.clients));
Modes.maxfd = -1; Modes.maxfd = -1;
if (anetInit(Modes.aneterr) != ANET_OK)
{
fprintf(stderr, "Error starting anet: %s\n",
Modes.aneterr);
exit(1);
}
for (j = 0; j < MODES_NET_SERVICES_NUM; j++) { for (j = 0; j < MODES_NET_SERVICES_NUM; j++) {
int s = anetTcpServer(Modes.aneterr, modesNetServices[j].port, NULL); int s = anetTcpServer(Modes.aneterr, modesNetServices[j].port, NULL);
if (s == -1) { if (s == -1) {
fprintf(stderr, "Error opening the listening port %d (%s): %s\n", fprintf(stderr, "Error opening the listening port %d (%s): %s\n",
modesNetServices[j].port, modesNetServices[j].port,
modesNetServices[j].descr, modesNetServices[j].descr,
strerror(errno)); Modes.aneterr);
exit(1); exit(1);
} }
anetNonBlock(Modes.aneterr, s); anetNonBlock(Modes.aneterr, s);
*modesNetServices[j].socket = s; *modesNetServices[j].socket = s;
} }
#ifndef _MSC_VER
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif
} }
/* This function gets called from time to time when the decoding thread is /* This function gets called from time to time when the decoding thread is
@ -1937,9 +1990,9 @@ void modesAcceptClients(void) {
fd = anetTcpAccept(Modes.aneterr, *modesNetServices[j].socket, fd = anetTcpAccept(Modes.aneterr, *modesNetServices[j].socket,
NULL, &port); NULL, &port);
if (fd == -1) { if (fd == -1) {
if (Modes.debug & MODES_DEBUG_NET && errno != EAGAIN) if (Modes.debug & MODES_DEBUG_NET && !sockErrorIs(AGAIN) && !sockErrorIs(WOULDBLOCK))
printf("Accept %d: %s\n", *modesNetServices[j].socket, printf("Accept %d: %s\n", *modesNetServices[j].socket,
strerror(errno)); Modes.aneterr);
continue; continue;
} }
@ -1969,7 +2022,13 @@ void modesAcceptClients(void) {
/* On error free the client, collect the structure, adjust maxfd if needed. */ /* On error free the client, collect the structure, adjust maxfd if needed. */
void modesFreeClient(int fd) { void modesFreeClient(int fd) {
#ifdef _MSC_VER
shutdown(fd, SD_SEND);
closesocket(fd);
#else
close(fd); close(fd);
#endif
free(Modes.clients[fd]); free(Modes.clients[fd]);
Modes.clients[fd] = NULL; Modes.clients[fd] = NULL;
@ -2000,7 +2059,7 @@ void modesSendAllClients(int service, void *msg, int len) {
for (j = 0; j <= Modes.maxfd; j++) { for (j = 0; j <= Modes.maxfd; j++) {
c = Modes.clients[j]; c = Modes.clients[j];
if (c && c->service == service) { if (c && c->service == service) {
int nwritten = write(j, msg, len); int nwritten = send(j, msg, len, 0);
if (nwritten != len) { if (nwritten != len) {
modesFreeClient(j); modesFreeClient(j);
} }
@ -2234,7 +2293,7 @@ int handleHTTPRequest(struct client *c) {
int fd = -1; int fd = -1;
if (stat("gmap.html",&sbuf) != -1 && if (stat("gmap.html",&sbuf) != -1 &&
(fd = open("gmap.html",O_RDONLY)) != -1) (fd = open("gmap.html", OPEN_FILE_MODE)) != -1)
{ {
content = malloc(sbuf.st_size); content = malloc(sbuf.st_size);
if (read(fd,content,sbuf.st_size) == -1) { if (read(fd,content,sbuf.st_size) == -1) {
@ -2270,8 +2329,8 @@ int handleHTTPRequest(struct client *c) {
printf("HTTP Reply header:\n%s", hdr); printf("HTTP Reply header:\n%s", hdr);
/* Send header and content. */ /* Send header and content. */
if (write(c->fd, hdr, hdrlen) != hdrlen || if (send(c->fd, hdr, hdrlen, 0) != hdrlen ||
write(c->fd, content, clen) != clen) send(c->fd, content, clen, 0) != clen)
{ {
free(content); free(content);
return 1; return 1;
@ -2298,13 +2357,13 @@ void modesReadFromClient(struct client *c, char *sep,
{ {
while(1) { while(1) {
int left = MODES_CLIENT_BUF_SIZE - c->buflen; int left = MODES_CLIENT_BUF_SIZE - c->buflen;
int nread = read(c->fd, c->buf+c->buflen, left); int nread = recv(c->fd, c->buf+c->buflen, left, 0);
int fullmsg = 0; int fullmsg = 0;
int i; int i;
char *p; char *p;
if (nread <= 0) { if (nread <= 0) {
if (nread == 0 || errno != EAGAIN) { if (nread == 0 || (!sockErrorIs(AGAIN) && !sockErrorIs(WOULDBLOCK))) {
/* Error, or end of file. */ /* Error, or end of file. */
modesFreeClient(c->fd); modesFreeClient(c->fd);
} }
@ -2399,6 +2458,7 @@ void modesWaitReadableClients(int timeout_ms) {
/* ============================ Terminal handling ========================== */ /* ============================ Terminal handling ========================== */
#ifndef _MSC_VER
/* Handle resizing terminal. */ /* Handle resizing terminal. */
void sigWinchCallback() { void sigWinchCallback() {
signal(SIGWINCH, SIG_IGN); signal(SIGWINCH, SIG_IGN);
@ -2406,12 +2466,20 @@ void sigWinchCallback() {
interactiveShowData(); interactiveShowData();
signal(SIGWINCH, sigWinchCallback); signal(SIGWINCH, sigWinchCallback);
} }
#endif
/* Get the number of rows after the terminal changes size. */ /* Get the number of rows after the terminal changes size. */
int getTermRows() { int getTermRows() {
#ifndef _MSC_VER
struct winsize w; struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_row; return w.ws_row;
#else
CONSOLE_SCREEN_BUFFER_INFO w;
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &w);
return (int)w.dwSize.Y;
#endif
} }
/* ================================ Main ==================================== */ /* ================================ Main ==================================== */
@ -2560,8 +2628,10 @@ int main(int argc, char **argv) {
} }
} }
#ifndef _MSC_VER
/* Setup for SIGWINCH for handling lines */ /* Setup for SIGWINCH for handling lines */
if (Modes.interactive == 1) signal(SIGWINCH, sigWinchCallback); if (Modes.interactive == 1) signal(SIGWINCH, sigWinchCallback);
#endif
/* Initialization */ /* Initialization */
modesInit(); modesInit();
@ -2572,7 +2642,7 @@ int main(int argc, char **argv) {
} else { } else {
if (Modes.filename[0] == '-' && Modes.filename[1] == '\0') { if (Modes.filename[0] == '-' && Modes.filename[1] == '\0') {
Modes.fd = STDIN_FILENO; Modes.fd = STDIN_FILENO;
} else if ((Modes.fd = open(Modes.filename,O_RDONLY)) == -1) { } else if ((Modes.fd = open(Modes.filename, OPEN_FILE_MODE)) == -1) {
perror("Opening data file"); perror("Opening data file");
exit(1); exit(1);
} }
@ -2632,5 +2702,3 @@ int main(int argc, char **argv) {
rtlsdr_close(Modes.dev); rtlsdr_close(Modes.dev);
return 0; return 0;
} }

5
vs-2015/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*
!.gitignore
!*.sln
!*.vcxproj

28
vs-2015/dump1090.sln Normal file
View File

@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dump1090", "dump1090.vcxproj", "{16CF687B-1555-471F-BDAC-27BB21BDB260}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Debug|x64.ActiveCfg = Debug|x64
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Debug|x64.Build.0 = Debug|x64
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Debug|x86.ActiveCfg = Debug|Win32
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Debug|x86.Build.0 = Debug|Win32
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Release|x64.ActiveCfg = Release|x64
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Release|x64.Build.0 = Release|x64
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Release|x86.ActiveCfg = Release|Win32
{16CF687B-1555-471F-BDAC-27BB21BDB260}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

142
vs-2015/dump1090.vcxproj Normal file
View File

@ -0,0 +1,142 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{16CF687B-1555-471F-BDAC-27BB21BDB260}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PreprocessorDefinitions>_TIMESPEC_DEFINED;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\librtlsdr\include;$(ProjectDir)..\..\pthreads-win32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<TargetMachine>MachineX86</TargetMachine>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>Ws2_32.lib;rtlsdr.lib;pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\librtlsdr\src\Debug;$(ProjectDir)..\..\pthreads-win32\lib\x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PreprocessorDefinitions>_TIMESPEC_DEFINED;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\librtlsdr\include;$(ProjectDir)..\..\pthreads-win32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<TargetMachine>MachineX86</TargetMachine>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>Ws2_32.lib;rtlsdr.lib;pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\librtlsdr\src\Release;$(ProjectDir)..\..\pthreads-win32\lib\x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PreprocessorDefinitions>_TIMESPEC_DEFINED;</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\librtlsdr\include;$(ProjectDir)..\..\pthreads-win32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<WarningLevel>Level3</WarningLevel>
</ClCompile>
<Link>
<AdditionalDependencies>Ws2_32.lib;rtlsdr.lib;pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\librtlsdr\src\Debug;$(ProjectDir)..\..\pthreads-win32\lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PreprocessorDefinitions>_TIMESPEC_DEFINED;</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\librtlsdr\include;$(ProjectDir)..\..\pthreads-win32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<WarningLevel>Level3</WarningLevel>
</ClCompile>
<Link>
<AdditionalDependencies>Ws2_32.lib;rtlsdr.lib;pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\librtlsdr\src\Release;$(ProjectDir)..\..\pthreads-win32\lib\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\anet.c" />
<ClCompile Include="..\dump1090.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\anet.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\gmap.html" />
<None Include="..\testfiles\modes1.bin" />
<None Include="..\tools\debug.html" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>