1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18
19 #include "osclconfig_io.h"
20 #include "oscl_socket_tuneables.h"
21
22 #if(PV_SOCKET_SERVER)
23 #include "oscl_socket_serv_imp_reqlist.h"
24
25 #include "oscl_scheduler_ao.h"
26 #include "oscl_socket_method.h"
27 #include "oscl_socket_types.h"
28 #include "oscl_socket_serv_imp.h"
29 #include "oscl_error.h"
30 #include "oscl_socket_imp.h"
31 #include "oscl_assert.h"
32
33
34
35 //
36 //OsclSocketServRequestList
37 //
38
39
OsclSocketServRequestList()40 OsclSocketServRequestList::OsclSocketServRequestList()
41 {
42 iContainer = NULL;
43 }
44
45
46 //Log app thread calls.
47 #define LOGAPP(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_MLDBG,obj->iSocketI->Logger(),PVLOGMSG_DEBUG,m);
48
49 //Log server thread calls.
50 #define LOGSERV(m) PVLOGGER_LOGMSG(PVLOGMSG_INST_MLDBG,iContainer->iLogger,PVLOGMSG_DEBUG,m);
51
52
Add(OsclSocketRequest * obj)53 void OsclSocketServRequestList::Add(OsclSocketRequest *obj)
54 //add a socket request to the server (from the app thread)
55 {
56 //Lock when adding requests to the input queue.
57 Lock();
58
59 bool empty = iAddRequests.empty();
60 iAddRequests.push_back(obj);
61
62 //signal the server thread anytime the queue goes from empty to non-empty.
63 if (empty)
64 Wakeup();
65
66 //may need to interrupt a blocking select call
67 iContainer->WakeupBlockingSelect();
68
69 Unlock();
70 }
71
StartCancel(OsclSocketRequest * obj)72 void OsclSocketServRequestList::StartCancel(OsclSocketRequest *obj)
73 //Cancel a socket request to the server (from the app thread)
74 {
75 //queue up a cancel request to the server. note: if the request is still
76 //in the new request queue, it might seem simpler to just complete the
77 //request here, but for thread safety all requests must be completed by
78 //the server thread.
79 Lock();
80 iCancelRequests.push_back(obj);
81 Unlock();
82
83 #if(PV_SOCKET_SERVER_IS_THREAD)
84 //may need to interrupt a blocking select call
85 iContainer->WakeupBlockingSelect();
86 #else
87 //for AO implementation, call the request processing routine
88 //so that the request will get canceled.
89 #if PV_SOCKET_SERVER_SELECT
90 iContainer->ProcessSocketRequests(iContainer->iNhandles, iContainer->iNfds);
91 #else
92 iContainer->ProcessSocketRequests();
93 #endif
94 #endif
95 }
96
GetNewRequests()97 void OsclSocketServRequestList::GetNewRequests()
98 //called by server to pick up new requests.
99 {
100 Oscl_Vector<OsclSocketRequest*, OsclMemAllocator>::iterator it;
101
102 //go through the new requests list and move them to the active requests list.
103 for (it = iAddRequests.begin(); it != iAddRequests.end(); it++)
104 {
105 OsclSocketServRequestQElem elem(*it);
106 iActiveRequests.push_back(elem);
107 }
108 iAddRequests.clear();
109
110 //now go through cancel requests and set cancel flags
111 //in the corresponding active requests.
112 for (it = iCancelRequests.begin(); it != iCancelRequests.end(); it++)
113 {
114 for (uint32 i = 0; i < iActiveRequests.size(); i++)
115 {
116 if (iActiveRequests[i].iSocketRequest == *it)
117 {
118 iActiveRequests[i].iCancel = true;
119 break;
120 }
121 }
122 }
123 iCancelRequests.clear();
124 }
125
Wakeup()126 void OsclSocketServRequestList::Wakeup()
127 //wakeup the server
128 {
129 #if(PV_SOCKET_SERVER_IS_THREAD)
130 //signal the thread
131 iSem.Signal();
132 #else
133 //wakeup the AO.
134 iContainer->WakeupAO();
135 #endif
136 }
137
Open(OsclSocketServI * s)138 void OsclSocketServRequestList::Open(OsclSocketServI* s)
139 {//implementation-specific open
140 iContainer = s;
141 #if(PV_SOCKET_SERVER_IS_THREAD)
142 iCrit.Create();
143 iSem.Create();
144 #endif
145 }
146
Close()147 void OsclSocketServRequestList::Close()
148 {//implementation-specific close
149 iActiveRequests.clear();
150 iActiveRequests.destroy();
151 iAddRequests.clear();
152 iAddRequests.destroy();
153 iCancelRequests.clear();
154 iCancelRequests.destroy();
155 #if(PV_SOCKET_SERVER_IS_THREAD)
156 iCrit.Close();
157 iSem.Close();
158 #endif
159 }
160
161
WaitOnRequests()162 void OsclSocketServRequestList::WaitOnRequests()
163 //called by server thread to wait on requests.
164 {
165 #if(PV_SOCKET_SERVER_IS_THREAD)
166 //see if there are new requests
167 iCrit.Lock();
168 bool empty = iAddRequests.empty();
169 iCrit.Unlock();
170 //if no new requests. then wait.
171 if (empty)
172 iSem.Wait();
173 #endif
174 }
175
Lock()176 void OsclSocketServRequestList::Lock()
177 {
178 #if(PV_SOCKET_SERVER_IS_THREAD)
179 iCrit.Lock();
180 #endif
181 }
182
Unlock()183 void OsclSocketServRequestList::Unlock()
184 {
185 #if(PV_SOCKET_SERVER_IS_THREAD)
186 iCrit.Unlock();
187 #endif
188 }
189
190 #endif//PV_SOCKET_SERVER
191
192
193
194
195
196
197