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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// osc responder class
// Copyright (C) 2008 Tim Blechmann
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#ifndef OSC_SERVER_HPP
#define OSC_SERVER_HPP
#include <string>
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include "branch_hints.hpp"
#include "osc_dispatcher.hpp"
#include "static_allocator.hpp"
#include "nova-tt/semaphore.hpp"
#include "nova-tt/thread_priority.hpp"
#include "nova-tt/name_thread.hpp"
namespace nova {
namespace detail {
using namespace boost::asio;
using namespace boost::asio::ip;
class network_thread
{
public:
void start_receive(void)
{
thread_ = std::move(std::thread(network_thread::start, this));
sem.wait();
}
~network_thread(void)
{
if (!thread_.joinable())
return;
io_service_.stop();
thread_.join();
}
io_service & get_io_service(void)
{
return io_service_;
}
void send_udp(const char * data, unsigned int size, udp::endpoint const & receiver)
{
udp::socket socket(io_service_);
socket.open(udp::v4());
socket.send_to(boost::asio::buffer(data, size), receiver);
}
private:
static void start(network_thread * self)
{
#ifdef NOVA_TT_PRIORITY_RT
thread_set_priority_rt(thread_priority_interval_rt().first);
#endif
name_thread("Network Receive");
self->run();
}
void run(void)
{
sem.post();
io_service::work work(io_service_);
io_service_.run();
}
protected:
io_service io_service_;
private:
semaphore sem;
std::thread thread_;
};
class osc_server:
private network_thread,
private osc_dispatcher
{
public:
osc_server(unsigned int port):
socket_(network_thread::io_service_, udp::endpoint(udp::v4(), port))
{
network_thread::start_receive();
start_receive();
}
void add_responder(std::string const & address, osc_responder * resp)
{
osc_dispatcher::add_responder(address, resp);
}
void remove_responder(std::string const & address, osc_responder * resp)
{
osc_dispatcher::remove_responder(address, resp);
}
private:
void start_receive(void)
{
socket_.async_receive_from(
buffer(recv_buffer_), remote_endpoint_,
boost::bind(&osc_server::handle_receive, this,
placeholders::error, placeholders::bytes_transferred));
}
void handle_receive(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
if (unlikely(error == error::operation_aborted))
return; /* we're done */
if (error == error::message_size)
{
overflow_vector.insert(overflow_vector.end(),
recv_buffer_.begin(), recv_buffer_.end());
return;
}
if (error)
{
std::cout << "osc_server received error code " << error << std::endl;
start_receive();
return;
}
if (overflow_vector.empty())
osc_dispatcher::handle_packet(recv_buffer_.begin(), bytes_transferred);
else
{
overflow_vector.insert(overflow_vector.end(),
recv_buffer_.begin(), recv_buffer_.end());
osc_dispatcher::handle_packet(overflow_vector.data(), overflow_vector.size());
overflow_vector.clear();
}
start_receive();
return;
}
udp::socket socket_;
boost::array<char, 1<<13 > recv_buffer_;
udp::endpoint remote_endpoint_;
typedef static_allocator<char, 1<<20> string_allocator; /* 1 MB memory pool ...
*
* not thread-safe, but that is
* not required here
*
* */
/* typedef std::basic_string<char, std::char_traits<char>, string_allocator> string; */
typedef std::string string;
string overflow_vector;
};
} /* namespace */
using detail::network_thread;
using detail::osc_server;
} /* namespace nova */
#endif /* OSC_SERVER_HPP */
|