/usr/include/ptclib/sockagg.h is in libpt-1.10.10-dev 1.10.10-3.1ubuntu1.
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | /*
* sockagg.h
*
* Generalised Socket Aggregation functions
*
* Portable Windows Library
*
* Copyright (C) 2005 Post Increment
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Post Increment
*
* Portions of this code were written with the financial assistance of
* Metreos Corporation (http://www.metros.com).
*
* Contributor(s): ______________________________________.
*
* $Log: sockagg.h,v $
* Revision 1.4 2006/01/03 04:23:32 csoutheren
* Fixed Unix implementation
*
* Revision 1.3 2005/12/23 06:44:30 csoutheren
* Working implementation
*
* Revision 1.2 2005/12/22 07:27:36 csoutheren
* More implementation
*
* Revision 1.1 2005/12/22 03:55:52 csoutheren
* Added initial version of socket aggregation classes
*
*
*/
#ifndef _SOCKAGG_H
#define _SOCKAGG_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib.h>
#include <ptlib/sockets.h>
/*
These classes implements a generalised method for aggregating sockets so that they can be handled by a single thread. It is
intended to provide a backwards-compatible mechanism to supplant the "one socket - one thread" model used by OpenH323
and OPAL with a model that provides a better thread to call ratio. A more complete explanation of this model can be
found in the white paper "Increasing the Maximum Call Density of OpenH323 and OPAL" which can be at:
http://www.voxgratia.org/docs/call%20thread%20handling%20model%201.0.pdf
There are two assumptions made by this code:
1) The most efficient way to handle I/O is for a thread to be blocked on I/O. Any sort of timer or other
polling mechanism is less efficient
2) The time taken to handle a received PDU is relatively small, and will not interfere with the handling of
other calls that are handled in the same thread
UDP and TCP sockets are aggregated in different ways. UDP sockets are aggregated on the basis of a simple loop that looks
for received datagrams and then processes them. TCP sockets are more complex because there is no intrinsic record-marking
protocol, so it is difficult to know when a complete PDU has been received. This problem is solved by having the loop collect
received data into a buffer until the handling routine decides that a full PDU has been received.
At the heart of each socket aggregator is a select statement that contains all of the file descriptors that are managed
by the thread. One extra handle for a pipe (or on Windows, a local socket) is added to each handle list so that the thread can
be woken up in order to allow the addition or removal of sockets to the list
*/
#include <ptlib.h>
#include <functional>
#include <vector>
/////////////////////////////////////////////////////////////////////////////////////
//
// this class encapsulates the system specific handle needed to specifiy a socket.
// On Unix systems, this is a simple file handle. This file handle is used to uniquely
// identify the socket and used in the "select" system call
// On Windows systems the SOCKET handle is used to identify the socket, but a seperate WSAEVENT
// handle is needed for the WSWaitForMultpleEvents call.
// This is further complicated by the fact that we need to treat some pairs of sockets as a single
// entity (i.e. RTP sockets) to avoid rewriting the RTP handler code.
//
class PAggregatedHandle;
class PAggregatorFD
{
public:
#ifdef _WIN32
typedef WSAEVENT FD;
typedef SOCKET FDType;
SOCKET socket;
#else
typedef int FD;
typedef int FDType;
#endif
PAggregatorFD(FDType fd);
FD fd;
~PAggregatorFD();
bool IsValid();
};
typedef std::vector<PAggregatorFD *> PAggregatorFDList_t;
/////////////////////////////////////////////////////////////////////////////////////
//
// This class defines an abstract class used to define a handle that can be aggregated
//
// Normally this will correspond directly to a socket, but for RTP this actually corresponds to two sockets
// which greatly complicates things
//
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
#endif
class PAggregatedHandle : public PObject
{
PCLASSINFO(PAggregatedHandle, PObject);
public:
PAggregatedHandle(BOOL _autoDelete = FALSE)
: autoDelete(_autoDelete), preReadDone(FALSE)
{ }
virtual PAggregatorFDList_t GetFDs() = 0;
virtual PTimeInterval GetTimeout()
{ return PMaxTimeInterval; }
virtual BOOL Init() { return TRUE; }
virtual BOOL PreRead() { return TRUE; }
virtual BOOL OnRead() = 0;
virtual void DeInit() { }
virtual BOOL IsPreReadDone() const
{ return preReadDone; }
virtual void SetPreReadDone(BOOL v = TRUE)
{ preReadDone = v; }
BOOL autoDelete;
protected:
BOOL preReadDone;
};
#ifdef _MSC_VER
#pragma warning(pop)
#endif
/////////////////////////////////////////////////////////////////////////////////////
//
// This class is the actual socket aggregator
//
class PHandleAggregator : public PObject
{
PCLASSINFO(PHandleAggregator, PObject)
public:
class EventBase
{
public:
virtual PAggregatorFD::FD GetHandle() = 0;
virtual void Set() = 0;
virtual void Reset() = 0;
};
typedef std::vector<PAggregatedHandle *> HandleContextList_t;
class WorkerThreadBase : public PThread
{
public:
WorkerThreadBase(EventBase & _event)
: PThread(100, NoAutoDeleteThread), event(_event), listChanged(TRUE)
{ }
virtual void Trigger() = 0;
void Main();
EventBase & event;
PMutex mutex;
BOOL listChanged;
HandleContextList_t handleList;
};
typedef std::vector<WorkerThreadBase *> WorkerList_t;
PHandleAggregator(unsigned _max = 10);
BOOL AddHandle(PAggregatedHandle * handle);
BOOL RemoveHandle(PAggregatedHandle * handle);
PMutex mutex;
WorkerList_t workers;
unsigned maxWorkerSize;
unsigned minWorkerSize;
};
/////////////////////////////////////////////////////////////////////////////////////
//
// This template class allows the creation of aggregators for sockets that are
// descendants of PIPSocket
//
template <class PSocketType>
class PSocketAggregator : public PHandleAggregator
{
PCLASSINFO(PSocketAggregator, PHandleAggregator)
public:
class AggregatedPSocket : public PAggregatedHandle
{
public:
AggregatedPSocket(PSocketType * _s)
: psocket(_s), fd(_s->GetHandle()) { }
BOOL OnRead()
{ return psocket->OnRead(); }
PAggregatorFDList_t GetFDs()
{ PAggregatorFDList_t list; list.push_back(&fd); return list; }
protected:
PSocketType * psocket;
PAggregatorFD fd;
};
typedef std::map<PSocketType *, AggregatedPSocket *> SocketList_t;
SocketList_t socketList;
BOOL AddSocket(PSocketType * sock)
{
PWaitAndSignal m(mutex);
AggregatedPSocket * handle = new AggregatedPSocket(sock);
if (AddHandle(handle)) {
socketList.insert(SocketList_t::value_type(sock, handle));
return true;
}
delete handle;
return false;
}
BOOL RemoveSocket(PSocketType * sock)
{
PWaitAndSignal m(mutex);
typename SocketList_t::iterator r = socketList.find(sock);
if (r == socketList.end())
return FALSE;
AggregatedPSocket * handle = r->second;
RemoveHandle(handle);
delete handle;
socketList.erase(r);
return TRUE;
}
};
#endif
|