3 /// Support classes for the pluggable socket routing system.
7 Copyright (C) 2008-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
23 #include "scoped_lock.h"
25 #include "protostructs.h"
33 ///////////////////////////////////////////////////////////////////////////////
34 // SocketDataHandler default methods
36 void SocketRoutingQueue::SocketDataHandler::Error(Barry::Error
&error
)
39 eout("SocketDataHandler: Error: " << error
.what());
43 SocketRoutingQueue::SocketDataHandler::~SocketDataHandler()
48 ///////////////////////////////////////////////////////////////////////////////
49 // SocketRoutingQueue constructors
51 SocketRoutingQueue::SocketRoutingQueue(int prealloc_buffer_count
,
52 int default_read_timeout
)
57 , m_seen_usb_error(false)
58 , m_timeout(default_read_timeout
)
59 , m_continue_reading(false)
61 pthread_mutex_init(&m_mutex
, NULL
);
63 pthread_mutex_init(&m_readwaitMutex
, NULL
);
64 pthread_cond_init(&m_readwaitCond
, NULL
);
66 AllocateBuffers(prealloc_buffer_count
);
69 SocketRoutingQueue::~SocketRoutingQueue()
72 if( m_continue_reading
) {
73 m_continue_reading
= false;
74 pthread_join(m_usb_read_thread
, NULL
);
78 ///////////////////////////////////////////////////////////////////////////////
84 /// Provides a method of returning a buffer to the free queue
85 /// after processing. The DataHandle class calls this automatically
86 /// from its destructor.
87 void SocketRoutingQueue::ReturnBuffer(Data
*buf
)
89 // don't need to lock here, since m_free handles its own locking
96 /// Convenience thread to handle USB read activity.
98 void *SocketRoutingQueue::SimpleReadThread(void *userptr
)
100 SocketRoutingQueue
*q
= (SocketRoutingQueue
*)userptr
;
102 // read from USB and write to stdout until finished
103 q
->m_seen_usb_error
= false;
104 while( q
->m_continue_reading
) {
106 q
->DoRead(1000); // timeout in milliseconds
108 catch (std::runtime_error
const &e
) {
109 eout("SimpleReadThread received uncaught exception: " << typeid(e
).name() << " what: " << e
.what());
112 eout("SimpleReadThread recevied uncaught exception of unknown type");
119 ///////////////////////////////////////////////////////////////////////////////
122 // These functions connect the router to an external Usb::Device
123 // object. Normally this is handled automatically by the
124 // Controller class, but are public here in case they are needed.
125 void SocketRoutingQueue::SetUsbDevice(Usb::Device
*dev
, int writeEp
, int readEp
,
126 SocketDataHandlerPtr callback
)
128 scoped_lock
lock(m_mutex
);
130 m_usb_error_dev_callback
= callback
;
135 void SocketRoutingQueue::ClearUsbDevice()
137 scoped_lock
lock(m_mutex
);
139 m_usb_error_dev_callback
.reset();
142 // wait for the DoRead cycle to finish, so the external
143 // Usb::Device object doesn't close before we're done with it
144 scoped_lock
wait(m_readwaitMutex
);
145 pthread_cond_wait(&m_readwaitCond
, &m_readwaitMutex
);
148 bool SocketRoutingQueue::UsbDeviceReady()
150 scoped_lock
lock(m_mutex
);
151 return m_dev
!= 0 && !m_seen_usb_error
;
157 /// This class starts out with no buffers, and will grow one buffer
158 /// at a time if needed. Call this to allocate count buffers
159 /// all at once and place them on the free queue. After calling
160 /// this function, at least count buffers will exist in the free
161 /// queue. If there are already count buffers, none will be added.
163 void SocketRoutingQueue::AllocateBuffers(int count
)
165 int todo
= count
- m_free
.size();
167 for( int i
= 0; i
< todo
; i
++ ) {
168 // m_free handles its own locking
169 m_free
.push( new Data
);
174 // DefaultRead (both variations)
176 /// Returns the data for the next unregistered socket.
177 /// Blocks until timeout or data is available.
178 /// Returns false (or null pointer) on timeout and no data.
179 /// With the return version of the function, there is no
180 /// copying performed.
182 /// This version performs a copy.
184 bool SocketRoutingQueue::DefaultRead(Data
&receive
, int timeout
)
186 DataHandle buf
= DefaultRead(timeout
);
190 // copy to desired buffer
191 receive
= *buf
.get();
196 /// This version does not perform a copy.
198 DataHandle
SocketRoutingQueue::DefaultRead(int timeout
)
200 if( m_seen_usb_error
&& timeout
== -1 ) {
201 // If an error has been seen and not cleared then no
202 // more data will be read into the queue by
203 // DoRead(). Forcing the timeout to zero allows any
204 // data already in the queue to be read, but prevents
205 // waiting for data which will never arrive.
209 // m_default handles its own locking
210 // Be careful with the queue timeout, since its -1 means "forever"
211 Data
*buf
= m_default
.wait_pop(timeout
== -1 ? m_timeout
: timeout
);
212 return DataHandle(*this, buf
);
218 /// Register an interest in data from a certain socket. To read
219 /// from that socket, use the SocketRead() function from then on.
221 /// Any non-registered socket goes in the default queue
222 /// and must be read by DefaultRead()
224 /// If not null, handler is called when new data is read. It will
225 /// be called in the same thread instance that DoRead() is called from.
226 /// Handler is passed the DataQueue Data pointer, and so no
227 /// copying is done. Once the handler returns, the data is
228 /// considered processed and not added to the interested queue,
229 /// but instead returned to m_free.
231 /// Throws std::logic_error if already registered.
233 void SocketRoutingQueue::RegisterInterest(SocketId socket
,
234 SocketDataHandlerPtr handler
)
236 // modifying our own std::map, need a lock
237 scoped_lock
lock(m_mutex
);
239 SocketQueueMap::iterator qi
= m_socketQueues
.find(socket
);
240 if( qi
!= m_socketQueues
.end() )
241 throw std::logic_error("RegisterInterest requesting a previously registered socket.");
243 m_socketQueues
[socket
] = QueueEntryPtr( new QueueEntry(handler
) );
248 // UnregisterInterest
250 /// Unregisters interest in data from the given socket, and discards
251 /// any existing data in its interest queue. Any new incoming data
252 /// for this socket will be placed in the default queue.
254 void SocketRoutingQueue::UnregisterInterest(SocketId socket
)
256 // modifying our own std::map, need a lock
257 scoped_lock
lock(m_mutex
);
259 SocketQueueMap::iterator qi
= m_socketQueues
.find(socket
);
260 if( qi
== m_socketQueues
.end() )
261 return; // nothing registered, done
263 // salvage all our data buffers
264 m_free
.append_from( qi
->second
->m_queue
);
266 // remove the QueueEntryPtr from the map
267 m_socketQueues
.erase( qi
);
269 // check the interest flag
270 m_interest
= m_socketQueues
.size() > 0;
276 /// Reads data from the interested socket cache. Can only read
277 /// from sockets that have been previously registered.
279 /// Blocks until timeout or data is available.
281 /// Returns false (or null pointer) on timeout and no data.
282 /// With the return version of the function, there is no
283 /// copying performed.
285 /// Throws std::logic_error if a socket was requested that was
286 /// not previously registered.
288 /// Copying is performed with this function.
290 bool SocketRoutingQueue::SocketRead(SocketId socket
, Data
&receive
, int timeout
)
292 DataHandle buf
= SocketRead(socket
, timeout
);
296 // copy to desired buffer
297 receive
= *buf
.get();
302 /// Copying is not performed with this function.
304 /// Throws std::logic_error if a socket was requested that was
305 /// not previously registered.
307 DataHandle
SocketRoutingQueue::SocketRead(SocketId socket
, int timeout
)
312 // accessing our own std::map, need a lock
314 scoped_lock
lock(m_mutex
);
315 SocketQueueMap::iterator qi
= m_socketQueues
.find(socket
);
316 if( qi
== m_socketQueues
.end() )
317 throw std::logic_error("SocketRead requested data from unregistered socket.");
319 // got our queue, save the whole QueueEntryPtr (shared_ptr),
320 // and unlock, since we will be waiting on the DataQueue,
321 // not the socketQueues map
323 // This is safe, since even if UnregisterInterest is called,
324 // our pointer won't be deleted until our shared_ptr
325 // (QueueEntryPtr) goes out of scope.
327 // The remaining problem is that wait_pop() might wait
328 // forever if there is no timeout... c'est la vie.
329 // Should'a used a timeout. :-)
334 // get data from DataQueue
335 // Be careful with the queue timeout, since its -1 means "forever"
336 Data
*buf
= dq
->wait_pop(timeout
== -1 ? m_timeout
: timeout
);
338 // specifically delete our copy of shared pointer, in a locked
341 scoped_lock
lock(m_mutex
);
345 return DataHandle(*this, buf
);
348 // Returns true if data is available for that socket.
349 bool SocketRoutingQueue::IsAvailable(SocketId socket
) const
351 scoped_lock
lock(m_mutex
);
352 SocketQueueMap::const_iterator qi
= m_socketQueues
.find(socket
);
353 if( qi
== m_socketQueues
.end() )
355 return qi
->second
->m_queue
.size() > 0;
361 /// Called by the application's "read thread" to read the next usb
362 /// packet and route it to the correct queue. Returns after every
363 /// read, even if a handler is associated with a queue.
364 /// Note: this function is safe to call before SetUsbDevice() is
365 /// called... it just doesn't do anything if there is no usb
366 /// device to work with.
368 /// Timeout is in milliseconds.
369 // This timeout is for the USB subsystem, so no special handling
370 // for it is needed... just use usbwrap's default timeout.
371 void SocketRoutingQueue::DoRead(int timeout
)
375 pthread_mutex_t
&m_Mutex
;
376 pthread_cond_t
&m_Cond
;
378 ReadWaitSignal(pthread_mutex_t
&mut
, pthread_cond_t
&cond
)
379 : m_Mutex(mut
), m_Cond(cond
)
383 scoped_lock
wait(m_Mutex
);
384 pthread_cond_signal(&m_Cond
);
386 } readwait(m_readwaitMutex
, m_readwaitCond
);
388 Usb::Device
* volatile dev
= 0;
390 DataHandle
buf(*this, 0);
392 // if we are not connected to a USB device yet, just wait
394 scoped_lock
lock(m_mutex
);
396 if( !m_dev
|| m_seen_usb_error
) {
397 lock
.unlock(); // unlock early, since we're sleeping
398 // sleep only a short time, since things could be
399 // in the process of setup or teardown
407 // fetch a free buffer
408 Data
*raw
= m_free
.pop();
410 buf
= DataHandle(*this, new Data
);
412 buf
= DataHandle(*this, raw
);
415 // take a chance and do the read unlocked, as this has the potential
416 // for blocking for a while
419 Data
&data
= *buf
.get();
421 if( !dev
->BulkRead(readEp
, data
, timeout
) )
422 return; // no data, done!
424 MAKE_PACKET(pack
, data
);
426 // make sure the size is right
427 if( data
.GetSize() < SB_PACKET_SOCKET_SIZE
)
428 return; // bad size, just skip
430 // extract the socket from the packet
431 uint16_t socket
= btohs(pack
->socket
);
433 // we have data, now lock up again to place it
434 // in the right queue
435 scoped_lock
lock(m_mutex
);
437 // search for registration of socket
439 SocketQueueMap::iterator qi
= m_socketQueues
.find(socket
);
440 if( qi
!= m_socketQueues
.end() ) {
441 SocketDataHandlerPtr
&sdh
= qi
->second
->m_handler
;
443 // is there a handler?
445 // unlock & let the handler process it
447 sdh
->DataReceived(*buf
.get());
451 qi
->second
->m_queue
.push(buf
.release());
459 // safe to unlock now, we are done with the map
462 // if we get here, send to default queue
463 m_default
.push(buf
.release());
465 catch( Usb::Timeout
& ) {
466 // this is expected... just ignore
468 catch( Usb::Error
&ue
) {
469 // set the flag first, in case any of the handlers
470 // are able to recover from this error
471 m_seen_usb_error
= true;
473 // this is unexpected, but we're in a thread here...
474 // Need to iterate through all the registered handlers
475 // calling their error callback.
476 // Can't be locked when calling the callback, so need
477 // to make a list of them first.
478 scoped_lock
lock(m_mutex
);
479 std::vector
<SocketDataHandlerPtr
> handlers
;
480 SocketQueueMap::iterator qi
= m_socketQueues
.begin();
481 while( qi
!= m_socketQueues
.end() ) {
482 SocketDataHandlerPtr
&sdh
= qi
->second
->m_handler
;
483 // is there a handler?
485 handlers
.push_back(sdh
);
490 SocketDataHandlerPtr usb_error_handler
= m_usb_error_dev_callback
;
493 std::vector
<SocketDataHandlerPtr
>::iterator hi
= handlers
.begin();
494 while( hi
!= handlers
.end() ) {
499 // and finally, call the specific error callback if available
500 if( usb_error_handler
.get() ) {
501 usb_error_handler
->Error(ue
);
506 void SocketRoutingQueue::SpinoffSimpleReadThread()
508 // signal that it's ok to run inside the thread
509 if( m_continue_reading
)
510 return; // already running
511 m_continue_reading
= true;
513 // Start USB read thread, to handle all routing
514 int ret
= pthread_create(&m_usb_read_thread
, NULL
, &SimpleReadThread
, this);
516 m_continue_reading
= false;
517 throw Barry::ErrnoError("SocketRoutingQueue: Error creating USB read thread.", ret
);