Real Time Open Sound Control librtosc
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
thread-link.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Mark McCurry
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef RTOSC_THREAD_LINK
26 #define RTOSC_THREAD_LINK
27 
28 #include <jack/ringbuffer.h>
29 
30 #include <cstring>
31 #include <cassert>
32 #include <cstdio>
33 #include <rtosc/rtosc.h>
34 
35 namespace rtosc {
36 typedef const char *msg_t;
37 
45 {
46  public:
47  ThreadLink(size_t max_message_length, size_t max_messages)
48  :MaxMsg(max_message_length),
49  BufferSize(MaxMsg*max_messages),
50  write_buffer(new char[BufferSize]),
51  read_buffer(new char[BufferSize])
52  {
53  ring = jack_ringbuffer_create(BufferSize);
54  jack_ringbuffer_mlock(ring);
55  }
56 
58  {
59  jack_ringbuffer_free(ring);
60  delete[] write_buffer;
61  delete[] read_buffer;
62  }
63 
68  void write(const char *dest, const char *args, ...)
69  {
70  va_list va;
71  va_start(va,args);
72  const size_t len =
73  rtosc_vmessage(write_buffer,MaxMsg,dest,args,va);
74  if(jack_ringbuffer_write_space(ring) >= len)
75  jack_ringbuffer_write(ring,write_buffer,len);
76  }
77 
82  void writeArray(const char *dest, const char *args, const rtosc_arg_t *aargs)
83  {
84  const size_t len =
85  rtosc_amessage(write_buffer, MaxMsg, dest, args, aargs);
86  if(jack_ringbuffer_write_space(ring) >= len)
87  jack_ringbuffer_write(ring,write_buffer,len);
88  }
89 
93  void raw_write(const char *msg)
94  {
95  const size_t len = rtosc_message_length(msg, -1);//assumed valid
96  if(jack_ringbuffer_write_space(ring) >= len)
97  jack_ringbuffer_write(ring,msg,len);
98  }
99 
103  bool hasNext(void) const
104  {
105  return jack_ringbuffer_read_space(ring);
106  }
107 
111  msg_t read(void) {
112  ring_t r[2];
113  jack_ringbuffer_get_read_vector(ring,(jack_ringbuffer_data_t*)r);
114  const size_t len =
116  assert(jack_ringbuffer_read_space(ring) >= len);
117  assert(len <= MaxMsg);
118  jack_ringbuffer_read(ring, read_buffer, len);
119  return read_buffer;
120  }
121 
125  msg_t peak(void) const
126  {
127  return read_buffer;
128  }
129 
133  char *buffer(void) {return write_buffer;}
137  size_t buffer_size(void) const {return BufferSize;}
138  private:
139  const size_t MaxMsg;
140  const size_t BufferSize;
141  char *write_buffer;
142  char *read_buffer;
143 
144  //assumes jack ringbuffer
145  jack_ringbuffer_t *ring;
146 };
147 };
148 #endif