From cadcafb23336b41af18b84c290e1d003d753a1df Mon Sep 17 00:00:00 2001 From: jcurtis <25715542+jcurtis-cc@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:09:03 +1100 Subject: [PATCH 1/3] Add Windows support --- build.sh | 11 +++++++++-- main.c | 33 ++++++++++++++++++++++++++++++--- tinyosc.c | 18 +++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/build.sh b/build.sh index 10f7416cc6..4161a3567c 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,14 @@ #!/bin/bash +# Check if the script is being run on Windows +if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then + LIBS="-lWs2_32" +else + LIBS="" +fi + if type "clang" > /dev/null 2>&1; then - clang *.c -Werror -O0 -g -o tinyosc + clang *.c -Werror -O0 -g -o tinyosc $LIBS else - gcc *.c -Werror -std=c99 -O0 -g -o tinyosc + gcc *.c -Werror -std=c99 -O0 -g -o tinyosc $LIBS fi diff --git a/main.c b/main.c index 6f82d8d525..f2e2ce450b 100644 --- a/main.c +++ b/main.c @@ -13,14 +13,19 @@ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ - +#ifdef _WIN32 +#include +#include +#else #include #include +#include +#endif + #include #include #include #include -#include #include "tinyosc.h" @@ -36,6 +41,15 @@ static void sigintHandler(int x) { */ int main(int argc, char *argv[]) { +#ifdef _WIN32 + WSADATA wsaData; + int result = WSAStartup(MAKEWORD(2,2), &wsaData); + if (result != 0) { + printf("WSAStartup failed: %d\n", result); + return 1; // Return immediately if WSAStartup fails + } +#endif + char buffer[2048]; // declare a 2Kb buffer to read packet data into printf("Starting write tests:\n"); @@ -51,7 +65,15 @@ int main(int argc, char *argv[]) { // open a socket to listen for datagrams (i.e. UDP packets) on port 9000 const int fd = socket(AF_INET, SOCK_DGRAM, 0); - fcntl(fd, F_SETFL, O_NONBLOCK); // set the socket to non-blocking + + // set the socket to non-blocking +#ifndef _WIN32 + fcntl(fd, F_SETFL, O_NONBLOCK); +#else + u_long mode = 1; // non blocking + ioctlsocket(fd, FIONBIO, &mode); +#endif + struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(9000); @@ -88,7 +110,12 @@ int main(int argc, char *argv[]) { } // close the UDP socket +#ifdef _WIN32 + closesocket(fd); + WSACleanup(); +#else close(fd); +#endif return 0; } diff --git a/tinyosc.c b/tinyosc.c index 3bfc616969..82fc32988e 100644 --- a/tinyosc.c +++ b/tinyosc.c @@ -19,8 +19,24 @@ #include #include #if _WIN32 -#include +#include #define tosc_strncpy(_dst, _src, _len) strncpy_s(_dst, _len, _src, _TRUNCATE) +unsigned long long htonll(unsigned long long val) { + //little endian check + if(htonl(1) == 1) { + return val; + } else { + return ((unsigned long long)htonl((unsigned int)(val & 0xFFFFFFFF)) << 32) | htonl((unsigned int)(val >> 32)); + } +} +unsigned long long ntohll(unsigned long long val) { + //little endian check + if(ntohl(1) == 1) { + return val; + } else { + return ((unsigned long long)ntohl((unsigned int)(val & 0xFFFFFFFF)) << 32) | ntohl((unsigned int)(val >> 32)); + } +} #else #include #define tosc_strncpy(_dst, _src, _len) strncpy(_dst, _src, _len) From d37ac271fc122dcaad9856c65f452175484e2c2a Mon Sep 17 00:00:00 2001 From: jcurtis <25715542+jcurtis-cc@users.noreply.github.com> Date: Mon, 1 Apr 2024 23:34:28 +1100 Subject: [PATCH 2/3] conditionally add htonll ntohll if not defined --- tinyosc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tinyosc.c b/tinyosc.c index 82fc32988e..85bb92c8c2 100644 --- a/tinyosc.c +++ b/tinyosc.c @@ -21,6 +21,7 @@ #if _WIN32 #include #define tosc_strncpy(_dst, _src, _len) strncpy_s(_dst, _len, _src, _TRUNCATE) +#ifndef htonll unsigned long long htonll(unsigned long long val) { //little endian check if(htonl(1) == 1) { @@ -29,6 +30,8 @@ unsigned long long htonll(unsigned long long val) { return ((unsigned long long)htonl((unsigned int)(val & 0xFFFFFFFF)) << 32) | htonl((unsigned int)(val >> 32)); } } +#endif +#ifndef ntohll unsigned long long ntohll(unsigned long long val) { //little endian check if(ntohl(1) == 1) { @@ -37,6 +40,7 @@ unsigned long long ntohll(unsigned long long val) { return ((unsigned long long)ntohl((unsigned int)(val & 0xFFFFFFFF)) << 32) | ntohl((unsigned int)(val >> 32)); } } +#endif #else #include #define tosc_strncpy(_dst, _src, _len) strncpy(_dst, _src, _len) From e65959e402884d82c2a20a59fb292a46dd81a0dd Mon Sep 17 00:00:00 2001 From: jcurtis <25715542+jcurtis-cc@users.noreply.github.com> Date: Mon, 1 Apr 2024 23:51:28 +1100 Subject: [PATCH 3/3] Rmv custom htonll ntohll defines --- tinyosc.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/tinyosc.c b/tinyosc.c index 85bb92c8c2..2a58aab504 100644 --- a/tinyosc.c +++ b/tinyosc.c @@ -21,26 +21,6 @@ #if _WIN32 #include #define tosc_strncpy(_dst, _src, _len) strncpy_s(_dst, _len, _src, _TRUNCATE) -#ifndef htonll -unsigned long long htonll(unsigned long long val) { - //little endian check - if(htonl(1) == 1) { - return val; - } else { - return ((unsigned long long)htonl((unsigned int)(val & 0xFFFFFFFF)) << 32) | htonl((unsigned int)(val >> 32)); - } -} -#endif -#ifndef ntohll -unsigned long long ntohll(unsigned long long val) { - //little endian check - if(ntohl(1) == 1) { - return val; - } else { - return ((unsigned long long)ntohl((unsigned int)(val & 0xFFFFFFFF)) << 32) | ntohl((unsigned int)(val >> 32)); - } -} -#endif #else #include #define tosc_strncpy(_dst, _src, _len) strncpy(_dst, _src, _len)