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 #ifndef OSCL_SOCKET_METHOD_H_INCLUDED
20 #define OSCL_SOCKET_METHOD_H_INCLUDED
21
22 #include "osclconfig_io.h"
23 #include "oscl_socket_types.h"
24 //#include "oscl_socket_serv_imp.h"
25 #include "oscl_scheduler_ao.h"
26 #include "oscl_socket_request.h"
27 #include "pvlogger.h"
28 #include "oscl_socket_tuneables.h"
29 #include "oscl_ip_socket.h"
30
31 #define MSEC_TO_MICROSEC 1000
32
33 class OsclSocketI;
34 class OsclSocketObserver;
35 class OsclSocketRequestAO;
36 class OsclIPSocketI;
37
38 /** OsclSocketMethod is the base class for all socket methods.
39 * Two AOs are required for each socket operation-- one to provide
40 * a timeout, and one to detect request completion. The OsclSocketMethod
41 * class implements the timeout and contains the request completion AO.
42 */
43 class OsclSocketMethod : public OsclTimerObject
44 {
45 public:
OsclSocketMethod(OsclIPSocketI & aContainer,const char * name,TPVSocketFxn fxn)46 OsclSocketMethod(OsclIPSocketI& aContainer, const char *name, TPVSocketFxn fxn)
47 : OsclTimerObject(PV_SOCKET_REQUEST_AO_PRIORITY, name)
48 , iContainer(aContainer)
49 , iSocketFxn(fxn)
50 , iSocketRequestAO(NULL)
51 {
52 }
~OsclSocketMethod()53 virtual ~OsclSocketMethod()
54 {}
55
Abort()56 void Abort()
57 {
58 Cancel();
59 }
60
61 inline void AbortAll();
62
63 inline void CancelMethod();
64
65 OsclIPSocketI& iContainer;
66
67 TPVSocketFxn iSocketFxn;
68
Alloc()69 Oscl_DefAlloc& Alloc()
70 {
71 return iContainer.Alloc();
72 }
73
74 protected:
ConstructL(OsclSocketRequestAO * aAO)75 void ConstructL(OsclSocketRequestAO *aAO)
76 {
77 if (!aAO)
78 OsclError::Leave(OsclErrGeneral);
79 iSocketRequestAO = aAO;
80 }
81
82 bool StartMethod(int32 aTimeoutMsec);
83
84 inline void MethodDone();
85
86 void Run();
87
88 OsclSocketRequestAO *iSocketRequestAO;
89 };
90
91 #include "oscl_socket_imp.h"
92
93 /** This is the base class for all the AOs that
94 interact with the socket server.
95 This object is contained within an OsclSocketMethod object
96 */
97 class OsclSocketRequestAO : public OsclActiveObject
98 {
99 public:
ConstructL()100 void ConstructL()
101 {
102 }
103 protected:
104
OsclSocketRequestAO(OsclSocketMethod & aContainer,const char * name)105 OsclSocketRequestAO(OsclSocketMethod& aContainer, const char *name)
106 : OsclActiveObject(PV_SOCKET_REQUEST_AO_PRIORITY, name)
107 , iContainer(aContainer)
108 , iSocketError(0)
109 , iParam(NULL)
110 , iParamSize(0)
111 {}
112
~OsclSocketRequestAO()113 virtual ~OsclSocketRequestAO()
114 {
115 CleanupParam(true);
116 }
117
118 OsclAny* NewRequest(const uint32 size) ;
119 void CleanupParam(bool deallocate = false);
120
121
Abort()122 void Abort()
123 {
124 Cancel();
125 }
126
RequestDone()127 void RequestDone()
128 {
129 iContainer.Abort();
130 }
131
132 inline int GetSocketError();
133
DoCancel()134 void DoCancel()
135 {
136 SocketI()->CancelFxn(iContainer.iSocketFxn);
137 //we launch the cancellation process here. oscl scheduler
138 //will wait on completion of this AO request, which will
139 //happen in the server thread.
140 }
141
142 void Run();
143
Success()144 virtual void Success()
145 {}
146
147 OsclSocketMethod& iContainer;
148 int32 iSocketError;
149 SocketRequestParam *iParam;
150 uint32 iParamSize;
151
SocketI()152 OsclSocketI *SocketI()
153 {
154 return iContainer.iContainer.iSocket;
155 }
156
SocketObserver()157 OsclSocketObserver* SocketObserver()
158 {
159 return iContainer.iContainer.iObserver;
160 }
Id()161 uint32 Id()
162 {
163 return iContainer.iContainer.iId;
164 }
Alloc()165 Oscl_DefAlloc& Alloc()
166 {
167 return iContainer.iContainer.Alloc();
168 }
169
170 friend class OsclSocketI;
171 friend class OsclSocketMethod;
172 friend class OsclSocketRequest;
173 };
174
AbortAll()175 inline void OsclSocketMethod::AbortAll()
176 {
177 Abort();
178 if (iSocketRequestAO)
179 iSocketRequestAO->Abort();
180 }
181
CancelMethod()182 inline void OsclSocketMethod::CancelMethod()
183 {
184 //cancel the timeout if any
185 Abort();
186 //cancel the request to the socket server.
187 iSocketRequestAO->DoCancel();
188 }
189
MethodDone()190 inline void OsclSocketMethod::MethodDone()
191 {
192 iSocketRequestAO->Abort();
193 }
194
195 #endif
196
197