-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.c
94 lines (82 loc) · 3.02 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright (c) 2015, Martin Roth ([email protected])
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h>
#include <sys/select.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "tinyosc.h"
static volatile bool keepRunning = true;
// handle Ctrl+C
static void sigintHandler(int x) {
keepRunning = false;
}
/**
* A basic program to listen to port 9000 and print received OSC packets.
*/
int main(int argc, char *argv[]) {
char buffer[2048]; // declare a 2Kb buffer to read packet data into
printf("Starting write tests:\n");
int len = 0;
char blob[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
len = tosc_writeMessage(buffer, sizeof(buffer), "/address", "fsibTFNI",
1.0f, "hello world", -1, sizeof(blob), blob);
tosc_printOscBuffer(buffer, len);
printf("done.\n");
// register the SIGINT handler (Ctrl+C)
signal(SIGINT, &sigintHandler);
// 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
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(9000);
sin.sin_addr.s_addr = INADDR_ANY;
bind(fd, (struct sockaddr *) &sin, sizeof(struct sockaddr_in));
printf("tinyosc is now listening on port 9000.\n");
printf("Press Ctrl+C to stop.\n");
while (keepRunning) {
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(fd, &readSet);
struct timeval timeout = {1, 0}; // select times out after 1 second
if (select(fd+1, &readSet, NULL, NULL, &timeout) > 0) {
struct sockaddr sa; // can be safely cast to sockaddr_in
socklen_t sa_len = sizeof(struct sockaddr_in);
int len = 0;
while ((len = (int) recvfrom(fd, buffer, sizeof(buffer), 0, &sa, &sa_len)) > 0) {
if (tosc_isBundle(buffer)) {
tosc_bundle bundle;
tosc_parseBundle(&bundle, buffer, len);
const uint64_t timetag = tosc_getTimetag(&bundle);
tosc_message osc;
while (tosc_getNextMessage(&bundle, &osc)) {
tosc_printMessage(&osc);
}
} else {
tosc_message osc;
tosc_parseMessage(&osc, buffer, len);
tosc_printMessage(&osc);
}
}
}
}
// close the UDP socket
close(fd);
return 0;
}