/usr/include/barry/router.h is in libbarry-dev 0.15-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | ///
/// \file router.h
/// Support classes for the pluggable socket routing system.
///
/*
Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
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 in the COPYING file at the
root directory of this project for more details.
*/
#ifndef __BARRY_ROUTER_H__
#define __BARRY_ROUTER_H__
#include "dll.h"
#include <stdint.h>
#include <map>
#include <tr1/memory>
#include <stdexcept>
#include <pthread.h>
#include "dataqueue.h"
namespace Usb { class Device; }
namespace Barry {
class DataHandle;
class BXEXPORT SocketRoutingQueue
{
friend class DataHandle;
public:
typedef void (*SocketDataHandler)(void *ctx, Data*); //< See RegisterInterest() for information on this callback.
struct QueueEntry
{
SocketDataHandler m_handler;
void *m_context;
DataQueue m_queue;
QueueEntry(SocketDataHandler h, void *c)
: m_handler(h)
, m_context(c)
{}
};
typedef std::tr1::shared_ptr<QueueEntry> QueueEntryPtr;
typedef uint16_t SocketId;
typedef std::map<SocketId, QueueEntryPtr> SocketQueueMap;
private:
Usb::Device * volatile m_dev;
volatile int m_writeEp, m_readEp;
volatile bool m_interest; // true if at least one socket has an interest.
// used to optimize the reading
mutable pthread_mutex_t m_mutex;// controls access to local data, but not
// DataQueues, as they have their own
// locking per queue
pthread_mutex_t m_readwaitMutex;
pthread_cond_t m_readwaitCond;
DataQueue m_free;
DataQueue m_default;
SocketQueueMap m_socketQueues;
// thread state
pthread_t m_usb_read_thread;
volatile bool m_continue_reading;// set to true when the thread is created,
// then set to false in the destructor
// to signal the end of the thread
// and handle the join
protected:
// Provides a method of returning a buffer to the free queue
// after processing. The DataHandle class calls this automatically
// from its destructor.
void ReturnBuffer(Data *buf);
// Thread function for the simple read behaviour... thread is
// created in the SpinoffSimpleReadThread() member below.
static void *SimpleReadThread(void *userptr);
public:
SocketRoutingQueue(int prealloc_buffer_count = 4);
~SocketRoutingQueue();
// These functions connect the router to an external Usb::Device
// object. Normally this is handled automatically by the
// Controller class, but are public here in case they are needed.
void SetUsbDevice(Usb::Device *dev, int writeEp, int readEp);
void ClearUsbDevice();
bool UsbDeviceReady();
Usb::Device* GetUsbDevice() { return m_dev; }
// This class starts out with no buffers, and will grow one buffer
// at a time if needed. Call this to allocate count buffers
// all at once and place them on the free queue.
void AllocateBuffers(int count);
// Returns the data for the next unregistered socket.
// Blocks until timeout or data is available.
// Returns false (or null pointer) on timeout and no data.
// With the return version of the function, there is no
// copying performed.
bool DefaultRead(Data &receive, int timeout = -1);
DataHandle DefaultRead(int timeout = -1);
// Register an interest in data from a certain socket. To read
// from that socket, use the SocketRead() function from then on.
// Any non-registered socket goes in the default queue
// and must be read by DefaultRead()
// If not null, handler is called when new data is read. It will
// be called in the same thread instance that DoRead() is called from.
// Handler is passed the DataQueue Data pointer, and so no
// copying is done. Once the handler returns, the data is
// considered processed and not added to the interested queue,
// but instead returned to m_free.
void RegisterInterest(SocketId socket, SocketDataHandler handler = 0, void *context = 0);
// Unregisters interest in data from the given socket, and discards
// any existing data in its interest queue. Any new incoming data
// for this socket will be placed in the default queue.
void UnregisterInterest(SocketId socket);
// Reads data from the interested socket cache. Can only read
// from sockets that have been previously registered.
// Blocks until timeout or data is available.
// Returns false (or null pointer) on timeout and no data.
// With the return version of the function, there is no
// copying performed.
bool SocketRead(SocketId socket, Data &receive, int timeout = -1);
DataHandle SocketRead(SocketId socket, int timeout = -1);
// Returns true if data is available for that socket.
bool IsAvailable(SocketId socket) const;
// Called by the application's "read thread" to read the next usb
// packet and route it to the correct queue. Returns after every
// read, even if a handler is associated with a queue.
// Note: this function is safe to call before SetUsbDevice() is
// called... it just doesn't do anything if there is no usb
// device to work with.
//
// Timeout is in milliseconds.
//
// Returns false in the case of USB errors, and puts the error
// message in msg.
bool DoRead(std::string &msg, int timeout = -1);
// Utility function to make it easier for the user to create the
// USB pure-read thread. If the user wants anything more complicated
// in this background thread, he can implement it himself and call
// the above DoRead() in a loop. If only the basics are needed,
// then this makes it easy.
// Throws Barry::ErrnoError on thread creation error.
void SpinoffSimpleReadThread();
};
//
// DataHandle
//
/// std::auto_ptr like class that handles pointers to Data, but instead of
/// freeing them completely, the Data objects are turned to the
/// SocketRoutingQueue from whence they came.
///
class BXEXPORT DataHandle
{
private:
SocketRoutingQueue &m_queue;
mutable Data *m_data;
protected:
void clear()
{
if( m_data ) {
m_queue.ReturnBuffer(m_data);
m_data = 0;
}
}
public:
DataHandle(SocketRoutingQueue &q, Data *data)
: m_queue(q)
, m_data(data)
{
}
DataHandle(const DataHandle &other)
: m_queue(other.m_queue)
, m_data(other.m_data)
{
// we now own the pointer
other.m_data = 0;
}
~DataHandle()
{
clear();
}
Data* get()
{
return m_data;
}
Data* release() // no longer owns the pointer
{
Data *ret = m_data;
m_data = 0;
return ret;
}
Data* operator->()
{
return m_data;
}
const Data* operator->() const
{
return m_data;
}
DataHandle& operator=(const DataHandle &other)
{
if( &m_queue != &other.m_queue )
throw std::logic_error("Trying to copy DataHandles of different queues!");
// remove our current data
clear();
// accept the new
m_data = other.m_data;
// we now own it
other.m_data = 0;
return *this;
}
};
} // namespace Barry
#endif
|