forked from mapmapteam/mapmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OscReceiver.cpp
91 lines (81 loc) · 2.48 KB
/
OscReceiver.cpp
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
#ifdef HAVE_OSC
#include "OscReceiver.h"
#include <iostream>
#include <cstdio>
bool OscReceiver::server_is_ok_ = true;
OscReceiver::OscReceiver(const std::string &port) :
port_(port),
server_(0)
{
server_ = lo_server_thread_new(port_.c_str(), error);
server_is_ok_ = (server_ != NULL);
// #ifdef CONFIG_DEBUG
// /* add method that will match any path and args */
// lo_server_thread_add_method(server_, NULL, NULL, genericHandler, this);
// #endif
}
OscReceiver::~OscReceiver()
{
// std::cout << "Freeing OSC server thread\n";
lo_server_thread_free(server_);
}
void OscReceiver::addHandler(const char *path, const char *types, lo_method_handler handler, void *userData)
{
if (server_is_ok_)
lo_server_thread_add_method(server_, path, types, handler, userData);
else
std::cout << "Could not add OSC handler " << path << std::endl;
}
void OscReceiver::listen()
{
if (server_is_ok_)
{
int lo_fd = lo_server_get_socket_fd(server_);
if (lo_fd == 0)
{
std::cout << "OSC port " << port_ << " is already in use." << std::endl;
}
else
{
std::cout << "Listening on port " << port_ << std::endl;
lo_server_thread_start(server_);
}
}
else
{
std::cout << "Could not start OSC receiver. Maybe that ";
std::cout << "OSC port " << port_ << " is already in use." << std::endl;
}
}
void OscReceiver::error(int num, const char *msg, const char *path)
{
std::cerr << "liblo server error " << num << " in path " << path
<< ": " << msg << std::endl;
OscReceiver::server_is_ok_ = false;
}
// #ifdef CONFIG_DEBUG
// /* catch any incoming messages and display them. returning 1 means that the
// * * message has not been fully handled and the server should try other methods */
// int OscReceiver::genericHandler(const char *path,
// const char *types, lo_arg **argv,
// int argc, void * /*data*/, void * /*user_data*/)
// {
// //OscReceiver *context = static_cast<OscReceiver*>(user_data);
// printf("path: <%s>\n", path);
// for (int i = 0; i < argc; ++i)
// {
// printf("arg %d '%c' ", i, types[i]);
// lo_arg_pp(static_cast<lo_type>(types[i]), argv[i]);
// printf("\n");
// }
// printf("\n");
// fflush(stdout);
//
// return 1;
// }
// #endif // CONFIG_DEBUG
std::string OscReceiver::toString() const
{
return "port:" + port_;
}
#endif // HAVE_OSC