Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows support #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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
33 changes: 30 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

#ifdef _WIN32
#include <WinSock2.h>
#include <WS2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/select.h>
#include <unistd.h>
#endif

#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

#include "tinyosc.h"

Expand All @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion tinyosc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <string.h>
#include <stdio.h>
#if _WIN32
#include <winsock2.h>
#include <Winsock2.h>
#define tosc_strncpy(_dst, _src, _len) strncpy_s(_dst, _len, _src, _TRUNCATE)
#else
#include <netinet/in.h>
Expand Down