reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
SelectInterruptor.cxx
Go to the documentation of this file.
2
3#include "rutil/ResipAssert.h"
4#include "rutil/Logger.hxx"
5
6#ifndef WIN32
7#include <unistd.h>
8#endif
9
10using namespace resip;
11
12#define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT
13
15{
16#ifdef WIN32
17 initNetwork(); // Required for windows
18
19 mSocket = ::socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
20
21 sockaddr_in loopback;
22 memset(&loopback, 0, sizeof(loopback));
23 loopback.sin_family = AF_INET;
24 loopback.sin_port = 0;
25 loopback.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
26 makeSocketNonBlocking(mSocket); //win32 woes
27 ::bind( mSocket, reinterpret_cast<sockaddr*>(&loopback), sizeof(loopback));
28 memset(&mWakeupAddr, 0, sizeof(mWakeupAddr));
29 int len = sizeof(mWakeupAddr);
30 int error = getsockname(mSocket, (sockaddr *)&mWakeupAddr, &len);
31 resip_assert(error == 0);
32 error= connect(mSocket, &mWakeupAddr, sizeof(mWakeupAddr));
33 resip_assert(error == 0);
34 mReadThing = mSocket;
35#else
36 int x = pipe(mPipe);
37 (void)x;
38 resip_assert( x != -1 );
39 // make write-side non-blocking to avoid deadlock
40 makeSocketNonBlocking(mPipe[1]);
41 // make read-side non-blocking so safe to read out entire pipe
42 // all in one go (also just safer)
43 makeSocketNonBlocking(mPipe[0]);
44 mReadThing = mPipe[0];
45#endif
46}
47
49{
50#ifdef WIN32
51 closesocket(mSocket);
52#else
53 close(mPipe[0]);
54 close(mPipe[1]);
55#endif
56}
57
58void
63
64void
66{
67#ifdef WIN32
68 fdset.setRead(mSocket);
69#else
70 fdset.setRead(mPipe[0]);
71#endif
72}
73
74void
76{
77#ifdef WIN32
78 char rdBuf[16];
79 recv(mSocket, rdBuf, sizeof(rdBuf), 0);
80#else
81 char rdBuf[16];
82 int x;
83 while ( (x=read(mPipe[0], rdBuf, sizeof(rdBuf))) == sizeof(rdBuf) )
84 ;
85 // WATCHOUT: EWOULDBLOCK *will* happen above when the pending
86 // number of bytes is exactly size of rdBuf
87 // XXX: should check for certain errors (like fd closed) and die?
88#endif
89}
90
91void
93{
94 if (fdset.readyToRead(mReadThing))
95 {
97 }
98}
99
100void
102{
103 if(mask & FPEM_Read)
104 {
106 }
107}
108
109
110void
112{
113 static char wakeUp[] = "w";
114#ifdef WIN32
115 u_long readSize = 0;
116 ioctlsocket(mSocket, FIONREAD, &readSize);
117 // Only bother signalling socket if there is no data on it already
118 if(readSize == 0)
119 {
120 int count = send(mSocket, wakeUp, sizeof(wakeUp), 0);
121 resip_assert(count == sizeof(wakeUp));
122 }
123#else
124 ssize_t res = write(mPipe[1], wakeUp, sizeof(wakeUp));
125
126 if ( res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) ) // Treat EGAIN and EWOULDBLOCK as the same: http://stackoverflow.com/questions/7003234/which-systems-define-eagain-and-ewouldblock-as-different-values
127 {
128 ; // this can happen when SipStack thread gets behind.
129 // no need to block since our only purpose is to wake up the thread
130 // also, this write can occur within the SipStack thread, in which
131 // case we get dead-lock if this blocks
132 }
133 else
134 {
135 resip_assert(res == sizeof(wakeUp));
136 }
137#endif
138}
139
140/* ====================================================================
141 * The Vovida Software License, Version 1.0
142 *
143 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
144 *
145 * Redistribution and use in source and binary forms, with or without
146 * modification, are permitted provided that the following conditions
147 * are met:
148 *
149 * 1. Redistributions of source code must retain the above copyright
150 * notice, this list of conditions and the following disclaimer.
151 *
152 * 2. Redistributions in binary form must reproduce the above copyright
153 * notice, this list of conditions and the following disclaimer in
154 * the documentation and/or other materials provided with the
155 * distribution.
156 *
157 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
158 * and "Vovida Open Communication Application Library (VOCAL)" must
159 * not be used to endorse or promote products derived from this
160 * software without prior written permission. For written
161 * permission, please contact vocal@vovida.org.
162 *
163 * 4. Products derived from this software may not be called "VOCAL", nor
164 * may "VOCAL" appear in their name, without prior written
165 * permission of Vovida Networks, Inc.
166 *
167 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
168 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
170 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
171 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
172 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
173 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
174 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
175 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
176 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
177 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
178 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
179 * DAMAGE.
180 *
181 * ====================================================================
182 *
183 * This software consists of voluntary contributions made by Vovida
184 * Networks, Inc. and many individuals on behalf of Vovida Networks,
185 * Inc. For more information on Vovida Networks, Inc., please see
186 * <http://www.vovida.org/>.
187 *
188 * vi: set shiftwidth=3 expandtab:
189 */
#define FPEM_Read
Definition FdPoll.hxx:29
#define resip_assert(x)
This file needs to work for both C and C++ code.
Object-oriented wrapper for your platform's file-descriptor set.
Definition Socket.hxx:162
bool readyToRead(Socket fd)
Definition Socket.hxx:189
void setRead(Socket fd)
Definition Socket.hxx:204
virtual void processPollEvent(FdPollEventMask mask)
Called by PollGrp when activity is possible.
void process(FdSet &fdset)
cleanup signalled fd
void interrupt()
cause the 'artificial' fd to signal
virtual void handleProcessNotification()
Called by the stack when messages are posted to it.
void buildFdSet(FdSet &fdset)
Used to add the 'artificial' fd to the fdset that will be responsible for interrupting a subsequent s...
dcm! – add UnusedChecking(_enum) below;
unsigned short FdPollEventMask
Definition FdPoll.hxx:28
void initNetwork()
set up network - does nothing in unix but needed for windows
Definition Socket.cxx:83
bool makeSocketNonBlocking(Socket fd)
Definition Socket.cxx:21