0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* System Includes
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
#include <stdio.h>
|
|
22 |
#include <assert.h>
|
|
23 |
#ifndef WIN32
|
|
24 |
#include <unistd.h>
|
|
25 |
#include <errno.h>
|
|
26 |
#endif
|
|
27 |
|
|
28 |
|
|
29 |
/*******************************************************************************
|
|
30 |
*
|
|
31 |
* Local Includes
|
|
32 |
*
|
|
33 |
******************************************************************************/
|
|
34 |
#include "CTCPTEChannel.h"
|
|
35 |
|
|
36 |
|
|
37 |
/*******************************************************************************
|
|
38 |
*
|
|
39 |
* Definitions
|
|
40 |
*
|
|
41 |
******************************************************************************/
|
|
42 |
#ifndef WIN32
|
|
43 |
#define SOCKET_ERROR (-1)
|
|
44 |
#define INVALID_SOCKET (-1)
|
|
45 |
#define Sleep(x) sleep(x/1000)
|
|
46 |
#else
|
|
47 |
#define SHUT_RDWR SD_BOTH
|
|
48 |
#endif
|
|
49 |
|
|
50 |
|
|
51 |
/*******************************************************************************
|
|
52 |
*
|
|
53 |
* Macro Functions
|
|
54 |
*
|
|
55 |
******************************************************************************/
|
|
56 |
#ifndef WIN32
|
|
57 |
#define closesocket(x) (shutdown(x,SHUT_RDWR),close(x))
|
|
58 |
#endif
|
|
59 |
|
|
60 |
|
|
61 |
/*******************************************************************************
|
|
62 |
*
|
|
63 |
* Constructor -- initialise all state
|
|
64 |
*
|
|
65 |
******************************************************************************/
|
|
66 |
CTcpTeChannel::CTcpTeChannel( TPhoneData *aPhoneData, CLog *aLog )
|
|
67 |
{
|
|
68 |
// verify args
|
|
69 |
assert( aPhoneData != NULL );
|
|
70 |
assert( aLog != NULL );
|
|
71 |
|
|
72 |
// initialise state
|
|
73 |
iPhoneData = aPhoneData;
|
|
74 |
iDatalink = NULL;
|
|
75 |
iFilter = NULL;
|
|
76 |
iLog = aLog;
|
|
77 |
iSock = INVALID_SOCKET;
|
|
78 |
iStreamBuffer[0] = 0;
|
|
79 |
iExitFlag = 0;
|
|
80 |
iSocketSetFlag = 0;
|
|
81 |
}
|
|
82 |
|
|
83 |
CTcpTeChannel::~CTcpTeChannel()
|
|
84 |
{
|
|
85 |
assert( iSock == INVALID_SOCKET );
|
|
86 |
}
|
|
87 |
|
|
88 |
|
|
89 |
/*******************************************************************************
|
|
90 |
*
|
|
91 |
* PUBLIC METHOD: RPC-THREAD. SetDatalink, called by the main setup call in
|
|
92 |
* cphone - links up all the bits.
|
|
93 |
*
|
|
94 |
******************************************************************************/
|
|
95 |
void CTcpTeChannel::SetDatalink( IProcessData *aDatalink )
|
|
96 |
{
|
|
97 |
iDatalink = aDatalink;
|
|
98 |
}
|
|
99 |
|
|
100 |
|
|
101 |
/*******************************************************************************
|
|
102 |
*
|
|
103 |
* PUBLIC METHOD: RPC-THREAD. SetFilter, called by the main setup call in
|
|
104 |
* cphone - links up all the bits.
|
|
105 |
*
|
|
106 |
******************************************************************************/
|
|
107 |
void CTcpTeChannel::SetFilter( IFilter *aFilter )
|
|
108 |
{
|
|
109 |
iFilter = aFilter;
|
|
110 |
}
|
|
111 |
|
|
112 |
|
|
113 |
/*******************************************************************************
|
|
114 |
*
|
|
115 |
* PUBLIC METHOD: SERVER-THREAD:
|
|
116 |
*
|
|
117 |
******************************************************************************/
|
|
118 |
TChannelError CTcpTeChannel::SetSocket( int aSocket )
|
|
119 |
{
|
|
120 |
// check that the socket is sane
|
|
121 |
assert( aSocket >= 0 );
|
|
122 |
|
|
123 |
// check that we haven't already set if
|
|
124 |
if( iSocketSetFlag != 0 ) {
|
|
125 |
return CE_SOCKET_ALREADY_SET;
|
|
126 |
}
|
|
127 |
|
|
128 |
// ok - set the socket
|
|
129 |
iSock = aSocket;
|
|
130 |
iSocketSetFlag = 1;
|
|
131 |
return CE_NONE;
|
|
132 |
}
|
|
133 |
|
|
134 |
|
|
135 |
/*******************************************************************************
|
|
136 |
*
|
|
137 |
* PUBLIC METHOD: LISTEN-THREAD: ListenOnChannel. This is the entry point for
|
|
138 |
* the listening thread. It sits in the loop below and passes all received data
|
|
139 |
* to the datalink layer.
|
|
140 |
*
|
|
141 |
******************************************************************************/
|
|
142 |
TChannelError CTcpTeChannel::ListenOnChannel( int *aErrCode )
|
|
143 |
{
|
|
144 |
TDataPathError derr;
|
|
145 |
int err;
|
|
146 |
int errcode;
|
|
147 |
|
|
148 |
// check the params
|
|
149 |
assert( aErrCode != NULL );
|
|
150 |
*aErrCode = 0;
|
|
151 |
|
|
152 |
// listen on the socket until it closes
|
|
153 |
while( 1 ) {
|
|
154 |
|
|
155 |
// if the socket is not defined then just wait for it
|
|
156 |
if( iSock == INVALID_SOCKET ) {
|
|
157 |
Sleep( 1000 );
|
|
158 |
if( iExitFlag != 0 ) {
|
|
159 |
return CE_NONE;
|
|
160 |
}
|
|
161 |
continue;
|
|
162 |
}
|
|
163 |
|
|
164 |
// read from socket if it is defined
|
|
165 |
err = recv( iSock, iStreamBuffer, KBUFFSIZE, 0 );
|
|
166 |
|
|
167 |
// if the socket has been closed then cleanup and return
|
|
168 |
if( err == 0 ) {
|
|
169 |
closesocket( iSock );
|
|
170 |
iSock = INVALID_SOCKET;
|
|
171 |
return CE_NONE;
|
|
172 |
}
|
|
173 |
|
|
174 |
// if an error has occured then report it and cleanup
|
|
175 |
if( err == SOCKET_ERROR ) {
|
|
176 |
*aErrCode = GetSocketError();
|
|
177 |
closesocket( iSock );
|
|
178 |
iSock = INVALID_SOCKET;
|
|
179 |
return CE_RECEIVE_FAILED;
|
|
180 |
}
|
|
181 |
|
|
182 |
// pass the data received through the filters.
|
|
183 |
// : the control flow below allows the filter to analyse the data, and also change
|
|
184 |
// the data in-place. However, it doesn't allow the interface to change the size of the
|
|
185 |
// data (limiting the modifications it can make). This isn't a problem now since no
|
|
186 |
// filters do this -- but when this is needed it should be implemented here, and in the same
|
|
187 |
// place in the uu interface.
|
|
188 |
if( iFilter != NULL ) {
|
|
189 |
iFilter->ProcessOutgoingData( iStreamBuffer, err );
|
|
190 |
}
|
|
191 |
|
|
192 |
// Now pass the data through to the datalink layer. A datalink layer MUST be
|
|
193 |
// defined if the socket is defined.
|
|
194 |
assert( iDatalink != NULL );
|
|
195 |
derr = iDatalink->ProcessTEData( iStreamBuffer, err, &errcode );
|
|
196 |
if( derr != DPE_NONE ) {
|
|
197 |
iLog->WriteLogEntry( SV_WARNING, "CTcpTeChannel::ListenOnChannel", "ProcessTEData returned error", derr, errcode );
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
// should never get here
|
|
202 |
assert( !"INVALID CODE PATH" );
|
|
203 |
return CE_NONE;
|
|
204 |
}
|
|
205 |
|
|
206 |
|
|
207 |
/*******************************************************************************
|
|
208 |
*
|
|
209 |
* PUBLIC METHOD: MAIN-THREAD: StopChannel. This closes the socket (if open)
|
|
210 |
* which will force the recv to return 0 and the listen-thread will return. It
|
|
211 |
* also sets the exit flag for when we want to stop the channel before it
|
|
212 |
* really even started (poor thing).
|
|
213 |
*
|
|
214 |
******************************************************************************/
|
|
215 |
void CTcpTeChannel::StopChannel()
|
|
216 |
{
|
|
217 |
// close the socket
|
|
218 |
if( iSock != INVALID_SOCKET ) {
|
|
219 |
closesocket( iSock );
|
|
220 |
iSock = INVALID_SOCKET;
|
|
221 |
}
|
|
222 |
|
|
223 |
// set the exit flag
|
|
224 |
iExitFlag = 1;
|
|
225 |
}
|
|
226 |
|
|
227 |
|
|
228 |
/*******************************************************************************
|
|
229 |
*
|
|
230 |
* PUBLIC METHOD: AIR INTERFACE THREAD: SendPacket. This is called by the data
|
|
231 |
* link layer when it receives some data from the air interface that it now
|
|
232 |
* decides to forward on.
|
|
233 |
*
|
|
234 |
******************************************************************************/
|
|
235 |
TDataPathError CTcpTeChannel::SendPacket( char *data, int len, int *aErrCode )
|
|
236 |
{
|
|
237 |
int err;
|
|
238 |
int bytes_to_send;
|
|
239 |
int bytes_sent;
|
|
240 |
|
|
241 |
// check params
|
|
242 |
assert( aErrCode != NULL );
|
|
243 |
*aErrCode = 0;
|
|
244 |
|
|
245 |
// if there is no connection then return. This is not unexpected or bad so
|
|
246 |
// no error is returned.
|
|
247 |
if( iSock == INVALID_SOCKET ) {
|
|
248 |
return DPE_NONE;
|
|
249 |
}
|
|
250 |
|
|
251 |
// otherwise send data
|
|
252 |
bytes_to_send = len;
|
|
253 |
bytes_sent = 0;
|
|
254 |
while( bytes_sent < bytes_to_send ) {
|
|
255 |
err = send( iSock, &(data[bytes_sent]), bytes_to_send - bytes_sent, 0 );
|
|
256 |
if( err == -1 ) {
|
|
257 |
*aErrCode = GetSocketError();
|
|
258 |
closesocket( iSock );
|
|
259 |
iSock = INVALID_SOCKET;
|
|
260 |
iSocketSetFlag = 0;
|
|
261 |
return DPE_SEND_FAILED;
|
|
262 |
}
|
|
263 |
bytes_sent += err;
|
|
264 |
}
|
|
265 |
|
|
266 |
// success
|
|
267 |
return DPE_NONE;
|
|
268 |
}
|
|
269 |
|
|
270 |
|
|
271 |
/*******************************************************************************
|
|
272 |
*
|
|
273 |
* PRIVATE METHODS: HELPER FUNCTIONS
|
|
274 |
*
|
|
275 |
******************************************************************************/
|
|
276 |
int CTcpTeChannel::GetSocketError()
|
|
277 |
{
|
|
278 |
#ifdef WIN32
|
|
279 |
return WSAGetLastError();
|
|
280 |
#else
|
|
281 |
return errno;
|
|
282 |
#endif
|
|
283 |
}
|